// JavaScript Document
var xmlHttp;
//general variables
//mustUseJob  if true user's query is validated to make sure a job type was entered
 
var debug= false;
var countries = new Array();
countries['USA'] = new Array();
countries['USA']['counties']=true;
countries['USA']['cities']=true;
countries['USA']['max_zip_length']=5;
countries['USA']['abbr']='us';



countries['INDIA'] = new Array();
countries['INDIA']['counties']=false;
countries['INDIA']['cities']=true;
countries['INDIA']['max_zip_length']=6;
countries['INDIA']['abbr']='in';
							  
 
 
$(document).ready(function(){
    $("#top").hide('fast',function()
								 {
									$("#main").fadeIn('slow'); 
									}
								 );
 

   Nifty("div#topSearchBar","transparent");
								
});



function GetXmlHttpObject(){
if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject){  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}





function stateChanged() {

if (xmlHttp.readyState==4){
 var counties= '<br /><br />';

	counties +=xmlHttp.responseText;
	 
	document.getElementById("secondaryGeoData").innerHTML = counties;
	xmlHttp=null;
	}

}


function doSearch(){
var query 	= document.getElementById('query').value;


if(query.length< 2 && mustUseJob == true){
	alert('Please Enter a Job');
	document.getElementById('query').select();
	document.getElementById('query').focus();
	//document.getElementById('query').style.backgroundColor ='0x330033';
	 
	return;
}

var state 	= document.getElementById('states').options[document.getElementById('states').selectedIndex].value;
var county	=	null;
var zip		= document.getElementById('zip').value;
var city 	= 	document.getElementById('city').value;
var citySelectB = null;

if( document.getElementById('citiesSb') !=null && document.getElementById('citiesSb') && document.getElementById('citiesSb').options[document.getElementById('citiesSb').selectedIndex].value != -1)
	citySelectB = document.getElementById('citiesSb').options[document.getElementById('citiesSb').selectedIndex].value;

 

if(checkZipCode(document.getElementById('zip') )  == false)
	zip = null;
if(city.length <2 )
	city =null;


if(city == null && citySelectB !=null && citySelectB != -1)
	city = citySelectB;



if(state  == -1)
	state = null;

if( document.getElementById('countiesSb') !=null && document.getElementById('countiesSb') &&  document.getElementById('countiesSb').options[document.getElementById('countiesSb').selectedIndex].value != -1)
	county = document.getElementById('countiesSb').options[document.getElementById('countiesSb').selectedIndex].value;

var radius = 10;
var hash=  document.getElementById('hash').value 

var location;
if(zip !=null)
	location = zip;
else if(city != null && state != null)
	location =''+city + '%20'+ state;
else if(city != null && state == null)
	location =''+city;
else if(county != null && state != null)
	location =''+county + '%20'+ state;
else if(state != null)
	location = state;

if(location == undefined || location.length< 2)
	{
	alert('Please Enter a Zip Code or Choose a City and State');
	document.getElementById('zip').select();
	document.getElementById('zip').focus();
		return ;
	}
var queryString = "?q=" + query +'&l='+location + '&state='+state +'&start=0&radius=' + radius + '&co='+countries[country]['abbr'] + '&userSearch='+hash;
queryString += "&sId="+   Math.random() 


 
var u =baseUrl + queryString;

window.location.href=u;

//document.getElementById('debug').innerHTML="<a target=='_blank'  href='"+u+"' />"+u +"</a>";
 
}



function loadSecondBox(){
	
	document.getElementById("secondaryGeoData").innerHTML="<br />Loading...<br /><img src='"+ajaxLoadingImgUrl2 +"' hspace='5' vspace='5' /> <br />";
	
var state = document.getElementById('states').options[document.getElementById('states').selectedIndex].value;
xmlHttp = GetXmlHttpObject();
 
 var url='';
 url += defaultUrl +"?country="+country+'&s='+state;
url += "&sid="+   Math.random() ;
//window.location.href=url;

if(debug)
	document.getElementById('debug').innerHTML="<a target=='_blank'  href='"+url+"' />"+url +"</a>";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}



function checkZipCode(obj){
var asStr = obj.value +"";

if(asStr.length < 3 || asStr.length > countries[country]['max_zip_length'])
	{
	 return false;
	}
for(i=0;i < asStr.length;i++){
	var char =asStr.charCodeAt(i);
	if(char > 31 && (char <48  || char >57 )  )
			{
			 return false;
			}
	}
return true;
}
