
// FORM VALIDATION
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    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 doValidation(mode) {
		if(window.document.membershipForm.username.value.search(/\S/) == -1) {
			alert("Please,Enter the  User Name");
			window.document.membershipForm.username.focus();
			return false;
		}
		
		if(mode == 'signup'){
			
			var chk = document.getElementById("showResult").innerHTML;
			
			var mySplitResult = chk.split("&nbsp;");
			var checkingWord = mySplitResult[1];
			
			if(checkingWord == "Not") {
				alert("This  Username is Not Available.");
				window.document.membershipForm.username.focus();
				return false;
			}
		
		}
		
		if(window.document.membershipForm.password.value.search(/\S/) == -1) {
			alert("Please,Enter the Password");
			window.document.membershipForm.password.focus();
			return false;
		}
		passLength = window.document.membershipForm.password.value.length;
		if(passLength <5 || passLength >15){
			alert("Password length should be atleast 5 characters but not more than 15 characters long");
			window.document.membershipForm.password.focus();
			return false;
		}
		
		var pwdChar;
		var isNumeric = 0;
		var isString  = 0;
		
		for (i = 0;i< passLength;i++ ){
				var pwd;
				pwd = window.document.membershipForm.password.value;
				pwdChar = pwd.charAt(i);
				if(isNaN(pwdChar)){
					isString  = 1;
				}
				if(!isNaN(pwdChar)){
					isNumeric  = 1;
				}
			}
		
        if(isString != 1 || isNumeric != 1){
			alert("Password must contain at least one alpha and one numeric character.");
			window.document.membershipForm.password.focus();
			return false;
		}
		
		
		
		if(window.document.membershipForm.title.value == "0") {
			alert("Please,Select your Title");
			window.document.membershipForm.title.focus();
			return false;
		}
		if(window.document.membershipForm.firstname.value.search(/\S/) == -1) {
			alert("Please,Enter the  First Name");
			window.document.membershipForm.firstname.focus();
			return false;
		}
		if(window.document.membershipForm.lastname.value.search(/\S/) == -1) {
			alert("Please,Enter the  Last Name");
			window.document.membershipForm.lastname.focus();
			return false;
		}
		
		if(window.document.membershipForm.job_title.value.search(/\S/) == -1) {
			alert("Please,Enter the  Job Title");
			window.document.membershipForm.job_title.focus();
			return false;
		}
		
		if(window.document.membershipForm.company_name.value.search(/\S/) == -1) {
			alert("Please,Enter the Company Name");
			window.document.membershipForm.company_name.focus();
			return false;
		}
		if(window.document.membershipForm.address1.value.search(/\S/) == -1) {
			alert("Please,Enter the  Address1");
			window.document.membershipForm.address1.focus();
			return false;
		}
		if(window.document.membershipForm.city.value.search(/\S/) == -1) {
			alert("Please,Enter the  City");
			window.document.membershipForm.city.focus();
			return false;
		}
		if(window.document.membershipForm.state.value == "0") {
			alert("Please,Select the  State");
			window.document.membershipForm.state.focus();
			return false;
		}
		if(window.document.membershipForm.zip.value.search(/\S/) == -1) {
			alert("Please,Enter the  Zip");
			window.document.membershipForm.zip.focus();
			return false;
		}
		if(!validateZip(window.document.membershipForm.zip.value) && !validateZip1(window.document.membershipForm.zip.value) &&!validateZip2(window.document.membershipForm.zip.value)){
			alert("Please follow this format in Zip code -> (xxxxx-xxxx or xxxxx or xxxxxxxxx)");
			window.document.membershipForm.zip.focus();
			return false;
		}
		if(window.document.membershipForm.phone.value.search(/\S/) == -1) {
			alert("Please enter the Phone");
			window.document.membershipForm.phone.focus();
			return false;
		}
		
		if(!validateUSPhone(window.document.membershipForm.phone.value) && !validateUSPhone1(window.document.membershipForm.phone.value)){
			alert("Please follow this format in Phone -> (xxx-xxx-xxxx or xxxxxxxxxx)");
			window.document.membershipForm.phone.focus();
			return false;
		}
		
		
		if(window.document.membershipForm.fax.value.search(/\S/) == -1) {
			alert("Please,Enter the  Fax");
			window.document.membershipForm.fax.focus();
			return false;
		}
		
		if(!validateFax(window.document.membershipForm.fax.value) && !validateFax1(window.document.membershipForm.fax.value)){
			alert("Please follow this format in Fax -> (xxx-xxx-xxxx or xxxxxxxxxx)");
			window.document.membershipForm.fax.focus();
			return false;
		}
	
	email=window.document.membershipForm.email
	if (email.value.search(/\S/) == -1) 
	{
	 alert("Please,Enter the Email");
	 window.document.membershipForm.email.focus();
	 return false; 
	}
   else
    {
    if (email.value.indexOf ('@',0) == -1 || email.value.indexOf ('.',0) == -1)
	  { 
		alert("Please,Enter a correct Email");
		window.document.membershipForm.email.focus();
		return false;
      }
	}
  
  
  if(document.membershipForm.otherServices.checked == true){
	   if(window.document.membershipForm.other_services.value.search(/\S/) == -1) {
			alert("Please,Enter Othe Services of interest");
			window.document.membershipForm.other_services.focus();
			return false;
		}
  }
  
  if(document.membershipForm.chamberofcommerce.checked == true){
	   if(window.document.membershipForm.chamber_commerce.value.search(/\S/) == -1) {
			alert("Please,Enter chamber of commerce");
			window.document.membershipForm.chamber_commerce.focus();
			return false;
		}
  }
  
  if(document.membershipForm.purchasingEvent.checked == true){
	  	 if(window.document.membershipForm.purchasing_event.value.search(/\S/) == -1) {
			alert("Please,Enter Purchasing Event");
			window.document.membershipForm.purchasing_event.focus();
			return false;
		}
  }
  
  if(document.membershipForm.business_forum.checked == true){
	  	if(window.document.membershipForm.sustainable_business_forum.value.search(/\S/) == -1) {
			alert("Please,Enter sustainable business forum");
			window.document.membershipForm.sustainable_business_forum.focus();
			return false;
		}
  }
  
  
  /*if(document.membershipForm.web_address.value.search(/\S/) != -1){
			url = isUrl(document.membershipForm.web_address.value);
				if(!url){
				alert("Please start the web address with http:// or https://");
				document.membershipForm.web_address.focus();
				return false;
				}
		}*/
  
  
  other_contact_email=window.document.membershipForm.other_contact_email
	if(other_contact_email.value.search(/\S/) != -1){ 
		if (other_contact_email.value.indexOf ('@',0) == -1 || other_contact_email.value.indexOf ('.',0) == -1) { 
			alert("Please,Enter a correct Email");
			window.document.membershipForm.other_contact_email.focus();
			return false;
		  }
	}
  

	}
	
	/*function isUrl(s) {
		var regexp = /^[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&\?\/.=]+$/
		return regexp.test(s);
        }*/
		
		
	function validateUSPhone( strValue ) {
		var objRegExp  = /^\d{3}\-\d{3}\-\d{4}$/;
		return objRegExp.test(strValue);
	}
	function validateUSPhone1( strValue ) {
		var objRegExp  = /^\d{10}$/;
		return objRegExp.test(strValue);
	}
	
	function validateZip( strValue ) {
		var objRegExp  = /^\d{5}\-\d{4}$/;
		return objRegExp.test(strValue);
	}
	function validateZip1( strValue ) {
		var objRegExp  = /^\d{5}$/;
		return objRegExp.test(strValue);
	}
	function validateZip2( strValue ) {
		var objRegExp  = /^\d{9}$/;
		return objRegExp.test(strValue);
	}
	function validateFax( strValue ) {
		var objRegExp  = /^\d{3}\-\d{3}\-\d{4}$/;
		return objRegExp.test(strValue);
	}
	function validateFax1( strValue ) {
		var objRegExp  = /^\d{10}$/;
		return objRegExp.test(strValue);
	}
