﻿//COMMON: START

//Auto tab functionality: Start
var isNN = (navigator.appName.indexOf("Netscape") != -1);
function autoTab(input, len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0, 8, 9] : [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
    if (input.value.length >= len && !containsElement(filter, keyCode)) {
        input.value = input.value.slice(0, len);
        try {
            input.form[(getIndex(input) + 1) % input.form.length].focus();      
        } catch (e) {
            //something here       
         }
    }

    function containsElement(arr, ele) {
        var found = false, index = 0;
        while (!found && index < arr.length)
            if (arr[index] == ele) found = true;
        else index++; return found;
    }
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input) index = i; else i++; return index;
    }
    return true;
}
//Auto tab functionality: End

//Open popup: Start

function OpenPopup(URL, windowName, width, height, location, status, resizable, scrollbars, toolbar, menubar) {
    var left = (screen.width / 2) - (width / 2);
    var top = (screen.height / 2) - (height / 2);
    window.open(URL, windowName, 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',location=' + location + ', status=' + status + ',resizable=' + resizable + ',scrollbars=' + scrollbars + ',toolbar=' + toolbar + ',menubar=' + menubar + '');
    return false;
}

//Open popup: End

//Check for values in controls underneath particular element
function controlsHasValue(parentControlId) {
    var controls = $('#' + parentControlId + ' input:text');
    for (var count = 0; count < controls.length; count++) {
        if ($.trim($('#' + controls[count].id).val()) != '') {
            return true;
        }
    }
    return false;
}
//COMMON: END
//QUOTE: START
//Show/Hide the controls for trip selection
function showHideQuoteControls(value, vsGetQuoteId, revTripCostId, rfvTripCostId, liTravelDatesId, divTripCostId, txtTripCostId) {
    try {
        //1 is for multi trip
        if (value == "1") {
            if (hasAgeRated()) {
                $("#" + liTravelDatesId + ",li.travelDates, #liTravelers, #" + divTripCostId).addClass("hide");
            } else {
            $("#" + liTravelDatesId + ",li.travelDates, #ageList, #" + divTripCostId).addClass("hide");
            }           
            ValidatorEnable(document.getElementById(revTripCostId), false);
            ValidatorEnable(document.getElementById(rfvTripCostId), false);
            var txtTripCost = document.getElementById(txtTripCostId);
            if (txtTripCost != null && $.trim(txtTripCost.value) == '')
                txtTripCost.value = "0";

            if (document.getElementById('liReturnDate') != null) {
                document.getElementById('liReturnDate').className = "hide";
            }             
        }
        else {
            if (hasAgeRated()) {
                $("#" + liTravelDatesId + ",li.travelDates, #liTravelers, #" + divTripCostId).removeClass("hide");
            } else {
            $("#" + liTravelDatesId + ",li.travelDates, #ageList, #" + divTripCostId).removeClass("hide");
            }            
            ValidatorEnable(document.getElementById(revTripCostId), true);
            ValidatorEnable(document.getElementById(rfvTripCostId), true);
            if (document.getElementById('liReturnDate') != null) {
                document.getElementById('liReturnDate').className = "";
            }
        }

        if (value != null && value != '') {
            $("#" + vsGetQuoteId).html('');
        }
    }
    catch (e) {
        alert(e);
    }
}

//Check if the age of travellers is entered or not
function checkRequiredAgeOfTravelers(source, args) {

    if (!isMultiTrip()) {
        args.IsValid = controlsHasValue('ageList');
    } else args.IsValid = true;
}

//Validate Age of travelers for numeric 
//and the entered age should be between 0 and 120
function validateAgeOfTravelers(source, args) {
    if (!isMultiTrip()) {
        args.IsValid = validateAge('ageList');
    }
    else args.IsValid = true;
}

//Check for valid age range
function validateAge(parentControlId) {
    var controls = $('#' + parentControlId + ' input:text');
    for (var count = 0; count < controls.length; count++) {
        if ($.trim($('#' + controls[count].id).val()) != '') {
            if (isNaN(parseInt($.trim($('#' + controls[count].id).val())))) {
                return false;
            } else if (parseInt($.trim($('#' + controls[count].id).val())) > 120
            || parseInt($.trim($('#' + controls[count].id).val())) < 1) {
                return false;
            }
        }
    }
    return true;
}

//Validate Trip cost range
//Trip cost should be between 0 and 1000000
function validateTripCost(source, args, strTripCost) {
    if (strTripCost != '' && !isNaN(parseFloat(strTripCost))) {
        var tripCost = parseFloat($.trim(strTripCost));
        if (tripCost < 0 || tripCost > 1000000)
            args.IsValid = false;
    }
}

//Validate Departure date
function validateDepartureDate(source, args) {
    if (!isMultiTrip()) {
        args.IsValid = controlsHasValue('spnDepartureDate');
        if (!args.IsValid) {
            source.errormessage = EticketLoaded() ? "Event Begin Date is required" : "Departure Date is required";
        }
    }
    else args.IsValid = true;
}

//Validate Return date
function validateReturnDate(source, args) {
    if (!isMultiTrip()) {
        args.IsValid = controlsHasValue('spnReturnDate');
        if (!args.IsValid) {
            source.errormessage = EticketLoaded() ? "Event End Date is required" : "Return Date is required";
        }
    }
    else args.IsValid = true;
}


//QUOTE: END


//PURCHASE PAGE: START

//This function show/hides the Final Trip Payment Date based on Y/N selection
function showHideFinalTripPaymentSection(clientId, value) {
    if (value == "Y") {
        $(clientId).show();
    }
    else {
        $(clientId).hide();
    }
}

//Compares email address
function compareEmail(source, args, emailClientId, confirmEmailClientId) {
    if ($.trim($(emailClientId).val()) == $.trim($(confirmEmailClientId).val())) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}

//Enable/Disable validation controls
function validateBillingAddressSection(sameAddClientId, homeAddClientId, cityClientId, stateClientId, zipClientId) {

    if ($(sameAddClientId).is(':checked')) {
        $(".secondAddress").removeClass("hide");
        ValidatorEnable($(homeAddClientId)[0], true);
        ValidatorEnable($(cityClientId)[0], true);
        ValidatorEnable($(stateClientId)[0], true);
        ValidatorEnable($(zipClientId)[0], true);
    }
    else {
        $(".secondAddress").addClass("hide");
        ValidatorEnable($(homeAddClientId)[0], false);
        ValidatorEnable($(cityClientId)[0], false);
        ValidatorEnable($(stateClientId)[0], false);
        ValidatorEnable($(zipClientId)[0], false);
    }
}

//Validates if the purchase agreement is checked
function validatePurchaseAgreementSelection(source, args, clientId) {
    if ($(clientId).is(':checked')) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}

//Validates Phone Number for length == 10 and IsNumber
function validatePhoneNumber(source, args, areaCodeClientId, prefixClientId, numberClientId) {
    var areaCode = $.trim($(areaCodeClientId).val());
    var prefix = $.trim($(prefixClientId).val());
    var number = $.trim($(numberClientId).val());
    if (areaCode != '' || prefix != '' || number != '') {
        if ((areaCode + prefix + number).length < 10 ||
            isNaN(parseInt(areaCode)) || isNaN(parseInt(prefix)) || isNaN(parseInt(number))) {
            args.IsValid = false;
            source.errormessage = "Phone number is invalid"
        }
    } else {
        source.errormessage = "Phone number is required"
        args.IsValid = false;
    }
}

//Validate Return date
function validatePurchaseDate(source, args) {
    args.IsValid = controlsHasValue('spnDateOfPurchase');
    if (!args.IsValid) {
        source.errormessage = EticketLoaded() ? "Event Ticket Purchase Date is required" : "Date You Purchased Your Trip is required";
    }
}

//PURCHASE PAGE: END


//FILE CLAIM:START

function validateIncidentDate(source, args) {
    args.IsValid = controlsHasValue('liIncidentDate');
}

//FILE CLAIM:END

//MODIFY POLICY:START

//Validate Departure date
function validate_DepartureDate(source, args) {
    args.IsValid = controlsHasValue('liDepartureDate');
    if (!args.IsValid) {
        source.errormessage = EticketLoaded() ? "Event Begin Date is required" : "Departure Date is required";
    }
}

//Validate Return date
function validate_ReturnDate(source, args) {
    args.IsValid = controlsHasValue('liReturnDate');
    if (!args.IsValid) {
        source.errormessage = EticketLoaded() ? "Event End Date is required" : "Return Date is required";
    }
}

//MODIFY POLICY:END
