function SubmitPage() {

    var Passed = true;
    var sErrMsg = "";
    for (var i = 0; i <= (document.forms[0].elements.length - 1); ++i) {
        if ((document.forms[0].elements[i].type == "text") && ((document.forms[0].elements[i].name != "phone") && (document.forms[0].elements[i].name != "address2") && (document.forms[0].elements[i].name != "province") && (document.forms[0].elements[i].name != "eMailaddress")))
        {
            if (document.forms[0].elements[i].value.length < 2)
            {
                sErrMsg = sErrMsg + document.forms[0].elements[i].name + "\n";
            }
        }
    }

    if (sErrMsg.length > 0 && Passed == true) {
        sErrMsg = "The following fields must be completed:\n\n" + sErrMsg + "\nPlease correct these errors to continue.";
        alert(sErrMsg);
        Passed = false;
    }

    if (Passed == true) {
        return true;
    } else {
        return false;
    }
}

//--------------------------------------------------------------------------------------------------------

function openit() {
    pass = open("page.asp?PageID=27", "passport", "dependent=yes,toolbar=no,width=400,height=310,directories=no,status=no,scrollbars=no,resize=yes,menubar=no,screenX=25,screenY=25");
}

//--------------------------------------------------------------------------------------------------------

function gotourl(id) {
    document.forms[id].submit();
}

//--------------------------------------------------------------------------------------------------------
//Function for checking the blank test.
function isBlank(s) {
    if (isNaN(s))
        return false;
    var len = s.length;
    var i = 0;
    if (len < 1)
        return true;
    for (i = 0; i < len; ++i)
    {
        if (s.charAt(i) != " ") {
            return false;
        }
    }
    return true;
}
//Function for checking the name test.
function nameCheck(Ctrl) {
	cmp = " '-.,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}

//Function for checking the address test.
function addressCheck(Ctrl) {
	cmp = " :,.()@+'\/#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}

//Function for checking the numeric test.
function numericCheck(Ctrl) {
	cmp = "0123456789"
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}

//Function for checking the Zip test.
function alphanumCheckForCanada(Ctrl) {
    cmp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}

//Function for checking the Zip test.
function passhintCheck(Ctrl) {
	cmp = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'"
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}

//Function for checking the address test.
function passwordCheck(Ctrl) {
	cmp = ":,.()@+'\#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    for (var i = 0; i < Ctrl.length; i++) {
        tst = Ctrl.substring(i, i + 1)
        if (cmp.indexOf(tst) < 0) {
            return (false)
        }
    }
    return (true)
}


function checkit(pFormName) {

    var passed = true;
    // regular expression to check if the email address is valid.
    var re = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/g;

    if (document.forms[pFormName].elements['firstName'])
    {
        var fname = document.getElementById('firstName').value;
        if (isBlank(fname))
        {
            alert("Please enter your first name to continue.");
            document.getElementById('firstName').focus();
            return false;
        }
        else if (!nameCheck(fname)) {
            alert("First name can only contain letters.");
            document.getElementById('firstName').focus();
            return (false)
        }
    }

    if (document.forms[pFormName].elements['lastName'])
    {
        var lname = document.getElementById('lastName').value;
        if (isBlank(lname)) {
            alert("Please enter your last name to continue.");
            document.getElementById('lastName').focus();
            return false;
        }
        else if (!nameCheck(lname)) {
            alert("Last name can only contain letters.");
            document.getElementById('lastName').focus();
            return (false)
        }
    }

    if (document.forms[pFormName].elements['loyalityId'] && document.forms[pFormName].elements['loyalityId'].value.length > 0)
    {
        var lid = document.getElementById('loyalityId').value;
        if (!numericCheck(lid)) {
            alert("If a Passport number is entered, then it must be all numeric characters.");
            document.getElementById('loyalityId').focus();
            return (false)
        }
    }

    if (document.forms[pFormName].elements['pAddress1'])
    {
        var addr1 = document.getElementById('pAddress1').value;
        if (isBlank(addr1)) {
            alert("Please enter your address to continue.");
            document.getElementById('pAddress1').focus();
            return false;
        }
        else if (!addressCheck(addr1)) {
            alert("Your address contains invalid characters.");
            document.getElementById('pAddress1').focus();
            return (false)
        }
    }

    if (document.forms[pFormName].elements['pAddress2'])
    {
        var addr2 = document.getElementById('pAddress2').value;
        if ((addr2.length > 0) && !addressCheck(addr2)) {
            alert("Please enter a valid Special Instructions to continue.");
            document.getElementById('pAddress2').focus();
            return (false)
        }
    }

    if (document.forms[pFormName].elements['pCity'])
    {
        var city = document.getElementById('pCity').value;
        if (isBlank(city)) {
            alert("Please enter your city to continue.");
            document.getElementById('pCity').focus();
            passed = false;
            return false;
        }
        else if (!nameCheck(city)) {
            alert("Your city contains invalid characters.");
            document.getElementById('pCity').focus();
            passed = false;
            return (false)
        }
    }

    if (document.forms[pFormName].elements['pState'])
    {
        var pcont = document.getElementById('pCountry').value;
        var psta = document.getElementById('pState').value;
        if ((pcont != 'USA') && (pcont != 'CAN') && (pcont != 'AUS') && (psta.length > 1)) {
            document.getElementById('pState').value = '';
            alert("State is for residents in USA, Canada or Australia only.  Please verify your selection of country and state.");
            document.getElementById('pState').focus();
            return (false)
        }
        else if ((psta.length < 1 ) && ((pcont == 'USA') || (pcont == 'CAN') || (pcont == 'AUS'))) {
            alert("Please enter your state to continue.");
            document.getElementById('pState').focus();
            passed = false;
            return false;
        }
    }

    if(document.forms[pFormName].elements['pPostalCode'])
    {
    	var zip = document.getElementById('pPostalCode').value;
        var country = document.getElementById('pCountry').value;
        if(country == 'USA' || country == 'CAN')
        {
        	// value is required for USA and Canada (USA includes Military addresses)
	        if (isBlank(zip)) 
	        {
	        	if(country == 'USA')
	        	{
					alert("Please re-enter your zip code.  For US addresses the zip code should be a 5 or 9-digit numeric code.");
				}
				else
				{
					alert("Please re-enter your zip code.  For Canadian addresses the zip code should be a 6-digit alpha-numeric code.");
				}
	            document.getElementById('pPostalCode').focus();
	            passed = false;
	            return false;
	        }
        	if(country == 'USA')
        	{
				var zipStripped = zip.replace(/\-/g,'');
				zipStripped = zipStripped.replace(/ /g,'');
	        	if (!numericCheck(zipStripped)) 
	        	{
            		alert("Please re-enter your zip code.  For US addresses the zip code should be a 5 or 9-digit numeric code.");
            		document.getElementById('pPostalCode').focus();
            		passed = false;
            		return false;
	        	}
        		if(zipStripped.length != 5 && zipStripped.length != 9)
        		{
					alert("Please re-enter your zip code.  For US addresses the zip code should be a 5 or 9-digit numeric code.");
            		document.getElementById('pPostalCode').focus();
            		passed = false;
            		return false;
        		}
			}
        	else if(!alphanumCheckForCanada(zip))
        	{
            	alert("Please re-enter your zip code.  For Canadian addresses the zip code should be a 6-digit alpha-numeric code.");
            	document.getElementById('pPostalCode').focus();
            	passed = false;
            	return false;
        	}
		} // if USA or CANADA
	} // pPostalCode

    if (document.forms[pFormName].elements['pPhoneNumber'])
    {
        var lname = document.getElementById('pPhoneNumber').value;
        if (isBlank(lname)) 
        {
            alert("Please enter your phone number to continue.");
            document.getElementById('pPhoneNumber').focus();
            passed = false;
            return false;
        }
        else 
        {
        	var billPhoneStr = document.getElementById('pPhoneNumber').value;
			var stripped = billPhoneStr.replace(/[\(\)\ \.\-]/g,'');
	        if (!numericCheck(stripped)) {
	        	alert("Phone number with illegal characters. Please enter your phone number to continue.");
	        	document.getElementById('pPhoneNumber').focus();
	        	passed = false;
	        	return (false)
	        }
	        else
	        {
				if (((document.getElementById('pCountry').value == 'USA') || (document.getElementById('pCountry').value == 'CAN')) &&
				    (stripped.length < 10) && (passed))
				    {
						alert("Please enter your complete phone number to continue.");
				        document.getElementById('pPhoneNumber').focus();
				        passed = false;
				        return false;
				    }
        	}
    	}
	} // pPhoneNumber

    if ((document.forms[pFormName].elements['pCountry'].value.length < 1) && (passed)) {
        alert("Please enter your country to continue.");
        document.getElementById('pCountry').focus();
        passed = false;
        return false;
    }

    if ((document.forms[pFormName].elements['login'].value.length < 1) && (passed)) {
        alert("Please enter your email address to continue.");
        document.getElementById('login').focus();
        passed = false;
        return false;
    }

    if ((document.forms[pFormName].elements['login'].value.length > 0) && (passed) && !(document.forms[pFormName].elements['login'].value.match(re))) {
        alert("Please enter a valid email address to continue.");
        document.getElementById('login').focus();
        passed = false;
        return false;
    }

    if (document.forms[pFormName].elements['password'])
    {
        var pword = document.getElementById('password').value;

        if ((isBlank(pword)) && (passed)) {
            alert("Please enter your password to continue.");
            document.getElementById("zone-error-hidden").innerHTML = "<!--Invalid Password-->";
            document.getElementById('password').focus();
            passed = false;
            return false;
        }
        else if ((pword.length < 7) || (!passwordCheck(pword)))
        {
            alert("Select a 7-10 character password that begins with a letter, and contains at least one number.(i.e. Chicos1) Your password may not contain your first name, last name, your email ID, the word 'password' or any blank spaces.");
            document.getElementById("zone-error-hidden").innerHTML = "<!--Invalid Password-->";
            document.getElementById('password').focus();
            passed = false;
            return false;
        }
    }



    if ((document.forms[pFormName].elements['password'].value.toUpperCase() != document.forms[pFormName].elements['confirmPassword'].value.toUpperCase()) && (passed)) {
        alert("Password and Confirm Password does not match..");
        document.getElementById("zone-error-hidden").innerHTML = "<!--Invalid Password-->";
        document.getElementById('password').focus();
        passed = false;
        return false;
    }

    if (document.forms[pFormName].elements['passwordHint'])
    {
        var pwordhint = document.getElementById('passwordHint').value;
        if ((pwordhint.length < 1) && (passed)) {
            alert("Please enter a word or phrase for your password hint");
            document.getElementById('passwordHint').focus();
            passed = false;
            return false;
        }
        if ((pwordhint == document.getElementById('password').value) && passed){
            alert("Password Hint cannot be the same as your password. Please choose a different hint");
            document.getElementById('passwordHint').focus();
            passed = false;
            return false;
        }
        else if (!passhintCheck(pwordhint))
        {
            alert("Your password hint must be a string between 1 and 50 characters. Please enter a new password hint to continue.");
            document.getElementById('passwordHint').focus();
            passed = false;
            return false;
        }
    }

    if (passed == true) {
        // TODO
        //        _hfs = 1;
        //        _hbSet('sf', 1);
        //        _hbSend();
		$('#hideShow').jqmShow({width:'220',height:'120',modal:'true',overlay:'50'});
        	return true;
    } else {
        return false;
    }
}


// This is a shorter version to check only for password,confirmpassword
function check_for_empty_values() {

    passed = true;


    if (document.forms['registration'].elements['password'])
    {
        var pword = document.getElementById('password').value;

        if ((isBlank(pword)) && (passed)) {
            alert("Please enter your password to continue.");
            document.getElementById('password').focus();
            passed = false;
            return false;
        }
        else if ((pword.length < 7) || (!passwordCheck(pword)))
        {
            alert("Select a 7-10 character password that begins with a letter, and contains at least one number.(i.e. Chicos1) Your password may not contain your first name, last name, your email ID, the word 'password' or any blank spaces.");
            document.getElementById('password').focus();
            passed = false;
            return false;
        }
    }

    //    if ((document.registration.password.value.length > 10) && (passed)) {
    //        alert("Your password can only be up to 10 characters long.  Please re-enter a shorter password to continue.");
    //        document.registration.password.focus();
    //        passed = false;
    //        return false;
    //    }

    if ((document.getElementById('password').value != document.getElementById('confirmPassword').value) && (passed)) {
        alert("Password and Confirm Password does not match..");
        document.getElementById('password').focus();
        passed = false;
        return false;
    }


    if (passed == true) {
        // TODO
        //        _hfs = 1;
        //        _hbSet('sf', 1);
        //        _hbSend();
	
        return true;
    } else {
        return false;
    }
}
function checkPhoneOpt() {
    if (document.getElementById('pPhoneNumber').value.length > 9) {
        document.getElementById('receivePhoneCall').disabled = false;
    } else {
        document.getElementById('receivePhoneCall').disabled = true;
        document.getElementById('receivePhoneCall').checked = false;
    }
}

function isValidEmail(str) {
    // http://regexlib.com/DisplayPatterns.aspx
    re = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/g
    return (re.exec(str) != null);


}

function clearText(thefield) {
    if (thefield.defaultValue == thefield.value)
        thefield.value = ""
}
