/* 
JavaScript source code for validating business card form values
created by: Sudin KK
created dt: Nov 21, 2007
modified date: 7:59 PM 6/11/2009 ... removed phone validation etc..
*/
function checkfrm()
{
	if(document.bcard_small.fname.value=="")
	{
	alert("Please enter your first name");
	document.bcard_small.fname.focus();
	return false;
	}
	if(document.bcard_small.lname.value=="")
	{
	alert("Please enter your last name");
	document.bcard_small.lname.focus();
	return false;
	}
	if(document.bcard_small.cname.value=="")
	{
	alert("Please enter company name");
	document.bcard_small.cname.focus();
	return false;
	}
	if(document.bcard_small.phone.value=="")
	{
	alert("Please enter a valid phone number");
	document.bcard_small.phone.focus();
	return false;
	}

	
  //---- oct 21, 2008 new validations end-----
	
	if(document.bcard_small.email.value=="")
	{
	alert("Please enter your email");
	document.bcard_small.email.focus();
	return false;
	}
	if (!(emailCheck(document.bcard_small.email.value))){
			alert("Please enter a valid Email address");
			document.bcard_small.email.focus();
			return;
		}	
	
	document.bcard_small.submit();
}

/*function verifyCaptcha()
{
if( document.bcard_small.verification2.value.toUpperCase() !=  document.bcard_small.verification.value.toUpperCase() )
	{
	alert("Please enter the verification code as shown in the image");
	document.bcard_small.verification.focus();
	return false;
	}
}

*/
function emailCheck (emailStr) {
    emailStr = emailStr.toLowerCase();
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("This username in email address contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	//alert("This domain name in email address contains invalid characters.");
	return false;
	   }
	}
	if (user.match(userPat)==null) {
	//alert("The username in email address doesn't seem to be valid.");
	return false;
	}
	
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
	
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	//alert("Destination IP address in email is invalid!");
	return false;
	   }
	}
	return true;
	}
	
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	//alert("The domain name in email address does not seem to be valid.");
	return false;
	   }
	}
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	//alert("The email address must end in a well-known domain or two letter " + "country.");
	return false;
	}
	
	
	if (len<2) {
	//alert("The email address is missing a hostname!");
	return false;
	}
	
	return true;
	}


	function ignoreSpaces(string,dlmtr) 
	{
		var temp = "";
		string = '' + string;
		splitstring = string.split(dlmtr);
		for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
		return temp;
	}
	function validate(field) {
		//var valid = "0123456789-zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP() ";
		var letters = "+-zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP() ";
		var valid = "0123456789";
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
				
		usphone = stripCharsInBag(field.value,letters);
		
		if (document.bcard_small.country.value.toUpperCase() == "UNITED STATES" || document.bcard_small.country.value.toUpperCase() == "USA" || document.bcard_small.country.value.toUpperCase() == "US" || document.bcard_small.country.value.toUpperCase() == "U.S.A" || document.bcard_small.country.value.toUpperCase() == "CANADA")
		{	
			if (usphone.length != 10)
			{
				alert(document.bcard_small.country.value.toUpperCase() + " phone number should be a 10 digit number");
				field.value = usphone;
				return true;
			}
			else
			{
				field.value = usphone;
				return false;
			}	
		}
		else if (usphone.length <= 10)
		{
			alert("Please enter a valid phone number with area code and country code");
			field.value = usphone;
			return true;
		}
		else
		{
			field.value = usphone;
			return false;
		}
}

//function to check captcha code on business card forms


// Removing non numeric characters from phone number	
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    var str1 = "01";
    var str = returnString;
    var a = str.length;
    for (i = 0; i < a; i++)
	{   
        // removing leading 0's and 1's
        var c = str.charAt(i);
        if (str1.indexOf(c) == -1)
        {
			returnString = str.substring(i, a);
			break;
         }
    }
    return returnString;
}

//** Show / Hide industry dropdown based on the value selected in Type of Business field.
function LoadIndustrylookup()
{  
  var browserType;
	var ind;//, indc, indref ;
	//test sept24
	if (document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value.toUpperCase() == "--------------------" )
	{
		//document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value = "Please Select";
		document.bcard_small.TypeOfBusiness.selectedIndex = 0;
		//alert("yes");
	}
		//alert("yes");
    if (document.layers) {browserType = "nn4";}
    if (document.all) {browserType = "ie";}
    if (window.navigator.userAgent.toLowerCase().match("gecko")) {browserType= "gecko";}

    if (browserType == "gecko" ){
       //ind  = eval('document.getElementById(\'divindustry\')'); }            
       ind  = eval('document.getElementById(\'indtr\')');}
    else if (browserType == "ie"){
       //ind  = eval('document.all[\'divindustry\']');}       
       ind  = eval('document.all[\'indtr\']');}
    else{
	     //ind  = eval('document.layers[\'divindustry\']');}     
	     ind  = eval('document.layers[\'indtr\']');     }
	     //indref  = eval('document.layers[\'refcode\']');}     
	     
    
    //alert(document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value ) ; 
        
    if((document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value.toUpperCase() == "RETAIL" )
      || (document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value.toUpperCase() == "DISTRIBUTION / WHOLESALE" ) //  "Distribution / Wholesale"
      || (document.bcard_small.TypeOfBusiness.options[document.bcard_small.TypeOfBusiness.selectedIndex].value.toUpperCase() == "MANUFACTURING" ))     
    {
	  ind.style.visibility = "visible";
    }
    else
    {
	  ind.style.visibility = "hidden";      	    	  
    }
  
}
// End of the function LoadIndustrylookup()

