/* General easy way to get the obj element by ID */
function $(id){return document.getElementById(id);}
/* General easy way to get the obj elements collection by name */
function $name(name){return document.getElementsByName(name);}
/* General easy way to get the input value that type is checkbox */
function checkboxValue(obj){
	var result="";
	for(i=0;i<obj.length;i++)
		if(obj[i].checked) result+=obj[i].value;
	return result
}
/* 
	signupCheck if use to check the sign-up part on the new version homepage's footer part 
	The validation is base the info user typed, Special is "End User" and intrested "Residential" or
	"Retail" will have more field need validate. Pass the validate then submit to create 
	new user in Cookies db.
*/
function signupCheck(){
	var emailreg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	var numReg= /\d+/;
	if($("fname").value==""){
		alert("Please input your First Name.");$("fname").focus();return false;
	}else if($("lname").value==""){
		alert("Please input your Last Name.");$("lname").focus();return false;
	}else if(!emailreg.test($("email").value)){
		alert("Please input correct Email Address.");$("email").focus();return false;
	}else if($("zipcode").value==""){
		alert("Please input your Zip Code.");$("zipcode").focus();return false;
	}else if(!checkboxValue($name("typeOfUnit"))){
		alert("Please choice the unit you interest in.");return false;
	}else if($("otherOpt").style.display!="block"){
		return showOpt();
	}else if(!checkboxValue($name("states"))){
		alert("Please select Coverage state you interest in.");return false;
	}else if(!checkboxValue($name("CraigCity"))){
		alert("Please select Coverage Area you interest in.");return false;
	}else  ;
	if(checkboxValue($name("typeOfUnit")).indexOf("Residential")!=-1 || checkboxValue($name("typeOfUnit")).indexOf("Retail")!=-1){
		if(!numReg.test($("minSqFt").value)){alert("Please input the Min Sq Ft(Must be number).");$("minSqFt").focus();return false;}
		if(!numReg.test($("maxSqFt").value)){alert("Please input the Max Sq Ft(Must be number).");$("maxSqFt").focus();return false;}
		if(checkboxValue($name("typeOfunit")).indexOf("Residential")!=-1 && checkboxValidate($name("bedroom"))!=""){alert("Please select the # Bedroom.");return false;}
		if(!numReg.test($("maxRent").value)){alert("Please input the Maximum Rent(Must be number).");$("maxRent").focus();return false;}
	}
	$("signupForm").submit();
}
/* dblookup is for sign up part on home page, use to get the city of state(s) user selected */
function dblookup(dbname,viewname,key,column){
	var url="dblookup?openagent&db="+dbname+"&viewname="+viewname+"&key="+key+"&column="+column+"&end"+Math.random();
	var request = createXmlHttpRequest();
	if (request){
        request.open("Get", url, true);
        request.onreadystatechange = function(){addCraigCity(request);};
        request.send(null);
    }
}
/* addCraigCity check the ajax request status, then update the Coverage Area options */
function addCraigCity(request){
	if((request.readyState == 4) && (request.status == 200)){
		var tmp = request.responseText;
		tmp = tmp.substr(0,tmp.length-1).split(",");
		var htmlTxt = "";
		for(i=0;i<tmp.length;i++){
			if(tmp[i]!="")
				htmlTxt += "<input type='checkbox' name='CraigCity' value='"+tmp[i]+"' />"+tmp[i];
		}
		$("CraigCity").innerHTML = htmlTxt;
	}
}
/* getCraigCity function is click event of the Coverage State option, use to call function update the Coverage Area options */
function getCraigCity(){
	var key="";
	var keyFields=document.getElementsByName("states");
	for(i=0;i<keyFields.length;i++){
		keyFields[i].checked?key+=keyFields[i].value+",":"";
	}
	if(key!=""){
		dblookup("NRentalT.nsf","proBySteCat",key,2);
		$("CraigCity").innerHTML="Data Loading...";
	}
}
/* showOpt is use to show or hidden input base info web user input, for sign up part of new home page's footer. */
function showOpt(){
	$("otherOpt").style.display="block";
	if(checkboxValue($name("userType"))!="Broker"){
		if(checkboxValue($name("typeOfUnit")).indexOf("Residential")!=-1 || checkboxValue($name("typeOfUnit")).indexOf("Retail")!=-1){
			$("minSqFtOpt").style.display="block";
			$("maxSqFtOpt").style.display="block";
			$("maxRentOpt").style.display="block";
		}else{
			$("minSqFtOpt").style.display="";
			$("maxSqFtOpt").style.display="";
			$("maxRentOpt").style.display="";
		}
		if(checkboxValue($name("typeOfUnit")).indexOf("Residential")!=-1)
			$("bedRoomOpt").style.display="block";
		else
			$("bedRoomOpt").style.display="";
	}
	return false;
}
