function CloseStateSelector(toClose) {
    document.getElementById(toClose).style.display = 'none';
    document.getElementById('fade').style.display = 'none';
}
function OpenStateSelector(toOpen, urlToGo, goToCtrl) {

    if (readCookie('AmericanRepublicStateSelector' + urlToGo) != null) {

        //alert(readCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo)); 

        if (urlToGo == 'medicare') {

            //alert(readCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo));          
        
            if (readCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo) != null) {
                document.location = '/StateRedirect.aspx?stateAbbr=' + readCookie('AmericanRepublicStateSelector' + urlToGo) + '&GoTo=' + urlToGo + '&EffectiveDate=' + readCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo);
                return;                
            }
            else {
                document.location = '/StateRedirect.aspx?stateAbbr=' + readCookie('AmericanRepublicStateSelector' + urlToGo) + '&GoTo=' + urlToGo;
                return;                
            }
        }
        
        JustGo(readCookie('AmericanRepublicStateSelector' + urlToGo), urlToGo);
    }
    else {
        document.getElementById(goToCtrl).value = urlToGo;
        document.getElementById(toOpen).style.display = 'block';
        document.getElementById('fade').style.display = 'block';
    }
}

function OpenStateSelectorCustom(toOpen, sectionToGo, goToCtrl, urlToGoTo) {

    if (readCookie('AmericanRepublicStateSelector' + sectionToGo) != null) {

        if (sectionToGo == 'medicare') {

            if (readCookie('AmericanRepublicStateSelectorEffectiveDate' + sectionToGo) != null) {
                //alert('/StateRedirect.aspx?stateAbbr=' + readCookie('AmericanRepublicStateSelector' + sectionToGo) + '&GoTo=' + sectionToGo + '&EffectiveDate=' + readCookie('AmericanRepublicStateSelectorEffectiveDate' + sectionToGo) + '&url=' + urlToGoTo);
                document.location = '/StateRedirect.aspx?stateAbbr=' + readCookie('AmericanRepublicStateSelector' + sectionToGo) + '&GoTo=' + sectionToGo + '&EffectiveDate=' + readCookie('AmericanRepublicStateSelectorEffectiveDate' + sectionToGo) + '&url=' + urlToGoTo;
                return;
            }
            else {
                document.location = '/StateRedirect.aspx?stateAbbr=' + readCookie('AmericanRepublicStateSelector' + sectionToGo) + '&GoTo=' + sectionToGo + '&url=' + urlToGoTo;
                return;                
            }
        }

        JustGo(readCookie('AmericanRepublicStateSelector' + sectionToGo), sectionToGo, urlToGoTo);
    }
    else {
        document.getElementById(goToCtrl).value = sectionToGo;
        document.getElementById('hfUrlToGo').value = urlToGoTo;
        document.getElementById(toOpen).style.display = 'block';
        document.getElementById('fade').style.display = 'block';
    }
}

function OpenStateSelectorGetQuote(toOpen, cookieToCheck, urlToGo, goToCtrl) {
    if (readCookie('AmericanRepublicStateSelector' + cookieToCheck) != null) {
       JustGo(readCookie('AmericanRepublicStateSelector' + cookieToCheck), urlToGo);
    }
    else {
        document.getElementById(goToCtrl).value = urlToGo;
        document.getElementById(toOpen).style.display = 'block';
        document.getElementById('fade').style.display = 'block';
    }
}


function OpenStateSelectorChooseNew(toOpen, urlToGo, goToCtrl) {
    document.getElementById(goToCtrl).value = urlToGo;
    document.getElementById(toOpen).style.display = 'block';
    document.getElementById('fade').style.display = 'block';
}


function JustGo(stateAbbr, goToCtrl, urlToGoTo) {
    var stateAbbr;
    document.location = '/StateRedirect.aspx?stateAbbr=' + stateAbbr + '&GoTo=' + goToCtrl + '&url=' + urlToGoTo;
}

function SetCookieAndGo(stateddl, goToCtrl, effectiveDate) {
    var stateAbbr;
    var urlToGo;
    var url;

    urlToGo = document.getElementById(goToCtrl).value;
    url = document.getElementById('hfUrlToGo').value;
    

    s = document.getElementById(stateddl);
    stateAbbr = s.value;

    if (stateAbbr == 'state') {
        alert('Please Select a State to Continue.');
    }
    else {
        //Create the cookie
        createCookie('AmericanRepublicStateSelector' + urlToGo, stateAbbr, 1000);
        if (effectiveDate != '') {
            createCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo, effectiveDate, 1000);
            //alert(readCookie('AmericanRepublicStateSelectorEffectiveDate' + urlToGo));

            if (document.getElementById('hfUrlToGo').value = '') {
                document.location = '/StateRedirect.aspx?stateAbbr=' + stateAbbr + '&GoTo=' + urlToGo + '&EffectiveDate=' + effectiveDate;
                return;
            }
            else {
                document.location = '/StateRedirect.aspx?stateAbbr=' + stateAbbr + '&GoTo=' + urlToGo + '&EffectiveDate=' + effectiveDate + '&url=' + url;
                return;
            }
        }
        else {

            if (document.getElementById('hfUrlToGo').value = '') {
                document.location = '/StateRedirect.aspx?stateAbbr=' + stateAbbr + '&GoTo=' + urlToGo;
                return;
            }
            else {
                document.location = '/StateRedirect.aspx?stateAbbr=' + stateAbbr + '&GoTo=' + urlToGo + '&url=' + url;
                return;
            }
        }
    }
}

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);
}

