//preload some images
//var myImage=new Image();
//myImage.src="images/loading.gif";

function toggle(obj) {
	if (obj.style.display == "none") {
		obj.style.display = "";
	}
	else {
		obj.style.display = "none";
	}
}

function doSearchPlace(sform){
	if(sform.placename.value =="" || sform.placename.value=="Enter place name"){alert("Please enter place name to search"); return false;}
	if(sform.placetype.value =="" ){alert("Please select place type"); return false;}
	var myCF=sform.mycf.value;
	//do our search
	var sspec=sform.placetype.value;
	var url="getPOI.cfm?spec="+sspec+"&latlong="+latlong+"&term="+sform.placename.value+"&nc="+Math.random();
	var  xmlhttp=getHTTP();	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
		isBusy=false;

		if(myCF!="" && addr ==""){	markCurrentLocation();}
		if(myCF!="" && addr!=""){	markCurrentLocation2();}
		processJSON(xmlhttp.responseText,"resultDiv");
	
		} else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
			isBusy=false;
        }
 		}
	};
	
	if(isBusy){return;}
	
	offAll2();
	waitGen("resultDiv");
	map.DeleteAllShapes();
  	xmlhttp.open("GET",url,true)
  	xmlhttp.send(null)
	isBusy=true;
	
	
}

function swapClass(sObj,clName){
	sObj.className=clName;
}

function changeCity(scity){
	var vcity=scity+",tx";
		map.Find("",vcity, null, null, 1, 1, false, false, false, true);
}

var curDiv=null;
var curDiv2=null;

function keepThis(divObj,sname){
	offAll();
	curDiv=divObj;
	divObj.className='sideItemOver';
	loadWait(divObj.innerText);

	loadData("getListing",divObj.id,"listingDiv",divObj.innerText);
}
function loadWait(stitle){
	document.getElementById("miTitle").innerText=stitle;
	document.getElementById("listingDiv").innerHTML="<div style='font-weight:400;text-align:center;width:650px;padding-top:50px;color:#333333;font-size:11px;'><img src='images/loading.gif'><br>Loading...</div>";
}

function waitGen(divID){
document.getElementById(divID).innerHTML="<div style='font-weight:400;text-align:center;width:auto;padding-top:10px;color:#333333;font-size:11px;'><img src='images/loading.gif'><br>Loading...</div>";
}

function offAll(){

		for(var i=1;i<9;i++){
			if(i!=4){
			document.getElementById("item_"+i).className="sideItem";
			}
		}
}

function offAll2(){
	var myDivNum=parseInt(document.getElementById("totalSideMenu").innerText);
	
	for(var i=0;i<myDivNum;i++){
		document.getElementById("placeDiv_"+i).className='sideItem';	
	}		
}

function switchCL(divObj,clsName){
	if(clsName=="sideItem" && curDiv==divObj) {return};
	divObj.className=clsName;
}

function switchCL2(divObj,clsName){
	if(clsName=="sideItem" && curDiv2==divObj) {return};
	
	divObj.className=clsName;
}
function updateFilter(targDiv,sval){
	document.getElementById(targDiv).innerHTML=sval;
}
 
var isBusy=false;
function swapDiv(div1,div2){
	document.getElementById(div1).style.display='';
	document.getElementById(div2).style.display='none';
	
}
function changeCensus(sid){
waitGen("censusDiv");
loadData("getCensus",sid.value,"censusDiv")

}

function Form1_Validator(theForm){
		//need looking into 
		return true;
}

function changesalePrice(sObj){
	deleteselectedItemsFromList(document.getElementById("priceRange"));
	//addd new price
	var myprice=new Array();
	myprice[1]=new Array("All Price Range","");
	myprice[2]=new Array("Under $120,000","0 and 120000");
	myprice[3]=new Array("$120,001-$150,000","120001 and 150000");
	myprice[4]=new Array("$150,001-$190,000","150001 and 190000");
	myprice[5]=new Array("$190,001-$250,000","190001 and 250000");
	myprice[6]=new Array("$250,001-$350,000","250001 and 350000");
	myprice[7]=new Array("$350,001-$500,000","350001 and 500000");
	myprice[8]=new Array("$500,001-$1 Million","500001 and 1000000");
	myprice[9]=new Array("$1 Million and above","1000000 and 100000000");
	for(var i=1;i<myprice.length;i++){
		var newObj=makeNewOption(myprice[i][0],myprice[i][1]);
		//alert(newObj);
		document.getElementById("priceRange").appendChild(makeNewOption(myprice[i][0],myprice[i][1]));
	}
}

function changeLeasePrice(sObj){

	deleteselectedItemsFromList(document.getElementById("priceRange"));
	//addd new price
	var myprice=new Array();
	
	myprice[1]=new Array("All Price Range","");
	myprice[2]=new Array("$100-$500","100 and 500");
	myprice[3]=new Array("$501-$1,000","501 and 1000");
	myprice[4]=new Array("$1,001-$1,500","1001 and 1500");
	myprice[5]=new Array("$1,501-$2,000","1501 and 2000");
	myprice[6]=new Array("$2,001-$3,000","2,001 and 3000");
	myprice[7]=new Array("$3,001-$4,000","3001 and 4000");
	myprice[8]=new Array("$4,001-$5,000","4001 and 5000");
	myprice[9]=new Array("$5,001-$6,000","5001 and 6000");
	myprice[10]=new Array("$6,001-$7,000","6001 and 7000");
	myprice[11]=new Array("$7,000 and above","7000 and 20000");

	for(var i=1;i<myprice.length;i++){
		var newObj=makeNewOption(myprice[i][0],myprice[i][1]);
	
		document.getElementById("priceRange").appendChild(makeNewOption(myprice[i][0],myprice[i][1]));
	}
	

}

 


function sortPricePre(selObj,sloc,slocval,sfor){
	//alert(selObj.value);
	sortPrice(selObj,sloc,slocval,sfor,selObj.value);
} 
 
function sortPrice(divObj,sloc,slocval,sfor,svalue,slabel){
	curPrice=svalue;
	waitGen("listingDiv");
	var url="doSort.cfm?for=price&sloc="+sloc+"&slocval="+slocval+"&sfor="+sfor+"&sprice="+svalue+"&stype="+curType+"&nc="+Math.random();
	var  xmlhttp=getHTTP();	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
           document.getElementById("listingDiv").innerHTML=xmlhttp.responseText;
		   isBusy=false;
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
				window.open().document.write(xmlhttp.responseText);
        }
 		}
	};
	if(isBusy){return;}
	
	//updateFilter("pPrice_span",slabel);
	//offAll2();
	//curDiv2=divObj;
	//divObj.className='sideItemOver1';
  	xmlhttp.open("GET",url,true)
  	xmlhttp.send(null)
	isBusy=true;
}

function sortTypePre(selObj,sloc,slocval,sfor){
	//alert(selObj.value);
	
	if(lastSort == "7" && selObj.value !="7"){
		changesalePrice(selObj);
		curPrice="";
	}
	
	if(selObj.value =="7"){
		//change the price
		changeLeasePrice(selObj);
		curPrice="";
	
	}
	sortType(selObj,sloc,slocval,sfor,selObj.value);


		lastSort=selObj.value;
} 

function makeNewOption(txt,sval) { 
	var newItem = document.createElement("option") 
	newItem.innerHTML = txt 
	newItem.value=sval;
	return newItem 
} 


function deleteselectedItemsFromList(sourceList) { 
var maxCnt = sourceList.options.length; 
	for(var i = maxCnt - 1; i >= 0; i--) { 
		if ((sourceList.options[i]!= null) ) { 
			sourceList.options[i] = null; 
		
		} 
	} 
} 
function keepThis2(divObj){
	offAll();
	curDiv=divObj;
	divObj.className='sideItemOver';
	document.getElementById("miTitle").innerHTML=divObj.innerHTML;
	waitGen("listingDiv");
	
	var url="doSort.cfm?sfor="+divObj.id+"&slocval="+uTX+"&sprice="+curPrice+"&stype="+curType+"&nc="+Math.random();
	var  xmlhttp=getHTTP();	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
           document.getElementById("listingDiv").innerHTML=xmlhttp.responseText;
		   uItem=divObj.id;
		   isBusy=false;
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
				window.open().document.write(xmlhttp.responseText);
        }
 		}
	};
	if(isBusy){return;}

  	xmlhttp.open("GET",url,true)
  	xmlhttp.send(null)
	isBusy=true;



}

function sortType(divObj,sloc,slocval,sfor,svalue,slabel){
	curType=svalue;
	waitGen("listingDiv");
	var url="doSort.cfm?for=type&sfor="+sfor+"&sloc="+sloc+"&slocval="+slocval+"&stype="+svalue+"&sprice="+curPrice+"&nc="+Math.random();
	var  xmlhttp=getHTTP();	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
		isBusy=false;
           document.getElementById("listingDiv").innerHTML=xmlhttp.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
        }
 		}
	};
	if(isBusy){return;}

  	xmlhttp.open("GET",url,true)
  	xmlhttp.send(null)
		isBusy=true;
}

function changeCity2(selObj){
		document.location="/real_estate/"+selObj.value+"-tx.html";
	
}

function saveMyLocation(fObj){
	if(validateForm(fObj)){
		var address=fObj.address.value+","+fObj.city.value+","+fObj.state.value;
		var url="/Ajaxcall/API.cfm?action=saveLocation&arg1="+fObj.address.value+"&arg2="+fObj.city.value+"&arg3="+fObj.state.value+"&nc="+Math.random();
		loadData(url,"locDiv","markCurrentLocation();");
		disableSel(address);
		//get cookie
		
	}
}

function markCurrentLocation(){
	llstr=unescape(readCookie("MYLOCATION"));
	myAdd=unescape(readCookie("ADDRESS"));
	//alert(myAdd);
	if(llstr!="null" && llstr!=""){
	var myAdd2=myAdd.split(",");
	var llstr2=llstr.split(",");
	latlong=llstr;
					var latLon = new VELatLong(llstr2[0], llstr2[1]); 
					var shape = new VEShape(VEShapeType.Pushpin,latLon); 
					var myName="My Location";
					var myDesc="<b>Address</b><br>"+myAdd2[0]+"<br>"+myAdd2[1]+", "+myAdd2[2];
					shape.SetTitle(myName);
					shape.SetDescription(myDesc);
					map.DeleteAllShapes();
					map.AddShape(shape);
					map.onLoadMap=new function(){
						map.SetCenter(latLon);
					}
						
					changePlaceLabel("Places in My Location");
	}
				
}

function markCurrentLocation2(){

					var myAdd2=addr.split(",");
					var llstr2=latlong.split(",");
					var latLon = new VELatLong(llstr2[0], llstr2[1]); 
					var shape = new VEShape(VEShapeType.Pushpin,latLon); 
					var myName="My Location";
					var myDesc="<b>Address</b><br>"+myAdd2[0]+"<br>"+myAdd2[1]+", "+myAdd2[2];
					shape.SetTitle(myName);
					shape.SetDescription(myDesc);
					//map.DeleteAllShapes();
					map.AddShape(shape);
					map.onLoadMap=new function(){
						map.SetCenter(latLon);
					}	
}


function changePlaceLabel(tex){
		if(tex!="" && tex!=undefined){
			document.getElementById("myplaceLabel").innerHTML=tex;	
		}else{
			
			document.getElementById("myplaceLabel").innerHTML=document.getElementById("myplaceLabelOrg").innerHTML
		}	
}

function disableSel(myloc){
			document.getElementById("tempLocHolder").innerHTML=document.getElementById("myPlaceDiv").innerHTML;
			document.getElementById("myPlaceDiv").innerHTML="<div class='blue'>"+myloc+"</div>";
			changePlaceLabel("Places in My Location");
			
}
function enableSel(){
		document.getElementById("myPlaceDiv").innerHTML=document.getElementById("tempLocHolder").innerHTML;
		document.getElementById("tempLocHolder").innerHTML="";
		changePlaceLabel();
}
function getMyLocation(){
	var url="/Ajaxcall/API.cfm?action=getLocation&nc="+Math.random();
	loadData(url,"locDiv","changeStyle('locDiv','display','block');");
		
}

function resetLocation(){
	var url="/Ajaxcall/API.cfm?action=deleteLocation&nc="+Math.random();
	loadData(url,"locDiv","changeStyle('locDiv','display','');");
	map.DeleteAllShapes();
	//del_cookie("address");
	//del_cookie("myLocation");
	if(prevlatlong!="0,0"){
		latlong=prevlatlong;	
	}
	
	enableSel();
}

function findPOI(divObj,sspec,targDiv,cf){

	var  xmlhttp=getHTTP();	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
		isBusy=false;
			processJSON(xmlhttp.responseText,targDiv);
	
		} else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
			isBusy=false;
        }
 		}
	};
	
	//if(isBusy){return;}
	
	offAll2();
	waitGen(targDiv);
	curDiv2=divObj;

	divObj.className='sideItemOver';

	map.DeleteAllShapes();
	if(cf!="" && addr ==""){markCurrentLocation();}
	if(cf!="" && addr!=""){markCurrentLocation2();}
	var url="getPOI.cfm?spec="+sspec+"&latlong="+latlong+"&nc="+Math.random();

	//alert(url);
  	xmlhttp.open("GET",url,true)
  	xmlhttp.send(null)
	isBusy=true;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function processJSON(jsText,tDiv){
	try{
	eval(trim(jsText));
	}
	catch(e){
		document.getElementById(tDiv).innerHTML="<div style='text-align:center;color:#CC3300'>Data not available</div>";	
		return;
	}

	var dText="<div style='text-align:center;' class='red'>No Places Found!</div>";
	if(retJSON.myresults.length>0){
			var dText="<table class='dataBox' width='100%'><tr><th></th><th>Place Name</th><th>Address</th><th>City</th><th>State</th><th>Zip Code</th><th>Phone</th></tr>";		
	
			for(var i=0;i<retJSON.myresults.length-1;i++){
				var lat=parseFloat(retJSON.myresults[i].lat);
				var lstr=parseFloat(retJSON.myresults[i].lstr);
				var myName=String(retJSON.myresults[i].placename);
				var curIX=i+1;
				var myDesc="<div style='height:auto;width:auto;text-align:left;'><div><b>Address</b><br>"+retJSON.myresults[i].address+"<br>"+retJSON.myresults[i].city+", "+retJSON.myresults[i].state+" "+retJSON.myresults[i].zip+"</div><div>Phone: "+retJSON.myresults[i].phone+"</div></div>";
				var shape = new VEShape(VEShapeType.Pushpin,new VELatLong(lat, lstr)); 
				shape.SetCustomIcon("<div class='pinStyle1'><div class='text'>"+curIX+"</div></div>"); 
				shape.SetTitle(myName);
				shape.SetDescription(myDesc);
			try{
					map.AddShape(shape);
				}
				catch(e){
					alert(e.message);	
				}
				dText+="<tr><td><div class='pinStyle1' style='position:static;'><div class='text'>"+curIX+"</div></div></td><td>"+retJSON.myresults[i].placename+"</td><td>"+retJSON.myresults[i].address+"</td><td>"+retJSON.myresults[i].city+"</td><td>"+retJSON.myresults[i].state+"</td><td>"+retJSON.myresults[i].zip+"</td><td>"+retJSON.myresults[i].phone+"</td></tr>";
			}
			dText+="</table>";
	}
	document.getElementById("resultDiv").innerHTML=dText;

		//map.SetZoomLevel(13);

}


function getHTTP()
{
	var xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	  {
	 return new XMLHttpRequest()
	  }
	// code for IE
	else if (window.ActiveXObject)
	  {
	  return new ActiveXObject("Microsoft.XMLHTTP")
	  }
	if (xmlhttp==null)
	  {
	  alert("Your browser does not support XMLHTTP.")
	  }
}


function getElementsByNameIE(tag, name) {
       var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
	//	alert(c);
		if (c.indexOf(nameEQ) == 0){
			//alert("found "+c);
			return c.substring(nameEQ.length,c.length);
			
		}
	}
	return null;
}
// this deletes the cookie when called
function del_cookie(name) {

document.cookie = name +
'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

	
