﻿//this should be factored out to pixel
setSEM('cname');
setSEM('csource');
setSEM('cmethod');
setSEM('cterm');
setSEM('ccontent');

function setSEM(name) {
    if (getQueryVariable(name)) {
        createCookie(name, getQueryVariable(name), 30);
    }
}

function campaignString(campaign) {
    var ret = '';
    //prepending paid identifier
    if (readCookie('cname')) {
        ret = readCookie('cname') + ":";
        ret += readCookie('csource') ? readCookie('csource') + ":" : '';
        ret += readCookie('cterm') ? readCookie('cterm') + ":" : '';
        ret += readCookie('ccontent') ? readCookie('ccontent') + ":" : '';
        ret += readCookie('cmethod') ? readCookie('cmethod') + ":" : '';
    }
    return (campaign + ret )
}

function gotoInsurance(zipCtrlID, typeCtrlID, campaign, launchNewWindow, urlID) {
        var zip;
        var zipValue;
        var url;
        var regEx_zipcode = /(^\d{5}$)/;
        var typeControl;
        var insuranceType
        var isInsured = "Y";

        var isInsuredControl;
        if ((typeof (launchNewWindow) == 'undefined')) {
            launchNewWindow = false;
        }
        
        if (typeof (urlID) == 'undefined') {
            urlID = 1
        }
        if (typeof (campaign) == 'undefined') {
            campaign = "InsuranceTree"
            if(typeof (campaignText) != 'undefined')
            {
                campaign = campaignText;
            }
        }
        typeControl = document.getElementById(typeCtrlID);
        zip = document.getElementById(zipCtrlID);
        zipValue = zip?zip.value:'';
        insuranceType = typeControl.value;

        if (!(zipCtrlID) || (regEx_zipcode.test(zipValue))) {
            //prepending paid identifier

            campaign = campaignString(campaign);
            
            switch (insuranceType.toLowerCase()) {
                case "auto":
                    isInsuredControl = document.getElementById("no");
                    if (isInsuredControl != null && isInsuredControl.checked) {
                        isInsured = "N"
                    }
                    /*switch (urlID) {
                        case 1: url = 'http://www.insurance.com/auto.aspx?cp=2311&customersource=InsuranceTreeMain&zip=' + zipValue + '&insured=' + isInsured;
                            break;
                        case 2: 
                            break;
                    }*/
         		url = 'http://quotes.usinsuranceonline.com/autocontact.php?refID=12809&zip=' + zipValue + '&refCampaign=' + campaign;
                    break;
                case "life": url = 'https://mdquote.matrixdirect.com/quote/clientInput?dnis=10200&subMarketerCode2=' + campaign.substring(0, 50) + '&zipCode=' + zipValue;
                    break;
                case "family": url = 'http://offers.insurancetree.com/insuranceformloader.aspx?tID=Health-IFP-Student-ST-QF&type=IFP&sid=' + campaign + '&zipCode=' + zipValue;
                    break;
                case "shortterm": url = 'http://offers.insurancetree.com/insuranceformloader.aspx?tID=Health-IFP-Student-ST-QF&type=ST&sid=' + campaign + '&zipCode=' + zipValue;
                    break;
                case "student": url = 'http://offers.insurancetree.com/insuranceformloader.aspx?tID=Health-IFP-Student-ST-QF&type=STU&sid=' + campaign + '&zipCode=' + zipValue;
                    break;
                case "health": url = 'http://offers.insurancetree.com/insuranceformloader.aspx?tID=Health-IFP-Student-ST-QF&type=IFP&sid=' + campaign + '&zipCode=' + zipValue;
                    break;
                case "home":
                    url = 'https://www.netquote.com/quotes/home-insurance.aspx?nqid=15234&status=' + campaign + '&zipcode=' + zipValue;
                    break;
                default: alert("Please select a valid Insurance type")
                    return false;

            }
            //Matrix Direct - mdquote.matrixdirect.com
            //eHealthInsurance - www.ehealthinsurance.com
            //AllWebLeads - quotes.usinsuranceonline.com
            //NetQuotes - www.netquote.com
           
            launchNewWindow ? window.open(url) : window.open(url, 'quotes');
        }
        else {
            alert("Please enter a valid 5-digit ZIP Code.");
            zip.value = "";
            zip.focus();
        }
       
        return false;
    }
    function closeMe() {
        document.getElementById("FrameCanvas").style.display = "none";
        document.getElementById("FrameContainer").style.display = "none";
    }

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0].toLowerCase() == variable.toLowerCase()) {
                return pair[1];
            }
        }
    }

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }


    function isInt(e) {
        var code;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        if ((code > 47 && code < 58) || code == 8) {
            return true;
        }

        return false;
    }

    function showAddChildren() {
        if (document.getElementById('linkAddChildren')) {
            document.getElementById('add-children3').style.display = "block";
            document.getElementById('add-children4').style.display = "block";
            document.getElementById('add-children5').style.display = "block";
            document.getElementById('linkAddChildren').style.display = "none";
        }
    }
