﻿
// AJAX

// jquery stuff for personalDataCollector zipcode validation


function validateZipcode() {
    
    $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").blur(function() {

        //$("#ctl00_phInternalContent_personalDataCollector_cityTextBox").animate({ height: '0px', opacity: '25%' }, "fast");
        //$("#ctl00_phInternalContent_personalDataCollector_stateDropDownList").animate({ height: '0px', opacity: '25%' }, "fast");
        //alert("Zipcode onBlur() fired");
        $.get('../Includes/Services/GetZipcodeInfo.aspx?zipcode=' + $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").val(),
                function(responseText) {
                    // alert("Bing!");
                    var arrayCSZ = responseText.split('~');

                    if (arrayCSZ[0] == 'VALID') {
                        $("#ctl00_phInternalContent_personalDataCollector_cityTextBox").val(arrayCSZ[1]);
                        $("#ctl00_phInternalContent_personalDataCollector_stateDropDownList").val(arrayCSZ[2]);
                        $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").css("color", "black");
                    }
                    else {

                        if ($("#ctl00_phInternalContent_personalDataCollector_zipTextBox").val() != 'INVALID ZIPCODE') {
                            $("#ctl00_phInternalContent_personalDataCollector_cityTextBox").val('');
                            $("#ctl00_phInternalContent_personalDataCollector_stateDropDownList").val('Please select');
                            $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").val('INVALID ZIPCODE');
                            $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").css("color", "red");
                            $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").focus();
                            $("#ctl00_phInternalContent_personalDataCollector_zipTextBox").select();
                        }

                    }
                    //$("#ctl00_phInternalContent_personalDataCollector_cityTextBox").animate({ height: '16px', opacity: '100%' }, "fast");
                    //$("#ctl00_phInternalContent_personalDataCollector_stateDropDownList").animate({ height: '22px', opacity: '100%' }, "fast");
                });
    });
}




function validateConsumerRegistration(formName) {


    var errMsg = "";

    // firstName
    if (formName.ctl00_phInternalContent_personalDataCollector_firstNameTextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_firstNameRequiredValidator').innerHTML = ' Please include your first name.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_firstNameRequiredValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    // lastName
    if (formName.ctl00_phInternalContent_personalDataCollector_lastNameTextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_lastNameRequiredValidator').innerHTML = ' Please include your last name.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_lastNameRequiredValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    // address1
    if (formName.ctl00_phInternalContent_personalDataCollector_address1TextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_address1RequiredFieldValidator').innerHTML = ' Please include your address.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_address1RequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    // city
    if (formName.ctl00_phInternalContent_personalDataCollector_cityTextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_cityRequiredFieldValidator').innerHTML = ' Please include your city.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_cityRequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    //state
    if (formName.ctl00_phInternalContent_personalDataCollector_stateDropDownList.selectedIndex == 0) {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_stateRequiredFieldValidator').innerHTML = ' Please specify your state.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_stateRequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";

    }

    // zip
    if (formName.ctl00_phInternalContent_personalDataCollector_zipTextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_zipRequiredFieldValidator').innerHTML = ' Please include your zip.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_zipRequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    // email exists
    if (formName.ctl00_phInternalContent_personalDataCollector_email1TextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email1RequiredFieldValidator').innerHTML = ' Please include your email.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email1RequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";
    }
    else {
        // email validation
        if (echeck(formName.ctl00_phInternalContent_personalDataCollector_email1TextBox.value) == false) {
            document.getElementById('ctl00_phInternalContent_personalDataCollector_email1RequiredFieldValidator').innerHTML = ' Please enter a valid email address.';
            document.getElementById('ctl00_phInternalContent_personalDataCollector_email1RequiredFieldValidator').style.display = 'inline';
            errMsg = "invalid";
        }
    }

    // email confirmation
    if (formName.ctl00_phInternalContent_personalDataCollector_email2TextBox.value == '') {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email2RequiredValidator').innerHTML = ' Please include your email confirmation.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email2RequiredValidator').style.display = 'inline';
        errMsg = "invalid";
    }

    // email confirmation
    if (formName.ctl00_phInternalContent_personalDataCollector_email1TextBox.value.toUpperCase() != formName.ctl00_phInternalContent_personalDataCollector_email2TextBox.value.toUpperCase()) {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email2RequiredValidator').innerHTML = ' You email does not match your email confirmation.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_email2RequiredValidator').style.display = 'inline';
        errMsg = "invalid";

    }

    // DOB
    if (formName.ctl00_phInternalContent_personalDataCollector_monthDropDown.selectedIndex == 0 || formName.ctl00_phInternalContent_personalDataCollector_dayDropDown.selectedIndex == 0 || formName.ctl00_phInternalContent_personalDataCollector_yearDropDown.selectedIndex == 0) {
        document.getElementById('ctl00_phInternalContent_personalDataCollector_birthdayMonthRequiredFieldValidator').innerHTML = ' Please specify your date of birth.';
        document.getElementById('ctl00_phInternalContent_personalDataCollector_birthdayMonthRequiredFieldValidator').style.display = 'inline';
        errMsg = "invalid";
    }
    else {


        var month = document.getElementById('ctl00_phInternalContent_personalDataCollector_monthDropDown').value;
        var day = document.getElementById('ctl00_phInternalContent_personalDataCollector_dayDropDown').value;
        var year = document.getElementById('ctl00_phInternalContent_personalDataCollector_yearDropDown').value;

        if (month == 'Jan') { month = '01'; }
        else if (month == 'Feb') { month = '02'; }
        else if (month == 'Mar') { month = '03'; }
        else if (month == 'Apr') { month = '04'; }
        else if (month == 'May') { month = '05'; }
        else if (month == 'Jun') { month = '06'; }
        else if (month == 'Jul') { month = '07'; }
        else if (month == 'Aug') { month = '08'; }
        else if (month == 'Sep') { month = '09'; }
        else if (month == 'Oct') { month = '10'; }
        else if (month == 'Nov') { month = '11'; }
        else if (month == 'Dec') { month = '12'; }

        var now = new Date;

        //fix length of digits
        if (day.length == 1) { day = '0' + day; }

        var month13 = (now.getMonth() + 1) + '';
        if (month13.length == 1) { month13 = '0' + month13; }

        var yrsAgo13 = '' + now.getFullYear() - 13 + '' + month13 + '' + now.getDate();

        var bthDay = '' + year + '' + month + '' + day;

        //        alert("month = " + month + "  bthDay = " + bthDay + "  yrsAgo13 = " + yrsAgo13);
        //        return false;

        if (bthDay > yrsAgo13) {
            errMsg = "In accordance with the Children's Online Privacy Protection Act, we cannot collect information from anyone under the age of 13 without their parent's permission.\n";
        }
    }

    // Gender
    if (document.getElementById('ctl00_phInternalContent_personalDataCollector_genderMaleRadioButton').checked == false && document.getElementById('ctl00_phInternalContent_personalDataCollector_genderFemaleRadioButton').checked == false) {
        //document.getElementById('ctl00_phInternalContent_personalDataCollector_firstNameRequiredValidator').innerHTML = ' Please specify your gender.';
        //document.getElementById('ctl00_phInternalContent_personalDataCollector_firstNameRequiredValidator').style.display = 'inline';
        errMsg = "invalid";
    }



    if (errMsg != "") {
        if(errMsg != "invalid"){
            alert(errMsg);
        }
        return false;
    }
    else {
        return true;
    }

}





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
}