//FUNCTION: validateEmail
//Contains a least one character procedding the @
//Contains a "@" following the procedding character(s)
//Contains at least one character following the @, followed by a dot (.), 
//	followed by either a two character or three character string 
//	(a two character country code or the standard three character US code, such as com, edu etc)
function validateEmail(sEmailAddress){
	//create the regexp to test the email address text
	var oEmailRegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	//perform the test and return success as appropriate
	if (oEmailRegExp.test(sEmailAddress)){
		return true;
	}else{
		return false;
	}
}
function isTelephoneNumber(strString){
   var strValidChars = "0123456789()- ";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function validateDownloadForm(){
	var bValid = true;
	var sName = document.getElementById("txtName").value;
	var sEmail = document.getElementById("txtEmail").value;
	var sOrg = document.getElementById("txtOrganisation").value;
	var sTelephone = document.getElementById("txtTelephone").value;
	
	if(sName == ""){
		alert("You must enter your Name.");
		document.getElementById("txtName").focus();
		bValid = false;
	}else if(sOrg == ""){
		alert("You must enter your Organisation name.");
		document.getElementById("txtOrganisation").focus();
		bValid = false;
	}else if(sEmail == ""){
		alert("You must enter your Email address.");
		document.getElementById("txtEmail").focus();
		bValid = false;
	}else if(!validateEmail(sEmail)){
		alert("You must enter a valid Email address.");
		document.getElementById("txtEmail").focus();
		bValid = false;
	}else if(sTelephone == ""){
		alert("You must enter your Telephone number.");
		document.getElementById("txtTelephone").focus();
		bValid = false;
	}else if(!isTelephoneNumber(sTelephone)){
		alert("You must enter a valid Telephone number.");
		document.getElementById("txtTelephone").focus();
		bValid = false;
	}
	return bValid;
}

/* Name		:createCookie
* Purpose	:creates a new cookie
* Returns	:n/a
*/
function createCookie(name, value, expireInDays){
	if(expireInDays){
		var dDate = new Date();
		dDate.setTime(dDate.getTime() + (expireInDays * 24 * 60 * 60 * 1000));
		var sExpires = "; expires=" + dDate.toGMTString();
	}else{
		var sExpires = "";
	}
	document.cookie = name + "=" + value + sExpires + "; path=/";
}
/* Name		:eraseCookie
* Purpose	:erases a cookie
* Returns	:n/a
*/
function eraseCookie(name){
	createCookie(name, "", -1);
}
/* Name		:readCookie
* Purpose	:reads a cookie
* Returns	:n/a
*/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}