// Set up XPlus1 variables as empty
var xp1state = '';
var xp1LT2 = '';
var xp1LT3 = '';
var xp1LT5 = '';
var xp1LT5click = '';
var xp1LT6 = '';
var xp1LT6click = '';
var xp1LT7click = '';
var xp1AO = '';

// Set up default answers in case XPlus1 does not answer in time
var defaultState = '0';
var defaultLT2 = '';
if (ratesAvailable){defaultLT2 = 'Hero2';}else{defaultLT2 = 'Hero4';}
if(xp1LT5 !=''){LT5 = 'LT5' + xp1LT5;}else{LT5 = defaultLT5;}
var defaultLT2disclosure = '';
var defaultLT3 = 'Text1';
var defaultLT5 = 'LT5ProdA';
var defaultLT5click = '';
var defaultLT6 = 'LT6ProdC';
var defaultLT6click = '';
var defaultLT7click = '';
var defaultLTAddOffersShow = '0';

// Set up the variables that we will use to display the page content
var selectedState = '0';
var EchoState = 'your state';
var LT2 = '';
var LT3 = '';
var LT5 = '';
var LT5click = '';
var LT6 = '';
var LT6click = '';
var LT7click = '';
var LTAddOffersShow = '';
var XP1OrNot = '';

// Set up timing items
var FrequencyToCheck = '100'; // check every 1/10th of a second
var checkTimeoutID = ''; // Declaring it here so I can use it in a self-repeating function
var useDefaultsTimeoutID = setTimeout(useDefaults, 2000); // Set Max Time to wait until default values will be used (1000 = 1 second)

function SetUpContent(){
    // Call to X+1 goes here
    checkXPlus1Response(); // Check to See if XPlus1 has set all expected values or not
}


function checkXPlus1Response(){
    // Check all xp1 variables for a non-empty value
    if (xp1state != '' && xp1LT2 != '' && xp1LT3 != '' && xp1LT7click != '' && xp1AO !=''){
    // All items have values - stop the timer that makes us use the defaults
    clearTimeout(useDefaultsTimeoutID);
    useXPlus1Answers();
    }
else{
    // We do not have all of the answers we are looking for... check again in another 
    checkTimeoutID = setTimeout(checkXPlus1Response,FrequencyToCheck);
    }
}

function useDefaults(){
    // Alright - we have waited long enough - kill the self-repeating timer that checks for answers from XPlus1
    //alert('no answer - assigning defaults');
    clearTimeout(checkTimeoutID);
    // Assign defaults to all answers
    LT2 = defaultLT2;
    LT3 = defaultLT3;
    LT5 = defaultLT5;
    LT5click = defaultLT5click;
    LT6 = defaultLT6;
    LT6click = defaultLT6click;
    LT7click = defaultLT7click;
    LTAddOffersShow = defaultLTAddOffersShow;
    XP1OrNot = 'NOANSWER';
    setState();
}

function useXPlus1Answers(){
    //alert('XPlus1 answered');
    if(ratesAvailable){
        LT2 = xp1LT2;}
    else{
        //alert('rates not available and X+1 said to use ' + xp1LT2);
        if (xp1LT2=='Hero2' || xp1LT2=='Hero3'){
        //alert('not allowed to show ' + xp1LT2 + '. using Hero4 instead');
        LT2 = defaultLT2;}
        else{
        LT2 = xp1LT2;}
    }
    LT3 = xp1LT3;
    LT7click = xp1LT7click;
    LTAddOffersShow = xp1AO;
    //alert(xp1AO);
    if(xp1LT5 !=''){LT5 = 'LT5' + xp1LT5;}else{LT5 = defaultLT5;}
    if(xp1LT5click !=''){LT5click = xp1LT5click;}else{LT5click = defaultLT5click;}
    if(xp1LT6 !=''){LT6 = 'LT6' + xp1LT6;}else{LT6 = defaultLT6;}
    if(xp1LT6click !=''){LT6click = xp1LT6click;}else{LT6click = defaultLT6click;}
    setState();

    //If a FLASH module is returned as the value for LT2, we need to check to see that the user has Flash installed
    if (FlashInstalled()){
        //Do nothing, we are OK
    }
    else{
        //User does not have Flash
        if (xp1LT2 == 'Hero1'){
            LT2 = defaultLT2;
        }
    }
}

function setState(){
// Set up "state"
    switch(xp1state.toUpperCase()){
        case "AL":
            selectedState = "2";
            EchoState = "Alabama";
            break;
        case "AK":
            selectedState = "1";
            EchoState = "Alaska";
            break;
        case "AZ":
            selectedState = "4";
            EchoState = "Arizona";
            break;
        case "AR":
            selectedState = "3";
            EchoState = "Arkansas";
            break;
        case "CA":
            selectedState = "5";
            EchoState = "California";
            break;
        case "CO":
            selectedState = "6";
            EchoState = "Colorado";
            break;
        case "CT":
            selectedState = "7";
            EchoState = "Connecticut";
            break;
        case "DE":
            selectedState = "9";
            EchoState = "Delaware";
            break;
        case "DC":
            selectedState = "8";
            EchoState = "District of Columbia";
            break;
        case "FL":
            selectedState = "10";
            EchoState = "Florida";
            break;
        case "GA":
            selectedState = "11";
            EchoState = "Georgia";
            break;
        case "HI":
            selectedState = "12";
            EchoState = "Hawaii";
            break;
        case "ID":
            selectedState = "14";
            EchoState = "Idaho";
            break;
        case "IL":
            selectedState = "15";
            EchoState = "Illinois";
            break;
        case "IN":
            selectedState = "16";
            EchoState = "Indiana";
            break;
        case "IA":
            selectedState = "13";
            EchoState = "Iowa";
            break;
        case "KS":
            selectedState = "17";
            EchoState = "Kansas";
            break;
        case "KY":
            selectedState = "18";
            EchoState = "Kentucky";
            break;
        case "LA":
            selectedState = "19";
            EchoState = "Louisiana";
            break;
        case "ME":
            selectedState = "22";
            EchoState = "Maine";
            break;
        case "MD":
            selectedState = "21";
            EchoState = "Maryland";
            break;
        case "MA":
            selectedState = "20";
            EchoState = "Massachusetts";
            break;
        case "MI":
            selectedState = "23";
            EchoState = "Michigan";
            break;
        case "MN":
            selectedState = "24";
            EchoState = "Minnesota";
            break;
        case "MS":
            selectedState = "26";
            EchoState = "Mississippi";
            break;
        case "MO":
            selectedState = "25";
            EchoState = "Missouri";
            break;
        case "MT":
            selectedState = "27";
            EchoState = "Montana";
            break;
        case "NE":
            selectedState = "30";
            EchoState = "Nebraska";
            break;
        case "NV":
            selectedState = "34";
            EchoState = "Nevada";
            break;
        case "NH":
            selectedState = "31";
            EchoState = "New Hampshire";
            break;
        case "NJ":
            selectedState = "32";
            EchoState = "New Jersey";
            break;
        case "NM":
            selectedState = "33";
            EchoState = "New Mexico";
            break;
        case "NY":
            selectedState = "35";
            EchoState = "New York";
            break;
        case "NC":
            selectedState = "28";
            EchoState = "North Carolina";
            break;
        case "ND":
            selectedState = "29";
            EchoState = "North Dakota";
            break;
        case "OH":
            selectedState = "36";
            EchoState = "Ohio";
            break;
        case "OK":
            selectedState = "37";
            EchoState = "Oklahoma";
            break;
        case "OR":
            selectedState = "38";
            EchoState = "Oregon";
            break;
        case "PA":
            selectedState = "39";
            EchoState = "Pennsylvania";
            break;
        case "RI":
            selectedState = "40";
            EchoState = "Rhode Island";
            break;
        case "SC":
            selectedState = "41";
            EchoState = "South Carolina";
            break;
        case "SD":
            selectedState = "42";
            EchoState = "South Dakota";
            break;
        case "TN":
            selectedState = "43";
            EchoState = "Tennessee";
            break;
        case "TX":
            selectedState = "44";
            EchoState = "Texas";
            break;
        case "UT":
            selectedState = "45";
            EchoState = "Utah";
            break;
        case "VT":
            selectedState = "47";
            EchoState = "Vermont";
            break;
        case "VA":
            selectedState = "46";
            EchoState = "Virginia";
            break;
        case "WA":
            selectedState = "48";
            EchoState = "Washington";
            break;
        case "WV":
            selectedState = "50";
            EchoState = "West Virginia";
            break;
        case "WI":
            selectedState = "49";
            EchoState = "Wisconsin";
            break;
        case "WY":
            selectedState = "51";
            EchoState = "Wyoming";
            break;
        default:
            selectedState = defaultState;
            break;
    }
    //alert('state values set');
}

function PresentContent(){
// Display the appropriate LT content
    //alert('presenting content');
    //content for DIV id=mainImageTest and disclosures in the footer
    if((document.getElementById(LT2)) && (document.getElementById(LT2).style)){
        document.getElementById(LT2).style.display = 'block';
        if (LT2 == 'Hero1'){
            document.getElementById('Hero1Disclosure').style.display = 'block';
        }
        if (LT2 == 'Hero4'){
            document.getElementById('Hero4Disclosure').style.display = 'block';
        }
        if (LT2 == 'Hero5'){
            document.getElementById('Hero5Disclosure').style.display = 'block';
        }
    }
    else{
	    if (document.getElementById(defaultLT2)){
    	    document.getElementById(defaultLT2).style.display = 'block';
        }
	    if (document.getElementById(defaultLT2disclosure)) {
	        document.getElementById(defaultLT2disclosure).style.display = 'block';
	    }
    }

    //content for DIV id=callToActionEcho
    if((document.getElementById(LT3)) && (document.getElementById(LT3).style)){
        document.getElementById(LT3).style.display = 'block';
    }
    else{
	    if (document.getElementById(defaultLT3)){
    	    document.getElementById(defaultLT3).style.display = 'block';
        }
    }

    //preselect the state
    selectDropDownItem(document.form1.PROP_STATE_CONST_MORT, selectedState, 1)
    //preselect the loan_type
    selectDropDownItem(document.form1.LOAN_TYPE, loan_type, 1)

    //content for DIV id=promoWrapper and additional offers
    if (!(LTAddOffersShow == '0')){
        XP1OrNot = 'YES';
        document.getElementById('promoWrapper').style.display = 'block';
        if((document.getElementById(LT5)) && (document.getElementById(LT5).style)){
            document.getElementById(LT5).style.display = 'block';
        }
        else{
	    if (document.getElementById(defaultLT5)){
    	    document.getElementById(defaultLT5).style.display = 'block';
        }
        }

        if((document.getElementById(LT6)) && (document.getElementById(LT6).style)){
            document.getElementById(LT6).style.display = 'block';
        }
        else{
	    if (document.getElementById(defaultLT6)){
    	    document.getElementById(defaultLT6).style.display = 'block';
        }
        }
    }else{
        XP1OrNot = 'NO';
    }
}

function showDiv(id)
{
    var dropdown = document.getElementById("LOAN_TYPE");
    var index = dropdown.selectedIndex;
    var selectedLoanType = dropdown.options[index].value;

    if (id=="yes")
    {
            dropdown.options.length = 0;
            dropdown.options[dropdown.options.length] = new Option('Select Loan Type','0');
            dropdown.options[dropdown.options.length] = new Option('Purchase Mortgage','1');
            dropdown.options[dropdown.options.length] = new Option('Refinance Mortgage','2');
            dropdown.options[dropdown.options.length] = new Option('Home Equity Loan/Line','3');
            dropdown.options[dropdown.options.length] = new Option('Auto & Motorcycle Loan','4');
    }
    else
    {
            dropdown.options.length = 0;
            dropdown.options[dropdown.options.length] = new Option('Select Loan Type','0');
            dropdown.options[dropdown.options.length] = new Option('Purchase Mortgage','1');
            dropdown.options[dropdown.options.length] = new Option('Auto & Motorcycle Loan','4');
            dropdown.options[dropdown.options.length] = new Option('Personal Loan','5');
    }

    var bselectedLoanType = false;
    var numOpts = dropdown.length;
    //alert('numOpts=' + numOpts);
    //alert('selectedLoanType=' + selectedLoanType);
    for (var i = 0; i < numOpts; i++)
    {
    //alert('dropdown.options[i].value=' + dropdown.options[i].value);
        if (bselectedLoanType == false && dropdown.options[i].value==selectedLoanType)
          {
            bselectedLoanType = true;
            dropdown.options[i].selected = "true";
            //alert('here');
          }
          else
          {
            //alert('there');
          }
    }
}

function LT5Redirect(LTURL){
    //alert(xp1LT5click);
	link = xp1LT5click + LTURL + '&APL_XPLUS1=' + XP1OrNot;
	window.location = link;
}

function LT6Redirect(LTURL){
    //alert(xp1LT6click);
	link = xp1LT6click + LTURL + '&APL_XPLUS1=' + XP1OrNot;
	window.location = link;
}

function LT7Redirect(LTURL){
    //alert(xp1LT7click);
	link = xp1LT7click + LTURL + '&APL_XPLUS1=' + XP1OrNot;
	window.location = link;
}

