
var gForm = "";

if(document.MojoStSeoSearchForm)
	gForm = "MojoStSeoSearchForm";
else if(document.MojoSeoSearchForm)
	gForm = "MojoSeoSearchForm";
else
	gForm = "SearchForm";

var busloc = "";
var wbSiteWSn = new String();

function ValidatesearchBusiness(frmname,act,wbSite)
{
	/* Commenmtd by for Bug No QC 12	
	if(document.MojoStSeoSearchForm)
		gForm = "MojoStSeoSearchForm";
	else if(document.MojoSeoSearchForm)
		gForm = "MojoSeoSearchForm";
	else
		gForm = "SearchForm";
	*/
	gForm = frmname.name;
	
	wbSiteWSn = wbSite;	
	//frm = document.SearchForm;
	frm = eval("document."+gForm);
	var is_zip = 0;	

	if(frm.busKeyword.value.match(/^\s*$/) || frm.busKeyword.value=='name or category')
	{
		alert("Please enter a business name or category to perform a search");
		frm.busKeyword.focus();
		return false;
	}
	
	if(frm.buslocation.value.match(/^\s*$/))
	{
		alert("Please enter the location as City, State");
		frm.buslocation.focus();
		return false;
	}
	
	searchLocation=trimAllVSerarch(frm.buslocation.value);
	if(searchLocation.length<5){
		alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
		return false;
		frm.buslocation.focus();
	}
	locationArray=searchLocation.split(",");	
	if(locationArray.length==1)
	{ 
		alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
		frm.buslocation.focus();
		return false;					
		// User has enteredeither city or zipcode only
		/*
		if(isNaN(locationArray[0])){
			// If city only
			if(locationArray[0].match(/[0-9]+/)){
				//alert("Please enter proper city name i.e. San Diego");
				alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
				frm.buslocation.focus();
				return false;
			} 
		}else if(locationArray[0].length>5){
			// If zip code only			
			alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;					
		} */
	}
	else if(locationArray.length==2)
	{
		// location is having more than one element
		locationArray[0]=trimAllVSerarch(locationArray[0]);
		locationArray[1]=trimAllVSerarch(locationArray[1]);
		
		if(locationArray[0].match(/[0-9]+/)){
				//alert("Please enter proper city name i.e. San Diego, CA");
				alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
				frm.buslocation.focus();
				return false;
		}
		else 
		{			
			stateArray=locationArray[1].split(" ");
			if(stateArray.length>2){
				alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
				frm.buslocation.focus();
				return false;
			}
			stateArray[0]=trimAllVSerarch(stateArray[0]);
			if(stateArray[0].match(/[0-9]+/)){
				//alert("Please enter proper State name.");
				alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
				frm.buslocation.focus();
				return false;
			} 
			
			if(stateArray[0].length<2){				
				alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
				frm.buslocation.focus();
				return false;
			}	
			if(stateArray.length==2){		
				stateArray[1]=trimAllVSerarch(stateArray[1]);
				if(!stateArray[1].match(/^[0-9]{5}$/)){				
					//alert("Please enter proper Zipecode after state name, Entered zip code should be of five charaters");
					alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
					frm.buslocation.focus();
					return false;
				}
			}
		}
	}
	else if(locationArray.length==3)
	{		
		// location is having more than one element
		locationArray[0]=trimAllVSerarch(locationArray[0]);
		locationArray[1]=trimAllVSerarch(locationArray[1]);
		locationArray[2]=trimAllVSerarch(locationArray[2]);
		if(locationArray[0].match(/[0-9]+/))
		{
			//alert("Please enter proper city name i.e. San Diego, CA");
			alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;
		}
		else if(locationArray[1].length>2)
		{
			alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;
		}
		else if(locationArray[1].match(/[0-9]+/))
		{
			//alert("Please enter proper State name.");
			alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;
		}
		else if(!locationArray[2].match(/^[0-9]{5}$/))
		{				
			//alert("Please enter proper Zipecode after state name, Entered zip code should be of five charaters");
			alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;
		}
		busloc = locationArray[2];
	}
	else
	{
		alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
		frm.buslocation.focus();
		return false;
	}

	if(busloc == "")
		busloc = frm.buslocation.value;
	
	if(!isNaN(busloc))
	{
		is_zip = 1;
	}
	else if(act == 1)
	{
		busloc = ReplaceSpecialCharsWithBlank(busloc);		
		buslocArray=busloc.split(",");
		
		state = '';
		city = '';
		
		if(buslocArray[0] != '' && busloc.length>0)
		{
			city = trimAllVSerarch(buslocArray[0].toLowerCase());
			city = city.replace(/ /g,"-");
			city = ReplaceSpecialCharsWithBlank(city);
		}
		
		if(buslocArray[1] != '' && busloc.length>0)
		{
			state = trimAllVSerarch(buslocArray[1]);
			rowstr = trimAllVSerarch(buslocArray[1]);
			rowstrarr = rowstr.split(" ");
			stateLwr = rowstrarr[0].toLowerCase();						
			stateLwr = stateLwr.replace(/ /g,"");
			stateLwr = stateLwr.replace(/[ \f\n\r\t\v]+/g,""); 	
			stateLwr = stateLwr.replace(/(^\s+)|(\s+$)/g,"");
			stateLwr = stateLwr.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
			stateLwr = stateLwr.replace(/\s/g,"");								
			stateLwr = ReplaceSpecialCharsWithBlank(stateLwr);
			state = trimAllVSerarch(stateLwr);
		}
		
		keyword = frm.busKeyword.value.replace(/&/g,"-amp-");
		keyword = trimAllVSerarch(keyword);
		keyword = ReplaceSpecialCharsWithBlank(keyword);
		keyword = keyword.replace(/ /g,"-");
		keyword = keyword.replace(/%/g,"%25");
		keyword = keyword.toLowerCase();
		
		//showkeyword = frm.busKeyword.value;
		//showkeyword = ReplaceSpecialCharsWithBlank(showkeyword);
		if(keyword.match(/^\s*$/))
		{
			alert("Please enter a business name or category to perform a search..");
			frm.busKeyword.focus();
			return false;
		}
		if(state.match(/^\s*$/) || city.match(/^\s*$/))
		{
			alert("Please Enter your location in the following format.\nCity, State (San Diego, CA)");
			frm.buslocation.focus();
			return false;
		}
	
		url = wbSite+"s/"+trimAllVSerarch(keyword)+"/"+trimAllVSerarch(city)+"/"+trimAllVSerarch(state);			
		ValidateSearch_H(url);
		
	}
	
	if(act==1 && is_zip)
	{		
		alert("Enter your location in the following format.\nCity, State (San Diego, CA)");
		frm.buslocation.focus();
		return false;
	}
}

function ReplaceSpecialCharsWithBlank(keyword)
{	
	var arr;
	arr = new Array('!','@','#','$','%','^','(',')','{','}','[',']','/','\\','~','`','<','>','"','?');
	for(i=0; i<arr.length; i++)
	{
		while (keyword.indexOf(arr[i]) > -1) 
		{
			keyword = keyword.replace(arr[i],"");
		}
	}
	return keyword;	
}

function ValidateSearch_H(url)
{
	if(gForm == "SearchForm" || gForm == "MojoStSeoSearchForm" || gForm == "MojoSeoSearchForm")
	{
		gfrm = eval("document."+gForm);
		gfrm.action = url;			
		try{
			gfrm.submit();
		}catch(e){}
	}

}

function trimAllVSerarch(sString) 
{	
	while(sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while(sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function focusBlank(ele)
{
	ele.value='';
}