var tldomains = new Array("as", "ca", "com", "edu", "gl", "gov", "aero", "biz", "coop", "info", "museum", "name", "pro", "travel", "int", "iq", "mil", "net", "org", "pr", "tv", "um", "us", "vi ");
//globals
var emailcode;
var emailcodestr;
var minYear=1900;
var maxYear=2100;


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 etldck(eaddr) {

/*
 *	1 = missing @ email separator
 *	2 = unacceptable domain name (not in tldomains list)
 *	99 = E-mail accepted (passes all criteria)
 *
 */
	var leftside;
	var rightside;
	var tmpdstr;
	var tmpdomstr;
	var x;
	var tmptl;
	var tmpvar;
	var tsttld;
	var tsttldd;


	emailcode = 2;
	tmpdstr = ".";

	rightside = eaddr.split(/@/);

	var firstpos = rightside[1].lastIndexOf('.')+1;
	//console.log('firstpos: ' + firstpos);
	var lastpos = rightside[1].length;
	//console.log('lastpos: ' + lastpos);

	var Namer = rightside[1].substring(firstpos,lastpos);
	//console.log('Namer: ' + Namer);

	tsttldd = rightside[1].split('.');
	//tsttld = "." + tsttldd[1];
	tsttld = "." + Namer;

	if(tsttld.match(tmpdstr) != null) { //contains "." in rightside... good.
	for (x in tldomains)
	{
		tmptl = "." + tldomains[x];
		if(tsttld == tmptl) {
			emailcode = 99;
			emailcodestr = "Properly formatted e-mail address";
	   		return true;
		}// else {
//			emailcode = 2;
//			//$emailcodestr = "Invalid top level domain name";
//			tmpvar = "@" + rightside;
//			emailcodestr = "ERROR: " + tmpvar + " - Unacceptable domain name found in e-mail address";
//			console.log('emailcode: ' +emailcode);
//			console.log('emailcodestr: ' +emailcodestr);
//	   		return false;
//		}
	}

	if(emailcode == 2) {
		tmpvar = "@" + rightside[1];
		emailcodestr = "ERROR: " + tmpvar + " - Unacceptable domain name found in e-mail address";
   		return false;
	} else {
   		return true;
	}


//		foreach (@tldomains) {
//			#print "domain: $_\n";
//			$tmpdomstr = "." .$_;
//			#print "tmpdomstr: $tmpdomstr\n";
//			if($rightside =~ m/$tmpdomstr$/) {
//				#email code already set to 2.  If a good tldomain match is found, it
//				#will be set to 99.
//				#print "proper tlc domain found \n"; 
//				$emailcode = 99;
//				$emailcodestr = "Properly formatted e-mail address";
//			} else {
//				#redundant, I know.
//				#print "proper tlc domain NOT found! \n"; 
//			}
//		}
			
	}
}


//sub validate_emailaddy {
//#e-mail validity code
//#
//# 1 = missing @ email separator
//# 2 = unacceptable domain name (not in tldomains list)
//# 99 = E-mail accepted (passes all criteria)
//#
//
//	my $leftside;
//	my $rightside;
//	my $tmpdstr;
//	my $tmpdomstr;
//
//
//	($leftside,$rightside) = split(/@/, $emailckval);
//	#print "leftside: $leftside \n rightside: $rightside\n\n";
//	$emailcode = 2;
//	$tmpdstr = ".";
//
//	if($rightside =~ m/$tmpdstr/) {
//		#contains "." in rightside... good.
//		foreach (@tldomains) {
//			#print "domain: $_\n";
//			$tmpdomstr = "." .$_;
//			#print "tmpdomstr: $tmpdomstr\n";
//			if($rightside =~ m/$tmpdomstr$/) {
//				#email code already set to 2.  If a good tldomain match is found, it
//				#will be set to 99.
//				#print "proper tlc domain found \n"; 
//				$emailcode = 99;
//				$emailcodestr = "Properly formatted e-mail address";
//			} else {
//				#redundant, I know.
//				#print "proper tlc domain NOT found! \n"; 
//			}
//		}
//	}
//
//	if($emailcode == 2) {
//		my $tmpvar = "@" . $rightside;
//		$emailcodestr = "ERROR: $tmpvar - Unacceptable domain name found in e-mail address";
//	}
//
//	#print "emailcode: $emailcode\nemailcode string: $emailcodestr\n\n";
//}
//



function validatePhone(phstr) {
   var error = 0;
   var stripped = phstr.replace(/[\(\)\.\-\ ]/g, '');    
    if (isNaN(parseInt(stripped))) {
        //error = "The phone number contains illegal characters.\n";
        error = 1;
        //fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        //error = "The phone number is the wrong length. Make sure you included an area code.\n";
        error = 2;
        //fld.style.background = 'Yellow';
    } 
    return error;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    //alert("Invalid E-mail ID")
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }
		
	 if (str.indexOf(" ")!=-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

 	 return true;
}

function hccvalidate (f) {

var allok = 0;
var bdstr;
//var name = document.getElementById('fname');
//var comp = document.getElementById('company');
//var hcountry = document.getElementById('country');
//var sprov = document.getElementById('state');
//var phone = document.getElementById('phone');
//var phone2 = document.getElementById('phone2');

var fname = document.getElementById('attrib_114866');
var lname = document.getElementById('attrib_114867');
var hcountry = document.getElementById('attrib_114832');
var sprov = document.getElementById('attrib_114831');
var addr = document.getElementById('attrib_114829');
var ybirth = document.getElementById('attrib_114835');
var hcity = document.getElementById('attrib_114830');
var hzip = document.getElementById('attrib_114860');
var htermc = document.getElementById('hclegalaccept');

//bdstr = "<ul style=\"position:relative;left:0px;display:list-item;list-style: disc inside;font-size:10px;background:none;\">";
//bdstr = "<ul style=\"position:relative;left:0px;font-size:10px;background:none;\">";
bdstr = ""
if (fname.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:30px;display:list-item;list-style-type: disc;font-size:10px;padding: 2px;background:none;\">First Name &#187;Not Provided\n";
//	bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;First Name Not Provided\n";
	bdstr = bdstr + "First Name Not Provided\n";
	allok = 1;
}
if (lname.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:30px;display:list-item;list-style-type: disc;font-size:10px;padding: 2px;background:none;\">Last Name Not Provided\n";
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Last Name Not Provided\n";
	bdstr = bdstr + "Last Name Not Provided\n";
	allok = 1;
}
if (addr.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:30px;display:list-item;list-style-type: disc;font-size:10px;padding: 2px;background:none;\">Address Not Provided\n";
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Address Not Provided\n";
	bdstr = bdstr + "Address Not Provided\n";
	allok = 1;
}
//if (comp.value == "") {
//	bdstr = bdstr + "<li>Company Name Not Provided<br/>";
//	allok = 1;
//}
if (hcountry.value == "Select") {
	//bdstr = bdstr + "<li style=\"position:relative;left:30px;display:list-item;list-style-type: disc;font-size:10px;padding: 2px;background:none;\">Country Not Selected<br/>";
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Country Not Selected<br/>";
	bdstr = bdstr + "Country Not Selected\n";
	allok = 1;
}

//var mytmp2 = tmpstr.search(/Choose/i);

var theindex = document.getElementById('attrib_114831').selectedIndex;
//if(browser == 'Firefox') { console.log('mytmp2:' + mytmp2);}
//if(browser == 'Explorer') { window.status = 'uss0:' + uss[0];}
if(theindex == 0){
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;State or Province Not Selected<br/>";
	bdstr = bdstr + "State or Province Not Selected\n";
	allok = 1;
}
//if (phone.value == "") {
//	bdstr = bdstr + "<li>Telephone Not Provided<br/>";
//	allok = 1;
//}
if (hcity.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;City Not Provided<br/>";
	bdstr = bdstr + "City Not Provided\n";
	allok = 1;
}
if (hzip.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Zip Code Not Provided<br/>";
	bdstr = bdstr + "Zip Code Not Provided\n";
	allok = 1;
}
if (ybirth.value == "") {
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Year of Birth Not Provided<br/>";
	bdstr = bdstr + "Year of Birth Not Provided\n";
	allok = 1;
}

//if(browser == 'Firefox') { console.log('ybirth.value:' + ybirth.value);}
//if(browser == 'Firefox') { console.log('ybirth.value.length:' + ybirth.value.length);}
if (ybirth.value.length != 4 || ybirth.value==0 || ybirth.value<minYear || ybirth.value>maxYear){
	bdstr = bdstr + "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+ "\n";
	allok = 1;
}
/*
if (ybirth.length != 4){
	bdstr = bdstr + "Please enter a valid 4 digit year \n";
	allok = 1;
}
*/

//	if (ybirth.value<minYear || ybirth.value>maxYear){
//		bdstr = bdstr + "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+ "\n";
//		allok = 1;
//	}
//	if (ybirth.length != 4 || ybirth.value==0 || ybirth.value<minYear || ybirth.value>maxYear){
//		bdstr = bdstr + "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+ "\n";
//		allok = 1;
//	}

if (!htermc.checked) {
	//bdstr = bdstr + "<li style=\"position:relative;left:5px;font-size:10px;padding: 2px;background:none;\">&#187;&nbsp;Terms and Conditions Agreement Not Checked<br/>";
	bdstr = bdstr + "Terms and Conditions Agreement Not Checked\n";
	allok = 1;
}

if(allok == 1) {
	//bdstr = bdstr + "</ul>";
	//DumpValidHC(bdstr);
	alert(bdstr);
}

//if(allok == 0) {
//var thePhone = dForm.elements['phone'].value;
//var thePhone2 = dForm.elements['phone2'].value;

////Now, verify telephone
//        //error 1 = "The phone number contains illegal characters.\n";
//        //error 2 = "The phone number is the wrong length. Make sure you included an area code.\n";
//if (validatePhone(thePhone) == 1) {
//	DumpErr("Invalid Telephone Number Entered(Illegal Characters)!", "You must enter a valid Telephone Number to continue.");
//	allok = 1;
//
//} else if (validatePhone(thePhone) == 2) {
//
//	DumpErr("Invalid Telephone Number Entered!(Wrong Length)", "You must enter a valid Telephone Number to continue. Make sure you include an area code");
//	allok = 1;
//}
//} //if allok == 0
	return allok;
}

function validemail(eaddr){
var alloke = 0;
var errstr;

//var eaddr = dForm.elements['email'].value;
//var theEaddr2 = dForm.elements['email2'].value;
if(eaddr == "") {
	//DumpErr("No E-mail Address Provided!", "You must enter your e-mail address to continue.");
	alert("No E-mail Address Provided!\n\nYou must enter your e-mail address to continue.");
	alloke = 1;
} else if (echeck(eaddr)==false){
	//DumpErr("Invalid E-mail Address Provided!", "You must enter a valid e-mail address to continue.");
	alert("Invalid E-mail Address Provided!\n\nYou must enter a valid e-mail address to continue.");
	alloke = 1;
}  else if (etldck(eaddr)==false){
	errstr = emailcodestr + "  " + "You must enter a valid e-mail address to continue.";
	//DumpErr("Invalid E-mail Address Provided!", errstr);
	alert("Invalid E-mail Address Provided!\n\n" + errstr);
	alloke = 1;
}
//else if(eaddr2 == "") {
//	DumpErr("E-mail Address Not Re-Entered!", "You must re-enter your e-mail address to continue.");
//	alloke = 1;
//} else if (echeck(eaddr2)==false){
//	DumpErr("Invalid E-mail Address Re-Entered!", "You must enter the same valid e-mail address to continue.");
//	alloke = 1;
//} else if(eaddr != eaddr2) {
//	DumpErr("E-mail Addresses Do Not Match!", "The Two e-mail addresses you provided do not Match.");
//	alloke = 1;
//}
//console.log('alloke: ' + alloke);
return alloke;

}
