// Functions for Webfor-designed sites - created by Alan Forsyth (webfor.cz)

// Function to pre-select a dropdown (select) box according to the value of a url ('GET') parameter.
// Requires form name, select box name and URL parameter name (e.g. 'h') as function parameters
function preselect(strFormNm,strSelectNm,strParam) {
	
	var arrParam,val;
	
	// Get querystring name/value pairs from document URL
	var qs = String(window.location.search).slice(1);
	var arrPair = qs.split('&');
	
	// Search for required parameter in querystring pairs
	for (var i=0; i<arrPair.length; i++) {
		arrParam = arrPair[i].split('=');
		if (arrParam[0] == strParam && !val) {		// Parameter value found
			val = arrParam[1];
		}
	}
	
	// Check for valid form and selectbox
	if (val) {
		var objSel = eval('document.'+strFormNm+'.elements[\''+strSelectNm+'\']');
		if (objSel) {
			
			// Attempt to select the item having the parameter value specified
			for (var i=0; i<objSel.options.length; i++) {
				if (objSel.options[i].value == val) {
					objSel.selectedIndex = i;
				}
			}
		}
	}	
}

// Function to check a list of form fields for non-empty values; returns true if all are non-empty, or
// false (and displays an alert box) otherwise. Requires first parameter to contain form object containing
// all fields, and one or more following parameters containing the name of the form fields to validate
function validate() {
	
	var nm,filled,ftype,flist;
	var msg = 'Následujici polozky musi byt vyplneny:\n\n';
	var validform = false;	// default output = do not submit form
	
	// Check for a valid form object
	var objForm = arguments[0];	
	if (objForm) {
		
		validform = true;
		flist = '';
		
		// Get form field parameters
		for (var i=1; i<arguments.length; i++) {
			
			nm = arguments[i];
				
			// Check if field exists and contains a value, according to type
			if (objForm.elements[nm]) {
				
				filled = false;
				ftype = objForm.elements[nm].type;
				val = String(objForm.elements[nm].value);
				//alert('field=' + nm + ',validform='+validform+',type='+ftype+',val='+val);
				
				switch (ftype) {
					
					case 'text':
					case 'textarea':
						filled = (val.length) ? true : false;
						break;
						
					case 'checkbox':
						filled = (objForm.elements[nm].checked) ? true : false;
						break;
						
					case 'select-one':
					case 'select-multiple':
						var s = objForm.elements[nm];
						filled = (s.options[s.selectedIndex].value) ? true : false;
						break;
						
					default:
						filled = (val != '' && val != null) ? true : false;
				}
				
				if (!filled) {
					flist = (flist.length) ? flist + ',' + nm : nm;
					validform = false;
				}
			}		
		}
		
		// Check if message should be displayed (and move focus to first incorrect field if necessary)
		if (!validform && flist.length) {
			var t1 = flist.split(',');
			var t2 = t1[0];
			if (objForm.elements[t2]) {
				objForm.elements[t2].focus();
			}
			alert (msg + flist);
		}
	}
	return validform;
}

