/**********************************************
 * CMS form query string parser. Populates form
 * elements with corresponding values in the
 * query string.
 *
 * By Robert Mark, McGill University
 **********************************************/
 
//set up this useful prototype that is in php but not in javascript core
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
//set up a function that will more accurately gauge the element type
function CMStypeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (typeof value.length === 'number' &&
                    !(value.propertyIsEnumerable('length')) &&
                    typeof value.splice === 'function') {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

function CMSquerystring(qs) {
    //qs - optionally pass a CMSquerystring to parse
	this.params = {};
	this.keys = new Array();
	this.get=CMSquerystring_get;

	if (qs == null){
		qs=location.search.substring(1,location.search.length);
	}

	if (qs.length == 0){
		return;
	}

	qs = qs.replace(/\+/g, ' ');
	//parse out name/value pairs separated by &
	var args = qs.split('&'); 

    //split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var pair = args[i].split('=');
		var name = unescape(pair[0]);

		var value = (pair.length==2)
			? unescape(pair[1])
			: name;
        //check if the name already exists, if so, make an array
        if(this.keys.inArray(name)){
            if(CMStypeOf(this.params[name]) == 'array'){
                this.params[name].push(this.params[name]);
            }else{
                this.params[name]= new Array(this.params[name]);
                this.params[name].push(value);
            }
		}else{
            this.params[name] = value;
            this.keys.push(name);
		}
	}
}

function CMSquerystring_get(key, default_) {
	var value=this.params[key];
	return (value!=null) ? value : default_;
}



///now go through the forms
window.onload=function(){
    if(document.forms){
        QSval = new CMSquerystring();
        var i=0;
        var ii=0;
        var iii=0;
        for (i=0;i<document.forms.length;i++){
            //loop through the form elements
            for (ii=0;ii<document.forms[i].elements.length;ii++){
                //get the value of the element from the query string
                qsValue = QSval.get(document.forms[i].elements[ii].name);
                if(qsValue){
                    //load the value into the element depending on type
                    switch(document.forms[i].elements[ii].type){
                        case 'checkbox':
                            //make sure the values are in an array
                            if(CMStypeOf(qsValue) != 'array'){
                                qsValue = new Array(qsValue);
                            }
                            if(qsValue.inArray(document.forms[i].elements[ii].value)){
                                document.forms[i].elements[ii].checked=true;
                            }
                            break;
                        case 'hidden':
                            document.forms[i].elements[ii].value = qsValue;
                            break;
                        case 'radio':
                            if(qsValue == document.forms[i].elements[ii].value){
                                document.forms[i].elements[ii].checked=true;
                            }
                            break;
                        case 'select-one':
                            document.forms[i].elements[ii].value = qsValue;
                            break;
                        case 'select-multiple':
                            //make sure the values are in an array
                            if(CMStypeOf(qsValue) != 'array'){
                                qsValue = new Array(qsValue);
                            }
                            //loop through all of the options to see which were selected
                            for(iii=0;iii<document.forms[i].elements[ii].options.length;iii++){
                                //if the value is in the array then mark it selected
                                if(qsValue.inArray(document.forms[i].elements[ii].options[iii].value)){
                                    document.forms[i].elements[ii].options[iii].selected=true;
                                }
                            }
                            break;
                        case 'text':
                            document.forms[i].elements[ii].value = qsValue;
                            break;
                        case 'textarea':
                            document.forms[i].elements[ii].value = qsValue;
                            break;
                    }

                }
            }
        }
    }
}

