// function validates a form, will be run onSubmit of form
// checks on required and format
function validateForm(elForm) {
	invalidClassName = 'error';
	emailRegex = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/);
	phoneRegex1 = new RegExp(/^\+([0-9]){10,14}$/);
	phoneRegex2 = new RegExp(/^0([0-9]){9}$/);
	zipcodeRegex = new RegExp(/^([0-9]{4})( )?([A-Z]){2}$/);
	numberRegex = new RegExp(/^([0-9])*$/);
	allValid = true;
	// check all inputs
	$A($(elForm).getElementsByTagName('input')).each(function(elInput) {
		elInput = $(elInput);
		curValid = true;
		format = elInput.getAttribute('format');
		isRequired = elInput.getAttribute('required');
		if (isRequired == '1') {
			isRequired = true;
		} else {
			isRequired = false;
		}
		// check required
		if (isRequired) {
			switch (elInput.getAttribute('type')) {
				case 'password':
				case 'text':
					// check on required
					if (!elInput.present()) {
						curValid = false;
					}
					break;
				case 'checkbox':
				case 'radio':
					// check on required
					cntChecked = 0;
					$A(document.getElementsByName(elInput.name)).each(function(elInput) {
						if (elInput.checked) {
							cntChecked++;
						}
					});
					if (cntChecked == 0) {
						curValid = false;
					}
					break;
			}
		}
		// check format
		if ((elInput.getAttribute('type') == 'text') && (format !== false)) {
			// trim all values
			elInput.value = trim(elInput.getValue());
			if (elInput.getValue() !== '') {
				switch (elInput.getAttribute('format')) {
					case 'email':
						curValid = emailRegex.test(elInput.getValue());
						break;
					case 'phone':
						elInput.value = elInput.getValue().replace(/ |\-|/g, '');
						if (elInput.getValue().substr(0, 2) == '00') {
							elInput.value = '+' + elInput.getValue().substr(2)
						}
						curValid = phoneRegex1.test(elInput.getValue()) || phoneRegex2.test(elInput.getValue());
						break;
					case 'zipcode':
						elInput.value = elInput.getValue().toUpperCase();
						curValid = zipcodeRegex.test(elInput.getValue());
						elInput.value = elInput.getValue().replace(/ /g, '');
						break;
					case 'number':
						curValid = numberRegex.test(elInput.getValue());
						break;
				}
			}
		}
		// CHECKED
		// add the invalid class if so, else remove it if persent
		elInput.removeClassName(invalidClassName);
		if (!curValid) {
			elInput.addClassName(invalidClassName);
		}
		// check if anything was invalid
		if (!curValid) allValid = false;
	});
	// check all selects
	$A($(elForm).getElementsByTagName('select')).each(function(elInput) {
		elInput = $(elInput);
		curValid = true;
		isRequired = elInput.getAttribute('required');
		if (isRequired == '1') {
			isRequired = true;
		} else {
			isRequired = false;
		}
		// check required
		if (isRequired) {
			if (elInput.getValue() == '') {
				curValid = false;
			}
		}
		// CHECKED
		// add the invalid class if so, else remove it if persent
		elInput.removeClassName(invalidClassName);
		if (!curValid) {
			elInput.addClassName(invalidClassName);
		}
		// check if anything was invalid
		if (!curValid) allValid = false;
	});
	// check all textarea's
	$A($(elForm).getElementsByTagName('textarea')).each(function(elInput) {
		elInput = $(elInput);
		curValid = true;
		isRequired = elInput.getAttribute('required');
		if (isRequired == '1') {
			isRequired = true;
		} else {
			isRequired = false;
		}
		// check required
		if (isRequired) {
			if (trim(elInput.getValue()) == '') {
				curValid = false;
			}
		}
		// CHECKED
		// add the invalid class if so, else remove it if persent
		elInput.removeClassName(invalidClassName);
		if (!curValid) {
			elInput.addClassName(invalidClassName);
		}
		// check if anything was invalid
		if (!curValid) allValid = false;
	});
	return allValid;
}

// counts and trims an input or textarea to a maxlimit and outputs remaining count into a field
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) { // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	}
	countfield.value = maxlimit - field.value.length;
}

// shows one element in a list of divs, hides the others
function showGroupElement(elElement, strGroup) {
	$A(document.getElementsByTagName('div')).each(function(elDiv) {
		elDiv = $(elDiv);
		if (elDiv.getAttribute('group') == strGroup) {
			if (elDiv.id == $(elElement).id) {
				elDiv.show();
			} else {
				elDiv.hide();
			}
		}
	});
}

/////////////////////////////////////////////
//	Image swapping
/////////////////////////////////////////////
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;
	for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;
	for (i=0;i<(a.length-2);i+=3) {
		if ((x=MM_findObj(a[i]))!=null) {
			document.MM_sr[j++]=x;
			if (!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
		}
	}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	if (!d) d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if (!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
}

/////////////////////////////////////////////
//	Extending functions
/////////////////////////////////////////////

// string trimming
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

