function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/* Search Related Function */

function InputBG(DefaultText,ID,Focus)
{	
	if (Focus == 1)		{
		if (document.getElementById(ID).value == DefaultText) {
			document.getElementById(ID).value = '';
			document.getElementById(ID).style.color = 'black';			
		}
	} else {
		if (document.getElementById(ID).value == '') {
			document.getElementById(ID).value = DefaultText;
			document.getElementById(ID).style.color = '#888888';
		}			
	}
}

function SelSearchSection(Section,SearchAddress) {
	document.getElementById('SearchCategory').innerHTML='in ' + Section + ' <small>▼</small>';	
	document.getElementById('searchfile').value = SearchAddress;
}

function SelItem(Item,SaveValue,InpName,CtrlID) {
	document.getElementById(CtrlID).innerHTML=Item + ' <small>▼</small>';	
	document.getElementsByName(InpName)[0].value = SaveValue;
}

function ValidateSearchInputs()	{
	var inputs = document.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++) {
		if ((inputs[i].style.color != "black" && inputs[i].style.color != "") && (inputs[i].type == "text")) {
			inputs[i].value = '';	
		}
	}	
	SearchForm.action = document.getElementById('searchfile').value;
	SearchForm.submit();
}

/* End Search Related Functions */


function ConfirmAdult()
{
	document.cookie = 'ClassiAdult=1; path=/';
}

function ShowSearchBar(ID)
{
	document.getElementById('SearchAll').style.display = 'none';
	document.getElementById('SearchMotors').style.display = 'none';
	document.getElementById('SearchJobs').style.display = 'none';
	document.getElementById('SearchPets').style.display = 'none';
	document.getElementById('SearchSale').style.display = 'none';
	document.getElementById('SearchServices').style.display = 'none';
	document.getElementById('SearchRealEstate').style.display = 'none';
	document.getElementById('SearchRent').style.display = 'none';
	document.getElementById('SearchPersonals').style.display = 'none';
	document.getElementById('SearchCommunity').style.display = 'none';

	document.getElementById(ID).style.display = '';
}

function ExpandSection(secid,image)
{
	if (document.getElementById('cats'+secid).style.display == 'none') {
		document.getElementById('cats'+secid).style.display = '';
		image.src = '/images/minus_icon.jpg'; 
	}
	else {
		document.getElementById('cats'+secid).style.display = 'none';
		image.src = '/images/plus_icon.jpg'; 
	}
}

function ExpandSearch(link)
{
	if (document.getElementById('SearchArea').className == 'SearchBarBasic') {
		document.getElementById('SearchArea').className = 'SearchBarAdvanced';
		link.innerHTML = 'Hide Advanced Search';
		document.getElementById('AdvancedSearchArea').style.display = '';
		document.getElementById('AdvancedSearchIMG').src = '/images/up_arrow.gif';
		document.getElementById('advanced').value = '1';
	}
	else {
		document.getElementById('SearchArea').className = 'SearchBarBasic';	
		link.innerHTML = 'Show Advanced Search';
		document.getElementById('AdvancedSearchIMG').src = '/images/down_arrow.gif';
		document.getElementById('AdvancedSearchArea').style.display = 'none';
		document.getElementById('advanced').value = '0';
	}
}

function Logout()
{
    document.cookie = 'ClassiID=; expires=Fri, 31 Dec 1980 23:59:59 GMT; path=/';
    document.cookie = 'ClassiPass=; expires=Fri, 31 Dec 1980 23:59:59 GMT; path=/';
    window.location.replace('http://www.iupost.com');
}

function ValidateZip(ZipCode)
{
    var dok = window.frames.ValidateZipFrame.document;
    dok.getElementById('ValidZip').value = ZipCode.value;
    
    dok.ValidateZipForm.submit(); 
}

function emailCheck(str) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)) {
        return (true)
    }

    alert("Invalid email: "+str+". Please re-enter.")
    return (false)
}

function numericCheck(id) {
    var newvalue = '';
    var str = document.getElementById(id).value;
    
    for (i=0; i < str.length; i++) {
        if (str.charAt(i)>=0 && str.charAt(i)<=9) {
            newvalue = newvalue + str.charAt(i);
        }
    }
    document.getElementById(id).value = trim(newvalue);
}

function priceCheck(str) {
    var newprice = '';
    for (i=0; i < str.length; i++) {
        if (str.charAt(i)>=0 && str.charAt(i)<=9) {
            newprice = newprice + str.charAt(i);
        }
        if (str.charAt(i) == '.') {
            break;
        }
    }
    document.getElementById('Price').value = trim(newprice);
}

function ShippingFeesCheck(str) {
    var newprice = '';
    for (i=0; i < str.length; i++) {
        if (str.charAt(i)>=0 && str.charAt(i)<=9) {
            newprice = newprice + str.charAt(i);
        }
        if (str.charAt(i) == '.') {
            break;
        }
    }
    document.getElementById('ShippingFees').value = trim(newprice);
}

function EnableBuyNow(state)	{
	if (state) {
		document.getElementById('BuyNow').style.display = "";
		document.getElementById('ShippingInfo').style.display = "";
	}
	else {
		document.getElementById('BuyNow').style.display = "none";
		document.getElementById('ShippingInfo').style.display = "none";	
	}
}


function parseelement(thisone){
	var prefix="$";
	var wd;
	
	if (thisone.value.charAt(0)=="$")
		return wd="w";
		
	var tempnum=thisone.value;
	
	for (i=0;i<tempnum.length;i++){
		if (tempnum.charAt(i)=="."){
			wd="d";
			break;
		}
	}
	if (wd=="w")
		thisone.value=prefix+tempnum+".00";
	else {
		if (tempnum.charAt(tempnum.length-2)==".") {
			thisone.value=prefix+tempnum+"0"
		}
		else{
			tempnum=Math.round(tempnum*100)/100;
			thisone.value=prefix+tempnum;
		}
	}
}

function changeBG(img,id)	{
   newImage = "url(http://www.iupost.com/backgrounds/" + img + ")";
   document.getElementById('ad_content').style.backgroundImage = newImage;
   document.getElementById('BackgroundID').value = id;
}
