﻿// Function to validate inputs on form prior to submission
//
function validateForm()  {

	var isError = 0;
	var hasEmail = 0;
	var hasPhone = 0;

	document.EmailWTRequest.fromPage.value = document.body.id;
	
	document.EmailWTRequest.fromName.style.background = "white";		
	document.EmailWTRequest.fromEmail.style.background = "white";
	document.EmailWTRequest.fromPhone.style.background = "white";		
	document.getElementById('fixName').style.display = 'none';
	document.getElementById('fixName').style.visibility = 'hidden';
	document.getElementById('fixContact').style.display = 'none';
	document.getElementById('fixContact').style.visibility = 'hidden';
	document.getElementById('fixEmail').style.display = 'none';
	document.getElementById('fixEmail').style.visibility = 'hidden';
	document.getElementById('fixPhone').style.display = 'none';
	document.getElementById('fixPhone').style.visibility = 'hidden';

// Make sure name has been entered (length >0)
	if (document.EmailWTRequest.fromName.value.length == 0) {
		document.EmailWTRequest.fromName.style.background = "#ff9184";
		document.getElementById('fixName').style.display = 'block';		
		document.getElementById('fixName').style.visibility = 'visible';	
		isError = 1;
	}

// Make sure email address is valid (something@something.something) is something has been entered
	if (document.EmailWTRequest.fromEmail.value.length > 0 && eCheck(document.EmailWTRequest.fromEmail.value) == false) {
		document.EmailWTRequest.fromEmail.style.background = "#ff9184";
		document.getElementById('fixEmail').style.display = 'block';		
		document.getElementById('fixEmail').style.visibility = 'visible';
		isError = 1;
		hasEmail = 1;
	}
	else if (document.EmailWTRequest.fromEmail.value.length > 0) {
		hasEmail = 1;
	}

// Make sure phone number is valid (at least 10 numeric digits) if something has been entered
	var nums = 0;
	var str = document.EmailWTRequest.fromPhone.value;

	for (var i=0; i<str.length; i++) {
		if (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) {
			nums++;
		}
	}

	if (str.length > 0 && nums < 10) {
		document.EmailWTRequest.fromPhone.style.background = "#ff9184";	
		document.getElementById('fixPhone').style.display = 'block';			
		document.getElementById('fixPhone').style.visibility = 'visible';
		isError = 1;		
		hasPhone = 1;
	}
	else if (str.length > 0) {
		hasPhone = 1;
	}

// Make sure either email or phone has been entered
	if (hasEmail + hasPhone  == 0) {
		isError = 1;
		document.getElementById('fixContact').style.display = 'block';			
		document.getElementById('fixContact').style.visibility = 'visible';
		document.EmailWTRequest.fromEmail.style.background = "#ff9184";
		document.EmailWTRequest.fromPhone.style.background = "#ff9184";	
	}

// Display error message and stop form submission if an error was detected.
	if (isError) {
		showPopUp('errors');
		return false;
	}
}


// Function validate structure of email address - make sure it is x@y.z format
function eCheck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length - 1;
	var ldot = str.indexOf(dot);
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|,<>+=#$%&*~`';
	var i = 0;

	// If no @ or @ in 1st position or @ in last position
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	// If no . or . in 1st position or . in last position
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}

	// If more than one @
	if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	}

	// If . immediately before or after @
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	}

	// If not at least one . after @
	if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	}

	// If address contains any spaces
	if (str.indexOf(" ")!=-1){
	    return false;
	 }

	// If address contains any invalid characters
	for (i=0; i<invalidChars.length; i++) {
		if (str.indexOf(invalidChars.charAt(i),0) > -1) {
			return false;
		}
	}

	// If address contains any non-ASCII characters
	for (i=0; i<str.length; i++) {
		if (str.charCodeAt(i)>127) {
			return false;
		}
	}

	// Otherwise address looks good
	return true;					
}


function showPopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	cvr.style.display = "block"
	dlg.style.display = "block"
	document.getElementById('closeButton').focus();	
}


function closePopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	cvr.style.display = "none"
	dlg.style.display = "none"
}


function setMouse(ptype) {
	document.body.style.cursor = ptype;
}	

function toggleCBox(cbitem) {
	if (cbitem.checked == false)
		cbitem.checked = true;
	else
		cbitem.checked = false;
}

