
function arm_qstring()
{
    return function(a)
        {
            if (a == "") return {};
            var b = {};
            for (var i = 0; i < a.length; ++i)
            {
                var p=a[i].split('=');
                b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
            }
            return b;
       }(window.location.search.substr(1).split('&'))
}

function arm_writecookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function arm_readcookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	arm_writecookie(name,"",-1);
}

/**
 * Converts the given data structure to a JSON string.
 * Argument: arr - The data structure that must be converted to JSON
 * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
 * 			var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
 * http://www.openjs.com/scripts/data/json_encode.php
 */
function arm_array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(arm_array2json(value)); /* :RECURSION: */
            else parts[key] = arm_array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}

window.onload = function()
{

// get arm_adv cookie
var arm_prev = arm_readcookie('arm_adv');

var arm_vars = new Object();
if(arm_prev != null)
{
	// bring cookie array back to life
	var arm_vars = eval("("+arm_prev+")");
}

// use defaults if not set by cookie
if(typeof(arm_defaults) != "undefined")
{
	for(x in arm_defaults)
	{
		if(arm_vars[x] == null)
		{
			arm_vars[x] = arm_defaults[x];
		}
	}
}

//query string overrides all previous values
var qs = arm_qstring();
for(x in qs)
{
	arm_vars[x] = qs[x];
}

// set hidden form fields
for(x in arm_vars)
{
	document.getElementById(x).value = arm_vars[x];
}

// save all values back to the cookie
arm_writecookie('arm_adv',arm_array2json(arm_vars), 30);

}

