﻿function openInfoWindow(page)
{
    window.open(page, 'info', 'width=320,height=300,scrollbars=yes');
    return false;
}

function setCheckBox(checkboxID, state)
{
    var checkbox = document.getElementById(checkboxID);

    if (checkbox != undefined)
    {
        checkbox.checked = state;
    }
}

function valueExistsInList(valueToFind, list)
{
    var listArr = list.split(",");
    for (var x = 0; x < listArr.length; x++)
    {
        if (listArr[x] == valueToFind) return true;
    }
    return false;
}

function doSetup()
{
    hideButtonText();
    doNL("494,571,585,586,587,588,589,590,591,592,593,594,595");
    disablePostcode("449");
    disableBusinessContactFirstName("952,926");    
}

function disableBusinessContactFirstName(qids) {
    var qid = $.getQueryString({ id: "id" });

    if (valueExistsInList(qid, qids) == true) {
        setValidatorStatus("businessContactFirstNameValidator", false);
    }
}

function disablePostcode(qids) {

    var qid = $.getQueryString({ id: "id" });

    if (valueExistsInList(qid, qids) == true) {
        setValidatorStatus("postCodeValidator", false);
        setValidatorStatus("postCodeFormatValidator", false);
        setValidatorStatus("businessContactPostCodeValidator", false);
        setValidatorStatus("businessContactPostCodeFormatValidator", false);
    }
}

function doNL(qids)
{
    var qid = $.getQueryString({ id: "id" });

    if (valueExistsInList(qid, qids) == true)
    {
        hideConfirmCheckboxes();
        formatAddress();
        attachEvents();
        prePopulate();
    }
}

function formatAddress()
{
    var qid = $.getQueryString({ id: "id" });
   
    var x = $('#address2TextBox').parents("tr")[0];
    $(x).css({ 'display': 'none' });

    var s = "<p style='padding-left:25px;'><span class='infoText'>Order Contact Information will be used for issues relating to your order and shipping.<a href='#' class='infoLink' onclick='openInfoWindow(\"https://www.aquarium-software.com/clientimages/client_50_images/popups/popupContactInfo.htm\");return false;'>(info)</a></span></p>";
    $('#firstNamePanel').before(s);
}

function hideConfirmCheckboxes()
{
        var confirmTable = $("td:contains('Please check this box to commit your details and continue')").parents("table")[1];
        if (typeof confirmTable != 'undefined')
        {
            $(':checkbox', confirmTable).attr("checked", true);
            $(confirmTable).hide();
        }
    
}

function setParValues(one, two)
{
    $('#' + one).val('1000');
    $('#' + two).val('0.01');
}

function prePopulate()
{
    setParValues('textBox7707', 'textBox7708');
}

function attachEvents()
{
    $("input[name='optionGroup7938']").click(function() { checkRadioState(); });
    checkRadioState();

    var qid = $.getQueryString({ id: "id" });

    $("#textBox7701").blur(function() { checkSecondChoice(qid); });
    checkSecondChoice(qid);
}

function hideButtonText()
{
    var qid = $.getQueryString({ id: "id" });

    var qids = "514,521,522,523,524,525,526,527,528,529,538,539,540,541,542,543,544,545,546,547,675,679,700,787,797,925";
    var qidsBF = ",934,939,940,943,959,960,962,974,976";
    qids = qids + qidsBF;
    if (valueExistsInList(qid, qids) == true)
    {
        $('#Button_Continue').val('');
    }
}

function setValidatorStatus(validatorIDs, status)
{
    var validators = validatorIDs.split(",");
    for (var x = 0; x < validators.length; x++)
    {
        var validator = document.getElementById(validators[x]);
        if (validator)
        {
            ValidatorEnable(validator, status);
        }
    }
}


function checkSecondChoice(qid)
{
    var textbox = 0;
    var validator = 0;
    switch (qid)
    {
        case "494":
            textbox = "textBox7701";
            validator = "required7704";
            break;
    }
    
    
    
    if ($("#" + textbox).get(0) != undefined)
    {
        if ($("#" + textbox).val().length > 0)
        {
            setValidatorStatus(validator, true);
        }
        else
        {
            setValidatorStatus(validator, false);
        }
    }
}

function checkRadioState()
{
    var qid = $.getQueryString({ id: "id" });

    var fieldArray = [];
    
    switch (qid)
    {
        case "494":
            var options = document.getElementsByName("optionGroup7938");
            if (options && options.length > 0)
            {
                fieldArray[0] = { "id": "7709", "type": "textBox" };
                fieldArray[1] = { "id": "7712", "type": "dropDownList" };
                fieldArray[2] = { "id": "7713", "type": "textBox" };
                fieldArray[3] = { "id": "7939", "type": "textBox" };
                fieldArray[4] = { "id": "9087", "type": "textBox" };
                fieldArray[5] = { "id": "9261", "type": "textBox" };
            }
            break;

    }

    if (fieldArray.length > 0)
    {
        
      
        if (options && options[1].checked == true)
        {
            //yes, disable
            $('.greyable').css("color", "#bbb"); // change text
            SetFieldsStatus(fieldArray, false);
        }
        else
        {
            //no, enable
            $('.greyable').css("color", "#000"); // change text
            SetFieldsStatus(fieldArray, true);
        }
    }
}

function SetFieldsStatus(fieldArray, validatorStatus)
{
    var fieldArrayLength = fieldArray.length;
    for (var x = 0; x < fieldArrayLength; x++)
    {
        setValidatorStatus('required' + fieldArray[x].id, validatorStatus);
        $('#' + fieldArray[x].type + fieldArray[x].id).get(0).disabled = !validatorStatus;
    }
}