if(location.href.indexOf('10.0.1.100') != -1){
	var rootdomain="http://"+window.location.hostname+"/neuserealty.com/";
}else if(location.href.indexOf("/dev/neuserealty.com/") != -1){
	var rootdomain="http://"+window.location.hostname+"/dev/neuserealty.com/";
}else{
	var rootdomain="http://"+window.location.hostname+"/";
}

function $(el){return document.getElementById(el);}

function cleanValue(value){
	var value = value.replace(/&/g,'-,-');
	var value = value.replace(/#/g,'-,,-');	
	var value = value.replace(/=/g,'-,,,-');	
	var value = value.replace(/\+/g,'-,,,,-');
	var value = value.replace(/’/g,'\'');
	return value;
}

function loading(area,gif,top){
	if($('loadingDiv')){$('loadingDiv').parentNode.removeChild($('loadingDiv'));return;}
	
	var iconArea = $(area);
	//controlPanel.innerHTML = '';
	
	div = document.createElement('div');
	//div.className = 'onLoad';
	div.style.position = 'fixed'; //fixed so it will show regardless of scroll
	div.id = 'loadingDiv';
	if(top){
		div.style.top = top;
	}else{
		div.style.top = 48+'%';
	}
	div.style.left = '50%';
	div.style.height = '400px';
	div.align = 'center';
	img = document.createElement('img');
	if(gif){
		img.src = rootdomain + 'RootImages/'+gif;
	}else{
		img.src = rootdomain + 'RootImages/onLoad.gif';
	}
	div.appendChild(img);
	iconArea.appendChild(div);
}

function GetXmlHttpObject(url){
//alert('url: '+url);
var xmlHttp=null;
	try{  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){// Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
return xmlHttp;
}


function sendMail(){
var form = document.thisForm;
$('nameWarning').style.display = 'none';
$('emailWarning').style.display = 'none';
$('phoneWarning').style.display = 'none';
$('commentsWarning').style.display = 'none';
$('emailPanelWarning').style.display = 'none';

if(form.txtName.value == ''){$('nameWarning').style.display = '';form.txtName.focus();return false;}else{$('nameWarning').style.display = 'none';}
if(form.txtEmail.value == ''){ $('emailWarning').style.display = '';$('emailWarning').innerHTML = ' - Please insert your email';form.txtEmail.focus();return false;}else{$('emailWarning').style.display = '';}
if(validate(form.txtEmail.value,'email') == false){$('emailWarning').style.display = '';$('emailWarning').innerHTML = ' - Please insert a valid email';form.txtEmail.focus();return false;}else{$('emailWarning').style.display = '';}

var url=rootdomain+"RootIncludes/phpScript.php?req=contact-us.php";
for (i = 0; i < form.elements.length; i++) {
	var elName = form.elements[i].name;
	var elValue = cleanValue(form.elements[i].value);		
	url=url+"&"+elName+"="+elValue;
} //end for(

url=url+"&sid="+Math.random();

	if(!document.getElementById('loadingDiv')){loading('emailPanel');}
//	alert('url: '+url);

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){  alert ("Your browser does not support AJAX!");  return;  } 
	xmlHttp.onreadystatechange=function(){sendMailRsp()};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function sendMailRsp(){
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
		if(document.getElementById('loadingDiv')){document.getElementById('loadingDiv').parentNode.removeChild(document.getElementById('loadingDiv'));}
		alert(xmlHttp.responseText);
		var theGoods = xmlHttp.responseText.split('|');
		if(theGoods[0] == 1){
			$('emailPanel').innerHTML = '<div>Your email has been sent successfully. Thank you for your comments.  We will be in touch with you, shortly.</div>';
		}else{
			$('emailPanelWarning').style.display = '';
			$('emailPanelWarning').innerHTML = '<div>Unfortunately, your email was not sent. There appears to be a problem with this form.  Please contact us using the information above.</div>'			
		}

	}
}

function validate(aStr, type) {
/*------ Function to check inputs ------*/
	var expr = "";
	switch (type){
	case "date" : expr = /^[0-2]{0,1}([1-9]|30|31)\/0{0,1}([0-9]|11|12)\/[0-9]{4,4}$/; break;
	case "email" : expr = /^(.)+@{1,1}((.)+\.(.)+)+$/; break;
	case "integer" : expr = /^[0-9]*$/; break;
	case "decimal" : expr = /^[0-9]*\.{0,1}[0-9]*$/; break;
	case "username" : expr = /^([a-z]|[A-Z]|[0-9])*$/; break;
	case "password" : expr = /^([a-z]|[A-Z]|[0-9])*$/; break;
	case "string" : expr = /^([a-z]|[A-Z]|[ ]|[.])*$/; break;
	case "phonenumber" : expr = /^([0-9]|[ ]|[+]|[-])*$/; break;
	}
	if(expr != "")
		return expr.test(Trim(aStr));
	else
		return false;
}

function Trim(nStr){return nStr.replace(/(^\s*)|(\s*$)/g, "");}
