var gForm = "SearchForm1";
function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp = false;
	}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try
		{ 
			xmlhttp = new XMLHttpRequest(); 
		} 
		catch(e) 
		{ 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp;        
}
var ObjHTTP5 = getHTTPObject();
var busloc = "";
function ValidatesearchBusiness(frmname,act)
{
	//frm = document.SearchForm1;
	if(frmname == "SearchFormEND")
		gForm = "SearchFormEND";

	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 or Zip");
		frm.buslocation.focus();
		return false;
	}
	
	if(frm.busKeyword.value.match(/#/))
	{
		alert("Business name should not have special character like '#'.");
		frm.buslocation.focus();
		return false;
	}
	
	searchLocation=trimAll(frm.buslocation.value);
	if(searchLocation.length<5){
		alert("Search location should be more than 4 characters.\nEnter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
		return false;
		frm.buslocation.focus();
	}
	if(searchLocation.indexOf(',')!=-1)
	{
		locationArray=searchLocation.split(",");
		if(locationArray.length==1)
		{ 
			// 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 one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
					frm.buslocation.focus();
					return false;
				} 
			}else if(locationArray[0].length>5){
				// If zip code only			
				alert("Entered zip code should be of five charaters");
				frm.buslocation.focus();
				return false;					
			}
		}
		else if(locationArray.length==2)
		{
			// location is having more than one element
			locationArray[0]=trimAll(locationArray[0]);
			locationArray[1]=trimAll(locationArray[1]);
			if(locationArray[0].match(/[0-9]+/)){
					//alert("Please enter proper city name i.e. San Diego, CA");
					alert("Enter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
					frm.buslocation.focus();
					return false;
			}
			else 
			{			
				stateArray=locationArray[1].split(" ");
				if(stateArray.length>2){
					alert("Enter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
					frm.buslocation.focus();
					return false;
				}
				stateArray[0]=trimAll(stateArray[0]);
				if(stateArray[0].match(/[0-9]+/)){
					//alert("Please enter proper State name.");
					alert("Enter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
					frm.buslocation.focus();
					return false;
				} 
				if(stateArray[0].length<2){				
					alert("Enter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
					frm.buslocation.focus();
					return false;
				}	
				if(stateArray.length==2){		
					stateArray[1]=trimAll(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 one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nZip (92107)\nCity, State, zip (San Diego, CA, 92107)");
						frm.buslocation.focus();
						return false;
					}
				}
			}
		}
		else if(locationArray.length==3)
		{		
			// location is having more than one element
			locationArray[0]=trimAll(locationArray[0]);
			locationArray[1]=trimAll(locationArray[1]);
			locationArray[2]=trimAll(locationArray[2]);
			if(locationArray[0].match(/[0-9]+/))
			{
				//alert("Please enter proper city name i.e. San Diego, CA");
				alert("Enter your location in one of the following formats.\nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
				frm.buslocation.focus();
				return false;
			}
			else if(locationArray[1].length>2)
			{
				alert("Enter your location in one of the following formats.\nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
				frm.buslocation.focus();
				return false;
			}
			else if(locationArray[1].match(/[0-9]+/))
			{
				//alert("Please enter proper State name.");
				alert("Enter your location in one of the following formats.\nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
				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 one of the following formats.\nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
				frm.buslocation.focus();
				return false;
			}
			busloc = locationArray[2];
		}
	}
	else
	{
		if(!searchLocation.match(/^[0-9]{5}$/))
		{
			alert("Enter your location in one of the following formats.\nCity (San Diego) \nCity, State (San Diego, CA)\nCity, State zip (San Diego, CA 92107)\nCity, State, zip (San Diego, CA, 92107)\nZip (92107)");
			frm.buslocation.focus();
			return false;
		}
	}

	if(act==2)
		url=HTTP_LOCATION+'?mysession='+ encodeBase64("reviews_SearchBusiness") +'&buslocation='+frm.buslocation.value+'&busKeyword='+frm.busKeyword.value.replace(/&/g,"-amp-")+'&mode=search';

	if(busloc == "")
		busloc = frm.buslocation.value;

	if(!isNaN(busloc))
	{
		is_zip = 1;
	}
	else if(act == 1)
	{
		buslocArray=busloc.split(",");
		city = trimAll(buslocArray[0].toLowerCase());
		city = city.replace(/ /g,"-");
		state = trimAll(buslocArray[1]);
		rowstr = trimAll(buslocArray[1]);
		rowstrarr = rowstr.split(" ");
		state = trimAll(rowstrarr[0].toLowerCase());
		keyword = frm.busKeyword.value.replace(/&/g,"-amp-");
		keyword = ReplaceSpecialCharsWithBlank(keyword);
		keyword = keyword.replace(/ /g,"-");
		keyword = keyword.replace(/%/g,"%25");
		keyword = keyword.toLowerCase();
		showkeyword = frm.busKeyword.value;
		showkeyword = ReplaceSpecialCharsWithBlank(showkeyword);
		document.getElementById('showkey').innerHTML = showkeyword;	
		//url = HTTP_LOCATION+"cs/"+keyword+"/"+city+"/"+state;
		url = "http://www.mojopages.com/s/" +keyword+"/"+city+"/"+state;
		ValidateSearch_H(url);
	}
	
	if(act==1 && is_zip)
	{
		getCityState(busloc);
	}
	else if(act == 2)
	{
		document.getElementById('showkey').innerHTML = frm.busKeyword.value;
		ValidateSearch_H(url);
	}		
}
function getCityState(busloc)
{
	var url = "";	
	url = HTTP_LOCATION+"?mysession=" + encodeBase64('business_getCityState');
	url = url+"&zip="+busloc;
	ObjHTTP5.open("GET", url , true);
	ObjHTTP5.onreadystatechange = cb_getCityState;
	ObjHTTP5.send(null);
}
function cb_getCityState()
{	
	if (ObjHTTP5.readyState == 4 && ObjHTTP5.status == 200)	
	{				
		var str = ObjHTTP5.responseText;
		buslocarr = str.split(",");
		city = trimAll(buslocarr[0].toLowerCase());
		city = city.replace(/ /g,"-");
		state = trimAll(buslocarr[1].toLowerCase());
		keyword = frm.busKeyword.value.replace(/&/g,"-amp-");
		keyword = ReplaceSpecialCharsWithBlank(keyword);
		keyword = keyword.replace(/ /g,"-");
		keyword = keyword.replace(/%/g,"%25");
		keyword = keyword.toLowerCase();
		showkeyword = frm.busKeyword.value;
		showkeyword = ReplaceSpecialCharsWithBlank(showkeyword);
		document.getElementById('showkey').innerHTML = showkeyword;	
		//url = HTTP_LOCATION+"cs/"+keyword+"/"+city+"/"+state;
		url = "http://www.mojopages.com/s/" +keyword+"/"+city+"/"+state;
		ValidateSearch_H(url);
	}
}
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(document.getElementById('videoDiv'))
	{
		document.getElementById('videoDiv').style.visibility="hidden";
	}
	showTimer(20,30,"searchTimer");
	//document.getElementById('imgLoading').style.display = "";
	
	if (gForm == "SearchForm1")
	{
		window.location = url;
	}
	else if (gForm == "SearchFormEND")
	{
		gfrm = eval("document."+gForm);
		gfrm.action = url;	
		gfrm.submit();
	}		
}
function trimAll(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;
}
var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""
function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function mojotip(){
	if (ns6||ie){
		tipobj.innerHTML="Trust Level is a points based system MojoPages uses to rank the &quot;trustworthiness&quot; of members.  You will earn 1 point of Trust for every review you write and <span class=TxtOrangeBld>1</span> additional point of Trust for every person who votes one of your reviews as &quot;Helpful&quot;. By earning Trust Points you help other people to &quot;Trust&quot; your reviews because they know you are helpful, involved and respected in the community."
		enabletip=true
		return false
	}
}

function showreviewtip(ReviewTxt){
	if (ns6||ie){
		tipobj.innerHTML=ReviewTxt;
		enabletip=true
		return false
	}
}

function positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
		
		// if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
			tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
			tipobj.style.left="5px"
		else
			tipobj.style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
			tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
		tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"		
	}
}

function hidemojotip(){
	if (ns6||ie){
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}
document.onmousemove = positiontip;

function checkEnter(e){
	var keyCode1 = null;	
	if(e.which) 
	{
	  keyCode1 = e.which;
	}
	else if( e.keyCode )
	{
	  keyCode1 = e.keyCode;
	}	
	if(13 == keyCode1)
	{
		if(document.searchForm && document.searchForm.busKeywordC != null &&  document.searchForm.buslocationC != null)
		{
			if(document.searchForm.busKeywordC.value != "" &&  document.searchForm.buslocationC.value != "")
				ValidateSearch(1);
			else	
				ValidatesearchBusiness('SearchForm1',1);
		}
		else
		{
			ValidatesearchBusiness('SearchForm1',1);
		}	
		return false;
	}
	return true;
}

function checkBusiness()
{
	if(document.SearchForm1.busKeyword.value == "name or category")
		document.SearchForm1.busKeyword.value="";
	return true;
}
var	BrowserDetect = {
init: function () {
	this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version";
	this.OS = this.searchString(this.dataOS) || "an unknown OS";
},
searchString: function (data) {
	for (var i=0;i<data.length;i++)	{
		var dataString = data[i].string;
		var dataProp = data[i].prop;
		this.versionSearchString = data[i].versionSearch || data[i].identity;
		if (dataString) {
			if (dataString.indexOf(data[i].subString) != -1)
				return data[i].identity;
		}
		else if (dataProp)
			return data[i].identity;
	}
},
searchVersion: function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
},
dataBrowser: [
	{ 	string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		prop: window.opera,
		identity: "Opera"
	},
	{
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	},
	{		// for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	},
	{
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	},
	{
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	},
	{ 		// for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}
],
dataOS : [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
]

};
BrowserDetect.init();

function showTimer(H, W, Id)
{
	var DvLayerId = document.getElementById("searchBackground"); 			// Var for Main full Opaque Layer
	var DvMessageId = document.getElementById(Id); // Var for Main content div popup Layer
	var ScrollHeight = document.body.scrollHeight; 														// Value for scroll height of the page
	var ScrollWidth = document.body.scrollWidth; 																// Value for scroll width of the page
	var ClientHeight = document.documentElement.clientHeight; 			// Value for browser height of the page
	var ClietnWidth = document.documentElement.clientWidth; 					// Value for Browser width of the page
	var OffsetHeight = document.documentElement.offsetHeight; 			// Value for scroll height for Safari of the page
	var OffsetWidth = document.documentElement.offsetWidth; 					// Value for height height for Safari of the page
		
	DvLayerId.style.display="";
	
	 if(ScrollHeight<ClientHeight)
	 {
		if(BrowserDetect.browser=="Explorer" || BrowserDetect.browser=="Firefox" || BrowserDetect.browser=="Netscape")
			DvLayerId.style.height=ClientHeight+30+"px";
		else
			DvLayerId.style.height=ScrollHeight+30+"px";
	}
	else
		DvLayerId.style.height=ScrollHeight+100+"px";
	
	PosLeft = W/2;
	
	DvLayerId.style.width=ClietnWidth+"px";
	DvLayerId.style.top=0+"px";
	DvLayerId.style.left=0+"px";
	DvMessageId.style.left=70+"px";
	DvMessageId.style.top=270+"px";
	DvMessageId.style.width=900+"px";
	DvMessageId.style.display="";
}
var base64Str;
var base64Count;
var END_OF_INPUT = -1;
var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}
