// Date: 04/03/2008
// Company: SkySuite
// Developer: John Oliva

var validate_field = 0;

function isEmail(s) {

isEmail1    = /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
isEmail2    = /^.*@[^_]*$/;

  return (isEmail1.test(s) && isEmail2.test(s));

}


function validateForm(s) {

	isValidString = /^[A-Za-z0-9'\-\.\,\:\\\_\/\(\)\ ]+$/;

        var msg = "Please enter value(s) for: ";
        var error_msg = "";
	e1 = 0;
	e2 = 0;
	validate_field = 0;


        if ((document.main_form.email.value == "") || (document.main_form.email.value == null))
        {
                error_msg += "Email Address";
		validate_field = 1;
		e1 = 1;
        }
        else
        {
                if (!isEmail(document.main_form.email.value)) {
                        alert("Please enter a valid e-mail address.");
			validate_field = 1;
			e2 = 1;
                        return false;
                } else {

                if (document.main_form.email.value != document.main_form.custentity_confirm_email.value)
                {
                        alert("Email Address and Confirm Email Address do not match.");
			validate_field = 2;
			e2 = 1;
                        return false;
                }

		}
        }


        if ((document.main_form.custentity_confirm_email.value == "") || (document.main_form.custentity_confirm_email.value == null))
        {
		if (e1 == 1) {
                	error_msg += ", ";
		} else {
			validate_field = 2;
		}
                error_msg += "Confirm Email Address";
                e1 = 1;
        }
        else
        {
                if (!isEmail(document.main_form.custentity_confirm_email.value)) {
                        alert("Please enter a valid e-mail address.");
			validate_field = 2;
			e2 = 1;
		        return false;
                } else {

                if (document.main_form.email.value != document.main_form.custentity_confirm_email.value)
                {
                        alert("Email Address and Confirm Email Address do not match.");
			e2 = 1;
			if ((document.main_form.email.value == "") || (document.main_form.email.value == null)) {
				validate_field = 1;
			} else {
				validate_field = 2;
			}
                        return false;
                }

		}
        }


	var pval1 = document.main_form.password.value;
	var pval2 = document.main_form.password2.value;
	if (pval1 != pval2) {
                alert("Passwords don\'t match.");
		validate_field = 5;
                e2 = 1;
                return false;
	} else if ((pval1 == "") || (pval1 == null)) {
                //alert("Passwords cannot be empty.");
		//validate_field = 5;
                //e2 = 1;
                //return false;
		if (e1 == 1) {
                	error_msg += ", ";
		} else {
			validate_field = 5;
		}
                error_msg += "Login Password, Confirm Password";
                e1 = 1;
	} else {
		pmsg = "";
		if (pval1.length < 6) {
            		pmsg += "Passwords must be at least 6 characters long.\n";
        	}
        	if (!/[A-Za-z]/.test(pval1)) {
            		pmsg += "Passwords must contain at least one letter (A-Z).\n";
        	}
        	if (!/[0-9!@#$%^&*.:;~'`*",_|= \<\>\/\\\+\?\-\(\)\[\]\{\}]/.test(pval1)) {
            		pmsg += "Passwords must contain at least one number or special character.\n";
        	}
        	if (!/^[A-Za-z0-9!@#$%^&*.:;~'`*",_|= \<\>\/\\\+\?\-\(\)\[\]\{\}]+$/.test(pval1)) {
            		pmsg += "Passwords may contain only letters, numbers, and special characters.\n";
        	}
        	if (pmsg) {
                        alert(pmsg);
			validate_field = 5;
                	e2 = 1;
                	return false;
        	}
	}
        if ((document.main_form.email.value == document.main_form.password.value) && (pval1.length > 0)) {
		alert("Password must not be the same as the email address.");
                validate_field = 5;
                e2 = 1;
                return false;
	}


        if ((document.main_form.firstname.value == "") || (document.main_form.firstname.value == null))
        {
		if (e1 == 1) {
                	error_msg += ", ";
		} else {
			validate_field = 3;
		}
                error_msg += "First Name";
		e1 = 1;
        }
        else
        {
                if (!isValidString.test(document.main_form.firstname.value)) {
                        alert("Please enter a valid first name.");
			validate_field = 3;
			e2 = 1;
                        return false;
		}
        }


        if ((document.main_form.lastname.value == "") || (document.main_form.lastname.value == null))
        {
		if (e1 == 1) {
                	error_msg += ", ";
		} else {
			validate_field = 4;
		}
                error_msg += "Last Name";
		e1 = 1;
        }
        else
        {
                if (!isValidString.test(document.main_form.lastname.value)) {
                        alert("Please enter a valid last name.");
			validate_field = 4;
			e2 = 1;
                        return false;
		}
        }


	myOption = -1;
	for (i=document.main_form.custentity_collector.length-1; i > -1; i--) {
        	if (document.main_form.custentity_collector[i].checked) {
        		myOption = i; i = -1;
		}
	}
	if (myOption == -1) {
		if (e1 == 1) {
                	error_msg += ", ";
		}
                error_msg += "Collector";
		e1 = 1;
	}


        if (document.main_form.custentity_brands_7.checked == "1") {
		document.main_form.custentity_brands.options[6].selected = true;
        }
        if (document.main_form.custentity_brands_6.checked == "1") {
		document.main_form.custentity_brands.options[5].selected = true;
        }
        if (document.main_form.custentity_brands_5.checked == "1") {
		document.main_form.custentity_brands.options[4].selected = true;
        }
        if (document.main_form.custentity_brands_4.checked == "1") {
		document.main_form.custentity_brands.options[3].selected = true;
        }
        if (document.main_form.custentity_brands_3.checked == "1") {
		document.main_form.custentity_brands.options[2].selected = true;
        }
        if (document.main_form.custentity_brands_2.checked == "1") {
		document.main_form.custentity_brands.options[1].selected = true;
        }
        if (document.main_form.custentity_brands_1.checked == "1") {
		document.main_form.custentity_brands.options[0].selected = true;
        }



        var count = 0;

        if (document.main_form.custentity_where_do_you_buy_11.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[10].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_10.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[9].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_9.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[8].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_8.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[7].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_7.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[6].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_6.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[5].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_5.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[4].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_4.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[3].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_3.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[2].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_2.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[1].selected = true;
		count++;
        }
        if (document.main_form.custentity_where_do_you_buy_1.checked == "1") {
		document.main_form.custentity_where_do_you_buy.options[0].selected = true;
		count++;
        }



	if (count == 0) {
		if (e1 == 1) {
                	error_msg += ", ";
		}
                error_msg += "Where Do You Buy";
		e1 = 1;
	}



	myOption = -1;
	for (i=document.main_form.custentity_local_dealer.length-1; i > -1; i--) {
        	if (document.main_form.custentity_local_dealer[i].checked) {
        		myOption = i; i = -1;
		}
	}
	if (myOption == -1) {
		if (e1 == 1) {
                	error_msg += ", ";
		}
                error_msg += "Local Dealer";
		e1 = 1;
	}


        if (document.main_form.contest_terms.checked) {
		document.main_form.custentity_contest_terms.value = "T";
        } else {
		document.main_form.custentity_contest_terms.value = "F";
	}


        if (error_msg) {
		// to prevent double alerts in Mozilla???
		if (e2 == 0) {
                	alert(msg+error_msg);
		}
                return false;
        }


	// Set first and last name hidden fields
	document.main_form.custentity_firstname.value = document.main_form.firstname.value
	document.main_form.custentity_lastname.value = document.main_form.lastname.value

	
}


function validateEmail( field ) {

        var email = field.value;

        if ( email != '' ) {
                if ( !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email) ){
                        validate_field = 1;
                        alert("Please enter a valid e-mail address.");
                }
                else {
                        if (document.main_form.custentity_confirm_email.value != '') {
                                if (document.main_form.email.value != document.main_form.custentity_confirm_email.value) {
                                        validate_field = 1;
                                        alert("Email Address and Confirm Email Address do not match.");
                                } else {
                                        validate_field = 0;
                                }
                        } else {
                                validate_field = 0;
                        }
                }
        }
        else {
                validate_field = 0;
        }
}


function validateConfirm( field ) {

        var email = field.value;

        if ( email != '' ) {
                if ( !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email) ){
                        validate_field = 2;
                        alert("Please enter a valid e-mail address.");
                }
                else {
                        if (document.main_form.email.value != document.main_form.custentity_confirm_email.value) {
                                validate_field = 2;
                                alert("Email Address and Confirm Email Address do not match.");
                        } else {
                                validate_field = 0;
                        }
                }
        }
        else {
                validate_field = 0;
        }

}


function validatefname( field ) {

	isValidString = /^[A-Za-z0-9'\-\.\,\:\\\_\/\(\)\ ]+$/;

        if ((document.main_form.firstname.value == "") || (document.main_form.firstname.value == null))
        {
                alert("Please enter value for: First Name.");
                validate_field = 3;
                return false;
        }
        else if (!isValidString.test(document.main_form.firstname.value)) {
                        alert("Please enter a valid first name.");
                        validate_field = 3;
                        return false;
                } else {
                        validate_field = 0;
		}
}


function validatelname( field ) {

	isValidString = /^[A-Za-z0-9'\-\.\,\:\\\_\/\(\)\ ]+$/;

        if ((document.main_form.lastname.value == "") || (document.main_form.lastname.value == null))
        {
                alert("Please enter value for: Last Name.");
                validate_field = 4;
                return false;
        }
        else if (!isValidString.test(document.main_form.lastname.value)) {
                        alert("Please enter a valid Last Name.");
                        validate_field = 4;
                        return false;
                } else {
                        validate_field = 0;
		}
}


function checkValidatedFields() {

        if ( validate_field == 1 ) {
                document.main_form.email.select();
                document.main_form.email.focus();
        }
        else if ( validate_field == 2 ) {
                document.main_form.custentity_confirm_email.select();
                document.main_form.custentity_confirm_email.focus();
        }
        else if ( validate_field == 3 ) {
                document.main_form.firstname.select();
                document.main_form.firstname.focus();
        }
        else if ( validate_field == 4 ) {
                document.main_form.lastname.select();
                document.main_form.lastname.focus();
        }
}

