﻿//....................................Parse url...............................//

function WriteCookies() {
    var FORM_DATA1 = new Object();
    FORM_DATA1 = createRequestObject();
    var agency = FORM_DATA1['agency'];
    var agentID = FORM_DATA1['agentid'];
    setCookie('Agency', agency, 1);
    setCookie('AgentId', agentID, 1);
}

function getdata() {
    var data1 = getCookie("Agency");
    var data2 = getCookie("AgentId");
    alert('Agency = ' + data1 + ' and Agent ID = ' + data2);
}
function deleteCookies() {
    deleteCookie('Agency', '', '');
    deleteCookie('AgentId', '', '');
}
function createRequestObject()
{
    var FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
    separator = ',';
    // The token used to separate data from multi-select inputs
    query = '' + this.location;
    qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
    query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
    if (query.length < 1) { return false; }  // Perhaps we got some bad data?
    keypairs = new Object();
    numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
    while (query.indexOf('&') > -1) {
        keypairs[numKP] = query.substring(0, query.indexOf('&'));
        query = query.substring((query.indexOf('&')) + 1);
        numKP++;
        // Split the query string at each '&', storing the left-hand side
        // of the split in a new keypairs[] holder, and chopping the query
        // so that it gets the value of the right-hand string.
    }
    keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
    for (i in keypairs) {
        keyName = keypairs[i].substring(0, keypairs[i].indexOf('='));
        keyName=keyName.toLowerCase();
        // Left of '=' is name.
        keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
        // Right of '=' is value.
        while (keyValue.indexOf('+') > -1) {
            keyValue = keyValue.substring(0, keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
            // Replace each '+' in data string with a space.
        }
        keyValue = unescape(keyValue);
        // Unescape non-alphanumerics
        if (FORM_DATA[keyName]) {
            FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
            // Object already exists, it is probably a multi-select input,
            // and we need to generate a separator-delimited string
            // by appending to what we already have stored.
        } else {
            FORM_DATA[keyName] = keyValue;
            // Normal case: name gets value.
        }
    }
    return FORM_DATA;
}

//........................................Cookies Related.............................//

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.toGMTString());
}

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;
            //  alert(document.cookie.substring(c_start,c_end))
            return unescape(document.cookie.substring(c_start, c_end));

        }
    }
    return "";
}
function deleteCookie(name, path, domain)
{
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

