﻿function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function validateMobileDevice() {
    var aUserAgent = navigator.userAgent.toLowerCase();
    var isMobileDevice = "";
    if (aUserAgent.toLowerCase().indexOf("ipod") > -1 || aUserAgent.toLowerCase().indexOf("iphone") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("android") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("blackberry") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("palm") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("nokia") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("lg") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("sonyericsson") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("samsung") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("windows ce") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("windowsmobile") > -1)
    { isMobileDevice = "yes"; }
    else if (aUserAgent.toLowerCase().indexOf("opera mobi") > -1)
    { isMobileDevice = "yes"; }

    if (isMobileDevice.length > 0) {
        window.location = "http://m.calhfa.ca.gov";
    }

}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return ""
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toUTCString());
}

function checkCookie() {
    disableMobile = getCookie('calhfamobile');
    if (disableMobile === "" || disableMobile === null)
    { validateMobileDevice(); }
}


function checkQueryString() {
    disableQueryString = getQuerystring("disable");
    if (disableQueryString === "" || disableQueryString === null) { checkCookie(); }
    else { setCookie('calhfamobile', disableQueryString, 0); }

}


window.onload = checkQueryString;




