function setErrorMsg(ctrlId,msg,type){
	var spanTag="<span id="+ctrlId+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	
	if(document.getElementById(ctrlId+"_error")==null){
		new Insertion.After(ctrlId,spanTag);
	}
	
	if(type=="block"){
		document.getElementById(ctrlId).style.background = "#FFFE9E";
	}else{
		document.getElementById(ctrlId).style.background = "#ffffff";
	}	
	
	if(document.getElementById(ctrlId+"_error")!=null){
		document.getElementById(ctrlId+"_error").innerHTML=msg;
		document.getElementById(ctrlId+"_error").style.display=type;
	}
}

function validatezip(ctrlId,blankerrorMsg,errorMsg) 
{
	var field=document.getElementById(ctrlId).value;
	var valid = "0123456789-";
	var hyphencount = 0;
	
	
	if(field.length==0){
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}

	if (field.length!=5 && field.length!=10) 
	{
		setErrorMsg(ctrlId,errorMsg,"block");
		return false;
	}

	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") 
			hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			setErrorMsg(ctrlId,errorMsg,"block");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			setErrorMsg(ctrlId,errorMsg,"block");
			return false;
	    }
	}

	setErrorMsg(ctrlId,"","none");
	return true;
}

/*function ValidateString(ctrlId,minLength,maxLength,blankerrorMsg,errorMsg)
{
	var value = document.getElementById(ctrlId).value;
	var spanTag="<span id="+ctrlId+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	
	if(value == '')
	{
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}
	
	if(IsValidString(value))
	{
		if(value.length > maxLength)
		{
			setErrorMsg(ctrlId,"Length should be between 0-255.","block");
			return false;
		}
		if(value.length < minLength)
		{
			setErrorMsg(ctrlId,"Length should be between 0-255.","block");
			return false;
		}
	}else{
		setErrorMsg(ctrlId,errorMsg,"block");
		return false;
	}
	
	setErrorMsg(ctrlId,"","none");
	return true;
}*/

function ValidateString(ctrlId,minLength,maxLength,blankerrorMsg,errorMsg)
{
	var value = document.getElementById(ctrlId).value;
	var spanTag="<span id="+ctrlId+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	
	if(value == '')
	{
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}
	
	if(ctrlId == 'Text2' || ctrlId == 'Text9' || ctrlId == 'Text13' || ctrlId == 'Text17')
	{
		if(IsValidString_Text2(value))
		{
			if(value.length > maxLength)
			{
				setErrorMsg(ctrlId,"Length should be between 0-255.","block");
				return false;
			}
			if(value.length < minLength)
			{
				setErrorMsg(ctrlId,"Length should be between 0-255.","block");
				return false;
			}
		}else{
			setErrorMsg(ctrlId,errorMsg,"block");
			return false;
		}
	}
	else
	{

		if(IsValidString(value))
		{
			if(value.length > maxLength)
			{
				setErrorMsg(ctrlId,"Length should be between 0-255.","block");
				return false;
			}
			if(value.length < minLength)
			{
				setErrorMsg(ctrlId,"Length should be between 0-255.","block");
				return false;
			}
		}else{
			setErrorMsg(ctrlId,errorMsg,"block");
			return false;
		}
	}
	
	setErrorMsg(ctrlId,"","none");
	return true;
}

function IsValidString(strValue)
{
	var Alphabet;
	//Alphabet = "^[a-zA-Z]+$";
	Alphabet =/^[A-Za-z]+$/;
	//A-Z or a-z Alphabets only allowed

	var name = strValue;
	if (name.search(Alphabet) == -1) 
	{
		return false;
	}else{
		return true;
	}
}

function IsValidString_Text2(strValue)
{
	var Alphabet;
	//Alphabet = "^[a-zA-Z]+$";
	//Alphabet =/^[A-Za-z$]/;
	Alphabet =/(^[A-Za-z \-]+$)/;
	//A-Z or a-z Alphabets only allowed

	var name = strValue;
	if (name.search(Alphabet) == -1) 
	{
		return false;
	}else{
		return true;
	}
}

// checks the format of the email
function ValidateBlank(ctrlId,blankerrorMsg)
{
	var objEmail = document.getElementById(ctrlId);
	var value = document.getElementById(ctrlId).value;
	
	if ((value==null)||(value=="")){
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}
	
	setErrorMsg(ctrlId,"","none");
	return true;
}

// checks the format of the email
function ValidateEmail(ctrlId,blankerrorMsg,errorMsg)
{
	var spanTag="<span id="+ctrlId+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	var objEmail = document.getElementById(ctrlId);
	var value = document.getElementById(ctrlId).value;
	var emailPat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if ((value==null)||(value=="")){
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}else if (emailPat.test(value)==false){
		setErrorMsg(ctrlId,errorMsg,"block");
		return false;
	}else{
		setErrorMsg(ctrlId,"","none");
		return true;
	}
}

// checks the format of the email
function ValidateConfEmail(ctrlId1,ctrlId2,blankerrorMsg,errorMsg)
{
	var spanTag="<span id="+ctrlId1+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	var validate=ValidateEmail(ctrlId1,blankerrorMsg,errorMsg);
	if(validate){
		if(document.getElementById(ctrlId1).value==document.getElementById(ctrlId2).value){
			setErrorMsg(ctrlId1,"","none");
			return true;
		}else{
			setErrorMsg(ctrlId1,"Need to be same as Main Email.","block");
			return false;
		}		
	}else{
		return false;
	}
}

function validateDropdown(ctrlId,blankerrorMsg){
	var spanTag="<span id="+ctrlId+"_error class=\"validation-advice\" style=\"display: none;\"></span>";
	var value = document.getElementById(ctrlId).value;
	
	if ((value==null)||(value=="")){
		setErrorMsg(ctrlId,blankerrorMsg,"block");
		return false;
	}
	setErrorMsg(ctrlId,"","none");
	return true;
}


function validateRegSubmit(){
	var val=0;
	var focusId="";
	
	if(ValidateString("Text1",1,255,'This is a required field.','Enter a valid First Name.')){
		val++;
	}else{
		focusId="Text1";	
	}
	
	if(ValidateString("Text2",1,255,'This is a required field.','Enter a valid Last Name.')){
		val++;
	}else{
		if(focusId==""){
			focusId="Text2";
		}
	}
	
	if(ValidateBlank("Text3",'This is a required field.')){
		val++;
	}else{
		if(focusId==""){
			focusId="Text3";
		}
	}
	
	if(ValidateBlank("Text4",'This is a required field.')){
		val++;
	}else{
		if(focusId==""){
			focusId="Text4";
		}
	}

	if(validateDropdown("state",'Required field.')){
		val++;
	}else{
		if(focusId==""){
			focusId="state";
		}
	}

	if(validatezip('Text5','Required field.','Invalid Zipcode.')){
		val++;
	}else{
		if(focusId==""){
			focusId="Text5";
		}
	}
	
	if(ValidateEmail("Text20","This is a required field.","Invalid Email Address.")){
		val++;
	}else{
		if(focusId==""){
			focusId="Text20";
		}
	}
	
	if(ValidateConfEmail('Text21','Text20','This is a required field.','Invalid Confirmation Email.')){
		val++;
	}else{
		if(focusId==""){
			focusId="Text21";
		}
	}
	
	if(val!=8){
		if(focusId!=""){
			
			document.getElementById(focusId).focus();
		}
		return false;
	}
	
	return true;
}

function validateRaf(){
	var mandatoryFieldsValidated=false;
	var nonmandatoryFieldsValidated1=true;
	var nonmandatoryFieldsValidated2=true;
	
	if(ValidateBlank("Text7",'This is a required field.') && ValidateEmail("ref_email","This is a required field.","Invalid Email Address.")){
		mandatoryFieldsValidated= validateFriendInformation("Text8","Text9","Text10","Text11");
		
		if(document.getElementById("Text14").value!=""){
			nonmandatoryFieldsValidated1= validateFriendInformation("Text12","Text13","Text14","Text15");
		}
		
		if(document.getElementById("Text18").value!=""){
			nonmandatoryFieldsValidated2= validateFriendInformation("Text16","Text17","Text18","Text19");
		}
		return (mandatoryFieldsValidated && nonmandatoryFieldsValidated1 && nonmandatoryFieldsValidated2);
	}else{
		document.getElementById("Text7").focus();
	    ValidateEmail("ref_email","This is a required field.","Invalid Email Address.");
		validateFriendInformation("Text8","Text9","Text10","Text11");

		return false;
	}	
}

function validateFriendInformation(fname,lname,email,confemail){
	var val=0;
	var focusId="";

	if(ValidateString(fname,1,255,'This is a required field.','Enter a valid First Name.')){
		val++;
	}else{
		focusId=fname;	
	}
	
	if(ValidateString(lname,1,255,'This is a required field.','Enter a valid Last Name.')){
		val++;
	}else{
		if(focusId==""){
			focusId=lname;
		}
	}
	
	if(ValidateEmail(email,"This is a required field.","Invalid Email Address.")){
		val++;
	}else{
		if(focusId==""){
			focusId=email;
		}
	}
	
	if(ValidateConfEmail(confemail,email,'This is a required field.','Invalid Confirmation Email.')){
		val++;
	}else{
		if(focusId==""){
			focusId=confemail;
		}
	}
	
	if(val!=4){
		if(focusId!=""){
			document.getElementById(focusId).focus();
		}
		return false;
	}
	return true;
}