///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	CoPilot Creative  
//	Copy Right of Propel Studios, LLC.
//	Developed by Thomas Eisenbeis
//	
//	
//	File Name = propertyRequest.js
// 	This will hold all neccessary functions for calls to actions within site that will allow updates without refresh
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	
	var request_type;
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer"){
		
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
		
	}else{
		
		request_type = new XMLHttpRequest();
		
	}
	
	return request_type;
	
}

var http = createObject();

/* -------------------------- */
/* GET PROPERTY               */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

var nocache = 0;

function getAbout(id) {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('GET', '/includes/aboutContent.php?id='+id+'&nocache='+nocache, true);
	http.onreadystatechange = aboutOutPut;
	http.send(null);
	
}

/* -------------------------- */
/* LOGIN REPLY                */
/* -------------------------- */
/* This will display answer from the call to log in.  Either wrong credentials or a successfull login */

function aboutOutPut() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		values = response.split("|", 2);
		
		switch (values[0]) {
			
			case 'nav1':		   
				document.getElementById('subNav1').innerHTML = '<a href="#" onclick="javascript: getAbout(\'1\');"><img src="images/aboutSubNavRO.png" width="60" height="12" /></a>';
				//document.getElementById('subNav2').innerHTML = '<a href="#" onclick="javascript: getAbout(\'2\');"><img src="images/aHistorySubNav.png" width="97" height="12" class="domroll /images/aHistorySubNavRO.png" /></a>';
				document.getElementById('subNav3').innerHTML = '<a href="#" onclick="javascript: getAbout(\'3\');"><img src="images/ourMissionSubNav.png" width="121" height="12" class="domroll /images/ourMissionSubNavRO.png" /></a>';
				break;
			
			case 'nav2':		   		   		   		   
				document.getElementById('subNav1').innerHTML = '<a href="#" onclick="javascript: getAbout(\'1\');"><img src="images/aboutSubNav.png" width="60" height="12" class="domroll /images/aboutSubNavRO.png" /></a>';
				//document.getElementById('subNav2').innerHTML = '<a href="#" onclick="javascript: getAbout(\'2\');"><img src="images/aHistorySubNavRO.png" width="97" height="12" /></a>';
				document.getElementById('subNav3').innerHTML = '<a href="#" onclick="javascript: getAbout(\'3\');"><img src="images/ourMissionSubNav.png" width="121" height="12" class="domroll /images/ourMissionSubNavRO.png" /></a>';
				break;
			
			case 'nav3':		   		   
				document.getElementById('subNav1').innerHTML = '<a href="#" onclick="javascript: getAbout(\'1\');"><img src="images/aboutSubNav.png" width="60" height="12" class="domroll /images/aboutSubNavRO.png" /></a>';
				//document.getElementById('subNav2').innerHTML = '<a href="#" onclick="javascript: getAbout(\'2\');"><img src="images/aHistorySubNav.png" width="97" height="12" class="domroll /images/aHistorySubNavRO.png" /></a>';
				document.getElementById('subNav3').innerHTML = '<a href="#" onclick="javascript: getAbout(\'3\');"><img src="images/ourMissionSubNavRO.png" width="121" height="12" /></a>';
				break;
				
		}
		
		document.getElementById('contentAbout').innerHTML = values[1];
			
	
	}
	
}



function processSignup() {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	
	var nameCheck = document.getElementById('name').value;
	var emailc = document.getElementById('email').value;
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	if(nameCheck == 'name' || nameCheck == '') {
		
		document.getElementById('errorEmail').style.display = 'none';
		document.getElementById('errorName').style.display = 'block';
		document.getElementById('name').style.border = '2px solid #FF0000';
		document.getElementById('errorName').innerHTML = 'Please enter a name.';
		return false;
		
	} else if (!nameCheck.match(nameRegex)) {
		
		document.getElementById('errorEmail').style.display = 'none';
		document.getElementById('errorName').style.display = 'block';
		document.getElementById('name').style.border = '2px solid #FF0000';
		document.getElementById('errorName').innerHTML = 'Please enter a valid name.';
		return false;
		
	} else if (emailc == 'email' || emailc == '') {
		
		document.getElementById('errorName').style.display = 'none';
		document.getElementById('errorEmail').style.display = 'block';
		document.getElementById('email').style.border = '2px solid #FF0000';
		document.getElementById('errorEmail').innerHTML = 'Please enter an email address.';
		return false;
		
	} else if(!emailc.match(emailRegex) && !emailCheck(emailc)) {
		
		document.getElementById('errorName').style.display = 'none';
		document.getElementById('errorEmail').style.display = 'block';
		document.getElementById('email').style.border = '2px solid #FF0000';
		document.getElementById('errorEmail').innerHTML = 'Please enter a valid email address.';
		return false;
		
	} else {
		
		document.getElementById('errorName').style.display = 'none';
		document.getElementById('errorEmail').style.display = 'none';
	
		// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
		var name = encodeURI(document.getElementById('name').value);
		var email = encodeURI(document.getElementById('email').value);
		
		// Set te random number to add to URL request
		nocache = Math.random();
		
		// Pass the login variables like URL variable
		http.open('GET', '/includes/processContact.php?name='+name+'&email='+email+'&action=newsLetter&nocache='+nocache, true);
		http.onreadystatechange = signupOutput;
		http.send(null);
	
	}
	
}

/* -------------------------- */
/* LOGIN REPLY                */
/* -------------------------- */
/* This will display answer from the call to log in.  Either wrong credentials or a successfull login */

function signupOutput() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if (response == 1) {
		
			document.getElementById('form').innerHTML = '<div id="success">Thanks, you have been added to our list!<br />Keep an eye out for updates and news from C.R. Shea Homes.<br /><div id="close"><a href="#" onclick="javascript:parent.myLightWindow.deactivate();"><img src="/images/close.jpg" width="67" height="25" class="domroll /images/closeRO.jpg" /></a></div></div>';
		
			document.getElementById('error').style.display = 'none';
		
		} else if (response == 2) {
		
			document.getElementById('error').innerHTML = 'There was an error please try again.';
			
		} else if (response == 0) {
		
			document.getElementById('error').innerHTML = 'This email address already present in our list.';
			
		} else {
		
			document.getElementById('error').innerHTML = 'Please enter all fields below';
			
			
		}
			
	
	}
	
}



function emailCheck (emailStr) {

	var checkTLD=1;
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	var emailPat=/^(.+)@(.+)$/;
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	var quotedUser="(\"[^\"]*\")";
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	var atom=validChars + '+';
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
		
			return false;
		   }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	if (len<2) {
		return false;
	}
return true;
}