/************
SEE THE doFormInit METHOD FOR INITIALIZING THE FIRST FORM ON THE PAGE USING THE QUERY STRING
This function will also initialize the "param_order" variable, if one exists.

WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
This file depends on the getParam and getParamList functions.
*************/



// 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) {
		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;
}
// initializes the first form on the page with the url's query string
function doFormInit() {
	if (document.forms.length < 1) return;
	doFormInit0(document.forms[0]);
}

// initializes the specified form with the url's query string
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|"; // the following are not currently implemented: "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;
}

















if (window.attachEvent != null) 
	window.attachEvent("onload", doFormInit);
else
	doFormInit();
