/*
*
* E-Mail Vision Generic Join Webform Validation JavaScript - (c) 2002
*
*
*/

//var contactForm = document.getElementById('contactForm');
var contactForm = document.getElementById('contactForm');
// Script Pop Up Window
function popupWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}


// This function retrieves the values of the DOB drop down lists and concatenate them into the 'DOB_Name' hidden field
function makeDate(dob_m, dob_d, dob_y) {
	if  ( (document.contactForm.dob_m.options[document.contactForm.dob_m.selectedIndex].value != '')&&(document.contactForm.dob_d.options[document.contactForm.dob_d.selectedIndex].value != '')&&(document.contactForm.dob_y.options[document.contactForm.dob_y.selectedIndex].value != '') ) {
		var month = String(document.contactForm.dob_m.options[document.contactForm.dob_m.selectedIndex].value);
		var day = String(document.contactForm.dob_d.options[document.contactForm.dob_d.selectedIndex].value);
		var year = String(document.contactForm.dob_y.options[document.contactForm.dob_y.selectedIndex].value);
		var emvDob =	month + '/' + day + '/' + year;
	}
	else {
		var emvDob = '';
	}
	return emvDob;
}

// This 'function' creates a trim() function
String.prototype.trim = function() { return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");	}

// This function controls that a text field is filled in
function mandatoryText(input, name) {
	if (input.value.trim() == "" || input == null) {
		alert("Please enter your " + name + ".");
		input.focus();
		return false;
	}
	else {
		return true;
	}
}

// This function controls that a dropdown list field is filled in
function dropdown(input, name) {
	if (input.value.trim() == "" || input == "please select") {
		alert("Please enter your " + name + ".");
		input.focus();
		return false;
	}
	else {
		return true;
	}
}


// Drop Down Lists validator
function mandatoryCombo(dropdown, msg) {
	if (dropdown.options[0].selected) {
		alert(msg);
		dropdown.focus();
		return false;
	}
	return true;
}

// This function validates radio buttons arrays
function mandatoryRadio(radioList, radioAlert) {
	var radioValue = null;
	for (var i=0; i < radioList.length; i++) {
		if (radioList[i].checked) {
			radioValue = radioList[i].value;
			break;
		}
	}
	if (radioValue == null) {
		alert(radioAlert);
		radioList[0].focus();
		return false;
	}
	else {
		return true;
	}
}

/*
* This function validates the email address syntax and characters
* It also checks that the TLD is a 2 country code and if 3 or more, it ensures that it is in the Known Domains
*/

function isEmail(emailAddress) {
	emailAddressValue=emailAddress.value.toLowerCase();
	// Below reside knows 2 letters country TLD and 3 letter gTLDs
	var countryTLDs=/^(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/;
	var gTLDs=/^(aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org)$/;
	var basicAddress=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var validCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'-_.";
	var quotedUser="(\"[^\"]*\")";
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var validUser=new RegExp("^" + word + "(\\." + word + ")*$");
	var symDomain=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailAddressValue.match(basicAddress);
	if (matchArray==null) {
		alert("The e-mail address doesn't seem to be correct,\n(check syntax).");
		emailAddress.focus();
		return false;
	}	else {
		var user=matchArray[1];
		var domain=matchArray[2];
		for (i=0; i<user.length; i++) {
			if(validCharset.indexOf(user.charAt(i))==-1) {
				alert("The e-mail address contains invalid characters\n(check the username).");
				emailAddress.focus();
				return false;
			}
		}
		for (i=0; i<domain.length; i++) {
			if(validCharset.indexOf(domain.charAt(i))==-1) {
				alert("The e-mail address contains invalid characters\n(check the domain).");
				emailAddress.focus();
				return false;
			}
		}
		if (user.match(validUser)==null) {
			alert("The e-mail address doesn't seem to be correct,\n(user part).");
			emailAddress.focus();
			return false;
		}
		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) {
				alert("The e-mail address doesn't seem to be correct,\n(Check the domain name).");
				emailAddress.focus();
				return false;
			}
		}
		if ((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)) {
			alert("The e-mail address doesn't seem to be correct,\n(check domain suffix).");
			emailAddress.focus();
			return false;
		}
		if ((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)) {
			alert("The e-mail address doesn't seem to be correct,\n(check domain suffix).");
			emailAddress.focus();
			return false;
		}
		if ((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)) {
			alert("The e-mail address doesn't seem to be correct,\n(check domain suffix).");
			emailAddress.focus();
			return false;
		}
		if (len<2) {
			alert("The e-mail address doesn't seem to be correct,\n(missing hostname).");
			emailAddress.focus();
			return false;
		}
		return true;
	}
}

// Controls the invalid characters in a string
function charCtrl (input, charSet, msg) {
	// Set whcih characters are allowed in the field
	for (i=0; i<input.value.length; i++){
		if (charSet.indexOf(input.value.charAt(i)) == -1) {
			input.focus();
			alert(msg);
			return false;
		}
	}
	return true;
}

/*
*
* This function validates the form fields
* Use the functions above to make the relevant
* fields required
*
*/

// Submit function
function submitForTreatment(bCalledFromIndex) {
	//	SR 20080508 default bCalledFromIndex to false if no arguement passed
	var bCalledFromIndex = (bCalledFromIndex == undefined) ?  false : bCalledFromIndex;
	var message = '';
	var counter = 0;
	
	// New error system:
	var error = false;
	
	if (bCalledFromIndex == true) {
		var contactForm  = window.document.contactForm;
	} else {
		var contactForm  = window.document.contactForm2;
	}
	
	var handsetselect = document.getElementById('handsetselect');

	if (handsetselect != null)
	{
		if(handsetselect.value != 'SELECT')
		{
			counter++;
		}
		else
		{
			message += "Please select a handset\n\n";
			error = true;
		}
	}

	var charSet = new String("0123456789()\/-");
	//   SR 20080507 Renamed fields for move to cheetahmail
	// Assigning values to the checkboxes - returns 1 if checked and 0 if not checked
	//   if (contactForm.FIRSTNAME_FIELD.value != '') {
	if (contactForm.FNAME.value != '') {
		counter++;
	} else {
		message +=  "Please enter your Forename\n";
		error = true;
	}
	// if (!mandatoryText(contactForm.FIRSTNAME_FIELD, "forename")) {
	//     return;
	// }

	//   if (contactForm.LASTNAME_FIELD.value != '') {
	if (contactForm.LNAME.value != '') {
		counter++;
	} else {
		message +=  "Please enter your Surname\n";
		error = true;
	}

	//    if (contactForm.EMAIL_FIELD.value != '') {
	if (contactForm.email.value != '') {
		counter++;
	} else {
		message += "Please enter your E-mail address\n";
		error = true;
	}

	//if (counter >=3) {
	if (error == false)
	{
//		contactForm.action="http://www.mobileshop.com/newsletter-subscribe.php";
		contactForm.action="http://ebm.cheetahmail.com/r/regf2";
		contactForm.method="GET";
//		contactForm.method="POST";
		//      contactForm.target="_top";
		contactForm.submit();
	}
	else {
		alert(message);
	}

}

/*
*
* End of the Validation function
*
*/