// JavaScript Document


var stPnt=0;

function Validate_Strings_Original(string, return_invalid_chars, valid_chars) {
//  valid_chars = ' 1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  invalid_chars = '';
  if(string == null || string == '')
     return(true);

  //For every character on the string.   
  for(index = 0; index < string.length; index++) {
    char = string.substr(index, 1);                        
     
    //Is it a valid character?
    if(valid_chars.indexOf(char) == -1) {
      //If not, is it already on the list of invalid characters?
      if(invalid_chars.indexOf(char) == -1) {
        //If it's not, add it.
        if(invalid_chars == '')
          invalid_chars += char;
        else
          invalid_chars += ', ' + char;
      }
    }
  }
            
  //If the string does not contain invalid characters, the function will return true.
  //If it does, it will either return false or a list of the invalid characters used
  //in the string, depending on the value of the second parameter.
  if(return_invalid_chars == true && invalid_chars != '') {
    last_comma = invalid_chars.lastIndexOf(',');
    if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
    return(invalid_chars);
    }
  else
    return(invalid_chars == ''); 
}

function Validate_Strings(string, return_invalid_chars) {
  valid_chars = " 1234567890'-_,.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
}
function Validate_StringsWithSpecialChar(string, return_invalid_chars) {
  valid_chars = " 1234567890'-_.^~!,:;&#()=+$%abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
 }


function Validate_AlphaNumeric(string, return_invalid_chars) {
  valid_chars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
}

function Validate_Numbers(string, return_invalid_chars) {
  valid_chars = '1234567890';
    return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
}

function Validate_NumbersDecimal(string, return_invalid_chars) {
  valid_chars = '.1234567890';
    return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
}

function Validate_Alphabets(string, return_invalid_chars) {
//  valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  valid_chars = ' -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return Validate_Strings_Original(string, return_invalid_chars, valid_chars);
}



/*
var total="";  
	var el = document.getElementsByName('carPaymentMethod[]');
	for(var i=0; i < el.length; i++){
	  if(el[i].checked){
		total +=el[i].value + "\n";
	  }
	}
	if(total=="") {
	  stPnt++;
	}

*/

function validationControls_checkBox(fieldToTest,divToDisplay,applyclassto,checkBlank) {
	var total="";
	var el = document.getElementsByName(fieldToTest);
	for(var i=0; i < el.length; i++){
	  if(el[i].checked){
		total +=el[i].value + "\n";
	  }
	}
//	alert(total);
	if(total=="") {
		document.getElementById(divToDisplay).style.display="";
	  document.getElementById(divToDisplay).innerHTML="(This field is required)";
	  document.getElementById(applyclassto).className="row";
	  stPnt++;
	} else {
	  document.getElementById(divToDisplay).style.display=""; 
		document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		document.getElementById(applyclassto).className="";
	}
}

function validationControls_Alphabets(fieldToTest,divToDisplay,applyclassto,checkBlank) {
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if((Trim(document.getElementById(fieldToTest).value)=="" || Trim(document.getElementById(fieldToTest).value).length==0) && checkBlank==true){
	    document.getElementById(divToDisplay).style.display="";
	    document.getElementById(divToDisplay).innerHTML="(This field is required)";
	    if(stPnt==0) {
	        document.getElementById(fieldToTest).focus();
	    }
	    document.getElementById(applyclassto).className="row";
	    stPnt++;
	    return false;
	} else if(!Validate_Alphabets(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only letters, spaces & hyphens are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		document.getElementById(applyclassto).className="row";
		stPnt++;
	    return false;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		    document.getElementById(divToDisplay).style.display="none"; 
		} else {
		    document.getElementById(divToDisplay).style.display=""; 
		    document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	    return true;
	}
}

function validationControls_AlphaNumeric(fieldToTest,divToDisplay,applyclassto,checkBlank) {
  document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
  if(document.getElementById(fieldToTest).value=="" && checkBlank==true){
	document.getElementById(divToDisplay).style.display="";
	document.getElementById(divToDisplay).innerHTML="(This field is required)";		
	if(stPnt==0) {
	  document.getElementById(fieldToTest).focus();
	}
	document.getElementById(applyclassto).className="row";
	stPnt++;
	return false;
  } else if(!Validate_AlphaNumeric(document.getElementById(fieldToTest).value, false)) {
	document.getElementById(divToDisplay).style.display="";
	document.getElementById(divToDisplay).innerHTML="(Only letters & numbers are allowed)";
	if(stPnt==0) {
	  document.getElementById(fieldToTest).focus();
	}
	document.getElementById(applyclassto).className="row";
	stPnt++;
	return false;
  } else { 
	if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
	  document.getElementById(divToDisplay).style.display="none"; 
	} else {
	  document.getElementById(divToDisplay).style.display=""; 
	  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
	}
	document.getElementById(applyclassto).className="";	
	return true;
  }
}


function validationControls_Blank(fieldToTest,divToDisplay,applyclassto,checkBlank) {
  document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
  if(document.getElementById(fieldToTest).value=="" && checkBlank==true){
	document.getElementById(divToDisplay).innerHTML="";
	document.getElementById(divToDisplay).innerHTML="(This field is required)";
	document.getElementById(divToDisplay).style.display="";
	document.getElementById(applyclassto).className="row";
	stPnt++;
  } else { 
	if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
	  document.getElementById(divToDisplay).style.display="none"; 
	} else {
	  document.getElementById(divToDisplay).style.display=""; 
	  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
	}
	document.getElementById(applyclassto).className="";
  } 
}

function validationControls_NumbersDecimal(fieldToTest,divToDisplay,applyclassto,checkBlank) {

//var theElement = $(applyclassto);
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value==""  && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
				document.getElementById(divToDisplay).innerHTML="(This field is required)";
		if(stPnt==0) {
		document.getElementById(fieldToTest).focus();
		}
	//	theElement.addClassName('row');
	document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!Validate_NumbersDecimal(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only numbers and decimal(.) are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	}
}


function validationControls_Numbers(fieldToTest,divToDisplay,applyclassto,checkBlank) {

//var theElement = $(applyclassto);
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value==""  && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
				document.getElementById(divToDisplay).innerHTML="(This field is required)";
		if(stPnt==0) {
		document.getElementById(fieldToTest).focus();
		}
	//	theElement.addClassName('row');
	document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!Validate_Numbers(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only numbers are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	}
}

function validationControls_Phone(field1ToTest,field2ToTest,field3ToTest,divToDisplay,applyclassto,checkBlank) {

//var theElement = $(applyclassto);
	document.getElementById(field1ToTest).value=Trim(document.getElementById(field1ToTest).value);
	document.getElementById(field2ToTest).value=Trim(document.getElementById(field2ToTest).value);
	document.getElementById(field3ToTest).value=Trim(document.getElementById(field3ToTest).value);	
	if((document.getElementById(field1ToTest).value=="" || document.getElementById(field2ToTest).value=="" || document.getElementById(field3ToTest).value=="")  && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
				document.getElementById(divToDisplay).innerHTML="(This field is required)";
		if(stPnt==0) {
		document.getElementById(field1ToTest).focus();
		}
		//	theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!Validate_Numbers(document.getElementById(field1ToTest).value, false) || !Validate_Numbers(document.getElementById(field2ToTest).value, false) || !Validate_Numbers(document.getElementById(field3ToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only numbers are allowed)";
		if(stPnt==0) {
			document.getElementById(field1ToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	}
}


function validationControls_Strings(fieldToTest,divToDisplay,applyclassto,checkBlank) {
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value=="" && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This field is required)";		
		if(stPnt==0) {
		document.getElementById(fieldToTest).focus();
		}
		document.getElementById(applyclassto).className="row";
		stPnt++;
	    return false;
	} else if(!Validate_Strings(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only letters, numbers, comma, hiphens, underscore, point and tilde are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		document.getElementById(applyclassto).className="row";
		stPnt++;
	    return false;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	    return true;
	}
}

function validationControls_StringswithSpecialChar(fieldToTest,divToDisplay,applyclassto,checkBlank) {
//var theElement = $(applyclassto);
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value=="" && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This field is required)";		
		if(stPnt==0) {
		document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!Validate_StringsWithSpecialChar(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only letters, numbers and any of these characters [!.,-:;&#()=+$%] are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
	//	theElement.addClassName('row');
	document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	}
}

function SignInUserNameExist(fieldToTest) {
	

	userAlreadyExists(fieldToTest,"");
	return false;
}

function validationControls_UserNameExist(fieldToTest,divToDisplay,applyclassto,checkBlank) {
	validationControls_UserName(fieldToTest,divToDisplay,applyclassto,checkBlank);  
	userAlreadyExists(fieldToTest,divToDisplay,applyclassto);
}


function validationControls_UserName(fieldToTest,divToDisplay,applyclassto,checkBlank) {

	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value=="" && checkBlank==true){
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This field is required)";		
		if(stPnt==0) {
		  document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!Validate_Strings(document.getElementById(fieldToTest).value, false)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Only letters, numbers, hiphens, underscore, point and tilde are allowed)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
	//	theElement.addClassName('row');
	document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(document.getElementById(fieldToTest).value.length<8 && document.getElementById(fieldToTest).value!="") {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Enter atleast 8 characters)";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
	//	theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
		if(checkBlank==false && document.getElementById(fieldToTest).value=="") {
		  document.getElementById(divToDisplay).style.display="none"; 
		} else {
		  document.getElementById(divToDisplay).style.display=""; 
		  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
		}
		document.getElementById(applyclassto).className="";
	}
}


function validationControls_Email(fieldToTest,divToDisplay,applyclassto) {
//	alert(document.getElementById(fieldToTest).value);
//	alert(isValidEmail(document.getElementById(fieldToTest).value));
	if(document.getElementById(fieldToTest).value==""){
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This field is required)";		
		if(stPnt==0) {
		  //document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else if(!isValidEmail(document.getElementById(fieldToTest).value)) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This is not a valid email.)";
		if(stPnt==0) {
		//  document.getElementById(fieldToTest).focus();
		}
		//theElement.addClassName('row');
		document.getElementById(applyclassto).className="row";
		stPnt++;
	} else { 
	  document.getElementById(divToDisplay).style.display=""; 
	  document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
	  document.getElementById(applyclassto).className="";
//	alert("testing");
	//  emailAlreadyExists(fieldToTest,divToDisplay,applyclassto);
	}
}

function validationControls_ConfirmEmail(fieldToTest,fieldWithTest,divToDisplay,applyclassto) {
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value=="") {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(This field is required)";
		document.getElementById(applyclassto).className="row";
		stPnt++;
		return false;
	} else if(document.getElementById(fieldWithTest).value!=document.getElementById(fieldToTest).value) {
		document.getElementById(divToDisplay).style.display="";
		document.getElementById(divToDisplay).innerHTML="(Confirm Email is incorrect)";		
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		document.getElementById(applyclassto).className="row";
		stPnt++;
	    return false;
	} else { 
	    document.getElementById(divToDisplay).style.display=""; 
	    document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
	    document.getElementById(applyclassto).className="";
	    return true;
	}
}


function validationControls_Email2(fieldToTest,fieldWithTest,divToDisplay,applyclassto) {
	document.getElementById(fieldToTest).value=Trim(document.getElementById(fieldToTest).value);
	if(document.getElementById(fieldToTest).value=="" || document.getElementById(fieldWithTest).value!=document.getElementById(fieldToTest).value) {
		document.getElementById(divToDisplay).style.display="";
		if(stPnt==0) {
			document.getElementById(fieldToTest).focus();
		}
		document.getElementById(applyclassto).className="row";
		stPnt++;
	    return false;
	} else { 
	    document.getElementById(divToDisplay).style.display=""; 
	    document.getElementById(divToDisplay).innerHTML='<img src="images/click.gif" title="Done" id="myimg1"  />';
	    document.getElementById(applyclassto).className="";
	    return true;
	}
}

function divMessage(fieldToTest, firstMessage, lastMessage, defaultMessage, topPos, dispStatus, memType)
{
	memType = typeof(memType) != 'undefined' ? memType : "business";

	//divMessage('firstName','firstNameRequire','First Name:','Please fill in the first and last name ofthe main contact person for this business. Your information is kept private and only used for internal purposes.', 'true')	
	if(dispStatus == true) {
	//alert(document.getElementById('abcConstraint').style.top);
	  var zxc=document.getElementById(fieldToTest);
	  zxcObjLeft = zxc.offsetLeft;
	  zxcObjTop = zxc.offsetTop;
	  while(zxc.offsetParent.id!='forPositionFact'){	
	    zxcObjParent=zxc.offsetParent;
	    zxcObjLeft+=zxcObjParent.offsetLeft;
	    zxcObjTop+=zxcObjParent.offsetTop;
	    tttttt=zxc.offsetTop;
	    zxc=zxcObjParent;
	    if(zxc.offsetParent==null){ zxcObjTop=zxcObjTop-310; break; }
	  }
 
	  if(fieldToTest == "finishedButton") {
	    zxcObjTop = zxcObjTop-100;
	  }
      document.getElementById('abcConstraint').style.top=zxcObjTop+"px"; 
      //	  document.getElementById(divToDisplay).innerHTML="<table><tr><td valign='middle' align='center' ><img src='images/arrow_message.gif' alt='arrow' /></td><td valign='middle'>            <table width='220' id='messages'><tr><td valign='top'  class='upper_right_menu'><span class='welcome_orange_skinny'>" + firstMessage + "</span> " + lastMessage + "</td> </tr></table></td></tr></table>";
	  document.getElementById('messagesSpan').innerHTML="<span class='welcome_orange_skinny'>" + firstMessage + "</span> " + lastMessage ;
	} else {
	  var tempVar="";
	  if(memType=="business") {
		tempVar=" As you click on each form field, you will see these help boxes appear. Please read them as you go through the form, as they will help you better understand why we ask for the information we do, as well as how it can help your business.";
	  } else if(memType=="user") {
		tempVar=" As you click on each form field, you will see these help boxes appear. Please read them as you go through the form, as they will help you better understand why we ask for the information we do.";		
	  }
		
	  document.getElementById('abcConstraint').style.top=topPos+"px"; 
	  document.getElementById('messagesSpan').innerHTML="<span class='welcome_orange_skinny'>Filling out the Form:</span> " + tempVar ;
	}
}

function checkTime(strTime,fieldVal, applyclassto) //use for text field
{
	var errors;
	var timeExp = /^\s*[0-1]?\d(:[0-5]\d)?\s*((AM)|(PM))\s*$/i;
	
//	alert(document.getElementById(strTime).value);
	if(!timeExp.test(document.getElementById(strTime).value)) {
//		alert("wrong time");
		document.getElementById(fieldVal).style.display='';
		document.getElementById(applyclassto).className="row";
		document.getElementById(strTime).focus();
		return false;
	} else {
		document.getElementById(fieldVal).style.display='none';
		document.getElementById(applyclassto).className="";
		return true; 
	}
}

function zipcodeLength_Validation(fieldToTest, divToDisplay, applyclassto) {
	var VzipCode=Trim(document.getElementById(fieldToTest).value);
	if(VzipCode.length<5){
	  document.getElementById(divToDisplay).style.display="";
	  if(VzipCode.length==0) { 
	    document.getElementById(divToDisplay).innerHTML="(Zipcode is required.)";
	  } else {
	    document.getElementById(divToDisplay).innerHTML="(Enter atleast 5 characters.)";
	  }
	  document.getElementById(fieldToTest).focus();
	  document.getElementById(applyclassto).className="row";
	  stPnt++;
	  return false;
	}
	return true;
}