/* Without RegExps */
function isEmail(who) {
	function isEmpty(who) {
		var testArr=who.split("");
		if(testArr.length==0)
			return true;
		var toggle=0;
		for(var i=0; i<testArr.length; i++) {
			if(testArr[i]==" ") {
				toggle=1;
				break;
			}
		}
		if(toggle)
			return true;
		return false;
	}

	function isValid(who) {
		var invalidChars=new Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?");
		var testArr=who.split("");
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}

	function isfl(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		which=0;
		for(var i=0; i<2; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[which]==invalidChars[j]) {
					return false;
				}
			}
			which=testArr.length-1;
		}
		return true;
	}

	function isDomain(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		if(testArr.length<2||testArr.length>4) {
			return false;
		}
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}


	var testArr=who.split("@");
	if(testArr.length<=1||testArr.length>2) {
		return false;
	}
	else {
		if(isValid(testArr[0])&&isfl(testArr[0])&&isValid(testArr[1])) {
			if(!isEmpty(testArr[testArr.length-1])&&!isEmpty(testArr[0])) {
				var testArr2=testArr[testArr.length-1].split(".");
				if(testArr2.length>=2) {
					var toggle=1;
					for(var i=0; i<testArr2.length; i++) {
						if(isEmpty(testArr2[i])||!isfl(testArr2[i])) {
							toggle=0;
							break;
						}
					}
					if(toggle&&isDomain(testArr2[testArr2.length-1]))
						return true;
					return false;
				}
				return false;
			}
		}
	}
}

//Credit Card Validation - Venky
function ValidateCC(ccNumber){
  var bOdd = true;
  var sum = new Number(0);
  var num = new Number(0);
  // check for a valid length
  if (ccNumber.length <13 || ccNumber.length> 16) {
    return ""; // Invalid Credit Card Number";
  }
  // beginning with the rightmost digit, 
  // multiply the number by 1 if
  // the position is odd, or 2 if the position is even.
  // if the product is greater than 9, the units and tens 
  // position are added together. 
  // if the final answer is divisble by 10, it's good...
  // otherwise the card is invalid.
  for (var i = ccNumber.length - 1; i >= 0; i--){ 
    num = Number(ccNumber.substr(i,1));
    if ( bOdd ) { 
      sum = sum + (num * 1);
    }else{
      num = (num * 2);
      var tString = String(num);
      var n = (tString.length -1);
      for (n; n>=0; n--){
        sum = sum + Number(tString.substr(n,1));
      }
    }
    bOdd = !bOdd;
  }
  if ((sum % 10) != 0) return ""; 
  // once we know the number is good, loop the card types
  // for unmatched numbers, return a blank 
  // for valid cards, return the card name 
  if (Number(ccNumber.length) == 16 && 
    ccNumber.substring(0,2)>="51" && 
    ccNumber.substring(0,2)<="55") return "MASTERCARD";
  if (Number(ccNumber.length) == 16 && 
    ccNumber.substring(0,1)=="4") 
    return "VISA";
  if (Number(ccNumber.length) == 13 && 
    (ccNumber.substring(0,1)=="4")) 
    return "VISA";
  if (Number(ccNumber.length) == 16 && 
    ccNumber.substring(0,4)=="6011") 
    return "DISCOVER";
  if (Number(ccNumber.length) == 15 && 
    (ccNumber.substring(0,2)=="34" || 
    ccNumber.substring(0,2)=="37")) return "AMEX";
  if (Number(ccNumber.length) == 14 && 
    (ccNumber.substring(0,2)=="36" || 
    ccNumber.substring(0,2)=="30" || 
    ccNumber.substring(0,2)=="38")) return "DINERSCLUB";
  return ""; // unknown credit card type 
}


function searchValidator(searchengine) {

	if (document.searchengine.q.value == "")
	   {
		alert("Enter your search string.");
		document.searchengine.q.focus();
		return (false);
	   }   
	return (true);
}


function memFormValid(){

var CoName = document.frmMemship.firstname.value;
	if (CoName == "")
	{
		alert("\nThe First name field is blank.\n\nPlease enter your First name.")
		document.frmMemship.firstname.focus();
		return false;
	}
	
	for (var i = 0; i < CoName.length; i++) 
	{
		var ch = CoName.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
		{
			alert("\nThe First name field only accepts letters & spaces.\n\nPlease re-enter your name.");
			document.frmMemship.firstname.focus();
			return false;
	   	}
	}

var CoName1 = document.frmMemship.lastname.value;
	if (CoName1 == "")
	{
		alert("\nThe Last name field is blank.\n\nPlease enter your Last name.")
		document.frmMemship.lastname.focus();
		return false;
	}
	
	for (var i = 0; i < CoName1.length; i++) 
	{
		var ch = CoName1.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
		{
			alert("\nThe Last name field only accepts letters & spaces.\n\nPlease re-enter your name.");
			document.frmMemship.lastname.select();
			document.frmMemship.lastname.focus();
			return false;
	   	}
	}


var Company1=document.frmMemship.company;
	if (Company1.value == "") {
		alert("Please enter your Company Name");
		Company1.focus();
		return false;
	}

if((document.frmMemship.email.value == "") || (!isEmail(document.frmMemship.email.value)))
	{
		alert("Please Enter your E-mail");
		document.frmMemship.email.focus();
		return false;
	}
	
	
if((document.frmMemship.confirmation.value == "") || (!isEmail(document.frmMemship.confirmation.value)))
	{
		alert("Please Confirm your E-mail");
		document.frmMemship.confirmation.focus();
		return false;
	}
	
	return (true);
 }
 

function radio_button_checker()
{
	//Membership Choice Check
	//=======================
	// set var mship_choice to false
	var mship_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < pay_form.status.length; counter++)
	{
	// If a radio button has been selected it will return true
	// (If not it will return false)
	if (pay_form.status[counter].checked)
	mship_choice = true; 
	}
	
	if (!mship_choice)
	{
	// If there were no selections made display an alert box 
	alert("Please select membership option.")
	return (false);
	}
	
	//Payment Preference Check
	//=======================
	// set var pay_choice to false
	var pay_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < pay_form.paypref.length; counter++)
	{
	// If a radio button has been selected it will return true
	// (If not it will return false)
	if (pay_form.paypref[counter].checked)
		pay_choice = true;
	}
	
	if (!pay_choice)
	{
	// If there were no selections made display an alert box 
	alert("Please select payment option.")
	return (false);
	}
	
return (true);
}


 
function payFormValid(){

		    	//CCard Validation Start
			var CCNo=document.frmPayment.ccno.value;
			
			if (CCNo.value != ""){
				if ((CCNo.value == "") || !ValidateCC(CCNo.value) || (isNaN(CCNo.value))) {
					alert("Please Enter a Valid Credit Card Number");
					CCNo.value="";
					CCNo.focus();
					return false;
				}
			}
		
			var CCHolderName=document.frmPayment.cardholdername.value;
			if (CCHolderName == "")
			{
				alert("\nThe CARD HOLDER NAME field is blank.\n\nPlease enter card holder name.")
				document.frmPayment.cardholdername.focus();
				return false;
			}
			
			for (var i = 0; i < CCHolderName.length; i++) 
			{
				var ch = CCHolderName.substring(i, i + 1);
				if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
				{
					alert("\nThe CARD HOLDER NAME field only accepts letters & spaces.\n\nPlease re-enter your card holder name.");
					document.frmPayment.CCHolderName.select();
					document.frmPayment.CCHolderName.focus();
					return false;
			   	}
			}
			
			// Variables for the current date, year and month
			var right_now=new Date();
			var the_year=right_now.getYear();
			var the_month=right_now.getMonth();
			
			if ((frmPayment.u_month.value == true) || (frmPayment.u_year.value == true)){
			
				// Check for input in both the month and year fields
				if (frmPayment.u_month.value == "" || frmPayment.u_year.value == "")
				{
				alert("Please select a month and year");
				return false;
				}
				
				// Check to see if the request is within the current year and month
				if (frmPayment.u_year.value == the_year && frmPayment.u_month.value > the_month)
				{
					return (true);
				}
				else
				{
					alert("Please check the month of your request.");
					return (false);
				}
				
				// Check to see if the year request is valid
				if (frmPayment.u_year.value >= the_year)
				{
					return (true);
				}
				else
				{
					alert("Please check the year of your request.");
					return (false);
				}
				
			}
			//End CCard Validation End

	//return (true);
}

function eventFormValid(){
	
	if (!(document.frmEventsReg.conference_type[0].checked || document.frmEventsReg.conference_type[1].checked))
	{
		alert("Please select your conference registration option.");
		document.frmEventsReg.conference_type[0].focus();
		return false;
	}

	var Fname=document.frmEventsReg.fname.value;
	if (Fname == "")
	{
		alert("\nThe NAME field is blank.\n\nPlease enter your name.")
		document.frmEventsReg.fname.focus();
		return false;
	}
	
	for (var i = 0; i < Fname.length; i++) 
	{
		var ch = Fname.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
		{
			alert("\nThe NAME field only accepts letters & spaces.\n\nPlease re-enter your name.");
			document.frmEventsReg.fname.select();
			document.frmEventsReg.fname.focus();
			return false;
	   	}
	}

	var CoName = document.frmEventsReg.co_name.value;
	if (CoName == "")
	{
		alert("\nThe COMPANY NAME field is blank.\n\nPlease enter your company name.")
		document.frmEventsReg.co_name.focus();
		return false;
	}
	
	for (var i = 0; i < CoName.length; i++) 
	{
		var ch = CoName.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
		{
			alert("\nThe COMPANY NAME field only accepts letters & spaces.\n\nPlease re-enter your company name.");
			document.frmEventsReg.co_name.select();
			document.frmEventsReg.co_name.focus();
			return false;
	   	}
	}


	var Address=document.frmEventsReg.address;
	if (Address.value == "") {
		alert("Please Enter your Address");
		Address.focus();
		return false;
	}

	var City=document.frmEventsReg.city;
	if (City.value == "") {
		alert("Please enter your City Name");
		City.focus();
		return false;
	}
	
	if (document.frmEventsReg.state.selectedIndex == 0)
	{
		alert("Please select your state.");
		document.frmEventsReg.state.focus();
		return false;
	}


	var Zip1=document.frmEventsReg.zip1;
	if ((Zip1.value == "") || (Zip1.value.length < 5) || (isNaN(Zip1.value) || (Zip1.value.length > 5))) {
		alert("Please enter a Valid First 5 Digit Zipcode");
		Zip1.focus();
		return false;	
	}

/*	var Zip2=document.frmEventsReg.zip2;
	if ((Zip2.value == "") || (Zip2.value.length < 4) || (isNaN(Zip2.value) || (Zip2.value.length > 4))) {
		alert("Please enter a Valid Second 4 Digit Zipcode");
		Zip2.focus();
		return false;	
	}
*/	
	var Phone=document.frmEventsReg.phone;
	if ((Phone.value == "") || (Phone.value.length < 10) || (isNaN(Phone.value) || (Phone.value.length > 10))) {
		alert("Please enter a 10 digit Valid Phone Number");
		Phone.focus();
		return false;
	}

	if((document.frmEventsReg.email.value == "") || (!isEmail(document.frmEventsReg.email.value)))
	{
		alert("Please Enter a Valid E-mail!");
		document.frmEventsReg.email.focus();
		return false;
	}
	
	if (document.frmEventsReg.Payment_Method.selectedIndex == 0)
	{
		alert("Please select your payment method.");
		document.frmEventsReg.Payment_Method.focus();
		return false;
	}	

	if (document.frmEventsReg.Payment_Method.selectedIndex == 2)
	{	
		if (!(document.frmEventsReg.cctype[0].checked || document.frmEventsReg.cctype[1].checked))
		{
			alert("Please select Credit Card Type")
			document.frmEventsReg.cctype[0].focus();
			return false;
		}
			
		if ((document.frmEventsReg.ccno.value == "") || !ValidateCC(document.frmEventsReg.ccno.value) || (isNaN(document.frmEventsReg.ccno.value)))
		{
			alert("Please Enter a Valid Credit Card Number");
			document.frmEventsReg.ccno.value="";
			document.frmEventsReg.ccno.focus();
			return false;
		}
		
/*		if(document.frmEventsReg.cardholdername.value == "")
		{
			alert("Please Enter Card Holder\'s Name");
			document.frmEventsReg.cardholdername.focus();
			return false;
		}
*/		
		/*
		
		// Variables for the current date, year and month
		var right_now=new Date();
		var the_year=right_now.getYear();
		var the_month=right_now.getMonth();
		
//		if ((document.frmEventsReg.u_month.value == true) || (document.frmEventsReg.u_year.value == true))
//		{
		
			// Check for input in both the month and year fields
			if (document.frmEventsReg.u_month.value == "" || document.frmEventsReg.u_year.value == "")
			{
				alert("Please select a month and year");
				return false;
			}
			
			// Check to see if the request is within the current year and month
			if (document.frmEventsReg.u_year.value == the_year && document.frmEventsReg.u_month.value > the_month)
			{
				return (true);
			}
			else
			{
				alert("Please check the month of your request.");
				return (false);
			}
			
			// Check to see if the year request is valid
			if (document.frmEventsReg.u_year.value >= the_year)
			{
				return (true);
			}
			else
			{
				alert("Please check the year of your request.");
				return (false);
			}		
//		}
		*/
	}	

	return (true);
}

function mail_listValid(){

	var Fname=document.frmJoin.first_name;
	if (Fname.value == "") {
		alert("Please enter your First Name");
		Fname.focus();
		return false;
	}

	var Lname=document.frmJoin.last_name;
	if (Lname.value == "") {
		alert("Please enter your Last Name");
		Lname.focus();
		return false;
	}

	var CoName = document.frmJoin.co_name.value;
	if (CoName == "")
	{
		alert("\nThe COMPANY NAME field is blank.\n\nPlease enter your company name.")
		document.frmJoin.co_name.focus();
		return false;
	}
	
	var Address=document.frmJoin.address;
	if (Address.value == "") {
		alert("Please Enter your Address");
		Address.focus();
		return false;
	}

	var City=document.frmJoin.city;
	if (City.value == "") {
		alert("Please enter your City Name");
		City.focus();
		return false;
	}
	
	if (document.frmJoin.state.selectedIndex == 0)
	{
		alert("Please select your state.");
		document.frmJoin.state.focus();
		return false;
	}

	var Zip1=document.frmJoin.zip1;
	if ((Zip1.value == "") || (Zip1.value.length < 5) || (isNaN(Zip1.value) || (Zip1.value.length > 5))) {
		alert("Please enter a Valid First 5 Digit Zipcode");
		Zip1.focus();
		return false;	
	}

	if((document.frmJoin.email.value == "") || (!isEmail(document.frmJoin.email.value)))
	{
		alert("Please Enter a Valid E-mail ID!");
		document.frmJoin.email.focus();
		return false;
	}

	var Phone=document.frmJoin.phone;
	if ((Phone.value == "") || (Phone.value.length < 10) || (isNaN(Phone.value) || (Phone.value.length > 10))) {
		alert("Please enter a 10 digit Valid Phone Number");
		Phone.focus();
		return false;
	}

	if (document.frmJoin.countries_region.selectedIndex == 0)
	{
		alert("Please select Countries or Regions Your Company is Most Interested In.");
		document.frmJoin.countries_region.focus();
		return false;
	}
	
	return (true);
 }
