var isSingleAddress = false;

function submitForm(){
	processForm();

}

function resetForm(){
	resetState();
	var theform = document.theForm;
	theform.city.value="";
	theform.address.value="";
	theform.radius.value="1500";
	theform.zipcode.value="";
	theform.radiusDist[0].checked ="checked";
	
	var i = 0;
	var t= theform.firstMonth;
	for(i=0; i< t.length; i++){
		if(t[i].value ==10)
			t[i].selected = "selected";
		else
			t[i].selected = "";
		
	}
	t= theform.lastMonth;
	for(i=0; i< t.length; i++){
		if(t[i].value ==11)
			t[i].selected = "selected";
		else
			t[i].selected = "";
	}		
	
	t= theform.firstDay;
	for(i=0; i< t.length; i++){
		if(t[i].value ==1)
			t[i].selected = "selected";
		else
			t[i].selected = "";
	}		
	t= theform.lastDay;
	for(i=0; i< t.length; i++){
		if(t[i].value ==30)
			t[i].selected = "selected";
		else
			t[i].selected = "";
	}	
	
	t= theform.firstYear;
	for(i=0; i< t.length; i++){
		if(t[i].value ==2006)
			t[i].selected = "selected";
		else
			t[i].selected = "";
	}	
	t= theform.lastYear;
	for(i=0; i< t.length; i++){
		if(t[i].value ==2007)
			t[i].selected = "selected";
		else
			t[i].selected = "";
	}		
	
	
}

function displayMenus(thediv, d){	
/*	if( (thediv.style.display =="none" || !thediv.style.display ) && d==1){
		thediv.style.visibility = "visible";
		$(thediv).slideDown("slow");
	}
	
	else if(thediv.style.display !="none" && d==0){
			$(thediv).slideUp("slow");
			
	}
	*/
}


function formAndValidateObject(zipcode, city, address, radius, radim, fday, lday, st, q){


		var varStr="";

		if(address){
			address = cleanStr(address);
			holdVars.address= address;
			varStr +="&address="+address;
		
		}if(city){
			city = cleanStr(city);
			//since many people seem to be typing in the state, clean that too
			
			city= city.replace(/ ca/i, "");
			city= city.replace(/,ca/i, "");
			city= city.replace(/[0-9]{1,9}/i, "");
			holdVars.city = city;
			varStr +="&city="+city;
			
		}


		if(zipcode){
		
			if(isNumeric(zipcode)){
				zipcode = cleanStr(zipcode);
				holdVars.zipcode = zipcode;
				varStr +="&zipcode="+zipcode;
			}else
				zipcode=null;
		}	
		
		if(radim){
			radim = cleanStr(radim);
			holdVars.radim=radim;
			varStr +="&radim="+radim;
		}
		
		
		if(fday){
		
			if(isNumeric(fday)){
				fday = cleanStr(fday);
				holdVars.fday = fday;
				varStr +="&fday="+fday;
			}else
				fday=null;
		}
		if(lday){
			if(isNumeric(lday)){
				lday= cleanStr(lday);
				holdVars.lday = lday;
				varStr +="&lday="+lday;
			}else
				lday=null;
			
		}
	
		if(radius){
			if(!isNumeric(radius)){
				radius = null;
			}else{
				holdVars.radius=radius;
				varStr +="&radius="+radius;
			}
		}


			

		
		

		holdVars.varStr = varStr;
		var obj = new Object();
	
		obj.varStr = varStr;
		obj.address= address;
		obj.city = city;
		obj.st = st;
		obj.q=q;
		obj.zipcode = zipcode;
		
		if(obj.address){
			if(radius <= 0 || radius == null || radim==null){
				isSingleAddress= true;
			}	
		}
		

	
		return obj;
}



function processForm(){


//pre: user has hit the submit button, tsize refers to which html page submitted it
//post: read in all the inputs, create a url string with query, run geoCoder on an object containing url query and address string.
//also, form validation is done
/*
*/


var fday, lday;
var st= 0;
var errormsg="";

	var theform = document.theForm;

	
	var city = (theform.city.value).toLowerCase();

	

	var address = theform.address.value;
	var radius = theform.radius.value;
	var zipcode = theform.zipcode.value;
	if(!isNumeric(radius) || radius <= 0 ){
	//	radius=1;
	}
	
	var radim; 
	theform.radiusDist[0].checked ? radim="ft":radim="mi";
	

	holdVars.firstMonth = theform.firstMonth.value-1;
	holdVars.firstDay =theform.firstDay.value;
	holdVars.firstYear = theform.firstYear.value;
	holdVars.lastMonth = theform.lastMonth.value-1;
	holdVars.lastDay =theform.lastDay.value;
	holdVars.lastYear = theform.lastYear.value;
	
	var q;
	theform.radSort[0].checked ? q="1":q="2";
	holdVars.q = q;

	
	lday=theform.lastYear.value+""+addLeadingZeroes(theform.lastMonth.value)+""+addLeadingZeroes(theform.lastDay.value);		
	fday=theform.firstYear.value+""+addLeadingZeroes(theform.firstMonth.value)+""+addLeadingZeroes(theform.firstDay.value);
	
	if(parseFloat(fday)>parseFloat(lday)){
		errormsg+=" End date should be after the start date.";
	}
	

	if(errormsg ==""){
	

		var obj = formAndValidateObject(zipcode, city, address, radius, radim, fday, lday, st, q);
		
		if( (obj.address != "" && obj.city !="") ){
			 geocodeAddy(obj);
		}else if(obj.zipcode !="" && obj.zipcode > 0){
			doFoo(q,0,0,0,obj.varStr, zipcode);
			trace(zipcode);
		}
		else{
			doFoo(q, 0, 0, 0, obj.varStr);
		}
			
	 
	}
	else{
		alert("You need to fix your request: "+errormsg);
	}
	
	
	
}


function resetState(){
	arrDataRows = new Array();
	gArrMarkers = new Array();
	holdVars = new Object();
	isSingleAddress = false;
	HOME_POINT = new Object;
	bounds = new GLatLngBounds();
	map.clearOverlays();
	document.getElementById("resultSearch").style.border = "none";
	document.getElementById("resultSearch").innerHTML = "";
	document.getElementById("resultsMenu").innerHTML = "";	

}

function showLoader(xt){

	var ld = document.getElementById("loading");
	if(xt==0){
		ld.style.display = "none";
	
	}else{
	
		ld.style.top = "300px";
		ld.style.left = document.getElementById("map").offsetLeft + parseInt((document.getElementById("map").offsetWidth)/2) +"px";
		ld.style.display = "block";
	}

}




function addLeadingZeroes(it){
	if(it < 10)
		return String("0"+String(it));
	else
		return String(it);
		

}


function formatMoney(val){

	if(val<=1001)
		return "N/A";

	var v = val+"";
	switch(v.length){
		case 4:
		v= v.substring(0,1)+","+v.substring(1,4);
		break;
		case 5:
		v= v.substring(0,2)+","+v.substring(2,5);
		break;		
		case 6:
		v= v.substring(0,3)+","+v.substring(3,6);
		break;	
		case 7:
		v= v.substring(0,1)+","+v.substring(1,4)+","+v.substring(4,7);
		break;
		case 8:
		v= v.substring(0,2)+","+v.substring(2,5)+","+v.substring(5,8);
		break;		
	}
	
	
	return ("$"+v);
}


function getQV(variable, query) {

	if(query ==null)
		query = window.location.search.substring(1);
	
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	
	return null;
}


function getURL(){
	var q = window.location.href;
	
	var vars = q.split("?");
	return vars[0].replace("#","");
}

function cleanStr(str){
	
	str = str.replace(/%20/g, " ");
	str = str.replace(/[^a-zA-Z 0-9 \-]/g, "");
	return str;
}	

function isNumeric(PossibleNumber)

{

	var PNum = new String(PossibleNumber);

	var regex = /[^0-9.]/;

	
	return !regex.test(PNum);
	
	

}

function trace(str){

//document.getElementById("etc").innerHTML+="<pre>"+str+"</pre>";

}

document.onkeypress = KeyCheck;       


function KeyCheck(e)

{

   var KeyID = (window.event) ? event.keyCode : e.keyCode;
	return KeyID !=13;



}
if (document.layers) document.captureEvents(Event.KEYPRESS);

////JQUERY stuff

