
var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate() + 30;
var year = currentTime.getFullYear(); 

function getContentValue() {

    var QstringName = "_dc";
    var txtName = "CouponCode";

    var QstringVal = displayItem(QstringName);
    //if querystring Value is not exists, get the value from cookie
    if (QstringVal == null || QstringVal == "" || QstringVal == false) { 
    
        QstringVal = get_cookie(QstringName);
    }
    else 
    {
        set_cookie(QstringName, QstringVal, year, month, day); //refresh the cookie value
    }

    try {

        if (document.getElementById(txtName) != null) document.getElementById(txtName).value = QstringVal; 
    }
    catch (exp) {
        //alert(exp.message);
    }

}

/*
To be called as below
set_cookie ( "username", "John Smith", 2003, 01, 15, "", "elated.com", "secure" );

*/

function set_cookie(name, value, exp_y, exp_m, exp_d) 
{
    var cookie_string = name + "=" + escape(value);

    if (exp_y) {
        var expires = new Date(exp_y, exp_m, exp_d);
        cookie_string += "; expires=" + expires.toGMTString();    //alert(cookie_string);
    }
    document.cookie = cookie_string;
}

/*
To be called as delete_cookie ( "username" );
*/

function delete_cookie(cookie_name) {
    var cookie_date = new Date();  // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

/*
To be called as below 
var x = get_cookie ( "username" );
*/

function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
    
    if (results)
        return (unescape(results[2]));
    else
        return "false";
}


function PageQuery(q) {
    if (q.length > 1)
        this.q = q.substring(1, q.length);
    else
        this.q = null;

    this.keyValuePairs = new Array();
    if (q) {
        for (var i = 0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }

    this.getKeyValuePairs = function() { return this.keyValuePairs; }

    this.getValue = function(s) {

        for (var j = 0; j < this.keyValuePairs.length; j++) {
            if (this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }

        return '';
    }

    this.getParameters = function() {
        var a = new Array(this.getLength());

        for (var j = 0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }

    this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key) {
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key));
}

function displayItem(key) {
    var qValue = queryString(key);

    if (qValue != null) {
        //alert(key + "  " + queryString(key));
        return qValue; 
        
    }
}

// Removes leading whitespaces
function LTrim(value) {

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim(value) {

    return LTrim(RTrim(value));

}

window.onload = function() { getContentValue(); }