﻿//**************************************************************************//
//*** The get cookie function:	This function get a cookie value by the  ***//
//*** cookie name														 ***//
//*** The in vars are	c_name = Cookie name to check					 ***//
//*** The out var is	"null" if cookie or value doesn't exist			 ***//
//***					The cookie value								 ***//
//**************************************************************************//
function getCookie(c_name) {
    // First we check to see if there is a cookie stored.
    // Otherwise the length of document.cookie would be zero.
    if (document.cookie.length > 0) {
        //Second we check if the name of the cookie we look for is
        //stored in the "document.cookie" object if it's 
        //not stored the return var will be -1
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            //In the next two line we get the start index and
            //the end of the value stored in the cookie
            c_start += c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            //If the varible exist we return it
            if (c_end == -1) {
                return unescape(document.cookie.substring(c_start, document.cookie.length));
            }
            else {
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
    }
    return null;
}

//**************************************************************************//
//*** The set cookie function:	This function set cookie to the client	 ***//
//*** The in vars are	c_name = Cookie name							 ***//
//***					c_value = The cookie value to set to			 ***//
//***					c_expire = The experiation time in days			 ***//
//*** The out var is	none											 ***//
//**************************************************************************//
function setCookie(c_name, c_value, c_expire) {
    // Three variables are used to set the new cookie. 
    // The name of the cookie, the value to be stored,
    // and finally the number of days until the cookie expires.
    // The first lines in the function convert 
    // the number of days to a valid date.
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (c_expire * 24 * 3600 * 1000));
    // The next line stores the cookie, simply by assigning 
    // the values to the "document.cookie" object.
    // Note the date is converted to Greenwich Mean time using
    // the "toGMTstring()" function.
    document.cookie = c_name + "=" + escape(c_value) + ((c_expire == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + +"; path=/";
}

//**************************************************************************//
//*** The check cookie function: This function check if a cookie with    ***//
//*** a desired value exist if exist return "true" if not set the        ***//
//*** and return "false" the return var are boolean                      ***//
//*** The in vars are	c_name = Cookie name							 ***//
//***					c_value = The expected value					 ***//
//***					c_expire = The experiation time in days			 ***//
//*** The out var is	true if the cookie value is as the expected one  ***//
//***					false if the cookie value is not as				 ***//
//***					the expected one								 ***//
//**************************************************************************// 
function checkCookie(c_name, c_value, c_expire) {
    //Here we check if the value of the cookie is
    //like we expected
    if (c_value == getCookie(c_name)) {
        //Insert here the desired code if the match is ok
        //alert("cookie already set!!");
        return true;
    }
    //Here we set the cookie with the desired value
    setCookie(c_name, c_value, c_expire);
    //alert("cookie set!!");
    return false;
}

