/*==================================================
	Owner:	Smart e-Solutions, Inc.
	Title:		Smart e-Solutions Javascript Document
	Author:	Steve Kiernan
	Date: 		05/06/09
	Content:	Form Validation & Ajax Processing
===================================================*/

// Clear Form Field onFocus
function clearMe(whoClicked) {
	document.getElementById(whoClicked).innerHTML = "";
}

// Check for valid email address
function isValidEmail(strEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	// search email text for regular exp matches
	if (strEmail.search(validRegExp) == -1) {
		//alert('A valid e-mail address is required.\nPlease amend and retry');
		return false;
	} 
	return true; 
}

/*\\\\\\\\\\  START: Form Validation >>  Contact  \\\\\\\\\\*/
function validateContact(thisform, thisformID, formhandler) {
	valid = true;
	hasFocus = false;
	emailValid = true;
	
	//Get the values from the form
	var botVal = thisform.contact_body.value;
	var req1 = thisform.contact_name.value;
	var req2 = thisform.contact_email.value;
	var req3 = thisform.contact_message.value;
	emailValid = (isValidEmail(req2));

	//Check to see if the botDiv is empty (spam deterent)
	if (botVal == null || botVal == "") {
		valid = true; // Human interaction
	}
	else {
		valid = false; // Non-Human interaction (spambot)
	}
	
	//Check to see if any required fields are missing
	if ( req1 == null || req1 == "" || req1.charAt(0) == ' ' || req2 == null || req2 == "" || req2.charAt(0) == ' ' || emailValid == false  || req3 == null || req3 == "" || req3.charAt(0) == ' ' ||  req4 == null || req4 == "" || req4.charAt(0) == ' '){
		document.getElementById("errMess1").innerHTML = "Please correct the highlighted items below.<ul id='errList1'></ul>";
		document.getElementById("errMess1").className = "show";
		//set the tab index after validation
		thisform.contact_name.tabIndex = 1;
		thisform.contact_email.tabIndex = 2;
		thisform.contact_message.tabIndex = 3;
		valid = false;
		//Something failed... check to see what is was
		//Checking Required Field #1 (NAME)
		if ( req1 == null || req1 == "" || req1.charAt(0) == ' ' || isNaN(req1) == false ){
			li = document.createElement('li');	
			li.innerHTML = "Please enter a value for <strong>NAME</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("name").style.background = "#FFCC66";
			thisform.contact_name.value = "";
			if (hasFocus==false){
				thisform.contact_name.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("name").style.background = "#FFF";
		}
		//Checking Required Field #2 (EMAIL)
		if ( req2 == null || req2 == "" || req2.charAt(0) == ' ' || emailValid == false ){
			li = document.createElement('li');
			li.innerHTML = "Please enter a valid <strong>EMAIL ADDRESS</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("email").style.background = "#FFCC66";
			thisform.contact_email.value = "";
			if (hasFocus==false){
				thisform.contact_email.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("email").style.background = "#FFF";
		}
		//Checking Required Field #3 (MESSAGE)
		if ( req3 == null || req3 == "" || req3.charAt(0) == ' ' ){
			li = document.createElement('li');
			li.innerHTML = "Please compose a brief <strong>MESSAGE</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("message").style.background = "#FFCC66";
			if (hasFocus==false){
				thisform.contact_message.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("message").style.background = "#FFF";
		}
	}
	else { // All required fields have been populated... set valid to true
		valid = true;
	}
	
	if (valid == true){ // If form is valid... call Ajax function
		ajaxFunction(thisform, thisformID, formhandler);
	}
	return false; // Form is not valid... do nothing
}
// Reset Contact Form back to it's original state
function clearContact (thisform) {
	document.getElementById("errMess1").className = "hide";
	document.getElementById("name").style.background = "#FFF";
	document.getElementById("email").style.background = "#FFF";
	document.getElementById("message").style.background = "#FFF";
}
/*\\\\\\\\\\  END: Form Validation >> Contact  \\\\\\\\\\*/


/*\\\\\\\\\\  START: Form Validation >>  30 Day Trial  \\\\\\\\\\*/
function validateTrial(thisform, thisformID, formhandler) {
	valid = true;
	hasFocus = false;
	emailValid = true;
	
	//Get the values from the form
	var botVal = thisform.trial_body.value;
	var req1 = thisform.trial_name.value;
	var req2 = thisform.trial_company.value;
	var req3 = thisform.trial_email.value;
	emailValid = (isValidEmail(req3));

	//Check to see if the botDiv is empty (spam deterent)
	if (botVal == null || botVal == "") {
		valid = true; // Human interaction
	}
	else {
		valid = false; // Non-Human interaction (spambot)
	}
	
	//Check to see if any required fields are missing
	if ( req1 == null || req1 == "" || req1.charAt(0) == ' ' || req2 == null || req2 == "" || req2.charAt(0) == ' '  || req3 == null || req3 == "" || req3.charAt(0) == ' ' || emailValid == false ){
		document.getElementById("errMess1").innerHTML = "Please correct the highlighted items below.<ul id='errList1'></ul>";
		document.getElementById("errMess1").className = "show";
		//set the tab index after validation
		thisform.trial_name.tabIndex = 1;
		thisform.trial_company.tabIndex = 2;
		thisform.trial_email.tabIndex = 3;
		valid = false;
		//Something failed... check to see what is was
		//Checking Required Field #1 (NAME)
		if ( req1 == null || req1 == "" || req1.charAt(0) == ' ' || isNaN(req1) == false ){
			li = document.createElement('li');	
			li.innerHTML = "Please enter a value for <strong>NAME</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("name").style.background = "#FFCC66";
			thisform.trial_name.value = "";
			if (hasFocus==false){
				thisform.trial_name.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("name").style.background = "#FFF";
		}
		//Checking Required Field #2 (COMPANY)
		if ( req2 == null || req2 == "" || req2.charAt(0) == ' ' ){
			li = document.createElement('li');
			li.innerHTML = "Please enter a value for <strong>COMPANY NAME</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("company").style.background = "#FFCC66";
			if (hasFocus==false){
				thisform.trial_company.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("company").style.background = "#FFF";
		}
		//Checking Required Field #3 (EMAIL)
		if ( req3 == null || req3 == "" || req3.charAt(0) == ' ' || emailValid == false ){
			li = document.createElement('li');
			li.innerHTML = "Please enter a valid <strong>EMAIL ADDRESS</strong>";
			document.getElementById("errList1").appendChild(li);
			document.getElementById("errMess1").className = "show";
			document.getElementById("email").style.background = "#FFCC66";
			thisform.trial_email.value = "";
			if (hasFocus==false){
				thisform.trial_email.focus();
				hasFocus = true;
			}
			valid = false;
		}
		else {
			document.getElementById("email").style.background = "#FFF";
		}
	}
	else { // All required fields have been populated... set valid to true
		valid = true;
	}
	
	if (valid == true){ // If form is valid... call Ajax function
		ajaxFunction(thisform, thisformID, formhandler);
	}
	return false; // Form is not valid... do nothing
}
// Reset Contact Form back to it's original state
function clearContact (thisform) {
	document.getElementById("errMess1").className = "hide";
	document.getElementById("name").style.background = "#FFF";
	document.getElementById("company").style.background = "#FFF";
	document.getElementById("email").style.background = "#FFF";
}
/*\\\\\\\\\\  END: Form Validation >> 30 Day Trial  \\\\\\\\\\*/


/*\\\\\\\\\\  START: AJAX Functions  \\\\\\\\\\*/
function ajaxFunction(thisform, thisformID, formhandler) {
	var url = formhandler;
	var formdata = "";
	var xmlHttp;
	
	// Loop through form fields
	for (i=0; i < thisform.length; i++)	{ //Build Send String
		if (thisform.elements[i].type == "reset" || thisform.elements[i].type == "submit") { // Handle buttons
			formdata = formdata;
		}
		else if (thisform.elements[i].type == "text") { //Handle Textbox's
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
		else if (thisform.elements[i].type == "textarea") { //Handle textareas
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
		else if (thisform.elements[i].type == "checkbox") { //Handle checkbox's
			formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
		}
		else if (thisform.elements[i].type == "radio") { //Handle Radio buttons
			if (thisform.elements[i].checked==true) {
				formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
			}
		}
		else { //finally, this should theoretically be a select box.
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
	}
	//alert(formdata);
	
	// Setup HttpRequest
	try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari  
	catch (e) { // Internet Explorer  
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer 6.0+
	  catch (e) {
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } // Internet Explorer 5.5+
	    catch (e) {
				alert("Your browser does not support AJAX!"); // user has a very outdated browser
				return false;     
			}   
		}  
	} 
	
	// Process the Contact Form
	if (thisformID=="form_quickContact") {
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4) { // 4 = Loaded
				document.getElementById("errMess1").className="hide";
				document.getElementById("submitSuccessOverlay").className="show";
				document.getElementById("submitProcessingCopy").className="show";
			}
			if(xmlHttp.readyState==4) { // 4 = Loaded
				if (xmlHttp.status==200) { // 200 = OK
					// ...code to execute
					document.getElementById("submitSuccessCopy").innerHTML=xmlHttp.responseText;
					document.getElementById("submitSuccessCopy").className="show";
					document.getElementById("submitProcessingCopy").className="hide";
		    }
				else { 
					alert(xmlHttp.responseText); 
					document.getElementById("submitProcessingCopy").className="hide";
					document.getElementById("submitErrorCopy").className="show";
					document.getElementById("submitErrorCopy").innerHTML=xmlHttp.responseText;
				}
			}
		}
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", formdata.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(formdata);
		//staps the form from submitting normally
		return false;
	}
	
	// Process the 30 Day Trial Request Form 
	else if (thisformID=="form_accpacTrial") {
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4) { // 4 = Loaded
				document.getElementById("errMess1").className="hide";
				document.getElementById("submitSuccessOverlay").className="show";
				document.getElementById("submitProcessingCopy").className="show";
			}
			if(xmlHttp.readyState==4) { // 4 = Loaded
				if (xmlHttp.status==200) { // 200 = OK
					// ...code to execute
					document.getElementById("submitSuccessCopy").innerHTML=xmlHttp.responseText;
					document.getElementById("submitSuccessCopy").className="show";
					document.getElementById("submitProcessingCopy").className="hide";
		    }
				else { 
					alert(xmlHttp.responseText); 
					document.getElementById("submitProcessingCopy").className="hide";
					document.getElementById("submitErrorCopy").className="show";
					document.getElementById("submitErrorCopy").innerHTML=xmlHttp.responseText;
				}
			}
		}
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", formdata.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(formdata);
		//staps the form from submitting normally
		return false;
	}
	
	// Handle Exception
	else {
		alert('There was an unexpected error processing the request.');
		return false;
	}
}
/*\\\\\\\\\\  END: AJAX Functions  \\\\\\\\\\*/
