﻿function Cookie(sCookieName) {
    //onerror=function (sError,sURL,iLine){	return ClassException('Cookie',sError,sURL,iLine); }
    // This function save cookie
    this.setCookie = setCookie;
    this.getCookie = getCookie;
    this.sCookieName = sCookieName;

    function setCookie(sValue, iMinutes, sDomain) {
        if (iMinutes) {
            var date = new Date();
            date.setTime(date.getTime() + (iMinutes * 60 * 1000))
            var expires = "; expires=" + date.toGMTString()
        } else expires = ""
        var sCookieCont = sCookieName + "=" + sValue + expires + "; path=/"
        sCookieCont += sDomain != "" && sDomain ? " ; domain=" + sDomain : " ; domain=" + location.host;
        document.cookie = sCookieCont
    }
    // This function read the cookie that was saved by saveCookie function
    function getCookie() {
        var nameEQ = sCookieName + "="
        var ca = unescape(document.cookie).split(';')
        var sCookie
        for (var i = 0; i < ca.length; i++) {
            sCookie = ca[i];
            while (sCookie.charAt(0) == ' ') sCookie = sCookie.substring(1, sCookie.length)
            if (sCookie.indexOf(nameEQ) == 0) return sCookie.substring(nameEQ.length, sCookie.length)
        }
    }
}

// get query string paramter and return the value
function RequestQueryString(sParamName, sHref) {
    var arrHref;
    var nParamStartPos = 0, nParamEndPos = 0;
    var sParamValue = '';

    if (typeof (sHref) == "undefined")
        var sHref = document.location.href;

    arrHref = sHref.split("?");
    // return empty string if the query string not exsits
    if (arrHref.length <= 1)
        return (sParamValue);
    sParamName += '=';
    nParamStartPos = arrHref[1].indexOf(sParamName);
    // return empty string if the requested parameter is not exsits
    if (nParamStartPos == -1)
        return (sParamValue);
    nParamStartPos += sParamName.toString().length;
    nParamEndPos = arrHref[1].indexOf("&", nParamStartPos);
    if (nParamEndPos > -1 && nParamEndPos > nParamStartPos)
        sParamValue = arrHref[1].slice(nParamStartPos, nParamEndPos);
    else
        sParamValue = arrHref[1].slice(nParamStartPos);

    return (sParamValue);
}

