// JavaScript Document
//Checks the email address entered in the form
	
	function emailCheck (email) {
	var emailStr = email.value;
	
	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) {
	alert("Email address seems incorrect (check @ and .'s)");
	email.value=""
	email.focus()
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		alert("Ths Username in Email contains invalid characters.");
		email.value=""
		email.focus()
		return false;
   		}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("Ths Domain name in Email contains invalid characters.");
		email.value=""
		email.focus()
		return false;
	   }
	}

	if (user.match(userPat)==null) {
	alert("The Username in Email doesn't seem to be valid.");
	email.value=""
	email.focus()
	return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert("Destination IP address in Email is invalid!");
			email.value=""
			email.focus()
			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) {
			alert("The domain name in Email does not seem to be valid.");
			email.value=""
			email.focus()
			return false;
   		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The Email address must end in a well-known domain or two letter " + "country.");
		email.value=""
		email.focus()
		return false;
	}

	if (len<2) {
		alert("This Email address is missing a hostname!");
		email.value=""
		email.focus()
		return false;
	}

	return true;
}

// checking codes end

// code for checking the phone number entered in the form

	var digits = "0123456789";
	var phoneNumberDelimiters = "()- ";
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	var minDigitsInIPhoneNumber = 10;

	function isInteger(s){ 
		var i;
	    for (i = 0; i < s.length; i++){   
	        var c = s.charAt(i);
    	    if (((c < "0") || (c > "9"))) return false;
		}
	    return true;
	}

	function stripCharsInBag(s, bag){
		var i;
	    var returnString = "";
    	for (i = 0; i < s.length; i++){   
	        var c = s.charAt(i);
    	    if (bag.indexOf(c) == -1) returnString += c;
    	}
	    return returnString;
	}

	function checkInternationalPhone(strPhone){
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}

	function ValidatePhone(Phone){
	
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }

// code to check the phone number ends

// code to check the valid advisor number
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function isChar(char) {
	for (i = 0; i < characters.length; i++){   
		var c = characters.charAt(i);
		if (char==c) return true;
	}
	return false;
}

function validateAdvisor(advisorNo) {
	var num = advisorNo.value;
	if ((!isChar(num.charAt(0))) || (!isChar(num.charAt(1)))) {
		alert("Please Enter a Valid Advisor Number")
		advisorNo.value=""
		advisorNo.focus()
		return false;
	}
	for (i = 3; i < 7; i++) {
		if (!isInteger(num.charAt(i))) {
			alert("Please Enter a Valid Advisor Number")
			advisorNo.value=""
			advisorNo.focus()
			return false;
		}
	}
	
	return true;
}

// validation code for advisor number ends

function confirmAdvisorDelete(delURL) {
	if (window.confirm("It will delete this Advisor account permamnently.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmAdvisorAccept(delURL) {
	if (window.confirm("You are about to allow this user to be an Advisor.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmAdvisorLock(delURL) {
	if (window.confirm("You are about to lock the Advisor Account.\nThe Advisor will no longer be allowed to login or introduce any customer.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmAdvisorUnlock(delURL) {
	if (window.confirm("You are about to unlock the Advisor Account.\nThe Advisor will now be allowed to login or introduce new customers.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmProductLock(delURL) {
	if (window.confirm("You are about to lock a Product.\nThe Product will no longer be visible to users on the site.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmProductUnlock(delURL) {
	if (window.confirm("You are about to unlock a Product.\nThe Product will now be visible to users on the site.\nAre you sure?")) {
		document.location.href=delURL;
	}
}

function confirmProductDelete(delURL) {
	if (window.confirm("It will delete this Product permamnently.\nAre you sure?")) {
		document.location.href=delURL;
	}
}