﻿/// <reference path="jquery-1.3.1.js" />

//$(function() {
//    // Do some initial setup for the California popup.
//    $('#CaPopup').dialog({
//        autoOpen: false,
//        modal: true,
//        overlay: { backgroundColor: '#000', opacity: 0.6 },
//        buttons: {
//            'Ok': function() {
//                $(this).dialog('close');
//            },
//            'Cancel': function() {
//                $(this).dialog('close');
//            }
//        }
//    });
//});

// Number-Mask: Allows only numbers to be entered into a text field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
$(document).ready(function() {
    $(".mask-numbers").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});

// Month and Day Mask: Translates single digits into 2, e.g. "3" becomes "03"
$(document).ready(function() {
    var regex = new RegExp('^[0-9]{1}$');
    $(".mask-month-day").each(function() {
        $(this).blur(function() {
            var text = $(this).get(0).value;
            if (regex.test(text)) {
                $(this).get(0).value = '0' + text;
            }
        });
    });
});

// Year Mask: Translates a 2-digit year into a 4-digit year.
// Because this is for a birthdate field, and a user must be
// 18 to register, this mask assumes a prefix of "19".
$(document).ready(function() {
    var regex = new RegExp('^[0-9]{2}$');
    $(".mask-year").each(function() {
        $(this).blur(function() {
            var text = $(this).get(0).value;
            if (regex.test(text)) {
                $(this).get(0).value = '19' + text;
            }
        });
    });
});

// Text Mask: Only allows letters, space, apostrophe, hyphen, period.
// ASCII Values:
//  65-90 = A-Z
//  97-122 = a-z
//  32 = space
//  39 = apostrophe
//  45 = hyphen
//  46 = period
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
$(document).ready(function() {
    $(".mask-text").each(function() {
        $(this).keypress(function(e) {
        if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 32 || e.which == 39 || e.which == 45 || 
            e.which == 46 || (e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});

// Zip-Mask: Allows only numbers and hyphens to be entered into a text field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
//  45 = "-"
$(document).ready(function() {
    $(".mask-zip").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 45 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});

//function DetermineModalPopup(stateID, submitID, okButtonTxt, cancelButtonTxt, pdfFileName, serverName) {
//    var valid = Page_ClientValidate();
//    if (valid == true) {
//        var dropdown = document.getElementById(stateID);
//        var selIndex = dropdown.selectedIndex;
//        var selValue = dropdown.options[selIndex].value;

//        if (selValue == 'CA') {
//            $('#CaPopup').dialog(
//            {
//                autoOpen: false,
//                modal: true,
//                width: 600,
//                overlay: { backgroundColor: '#000', opacity: 0.6 },
//                buttons: {
//                    'Download Consent Form': function() { ShowCaliforniaPDF(serverName, submitID, pdfFileName); },
//                    'Continue Without Downloading': function() {
//                        $(this).dialog('close');
//                        var valid = Page_ClientValidate();
//                        if (valid == true) {
//                            __doPostBack(submitID, '');
//                        }
//                    }
//                }
//            });

//            $('#CaPopup').dialog('open');
//        }
//        else {
//            __doPostBack(submitID, '');
//        }
//    }
//}

//function ShowCaliforniaPDF(serverName, submitID, pdfFileName) {
//    window.open('http://' + serverName + pdfFileName);

//    var valid = Page_ClientValidate();
//    if (valid == true) {
//        __doPostBack(submitID, '');
//    }
//}

function ValidateBirthDateRequired(sender, args) {
    var monthID = sender.getAttribute('BirthMonthID');
    var dayID = sender.getAttribute('BirthDayID');
    var yearID = sender.getAttribute('BirthYearID');
    var dummyID = sender.getAttribute('BirthDummyID');

    $('#' + yearID).blur();

    var month = $('#' + monthID).get(0).value;
    var day = $('#' + dayID).get(0).value;
    var year = $('#' + yearID).get(0).value;

    if (month == null || month == 'undefined' || month == '' || month == 'MM' ||
        day == null || day == 'undefined' || day == '' || day == 'DD' ||
        year == null || year == 'undefined' || year == '' || year == 'YYYY') {
        args.IsValid = false;
        $('#' + monthID).addClass('error');
        $('#' + dayID).addClass('error');
        $('#' + yearID).addClass('error');

        // remove text from the dummy input so the other validators don't fire
        $('#' + dummyID).get(0).value = '';
    }
    else {
        args.IsValid = true;
        $('#' + monthID).removeClass('error');
        $('#' + dayID).removeClass('error');
        $('#' + yearID).removeClass('error');

        // add text to the dummy input so the other validators fire
        $('#' + dummyID).get(0).value = 'text';
    }
}

function ValidateBirthDateRegex(sender, args) {
    var monthID = sender.getAttribute('BirthMonthID');
    var dayID = sender.getAttribute('BirthDayID');
    var yearID = sender.getAttribute('BirthYearID');
    var dummyID = sender.getAttribute('BirthDummyID');

    var monthRegex = new RegExp(sender.getAttribute('BirthMonthRegex'));
    var dayRegex = new RegExp(sender.getAttribute('BirthDayRegex'));
    var yearRegex = new RegExp(sender.getAttribute('BirthYearRegex'));

    var month = $('#' + monthID).get(0).value;
    var day = $('#' + dayID).get(0).value;
    var year = $('#' + yearID).get(0).value;

    if (monthRegex.test(month) && dayRegex.test(day) && yearRegex.test(year)) {
        args.IsValid = true;
        $('#' + monthID).removeClass('error');
        $('#' + dayID).removeClass('error');
        $('#' + yearID).removeClass('error');
    }
    else {
        args.IsValid = false;
        $('#' + monthID).addClass('error');
        $('#' + dayID).addClass('error');
        $('#' + yearID).addClass('error');

        // remove text from the dummy input so the other validators don't fire
        $('#' + dummyID).get(0).value = '';
    }
}

function IsUser18(sender, args) {
    var monthID = sender.getAttribute('BirthMonthID');
    var dayID = sender.getAttribute('BirthDayID');
    var yearID = sender.getAttribute('BirthYearID');

    var birthMonth = parseInt(document.getElementById(monthID).value);
    var birthDay = parseInt(document.getElementById(dayID).value);
    var birthYear = parseInt(document.getElementById(yearID).value);

    if (!isNaN(birthMonth) && !isNaN(birthDay) && !isNaN(birthYear)) {
        var birth = new Date();
        birth.setFullYear(birthYear, birthMonth - 1, birthDay);

        var current = new Date();

        if (current.getFullYear() - birth.getFullYear() == 18) {
            if (current.getMonth() > birth.getMonth()) {
                args.IsValid = true;
                $('#' + monthID).removeClass('error');
                $('#' + dayID).removeClass('error');
                $('#' + yearID).removeClass('error');
            }

            else if (current.getMonth() == birth.getMonth()) {
                if (current.getDate() >= birth.getDate()) {
                    args.IsValid = true;
                    $('#' + monthID).removeClass('error');
                    $('#' + dayID).removeClass('error');
                    $('#' + yearID).removeClass('error');
                }

                else {
                    args.IsValid = false;
                    $('#' + monthID).addClass('error');
                    $('#' + dayID).addClass('error');
                    $('#' + yearID).addClass('error');
                }
            }

            else {
                args.IsValid = false;
                $('#' + monthID).addClass('error');
                $('#' + dayID).addClass('error');
                $('#' + yearID).addClass('error');
            }
        }

        else if (current.getFullYear() - birth.getFullYear() > 18) {
            args.IsValid = true;
            $('#' + monthID).removeClass('error');
            $('#' + dayID).removeClass('error');
            $('#' + yearID).removeClass('error');
        }

        else {
            args.IsValid = false;
            $('#' + monthID).addClass('error');
            $('#' + dayID).addClass('error');
            $('#' + yearID).addClass('error');
        }
    }

    else {
        args.IsValid = false;
        $('#' + monthID).addClass('error');
        $('#' + dayID).addClass('error');
        $('#' + yearID).addClass('error');
    }
}

function ValidatePhoneNumberRequired(sender, args) {
    var areaCodeID = sender.getAttribute('AreaCodeID');
    var prefixID = sender.getAttribute('PrefixID');
    var suffixID = sender.getAttribute('SuffixID');

    var areaCode = $('#' + areaCodeID).get(0).value;
    var prefix = $('#' + prefixID).get(0).value;
    var suffix = $('#' + suffixID).get(0).value;

    if (areaCode == null || areaCode == 'undefined' || areaCode == '' ||
        prefix == null || prefix == 'undefined' || prefix == '' ||
        suffix == null || suffix == 'undefined' || suffix == '') {
        args.IsValid = false;
        $('#' + areaCodeID).addClass('error');
        $('#' + prefixID).addClass('error');
    }
    else {
        args.IsValid = true;
        $('#' + areaCodeID).removeClass('error');
        $('#' + prefixID).removeClass('error');
    }
}

function ValidatePhoneNumberRegex(sender, args) {
    var areaCodeID = sender.getAttribute('AreaCodeID');
    var prefixID = sender.getAttribute('PrefixID');
    var suffixID = sender.getAttribute('SuffixID');

    var areaRegex = new RegExp(sender.getAttribute('AreaRegex'));
    var prefixRegex = new RegExp(sender.getAttribute('PrefixRegex'));
    var suffixRegex = new RegExp(sender.getAttribute('SuffixRegex'));

    var areaCode = $('#' + areaCodeID).get(0).value;
    var prefix = $('#' + prefixID).get(0).value;
    var suffix = $('#' + suffixID).get(0).value;

    if (areaCode == '' && prefix == '' && suffix == '') {
        args.IsValid = true;
    }
    else {

        if (areaRegex.test(areaCode) && prefixRegex.test(prefix) && suffixRegex.test(suffix)) {
            args.IsValid = true;
            $('#' + areaCodeID).removeClass('error');
            $('#' + prefixID).removeClass('error');
            $('#' + suffixID).removeClass('error');
        }
        else {
            args.IsValid = false;
            $('#' + areaCodeID).addClass('error');
            $('#' + prefixID).addClass('error');
            $('#' + suffixID).addClass('error');
        } 
    }
}

function ValidateSecurityAnswer(sender, args) {
    var questionID = sender.getAttribute('QuestionID');
    var answerID = sender.getAttribute('AnswerID');

    var questionText = $('#' + questionID).get(0).value;
    var answerText = $('#' + answerID).get(0).value;

    if (answerText.length > 255 || answerText == questionText) {
        args.IsValid = false;
        $('#' + answerID).addClass('error');
    }
    else {
        args.IsValid = true;
        $('#' + answerID).removeClass('error');
    }
}

function ValidateOtherBrands(sender, args) {
    var OtherBrandsID = sender.getAttribute('OtherBrandsID');


    var list = $("input", "#" + OtherBrandsID);

    for (i = 0; i < list.length; i++) 
    {
        if (list[i].checked) 
        {
            args.IsValid = true;
            return;
        }

        args.IsValid = false;
    }   
    
//    var questionText = $('#' + questionID).get(0).value;
//    var answerText = $('#' + answerID).get(0).value;

//    if (answerText.length > 255 || answerText == questionText) {
//        args.IsValid = false;
//        $('#' + answerID).addClass('error');
//    }
//    else {
//        args.IsValid = true;
//        $('#' + answerID).removeClass('error');
//    }
}

function ShowHideHowManyZoneBars(value) {
    if (value == '0' || value == "Don't Know") {
        $('#zone-bars').hide('slow');
    }
    else {
        $('#zone-bars').show('slow');
    }
}

function ShowHideOtherBrands(ddlBars, ddlZone) {
    var barValue = $('#' + ddlBars).get(0).value;
    var zoneValue = $('#' + ddlZone).get(0).value;

    if (barValue == zoneValue) {
        $('#other-brands').hide('slow');
    }
    else {
        $('#other-brands').show('slow');
    }
}
