/************
This script contains code for getting/setting cookies and 
getting GET parameters off the URL's query string.  There
are functions for initializing a form based on parameters
in the query string.
*************/


/************** COOKIE FUNCTIONS ******************/

// retrieves the value of the specified cookie, or "" if not found
function getCookie(name) {
	var retval = "";
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	
	if (startOfCookie != -1) {
		startOfCookie += searchName.length;
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		retval = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return retval;
}

// sets the value of the specified cookie
function setCookie(name, value, expires, path, domain, secure) {
	var expString = ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
	var pathString = ((path == null) ? "" : ("; path=" + path));
	var domainString = ((domain == null) ? "" : ("; domain="+domain));
	var secureString = ((secure == true) ? "; secure" : "");
	document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString;
}








/************** QUERY STRING FUNCTIONS ******************/


// returns a value from the query string
function getParam(paramName) {
	var str = location.search;
	var startLoc = str.indexOf(paramName);
	if (startLoc == -1) return '';
	var eqLoc = str.indexOf("=", startLoc);
	if (eqLoc == -1 || eqLoc == str.length -1 ) return '';
	
	var endLoc = str.indexOf("&", eqLoc);
	if (endLoc == -1) endLoc = str.length;
	
	return unescape(str.substring(eqLoc+1, endLoc).replace('+', ' '));
	
}
// returns array of values currently on the query string
function getParamListX() {
	var str = location.search+'';
	if (str.length == 0) return new Array();
	
	if (str.charAt(0) == '?') {
		if (str.length < 2) {
			return new Array();
		} else {
			str = str.substring(1);
		}
	} 
	
	var chunks = str.split("&");
	for (var i = 0; i < chunks.length; i++) {
		var eqloc = chunks[i].indexOf("=");
		if (eqloc == -1) continue;
		chunks[i] = chunks[i].substring(0, eqloc);
	}
		
	return chunks;
}











/************** FORM INIT FUNCTIONS ******************/

// initializes the first form on the page with the url's query string
// and also initializes the paramOrder hidden field, if it exists
function doFormInit() {
	if (document.forms.length < 1) return;
	doFormInit0(document.forms[0]);
}

// initializes the specified form with the url's query string
// and also initializes the paramOrder hidden field, if it exists
function doFormInit0(frm) {
	var pl = getParamListX();
	for (var i = 0; i < pl.length; i++) {
		doFieldInit(frm, pl[i], getParam(pl[i]));
	}
	if (frm.elements.length > 0 && frm.elements[0].focus)
		frm.elements[0].focus();
	buildParamOrder(frm);
}

// builds the paramOrder variable
function buildParamOrder(frm) {
	if (frm.elements['paramOrder'] == null) return;
	
    var els = frm.elements;
    var paramOrder = "";
    var specialFields = "|paramOrder|cfg|emailSubject|emailRecipient|csvHeader|csvDelimiter|csvDelimiter2|emailFrom|csvAttach|returnPage|";
    for (var i = 0; i < els.length; i++) {
        var nm = els[i].name;
        if (nm.length < 1) continue;
        if (specialFields.indexOf("|"+nm+"|") != -1) continue;
        if (paramOrder.indexOf(nm+"|") != -1) continue;
        paramOrder += nm;
        paramOrder += "|";
    }
    frm.paramOrder.value = paramOrder;
    return true;
}

// supporting functions follow


// loops through the checkbox or radio element and checks whichever widget's value matches elvalue
function doCheckboxInit(el, elvalue) {
	elvalue = elvalue.toLowerCase();
	if (!el.length) el = new Array(el);
	
	for (var i = 0; i < el.length; i++) {
		if (el[i].type == 'radio') {
			if (el[i].value.toLowerCase() == elvalue) {
				el[i].checked = true;
			}
		} else {
			el[i].checked = (el[i].value.toLowerCase() == elvalue);
		}
	}
}
// loops through the element's options and selects whichever option's value matches elvalue
function doSelectInit(el, elvalue) {
	elvalue = elvalue.toLowerCase();
	el = el.options;
	for (var i = 0; i < el.length; i++) {
		el[i].selected = (el[i].value.toLowerCase() == elvalue);
	}
}
// initializes the specified form element with the specified value
function doFieldInit(frm, elname, elvalue) {
	var el = frm.elements[elname];
	if (el == null) {
		alert(elname+" not found.");
		return;
	}
	var typ = el.type;
	if (typ == null) typ = '';

	if (typ.indexOf("select") != -1) 
		doSelectInit(el, elvalue);
	else if (typ == 'checkbox' || typ == 'radio' || el.length)
		doCheckboxInit(el, elvalue);
	else
		el.value = elvalue;
}




/*
// UNCOMMENT THIS BLOCK TO AUTOMATICALLY INITIALIZE THE FORM FROM THE URL
if (window.attachEvent != null) 
	window.attachEvent("onload", doFormInit);
else
	doFormInit();
*/