// This is advanced validation library for cool javascript validating of HTML and XHTML forms.
// (c)2005 Remo, All rights reserved, 
// Price for 1 license for 1 computer is is $500.
// Please, contact me on [r e m o @ t o u r i s t.s k] for payment information.

var	focusField = '';//stores the first field marked.. the one i should focus on


// this function marks field, which didn't passed the validation procedure,
// and it's label (default red border and red text color, but change it to
// whatever you want). It fills hint-div by appropriate hint-text too.
	function markField(id,hint) {
		field = document.getElementById(id);
		field.style.border = '1px solid red';
		document.getElementById(id+'lb').style.color='red';				
		if (document.getElementById(id+'ht').innerHTML == '') document.getElementById(id+'ht').innerHTML=hint;				
		document.getElementById(id+'ht').style.display = 'block';
		if (focusField=='') {
	 	focusField = id;
		}
	}

// restore fields to it's original (before error) look. Adjust it to fit your design.
  function restoreField(id) {
		document.getElementById(id).style.border = '1px solid gray';
		document.getElementById(id+'lb').style.color = 'black';
		document.getElementById(id+'ht').style.display = 'none';
		document.getElementById(id+'ht').innerHTML = '';
	}
	
// returns 0 if field is empty, and marks this field as non-valid
	function checkNonEmpty(id) { 
		if (document.getElementById(id).value == '' || document.getElementById(id).value == ' ') {
			markField(id,'Táto položka je povinná. Vyplňte ju prosím.');
			return false;
		}
		return true;
	}

// added for UBYTOVANIE NA SLOVENSKU page, no use for this on other pages i think
	function checkHotel(id) { 
		restoreField(id);
		if (document.getElementById(id).value != '') {
			var typy=['chata','hotel','penzión','privát','motel','kemping','botel','privat','penzion'];
			for (i=0;i<typy.length;i++) {
				if (document.getElementById(id).value.indexOf(typy[i]) >= 0) {
					markField(id,'Názov nesmie obsahovať typ zariadenia - '+typy[i]);
					return false;
				}
			}
		}	
		return true;
	}

// returns 0 if field is non-integer, and marks this field as non-valid	
	function checkInt(id) { 
			if ((parseInt(document.getElementById(id).value) != document.getElementById(id).value) && (document.getElementById(id).value != '')) {
				markField(id,'Táto položka musí obsahovať celé číslo.');
				return false;
			}
			return true;
	}

// returns 0 if field is not float number, and marks this field as non-valid, if it is truncates it to given n
function checkFloat(id,n) { 
			document.getElementById(id).value=document.getElementById(id).value.replace(',','.');
			if ((parseFloat(document.getElementById(id).value) != document.getElementById(id).value) && (document.getElementById(id).value != '')) {
				markField(id,'Táto položka musí obsahovať celé alebo reálne číslo.');
				return false;
			}
			if ((document.getElementById(id).value != '')) document.getElementById(id).value = parseFloat(document.getElementById(id).value).toFixed(n);
			return true;
	}

function checkEmail(id) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	if (email.test(document.getElementById(id).value)==false && document.getElementById(id).value!='') {
				markField(id,'Nesprávne zadaná e-mail adresa.');
				return false;
	}
	return true;
}

function validateForm(VA) {
	focusField = '';
	for (i=0;i<VA.length;i++) {
		restoreField(VA[i][0]);
		if (VA[i][1] != 0) checkNonEmpty(VA[i][0]);
		if (VA[i][2] != 0) checkInt(VA[i][0]);
		if (VA[i][3] != 0) checkFloat(VA[i][0],2);
		if (VA[i][4] != 0) checkEmail(VA[i][0]);
	}
	
	if (focusField == '') return true;
	document.getElementById(focusField).focus();
	return false;
}
