//ajax stuff
function showNameSuggestions(e,sObj,tID){
		
	var pos=new Array(2);	
	pos=findPos(sObj);
	var cleft=pos[0];
	var ctop=pos[1];
	if(sObj.value.length==0){
		//hide
		hideNameSuggestions();
	}
	if(sObj.value.length==1){
		var sgObj=document.getElementById(tID);
		sgObj.style.display="";
		sgObj.style.left=cleft;
		sgObj.style.top=ctop+19;
		var http=getHTTPObject();
		var url="getNameSuggestions.cfm?term=" +sObj.value+ "&nc="+Math.random();
		http.open("GET", url, false);
		http.send(null);

		//sgObj.innerHTML=http.responseText;
		var sugw=http.responseText;
		sgObj.innerHTML=sugw;
	}

}

function goToPage(txObj,maxn,curPage){
	if(txObj.value==""){alert("Please enter page number!");return false;}
	if(txObj.value>parseInt(maxn) || txObj.value<1){alert("Please enter page number between 1 and "+maxn);return false;}
	
	//var curNum=parseInt(parseInt(curPage)/50)+1;
	var newPage=(parseInt(txObj.value)-1)*50
	var myURL=document.location.href;
	var curNum=curPage-1;
	if(myURL.indexOf("StartRow")!=-1){
		var newURL=myURL.replace("StartRow="+curNum,"StartRow="+newPage);
	}else{
		if(myURL.indexOf("?")!=-1){
		var newURL=myURL+"&StartRow="+newPage;
		}else{
			var newURL=myURL+"?StartRow="+newPage;
		}
	}	
	document.location.href=newURL;

}

function hideNameSuggestions(){
		document.getElementById("namesugDiv").style.display="none";	
}

function showSuggestions(e,sObj,tID){
		
	var pos=new Array(2);	
	pos=findPos(sObj);
	var cleft=pos[0];
	var ctop=pos[1];
	if(sObj.value.length==0){
		//hide
		hideSuggestions();
	}
	if(sObj.value.length>0){
		var sgObj=document.getElementById(tID);
		sgObj.style.display="";
		sgObj.style.left=cleft;
		sgObj.style.top=ctop+19;
		var http=getHTTPObject();
		
		var url="doGetSuggestions.cfm?term=" +sObj.value+ "&nc="+Math.random();
		http.open("GET", url, false);
		http.send(null);
		//sgObj.innerHTML=http.responseText;
		var sugw=http.responseText;
		sgObj.innerHTML=sugw;
	}

}
 
 function hideSuggestions(){
	document.getElementById("sugDiv").style.display="none";	
}




function updateTerm(targName,termDv,id){
	//auto redirect or fill ?
	var sugTerm=document.getElementById(termDv).innerHTML;
	var filename =location.pathname.substring(location.pathname.lastIndexOf('/')+1);
	document.location="showdetail.cfm?sid="+id;
	//document.getElementById(targName).value=sugTerm;	
	//document.formSearch.submit();
}

function updateNameTerm(targName,termDv){
	//auto redirect or fill ?
	var filename =location.pathname.substring(location.pathname.lastIndexOf('/')+1);
	document.location=filename+"?sl_id="+termDv;
	//document.getElementById(targName).value=sugTerm;	
}

///////////////////////////////////////////////////////////////////////   end ajax stuff

function refreshNeighborhood(pageid)
{
	 pid  = ((pageid) ? pageid : "0");

	var divObj=document.getElementById("myCart");
	var cart_html="<table width='100%'><tr><td><div align='center'>&nbsp;<br><span class='smGrayText'>You have not selected any subdivisions.</span></div></td></td></table>";
	
	var listSUB = fnGetCookie("HAR_SUB");
	var listSUBName = fnGetCookie("HAR_SUB_NAME");
	var arSub = new Array();
	var arName = new Array();
	
	if (listSUB) 
	{
			cart_html="";
			arSub = listSUB.split(",");
			arName = listSUBName.split(",");
			cart_html="<table><tr><td colspan='2'><span class='smGrayText' align='center'>Click X to remove Neighborhood from your cart.</span></td></tr>";
	
			for(var i=0;i<arName.length;i++){
				if(arName[i] != null)
				{
					//cart_html+="<span><img src='imageurl'></span><span>"+arObj[i]+"</span>"
					//cart_html+="<tr bgcolor='DEE7EF' onMouseOver='javascript:doHover("+'"'+arSub[i]+'"'+","+'"on"'+")' onMouseOut='javascript:doHover("+'"'+arSub[i]+'"'+","+'"off"'+")'><td width='10' align='right' valign='bottom' bgcolor='DEE7EF'><span class='smBlueHeader'><a href='javascript:doRemove("+'"'+arSub[i]+'"'+','+'"'+pid+'"'+");refreshNeighborhood();doHover("+'"'+arSub[i]+'"'+","+'"off"'+");unCheck("+'"'+arSub[i]+'"'+");'><img src='images/x.gif' alt='test' name='text' width='18' height='16' border='0' id='text'></a></span></td><td width='175' valign='middle' bgcolor='DEE7EF'><span class='smBlueHeader'><a href='showdetail.cfm?sid="+arSub[i]+"'>"+arName[i]+"</a></span></td>"
					cart_html+="<tr bgcolor='DEE7EF'><td width='10' align='right' valign='bottom' bgcolor='DEE7EF'><span class='smBlueHeader'><a href='javascript:doRemove("+'"'+arSub[i]+'"'+','+'"'+pid+'"'+");refreshNeighborhood();doHover("+'"'+arSub[i]+'"'+","+'"off"'+");unCheck("+'"'+arSub[i]+'"'+");'><img src='images/x.gif' alt='test' name='text' width='18' height='16' border='0' id='text'></a></span></td><td width='175' valign='middle' bgcolor='DEE7EF'><span class='smBlueHeader'><a href='showdetail.cfm?sid="+arSub[i]+"'>"+arName[i]+"</a></span></td>"
			
				}
			}
	cart_html+="<tr><td></td><td align='left' valign='top'><div align='left'><input type='button' class='submit' value='COMPARE' onClick='javascript:goCompare()'></div></td></tr>";		
	 }
	cart_html+="</table>"
	divObj.innerHTML=cart_html;
}

function clearNeighborhood()
{
	var answer = confirm("Your 'My Neighborhoods' will now be cleared.")
	if (answer){
			fnDeleteCookie("HAR_SUB");
			fnDeleteCookie("HAR_SUB_NAME");
			unCheckAll();
			refreshNeighborhood();
	}	
}

function showMyNeighborhoods()
{
	
		document.getElementById('divMyNeighborhoods').style.display = 'block';
		document.getElementById('divSearch').style.display = 'none';
}

function goCompare() {
	var har_sub_cookies = fnGetCookie("HAR_SUB");
	if (har_sub_cookies) {
		document.location.href="showSubdivisionComparison.cfm?";
	}
	else {
		alert ('Please check one to six subdivisions to compare.');
	}
}



function checkform(searchOrZip) 
{
	if(searchOrZip == "search")
	{ 
		if (
			   document.formSearch.subname.value==""  
			&& document.formSearch.PRICE_MIN.selectedIndex==0 
			&& document.formSearch.PRICE_MAX.selectedIndex==0 
			&& document.formSearch.SQURE_FEET_MIN.selectedIndex==0 
			&& document.formSearch.SQURE_FEET_MAX.selectedIndex==0 
			&& document.formSearch.BEDROOM_NUM.selectedIndex==0 
			&& document.formSearch.BATHROOM_NUM.selectedIndex==0 
			&& document.formSearch.YearBlt_MIN.selectedIndex==0 
			&& document.formSearch.YearBlt_MAX.selectedIndex==0 
			&& document.formSearch.County[0].checked==false
			&& document.formSearch.County[1].checked==false
		  	&& document.formSearch.County[2].checked==false
			&& document.formSearch.County[3].checked==false
			&& document.formSearch.County[4].checked==false
		)
		{
			alert("Please enter search criteria.");
		}else
		{
		 	document.formSearch.submit()
		}
	}else if(searchOrZip == "zip")
	{
		
		if (document.formZip.zip.value=="" )
		{
			alert("Please enter a zip code.");
		}else
		{
		  document.formZip.submit()
		}
	
	}else if(searchOrZip == "email")
	{
		if(
		   document.frmEmail.toName.value==""
		   || document.frmEmail.toEmail.value==""
		   || document.frmEmail.fromName.value==""
		   || document.frmEmail.fromEmail.value==""
		   )
		{
			alert("Please fill in all fields marked with an *.");
		}else 
		{
			document.frmEmail.submit();
		}
		
	}
	//alert(document.formSearch.Zip_Code.value=="")
	
	//alert(document.formSearch.County1.checked==0)
	//alert(document.formSearch.minSQFT.selectedIndex==0)
	//
}

function doHover(id,onOff)
{
	var aVal = id;
	if(document.getElementById(id))
	{
	if (aVal) 
		{
			if(onOff == "on" || onOff == "On")
			{
				document.getElementById(id).style.backgroundColor='D1E8A4';
			}else if(onOff =="off" || onOff == "Off")
			{
				document.getElementById(id).style.backgroundColor='FFFFFF';
			}else if(onOff =="over" || onOff == "Over")
			{
				document.getElementById(id).style.backgroundColor='FFFF99';
			}
		}
	}
}



function doRemove(subid,pageid) 
{
 	var listSub = fnGetCookie("HAR_SUB");
	var listSubName = fnGetCookie("HAR_SUB_NAME");
	arraySub = listSub.split(",");
	arraySubName = listSubName.split(",");
	listSub = "";
	listSubName = "";
	
	for ( i=0; i<arraySub.length; i++) {
		if(arraySub[i]!=subid){
			listSub = listSub + "," + arraySub[i];
			listSubName = listSubName + "," + arraySubName[i];
		}
	}
	
	if (listSub) {
		// if still have MLNUM left, reset the cookie
		listSub = listSub.substring(1, listSub.length);
		listSubName = listSubName.substring(1, listSubName.length);
		fnSetCookie("HAR_SUB", listSub);
		fnSetCookie("HAR_SUB_NAME", listSubName);
	} else {
		// clear the cookie
		fnDeleteCookie("HAR_SUB");
		fnDeleteCookie("HAR_SUB_NAME");
	}
	
	//if we are on the showDetail.cfm page, toggle the  neighborhood to add 
	if(document.getElementById('divAddNeighborhood'))
	{
		if(document.getElementById('divAddNeighborhood').style.display != "har")
		{
		
			if(subid == pageid)
			{
				shoh('divAddNeighborhood');	
				shoh('divNeighborhood');	
			}
		}
	}
	refreshNeighborhood();
//	location.reload()
}

function checkForNeighborhood(id)
{

	var listSUB = fnGetCookie("HAR_SUB");
	var arraySub = new Array();
	i=0;
	if (listSUB) 
	{
				
				arraySub = listSUB.split(",");
				for ( i=0; i<arraySub.length; i++) 
					{
						if(arraySub[i]==id)
						{
							shoh('divAddNeighborhood');
							//the subdivision for this page is already in their cart, hide add button div, leave  div shoing
							return;
						}
					}
					shoh('divNeighborhood');
	}else
	{
			
		shoh('divNeighborhood');
	}
}

function setCompare(obj, name)
{
	var listSUB = fnGetCookie("HAR_SUB");
	var listSUBName = fnGetCookie("HAR_SUB_NAME");
	var arraySub = new Array();
	var arraySubName = new Array();
	i=0;
	//arraySub = listSUB.split(",");
	//arraySubName = listSUBName.split(",");
	if(obj.checked)
	{
		if (listSUB) 
			{
				arraySub = listSUB.split(",");
				arraySubName = listSUBName.split(",");
				if (arraySub.length >= 6) 
				{
					alert("You may only select up to 6 subdivisions to compare. \nClick on Compare button and you can delete saved subdivisions.")
					obj.checked = false;
					return;
				}else 
				{
					for ( i=0; i<arraySub.length; i++) 
					{
						if(arraySub[i]==obj.value)
						{
						//the selected mlnum is already in cookie, return don't do anything
						return;
						}
					}
				}
				// only set after checked less than 5, and no dups
				showMyNeighborhoods();
				fnSetCookie("HAR_SUB", listSUB + "," + obj.value);
				fnSetCookie("HAR_SUB_NAME", listSUBName + "," + name);
				doHover(obj.value,'on')
				}else 
				{
			// set first MLNUM
			showMyNeighborhoods();
			fnSetCookie("HAR_SUB", obj.value);
			fnSetCookie("HAR_SUB_NAME", name);
			doHover(obj.value,'on')
		}
	} else 	{
		// MLNUM from the list
		if (listSUB) 
		{
			arraySub = listSUB.split(",");
			arraySubName = listSUBName.split(",");
			listSUB = "";
			listSUBName = "";
			for ( i=0; i<arraySub.length; i++) {
				if(arraySub[i]!=obj.value){
					listSUB = listSUB + "," + arraySub[i];
					listSUBName = listSUBName + "," + arraySubName[i];
				}
			}
			if (listSUB) {
				// if still have MLNUM left, reset the cookie
				listSUB = listSUB.substring(1, listSUB.length);
				listSUBName = listSUBName.substring(1, listSUBName.length);
				fnSetCookie("HAR_SUB", listSUB);
				fnSetCookie("HAR_SUB_NAME", listSUBName);

			} else {
				// clear the cookie
				fnDeleteCookie("HAR_SUB");
				fnDeleteCookie("HAR_SUB_NAME");

			}
		doHover(obj.value,'over')
		}
	}

	if(i==0)
	{
		arraySub[0] = obj.value;	
		arraySubName[0] = name;
	}	
	k=i;
	if(obj.checked == false) 
	{ 
		i = i - 1;
		arraySub[i] = null;
		arraySubName[i] = null;
	}else
	{
		arraySub[k] = obj.value;
		arraySubName[k] = name;
	}	
	refreshNeighborhood();
}

function addToMyNeighborhoods(id,name)
{
	var listSUB = fnGetCookie("HAR_SUB");
	var listSUBName = fnGetCookie("HAR_SUB_NAME");
	var arraySub = new Array();
	var arraySubName = new Array();
	i=0;
	//arraySub = listSUB.split(",");
	//arraySubName = listSUBName.split(",");
	if (listSUB) 
		{
			arraySub = listSUB.split(",");
			arraySubName = listSUBName.split(",");
			if (arraySub.length >= 6) 
			{
				alert("You may only select up to 6 subdivisions to compare. \nClick on 'My Neighborhoods' tab if you would like to delete saved subdivisions.")
				return;
			}else 
			{
				for ( i=0; i<arraySub.length; i++) 
				{
					if(arraySub[i]==id)
					{
					//the selected mlnum is already in cookie, return don't do anything
					return;
					}
				}
			}
				// only set after checked less than 5, and no dups
				showMyNeighborhoods();
				
				fnSetCookie("HAR_SUB", listSUB + "," + id);
				fnSetCookie("HAR_SUB_NAME", listSUBName + "," + name);
				shoh('divAddNeighborhood');
				shoh('divNeighborhood');
				//doHover(id,'on')
		}else 
		{
			// set first MLNUM
			showMyNeighborhoods();
			fnSetCookie("HAR_SUB", id);
			fnSetCookie("HAR_SUB_NAME", name);
			shoh('divAddNeighborhood');
			shoh('divNeighborhood');
			//doHover(obj.value,'on')
		}
	refreshNeighborhood(id);
}



function fnGetCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function fnDeleteCookie(name,path,domain) {
    if (fnGetCookie(name)) document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fnSetCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}





function setTab()
	{
	if (fnGetCookie('HAR_SUB'))
	{	
		shoh('divSearch');
	}else
	{
		shoh('divMyNeighborhoods');
	}
}

function hideDiv(id)
{
	if (document.getElementById)
	{
		document.getElementById(id).style.display = 'none';		
	}
}

function showDiv(id)
{
	if (document.getElementById)
	{
		document.getElementById(id).style.display = 'block';		
	}
}

function shoh(id) { 
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';
				
		} else {
		document.getElementById(id).style.display = 'none';			
		}	
	} else { 
		if (document.layers) {	
			if (document.id.display == "none"){
				document.id.display = 'block';
				
			} else {
					
				document.id.display = 'none';
			}
		} else {
			if (document.all.id.style.visibility == "none"){
				document.all.id.style.display = 'block';
			} else {
				
				document.all.id.style.display = 'none';
			}
		}
	}
}



function unCheckAll()
{
//dummy function
}
function unCheck(){
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function blinkIt() {
 if (!document.all) return;
 else {
   for(i=0;i<document.all.tags('blink').length;i++){
      s=document.all.tags('blink')[i];
      s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
   }
 }
}





function DisableEnableLinks(xHow){
  objLinks = document.links;
  for(i=0;i<objLinks.length;i++){
    objLinks[i].disabled = xHow;
    //link with onclick
    if(objLinks[i].onclick && xHow){  
        objLinks[i].onclick = new Function("return false;" + objLinks[i].onclick.toString().getFuncBody());
    }
    //link without onclick
    else if(xHow){  
      objLinks[i].onclick = function(){return false;}
    }
    //remove return false with link without onclick
    else if(!xHow && objLinks[i].onclick.toString().indexOf("function(){return false;}") != -1){            
      objLinks[i].onclick = null;
    }
    //remove return false link with onclick
    else if(!xHow && objLinks[i].onclick.toString().indexOf("return false;") != -1){  
      strClick = objLinks[i].onclick.toString().getFuncBody().replace("return false;","")
      objLinks[i].onclick = new Function(strClick);
    }
  }
}

String.prototype.getFuncBody = function(){ 
  var str=this.toString(); 
  str=str.replace(/[^{]+{/,"");
  str=str.substring(0,str.length-1);   
  str = str.replace(/\n/gi,"");
  if(!str.match(/\(.*\)/gi))str += ")";
  return str; 
} 




function buildArray(lgt) {
    for (var i=0; i<= lgt; i++) this[i]=0;
    this.length = lgt;
    return this;
};

function buildGSpot(mlongtitude,mlatitude, mhtml, mlabel) { 
    this.longitude=mlongtitude;
    this.latitude=mlatitude;
	this.html = mhtml;
  	this.label = mlabel;
    return this;
};



	gspottemp = new buildArray(1);
	gspottemp[0] = 0	


	////center object property 	
gspotzomein = new buildArray(1);

	gspotzomein[0] = 5


gcenterspot = new buildArray(1); 







// JSScript from showdetail_Schools.cfm

function addToCompare(cmpItem,compare)
	{
		if(hasChecked())
		{
		
		frmSchool.action = "http://www.har.com/school/list/addToCompare.asp?cmpItem=" + cmpItem + "&compare=" + compare;
		frmSchool.target = "_blank";
		frmSchool.submit();
		}else{
			alert("Please select 1 to 6 schools to compare!");
		}
	}	

function hasChecked(){
	var allChkBox=document.frmSchool.checkbox_id;
	var hasCheck=false;
	var totChk=0;
	for(var i=0;i<allChkBox.length;i++){
		if(allChkBox[i].checked){
			hasCheck=true;
			totChk++;
		}
	}
	
	if(totChk<1 || totChk>6){
		return false;
	}else{
		return true;
	}
}



// BW: default for show zip ajax;
function showZipDiv2(obj,tID,fieldID)
{
	var pos=new Array(2);	
	pos=findPos(obj);
	var cleft=pos[0];
	var ctop=pos[1];
	var pDiv=document.getElementById(tID+"X");
	pDiv.style.display="";
	sgObj=document.getElementById(tID);
	var ospace=getSpace();
	
	if(cleft+360>ospace[0]){
		cleft=cleft-250;	
	}
		 
	pDiv.style.top=ctop+20+"px";
	pDiv.style.left= cleft+"px"; 
	waitGen(tID);
	sgObj.style.display="";

	sgObj.style.left=cleft;
	sgObj.style.top=ctop+'19px';
	http=getHTTPObject();
		

	var url='doGetZips.cfm?&nc='+Math.random()+'&fieldback='+fieldID;

	http.open("GET", url, true);
	http.send(null);
	//alert(http.readyState);
	//if (http.readyState ==4) {
	http.onreadystatechange = function() {
		if (http.readyState ==4) {	sgObj.innerHTML=http.responseText;	}
	}
}

function getHTTPObject() {
  var xmlhttp;
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  
  	return xmlhttp;
}

function getSpace(){	
	var d=document;
	if(d.getElementById){
	 if(d.documentElement&&d.documentElement.clientHeight){
	  xMax=d.documentElement.clientWidth;
	  yMax=d.documentElement.clientHeight;
	 }else{
	  xMax=(d.all)?d.body.clientWidth:window.innerWidth;
	  yMax=(d.all)?d.body.clientHeight:window.innerHeight;
	 }
	}else if(d.layers){
	 xMax=window.innerWidth;
	 yMax=window.innerHeight;
	}
	return[xMax,yMax];
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function waitGen(divID){
	document.getElementById(divID).innerHTML="<div style='text-align:center;width:auto;padding-top:10px;color:#c0c0c0;font-size:11px;'><img src='/images/lineart/loading.gif'><br>Loading...</div>";
}

