﻿// JScript File
var appPath = '/'
var ERROR_DISPLAY = 'ALERT';
var SERVICE_TYPE = 'Insurance';
var INITIAL_SERVICE = '-1'
var PARTNER = '';
var SUB_PARTNER = '';

var fromStateID = '';

function ClearFromZip() {
    fromStateID = '';
}

function Init() {

    if (INITIAL_SERVICE != '-1') {
        $('#ddlService').val(INITIAL_SERVICE);
    }

    $.ajax({
        type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: appPath + "WebServices/AJAXHelper.asmx/GetStateIDOfZipCode"
            , data: "{ zip : '10004' }"
            , dataType: "json"
    });
}

function GetStateIDFromZip(el) {    
    var s;
    $.ajax({
        async: false
            , type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: appPath + "WebServices/AJAXHelper.asmx/GetStateIDOfZipCode"
            , data: "{ zip : '" + $('#' + el).val() + "' }"
            , dataType: "text"
            , dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') { msg = JSON.parse(data); } else { msg = eval('(' + data + ')'); } if (msg.hasOwnProperty('d')) { return msg.d; } else { return msg; } }
            , success: function(msg) {
                s = msg;
            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
    });

    if (s != null) {
        return s;
    }

    return '';
}

function ValidateFields(el, sp, val, err) {

    if ($('#' + el).val() == val) {
        $('#' + sp).removeClass('validfields').addClass('invalidfields');
        return err;
    }

    $('#' + sp).removeClass('invalidfields').addClass('validfields');
    return '';
}

function ValidateZipCode(el, sp, req, inv) {

    if ($('#' + el).val() == '') {
        $('#' + sp).removeClass('validfields').addClass('invalidfields');
        return req;
    }
    else {
        fromStateID = GetStateIDFromZip(el);        
        if (fromStateID == '' || !($('#' + el).val().match(/^\d\d\d\d\d$/))) {
            $('#' + sp).removeClass('validfields').addClass('invalidfields');
            return inv;
        }
    }

    $('#' + sp).removeClass('invalidfields').addClass('validfields');
    return '';
}

function Validate() {
    var Errors = '';

    switch (SERVICE_TYPE) {
        case 'Insurance':
            Errors += ValidateFields('ddlService', 'spnInsuranceType', '-1', 'Type - Required\n\r');
            Errors += ValidateZipCode('txtZipCode', 'spnInsuranceZipCode', 'Zip Code - Required\n\r', 'Zip Code - Invalid\n\r');

            break;
    }

    if (Errors != '') {
        if (ERROR_DISPLAY == 'ALERT') {
            alert(Errors);
        }
        else {
            $('#spnErrors').show();
        }
        return false;
    }
    else {
        $('#spnErrors').hide();
        return true;
    }
}

function SubmitForm() {
    var destination = appPath + 'QuoteForm.aspx';
    var queryString = '';

    if (Validate()) {
        switch (SERVICE_TYPE) {
            case 'Insurance':
                queryString += "?Type=Insurance&PageNumber=1";
                queryString += "&ZipCode=" + $('#txtZipCode').val();
                queryString += "&ServiceId=" + $('#ddlService').val();

                break;
        }
        if (destination != '' && queryString != '') {
            queryString += PARTNER != '' ? "&Partner=" + PARTNER : '';
            queryString += SUB_PARTNER != '' ? "&Sub_Partner=" + SUB_PARTNER : '';
            //window.location.href = destination + queryString;
        }

        //if ($('#ddlService').val() == '2222') {
            window.location.href = destination + queryString;
        //}

       /* $.ajax({
            type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: appPath + "WebServices/AJAXHelper.asmx/HasMatchesByZip"
            , data: "{ serviceId : " + $('#ddlService').val() + ", countryId: '1', levelInd : '0505', levelVal : '" + $('#txtZipCode').val() + "', incDesc : false, token : 'VanLinesWS' }"
            , dataType: "text"
            , dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') { msg = JSON.parse(data); } else { msg = eval('(' + data + ')'); } if (msg.hasOwnProperty('d')) { return msg.d; } else { return msg; } }
            , success: function(msg) {
		
                if (msg) {
                    window.location.href = destination + queryString;
                }
                else {
                    if ($('#ddlService').val() == '2221') { //auto                       
                        window.location.href = appPath + 'bwrefer.aspx?prodid=300&zip=' + $('#txtZipCode').val();
                    }
                    else if ($('#ddlService').val() == '2223') { //life
                        window.location.href = appPath + 'bwrefer.aspx?prodid=260&zip=' + $('#txtZipCode').val();
                    }
                    else if ($('#ddlService').val() == '2220') { //home
                        window.location.href = appPath + 'bwrefer.aspx?prodid=400&zip=' + $('#txtZipCode').val();
                    }
                    else {
                        window.location.href = destination + queryString;
                    }
                }

            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
        });*/
        
    }
}

function CreateForm() {
    var html = '';
    html += "<div class='form_wrap'>";
    html += "	<h2>Get Insurance Quotes For All Your Needs</h2>";
    html += "	<div class='fields_'>";
    html += "		<div class='field_holder'>";
    html += "			<div class='spcs_s'><!-- --></div>";
    html += "			<div class='field'><span id='spnInsuranceType'>Insurance Type:</span></div>";
    html += "			<div class='value'>";
    html += "				<select id='ddlService'>";
    html += "					<option value='-1'>Select Type</option>";
    html += "					<option value='2221'>Auto Insurance</option>";
    html += "					<option value='2220'>Homeowners Insurance</option>";
    html += "					<option value='2222'>Renters Insurance</option>";
    html += "					<option value='2223'>Life Insurance</option>";
    html += "				</select>";
    html += "			</div>";
    html += "			<div class='spcs_s'><!-- --></div>";
    html += "		</div>";
    html += "		<div class='field_holder'>";
    html += "			<div class='field'><span id='spnInsuranceZipCode'>Zip Code:</span></div>";
    html += "			<div class='value'>";
    html += "				<input type='text' maxlength='5' id='txtZipCode' class='zip onkeypress='return disableKeyPress(event)' onchange='ClearFromZip();' />";
    html += "				<span id='spnZipFinder' onclick='ShowZipFinder(this)'>Zip Finder</span> ";
    html += "			</div>";
    html += "			<div class='spcs_s'><!-- --></div>";
    html += "		</div>";
    html += "		<div class='btn_'>";
    html += "           <img src='http://www.usinsurance.com/Images/start4.jpg' onclick='SubmitForm()'  \/>";
    html += "		</div>";
    html += "	</div>";
    html += "</div>";

    document.write(html);
    setTimeout("Init();", 500);
}


document.write('<div id="DhtmlDiv" class="popx"><p><span onclick="HideDhtml()">Close</span></p><iframe src="' + appPath + 'MiscPages/ZipCodeFinder.aspx" id="iFrameZipFinder" frameborder="0" scrolling="no"></iframe></div>');
function ShowZipFinder(obj) {
    findPosition(obj, "DhtmlDiv");
}

function findPosition(oElement, dhtmlDiv) {
    oDiv = document.getElementById(dhtmlDiv);
    if (typeof (oElement.offsetParent) != 'undefined') {
        for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        oDiv.style.top = posY; // - document.getElementById('divSmartLinking').scrollTop;
        if (dhtmlDiv == 'DhtmlDiv')
            oDiv.style.top = posY - 70 + 'px'; // - document.getElementById('divTree').scrollTop;

        oDiv.style.left = posX + 105 + 'px';
    }
    else {
        oDiv.style.top = oElement.y + 'px';
        oDiv.style.left = oElement.x + 'px';
    }
    oDiv.style.display = 'block';
    oDiv.style.visibility = 'visible';
}

function HideDhtml() {
    $('#DhtmlDiv').hide();
}

function SetZipFromZipFinder(zipCode) {
    $('#txtZipCode').val(zipCode);
    setTimeout("HideDhtml();", 100);
}

function disableKeyPress(e) {
    if (!e) e = window.event;
    if (e.keyCode == 13) {
        return false;
    }
    else {
        return true;
    }
}
