/* Written by Avenesh Ali 
Copyright  Apr 2009
*/
//Client Side Validation of New Agent details:
var ermsg = ""; //Error msg
function validateContactInfo(passedForm) {
ermsg=""; //clear it

//reset all backgrounds:
	document.getElementById("email").style.backgroundColor="#ffffff";
	document.getElementById("contactname").style.backgroundColor="#ffffff";
	document.getElementById("telephone").style.backgroundColor="#ffffff";

//************************************************************************
	checkName(passedForm);
    checkEmail(passedForm);
	checkTelephone(passedForm);

	if (ermsg== "")	return true;
	else {
	ermsg="The following errors were made: \n\n" + ermsg;
	alert(ermsg);
	return false;
	}

}



function checkName(frm) {
			if (frm.nameTxt.value == "") {
				document.getElementById("contactname").style.backgroundColor="#FF0000";
				ermsg+="You must enter a Name\n";
				frm.nameTxt.focus();
				return false;
			}
}

function checkEmail(frm) {		
			// check to see if the email's valid
			if (!testEmailAddress(frm)) {
				ermsg+="You entered an Invalid email address\n";
				frm.emailTxt.focus();
				frm.emailTxt.select();
				document.getElementById("email").style.backgroundColor="#FF0000";
			}
}


function testEmailAddress(frm) {
			invalidChars = " /:,;";
	
			if (frm.emailTxt.value == "") {		
				return false;
			}
			for (i=0; i<invalidChars.length; i++) {	// invalid chars
				badChar = invalidChars.charAt(i);
				if (frm.emailTxt.value.indexOf(badChar,0) > -1) {
					return false;
				}
			}
			atPos = frm.emailTxt.value.indexOf("@",1);			// must have "@" symbol
			if (atPos == -1) {
				return false;
			}
			if (frm.emailTxt.value.indexOf("@",atPos+1) != -1) {	// 1 "@" symbol
				return false;
			}
			periodPos = frm.emailTxt.value.indexOf(".",atPos);
			if (periodPos == -1) {	// and at least one "." after the "@"
				return false;
			}
			if (periodPos+3 > frm.emailTxt.value.length)	{//  2 characters after the "."
				return false;
			}
			return true;
}



//checks and makes sure that user entered a number
//checks and makes sure that the numbers are numeric
function checkTelephone(frm) {
	
	if ( frm.telCodeTxt.value == "" && frm.tel1Txt.value == "" && frm.tel2Txt.value == ""    ) {
		return true;
	}
	
	
	if (     (!(IsNumeric(frm.telCodeTxt.value))) || (!(IsNumeric(frm.tel1Txt.value))) || (!(IsNumeric(frm.tel2Txt.value)))    ) {
		ermsg+="If you are entering a telephone contact number, it must contain only digits\n";
		document.getElementById("telephone").style.backgroundColor="#FF0000";
		
	}
}


//  check for valid number strings
function IsNumeric(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 liveCheckTextArea(limit) {
   var old = document.getElementById("counterId").value;
   document.getElementById("counterId").value=document.getElementById("messageTxtId").value.length;

   //check and see if limit reached
   if(document.getElementById("counterId").value > limit && old <= limit) {
     alert('You have written too much text in the text Message Area! Please enter a max of ' + limit + ' letters...');
     //check if browser supports stylesheets and change style
     if(document.styleSheets) {
       document.getElementById("counterId").style.fontWeight = 'bold';
       document.getElementById("counterId").style.color = '#ff0000'; } }
   else if(document.getElementById("counterId").value <= limit && old > limit
	   && document.styleSheets ) {
       document.getElementById("counterId").style.fontWeight = 'normal';
       document.getElementById("counterId").style.color = '#000000'; } 
	   
   }





