function validateEmailInfo(EmailForm) {

	if (!checkEmail(EmailForm.email.value)) {
        alert("Please enter a valid e-mail address!");
        EmailForm.email.focus();
		EmailForm.email.select();
		return false;
    }

	if (EmailForm.email.value != EmailForm.vemail.value)
	{
        alert("Email addresses do not match! Please make sure your email address is correct!");
        EmailForm.vemail.focus();
		EmailForm.vemail.select();
		return false;
	}

	return true;	
}

function validateRegistrationInfo(RefForm) {

var sStr;

	if (RefForm.username.value == "") {
        	alert("Please choose a username!");
           	RefForm.username.focus();
      		return false;
	}

	if (!checkLogin(RefForm.username.value)) {
		alert("Username can only consist of letters and numbers!");
		RefForm.username.value = ""
		RefForm.username.focus();	           	
		return false;
	}

	if (RefForm.pw.value == "") {
        	alert("Please enter a password!");
           	RefForm.pw.focus();
      		return false;
	}

	if (RefForm.vpw.value == "") {
        	alert("Please retype the password!");
           	RefForm.vpw.focus();
      		return false;
	}

	if (RefForm.pw.value != RefForm.vpw.value) {
        	alert("Passwords do not match!");
			RefForm.pw.value = "";
			RefForm.vpw.value = "";
           	RefForm.pw.focus();
      		return false;
	}

	if (RefForm.fname.value == "") {
        	alert("Please enter your first name!");
           	RefForm.fname.focus();
      		return false;
	}
	if (!checkName(RefForm.fname.value)) {
		alert("Please enter your first name!");
		RefForm.fname.value = ""
		RefForm.fname.focus();	           	
		return false;
	}

	if (RefForm.lname.value == "") {
        	alert("Please enter your last name!");
           	RefForm.lname.focus();
      		return false;
	}
	if (!checkName(RefForm.lname.value)) {
		alert("Please enter your last name!");
		RefForm.lname.value = ""
		RefForm.lname.focus();	           	
		return false;
	}

	if (RefForm.gender.value == "") {
        	alert("Please select your gender!");
           	RefForm.gender.focus();
      		return false;
	}

	if (RefForm.month.value == "") {
        	alert("Please select the month you were born!");
           	RefForm.month.focus();
      		return false;
	}

	if (RefForm.day.value == "") {
        	alert("Please select the day you were born!");
           	RefForm.day.focus();
      		return false;
	}

	if (RefForm.year.value == "") {
        	alert("Please select the year you were born!");
           	RefForm.year.focus();
      		return false;
	}

	sStr = RefForm.month.value+'/'+RefForm.day.value+'/'+RefForm.year.value;
	if (isDate(sStr) == false){
           	RefForm.month.focus();
      		return false;
	}


	if (RefForm.city.value == "") {
        	alert("Please enter the city you live in!");
           	RefForm.city.focus();
      		return false;
	}

	if ((RefForm.state.value == "") && (RefForm.country.value == "")) {
        	alert("Please select the state or enter the country you live in!");
           	RefForm.state.focus();
      		return false;
	}

	if (RefForm.country.value == "") {
        	alert("Please enter the country you live in!");
           	RefForm.country.focus();
      		return false;
	}

	if (!checkEmail(RefForm.email.value)) {
        alert("Please enter a valid e-mail address!");
        RefForm.email.focus();
		RefForm.email.select();
		return false;
    }

	if (RefForm.email.value != RefForm.vemail.value)
	{
        alert("Email addresses do not match! Please make sure your email address is correct!");
        RefForm.vemail.focus();
		RefForm.vemail.select();
		return false;
	}

	if ((RefForm.phone1.value.length > 0) && (RefForm.phone1.value.length < 3))  {
		alert("Please enter in the entire phone number!")
        RefForm.phone1.focus();
		return false;
	}

	if ((RefForm.phone2.value.length > 0) && (RefForm.phone2.value.length < 3))  {
		alert("Please enter in the entire phone number!")
        RefForm.phone2.focus();
		return false;
	}

	if ((RefForm.phone3.value.length > 0) && (RefForm.phone3.value.length < 4))  {
		alert("Please enter in the entire phone number!")
        RefForm.phone3.focus();
		return false;
	}

	if (((RefForm.phone1.value.length > 0) || (RefForm.phone2.value.length > 0) || (RefForm.phone3.value.length > 0)) && ((RefForm.phone1.value.length == 0) || (RefForm.phone2.value.length == 0) || (RefForm.phone3.value.length == 0))) {
		alert("Please enter in the entire phone number!")
        RefForm.phone1.focus();
		return false;
	}

	if (RefForm.terms.checked == false) {
        alert("Please agree to the terms and conditions!");
      	return false;
	}

	return true;	
}

function checkName(checkSyntax) {

	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
	var flag
   
	for (i=0; i<checkSyntax.length; i++) 
	{
		loginchar = checkSyntax.charAt(i)
		flag = false
		for (j=0; j<validChars.length; j++) 
		{
			if (loginchar == validChars.charAt(j) ) 
				flag = true
		}
		if (flag == false) 
			return false
	}

     return true
}

function checkEmail(checkSyntax) {

	var invalidChars = " /:,;"
        
	if (checkSyntax == "") 
      	{ return false }
   
   	for (i=0; i<invalidChars.length; i++) {
      		badChar = invalidChars.charAt(i)
      		if (checkSyntax.indexOf(badChar,0) > -1) 
         		{ return false }
   	}
   
   	var atPos = checkSyntax.indexOf("@",1)
      	if (atPos == -1) 
     	 { return false }

   	if (checkSyntax.indexOf("@",atPos+1) > -1) 
      	{ return false }
      
   	var periodPos = checkSyntax.indexOf(".",atPos)
      	if (periodPos == -1) 
         	{ return false }
         
      	if (periodPos+3 > checkSyntax.length) 
         	{ return false }
       
     	return true
}

function checkLogin(checkSyntax) {

	var validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var flag
   
	for (i=0; i<checkSyntax.length; i++) 
	{
		loginchar = checkSyntax.charAt(i)
		flag = false
		for (j=0; j<validChars.length; j++) 
		{
			if (loginchar == validChars.charAt(j) ) 
				flag = true
		}
		if (flag == false) 
			return false
	}

     return true
}

function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
	alert("Please enter date as mm/dd/yyyy");
	return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Month "+month+" doesn`t have 31 days!")
	return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " does not have " + day + " days!");
			return false;
		}
	}

return true; // date is valid
}
