$(document).ready(function() {
	
	if(location.href.indexOf("#") == -1) {
		
		if (location.href == 'http://localhost:8081/' || location.href == 'http://dev.primefind.net/' || location.href == 'http://www.primefind.net/'){
			try{document.getElementById('area').focus();}catch(e){}
		} else {
			try{document.getElementById('company').focus();}catch(e){}
		}
	}

})

function overlayNewMarkers(){resetMarkers();for(var i=0;i<arrLat.length;i++){if((arrLat[i])&&(arrLng[i])){var point=new GLatLng(arrLat[i],arrLng[i])}if(arrLabel[i]){var marker=createTabbedMarker(point,arrHtml[i],arrLabel[i],arrIcons[i],arrTooltips[i])}else{var marker=createMarker(point,arrName[i],arrHtml[i],arrIcons[i],arrTooltips[i])}if(marker){map.addOverlay(marker);if(i==0){marker.openInfoWindowHtml(arrHtml[i])}}}}function createMarker(point,name,html,thisimage,thistooltip){var marker;if((point)&&(name)&&(html)&&(thisimage)){imgMarkers[intCount]=new GIcon(icon,thisimage);var marker=new GMarker(point,{icon:imgMarkers[intCount],title:thistooltip});gmarkers[intCount]=marker;GEvent.addListener(marker,"click",function(){marker.openInfoWindowHtml(html)});htmls[intCount]=html}intCount++;return marker}function createTabbedMarker(point,htmlsTabs,labels,thisimage,thistooltip){var marker;if((point)&&(htmlsTabs)&&(labels)&&(thisimage)){imgMarkers[intCount]=new GIcon(icon,thisimage);marker=new GMarker(point,{icon:imgMarkers[intCount],title:thistooltip});gmarkers[intCount]=marker;if(htmlsTabs.length>2){htmlsTabs[0]='<div style="width:'+htmlsTabs.length*88+'px">'+htmlsTabs[0]+'<\/div>'}var thisTab=new Array();for(var i=0;i<htmlsTabs.length;i++){thisTab[i]=new GInfoWindowTab(labels[i],htmlsTabs[i])}tabs[intCount]=thisTab;GEvent.addListener(marker,"click",function(){marker.openInfoWindowTabsHtml(thisTab)})}intCount++;return marker}function myclick(i){if(arrName[i]){gmarkers[i].openInfoWindowHtml(htmls[i])}else{gmarkers[i].openInfoWindowTabsHtml(tabs[i])}}function resetMarkers(){map.getInfoWindow().hide();map.clearOverlays();gmarkers=new Array();htmls=new Array();tabs=new Array()}function getData(i){var strUrl='http://localhost/classes/updater.php';var strPars='page='+i;var ar=new Ajax.Updater('clickarea',strUrl,{method:"get",parameters:strPars,evalScripts:true})}
			
			
function formValidator()
{
	// Make quick references to our fields
	var companyName	= document.getElementById('company');
	var contactName	= document.getElementById('contactname');
	var emailAdr	= document.getElementById('email');
	var phoneNum	= document.getElementById('tel');
	var numOfPeople	= document.getElementById('number');

	// Check each input in the order that it appears in the form!
	if(setNotEmpty(companyName, "Please include the Company Name. Thank you"))
	{
		if(setNotEmpty(contactName, "Please include the Contact Name. Thank you"))
		{
			if(setNumeric(numOfPeople, "Please enter the approximate number of people that will be in the office. Thank you"))
			{
				if(emailAdr.value.length > 0 || phoneNum.value.length > 0)
				{
					if(emailAdr.value.length !== 0)
					{	if(setEmail(emailAdr, "Please enter a valid email address please"))
						{
							return true;
						}
					}
					else if(phoneNum.value.length !== 0)
					{
						if(setNumeric(phoneNum, "Please enter a valid phone number (numbers and spaces only)"))
						{
							return true;
						}
					}
				}
				else
				{
					alert('Please include the email address OR phone number. Thank you.');
					return false;
				}
			}
		}
	}
	return false;
}


// If the length of the element's string is 0 then display helper message
function setNotEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.style.background = "#FFF7DF";
		elem.focus(); // set the focus to this input
		return false;
	}
	elem.style.background = "#FFF";
	return true;
}


// If the element's string matches the regular expression it is all letters
function setAlphabet(elem, helperMsg)
{
	var alphaExp = /^[a-zA-Z[:space:]]+$/;
	if(elem.value.match(alphaExp))
	{
		elem.style.background = "#FFF";
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.style.background = "#FFF7DF";
		elem.focus();
		return false;
	}
}


// If the element's string matches the regular expression it is all numbers
function setNumeric(elem, helperMsg)
{
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression))
	{
		elem.style.background = "#FFF";
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.style.background = "#FFF7DF";
		elem.focus();
		return false;
	}
}


// If the element's string matches the regular expression it is numbers and letters
function setAlphanumeric(elem, helperMsg)
{
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp))
	{
		elem.style.background = "#FFF";
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.style.background = "#FFF7DF";
		elem.focus();
		return false;
	}
}


function setEmail(elem, helperMsg)
{
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp))
	{
		elem.style.background = "#FFF";
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.style.background = "#FFF7DF";
		elem.focus();
		return false;
	}
}


function setMinimum(min)
{
	var term = document.getElementById('search-input');
	
	if (term.value.length < min) 
	{
		alert('Please enter an area or post code');
		return false;
	}
	else
	{
		return true;
	}
}
