/* Form submitting library
 * This Javascript package allows:
 * 	- fillig html form elements according to data transmidded in URL
 * 	- displaying of error messages given in URL parameter
 * 
 * so it is possible to submit GET form and gather parameters in plain HTML!
 * the core of package is GET handling routine and a form filing function 
 *
 * @author Tim Babych
 * @version 1.0
 */
 

//=============================================================
//		FORM UPDATING
//=============================================================


// here we will store GET params
var _GET = {}

/**
 * here we grab GET parameters and store em in assoc array _GET
 * sub-arrays akaare supported
 * ?param=value&compex_param[key1]=val1&complex_param[key2]=val2
 * will be translated to assoc array
 * _GET = {
 *	param : 'value',
 * 	complex_param : {
 *		key1 : val1,
 *		key2 : val2
 *		}
 *	}
 */
var query=this.location.search.substring(1);
if (query.length > 0){
	var params=query.split("&")
	for (var i=0 ; i<params.length ; i++){
		var pos = params[i].indexOf("=")
		var name = params[i].substring(0, pos)
		var value = unescape(params[i].substring(pos + 1))
		if ((b_pos = name.indexOf('[')) == -1) {
			_GET[name] = value
			continue
		} else {
			arr_name = name.substring(0, b_pos)
			arr_key = name.substring(b_pos + 1, name.length - 1)
			if (!_GET[arr_name])
				_GET[arr_name] = new Object
			_GET[arr_name][arr_key] = value
		}
	}
}

/**
 * This function loops through all inputs on page
 * and assigns them values given in _GET
 * add it to the <body's onLoad="ff_updateFormElements();"
 * you can specify the only form in GET[_form] if you want
 */
function ff_updateFormElements() {
	for(i=0; i < document.forms.length; i++) {
	
		// skip this form if it is not the one specified in GET[_form]
		if (_GET['_form'] && _GET['_form'] != document.forms[i].name)
			continue
			
		for(j=0; j < document.forms[i].length; j++) {
			elem = document.forms[i][j]
			if (get_param = _GET[elem.name])
				switch(elem.nodeName) {
					case 'TEXTAREA':
						elem.value = get_param
						break
					case 'INPUT':
						switch(elem.type) {
							case 'checkbox':
								elem.checked = true
								break
							case 'radio':
								if (elem.value == get_param)
									elem.checked = true
								break
							default:
								elem.value = get_param
						}
						break
					case 'SELECT':
						for(k=0; k< elem.options.length; k++)
							if (elem.options[k].value == get_param)
								elem.options[k].selected = true
				}
		}
	}
	
}

//==============================================================
//		ERROR DISPLAYING
//==============================================================

 /**
  * diplays an error message in GET parameter _errors[err_name]
  * usage <script>ff_printError("testField")</script>
  * if this error is not available in GET - does nothing
  * if "message" specified  will print a custom error message
  * @param name String The name of the error field.
  * @param message String A message to display if an error is set
  * @return String
  */
function ff_printError(err_name, message){
	if (_GET['_errors'] && _GET['_errors'][err_name])
		if (message)
			document.write(message)
		else
			document.write(_GET['_errors'][err_name])

}


