﻿function ClearTextField(element, initialValue) 
{
    var myElement = document.getElementById(element);

    if (myElement.value == initialValue) 
    {
        myElement.value = "";
    }
}

function ClearLabel(element) 
{
    var myElement = document.getElementById(element);
    myElement.style.display = "none";
}

/// <summary>
/// Finds the element that has specified part of a string name of a certain type
/// e.g. "Arrow"
/// </summary>
function GetElement(element, partialName, type) 
{
    if (element.tagName == type) if (element.id.indexOf(partialName) != -1) return element;
    if (element.childNodes.length < 1) return null;
    var elementArray = element.childNodes;
    var nodeCount = element.childNodes.length;

    for (var i = 0; i < nodeCount; i++) {
        childElement = GetElement(elementArray[i], partialName, type);
        if (childElement != null) return childElement;
    }
    return null;
}

function PopFullScreenWindow(url, name) 
{
    if (Page_ClientValidate()) 
    {
        var params;
        params = 'width=' + screen.width;
        params += ', height=' + screen.height;
        params += ', top=0, left=0'
        params += ', fullscreen=yes';
        params += ', menubar=yes, location=yes, scrollbars=yes,toolbar=yes,resizable=yes';

        newwin = window.open(url, name, params);

        if (newwin != null) 
        {
            if (window.focus) 
            {
                newwin.focus();
            }
        }
    }

    return false;
}

//function checkpopupblocker() 
//{
//    var popupBlockChecker = document.getElementById('hiddenPopupBlockerCheck');

//    if (popupBlockChecker.value=='') 
//    {
//        var sOption = "toolbar=no,location=no,directories=no,menubar=no,";
//        sOption += "scrollbars=no,width=1,height=1,left=0,top=0";
//        var i = window.open("popupblockertest.htm", "", sOption);

//        if (!i)
//            alert("Please disable your Popup Blocker before running a quote.");
//        else {
//            i.close();
//            popupBlockChecker.value='checked';
//        }
//    }
//}

////window.onload = detectPopupBlocker;
//function detectPopupBlocker() 
//{
//    var myTest = window.open("about:blank", "", "directories=no,height=100,width=100,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
//    if (!myTest) 
//    {
//        alert("Please disable your Popup Blocker before running a quote.");
//    } else 
//    {
//        myTest.close();
//    }
//}

function PopUpQuoteResultsWindow() 
{
    var myNewPage = document.getElementById("popQuoteResults");
    if(myNewPage!=null) myNewPage.click();
}

/// <summary>
/// Does an Ajax Call that will return the state that correspondes to the code
/// </summary>

/**
*
* @param {Element} input
*/
var selectedZipCode = "";
function GetStateForZipCode(input) {
//    if (input.value != "") {
//        page.GetStateForZipCode(input.value, function(args) {
//            GetStateForZipCodeCallBack(args, input);
//        });
    //    }
    if (input != "") {
        Insurint.ConsumerQuoting.InsurintQuoteInput.GetStateForZipCode(input, function(args) {
            GetStateForZipCodeCallBack(args, input);
        });
    }
    else 
    {
        var StateDropDown = document.getElementById("ctl00_ContentPlaceHolder1_CreateControlsTemplate1_State");
        var countyDropDown = document.getElementById("ctl00_ContentPlaceHolder1_CreateControlsTemplate1_county");    
        var gHiddenControl = document.getElementById("ctl00_HiddenControl");
        StateDropDown.disabled = false;
        StateDropDown.value = "--";
        gHiddenControl.value = "HIDDEN";
        if(countyDropDown != null)
        {
            countyDropDown.disabled = false;
            RemoveAllOptions(countyDropDown);
            AddOption(countyDropDown, "--", "--");                
            countyDropDown.value = "--";
            gHiddenControl.value = "--";            
        }
    }
}

/// <summary>
/// Call back for the GetStateForPostalCode function
/// </summary>
/**
*
* @param {Object} args
* @param {Element} input
*/
function GetStateForZipCodeCallBack(args, input) {
    if (!ValidateCallBack(args)) return;
    var gHiddenControl = document.getElementById("ctl00_HiddenControl");
    var states = args.value;
    //var table = GetTableElement(input, "TABLE");
    //var StateDropDown = GetElement(table, "StateDropDownList", "SELECT");
    var StateDropDown = document.getElementById("ctl00_ContentPlaceHolder1_CreateControlsTemplate1_State");
    //var countyCell = document.getElementById('CountyCell');
    //var countyDropDown = GetElement(countyCell, "CountyDropDown", "SELECT");
    var countyDropDown = document.getElementById("ctl00_ContentPlaceHolder1_CreateControlsTemplate1_county");    
    countyDropDown.disabled = false;
    var selectedState = 0;
    if (states.length > 0)
    {
        StateDropDown.value = states[0].Id;
        selectedState = states[0].Id;

        if (states[0].CountyList.length > 0) 
        {
            StateDropDown.disabled = true;
            RemoveAllOptions(countyDropDown);

            if (states[0].CountyList.length < 2) 
            {
                if (states[0].CountyList.length == 0 )
                {
                    AddOption(countyDropDown, "--", "--");
                }
                countyDropDown.disabled = true;
            }

            if (!countyDropDown.disabled) {                
                //AddOption(countyDropDown, "--", "--");
                countyDropDown.focus();
            }           

            for (var i = 0; i < states[0].CountyList.length; i++) {
                AddOption(countyDropDown, states[0].CountyList[i].Name, states[0].CountyList[i].Id);
            }
            gHiddenControl.value = states[0].CountyList[0].Id;
        }
    }
    else {
        StateDropDown.disabled = true;
        countyDropDown.disabled = true;
        StateDropDown.value = "--";
        countyDropDown.value = "--";
        gHiddenControl.value = "--";
    }

    //gHiddenControl = document.getElementById("HiddenControl");
    
//    if (gHiddenControl.QueueSelectCounty == true) {
//        var application = gHiddenControl.application;

//        var countyCell = document.getElementById('CountyCell');
//        var countyDropDown = GetElement(countyCell, "CountyDropDown", "SELECT");
//        var optionsLength = countyDropDown.options.length;
//        var selectedCounty = gHiddenControl.QueueSelectCountyValue;

//        for (var optionNumber = 0; optionNumber < optionsLength; ++optionNumber) {
//            if (countyDropDown.options[optionNumber].value == selectedCounty) {
//                countyDropDown.selectedIndex = optionNumber;
//                break;
//            }
//        }
//        gHiddenControl.QueueSelectCounty = false;
//        gHiddenControl.QueueSelectCountyValue = 0;
//    }
}



function AddOption(selectbox, text, value) {
    var option = document.createElement("OPTION");
    option.text = text;
    option.value = value;
    selectbox.options.add(option);
}

function RemoveAllOptions(selectbox) {
    for (var i = selectbox.options.length - 1; i >= 0; i--) {
        selectbox.remove(i);
    }
}

function ValidateCallBack(args, returnFunction) {
    var error;
    if (args == null) {
        error = Messages.AJAX_CALLBACK_NULLARGS.format(args.request.method);
    }
    else if (args.value == null) {
        if (args.error == null) {
            error = Messages.AJAX_CALLBACK_NULLERROR.format(args.request.method);
        }
        else {
            if (args.error.Type.contains('RedirectFailure')) {
                TransferToLoginPage();
            }
            error = Messages.AJAX_CALLBACK_ERROR.format(args.request.method, args.error.Type, args.error.Message);
        }
    }
    else {
        return true;
    }
    var errors = new ErrorCollection();
    errors.AddError(error);
    errors.DisplayAll(returnFunction, 0, true);
    return false;
}


function SetChangedCounty(object) {
    var gHiddenControl = document.getElementById("ctl00_HiddenControl");
    gHiddenControl.value = object.options[object.selectedIndex].value; 
    
}
