/*
* 	FORM VALIDATOR
*	onsubmit="return validate(this)"
*/


var tmpFormOnSubmit = "";
var currentFormObject = null;

function isTime(val) {
	if (!(val)) return false;

	if (!(val.match(/([0-9]{2}):([0-9]{2})/))) {
		return false;
	}

	var timePair = val.split(':');
	var timeHour = timePair[0];
	var timeMins = timePair[1];
	if ( (parseInt(timeHour) > 23) || (parseInt(timeMins) > 59)) {
		return false;
	}
	return true;
}

//window.onload = apply_form_validator;

//apply_form_validator()

function apply_form_validator()
{
	var elements = document.getElementsByTagName("*");
	for (var i=0; i<elements.length; i++) {
		input = elements[i];
		if (input.tagName.toLowerCase() == 'form') {			
			// save the content of onSubmit attribute to variable
			currentFormObject = input;
			tmpFormOnSubmit = input.getAttribute("onsubmit");

			//if (tmpFormOnSubmit=='') {
			//	input.setAttribute('onsubmit','return form_validate(this)');
			//}
			
			addEvent(input,"submit",assign_form_validator);
			
			var formEls = input.getElementsByTagName('*');
			for (var fEl=0; fEl<formEls.length; fEl++) {
				//if (formEls[fEl].required == 'true') {
				addEvent(input,"keydown",assign_charCounterAlert);
				addEvent(input,"keyup",assign_charCounterAlert);
				//}
				/*}
				if (formEls[fEl].tagName.toLowerCase() == 'input' && formEls[fEl].type.toLowerCase() == 'text')
					addEvent(input,"keydown",assign_charCounterAlert);
					addEvent(input,"keyup",assign_charCounterAlert);
				}*/
			}
		}
	}
}


function assign_form_validator(e)
{
	var obj = getEventSrc(e);
	return form_validate(obj);
}

function form_validate(form)
{
	var test = ''
	var elements = form.getElementsByTagName("*");
	var input = null;
	var ok = true;
	// Search for invalid inputs
	for (var i = 0; i < elements.length; i++) {
		input = elements[i];
		minLength = (input.minlength) ? input.minlength : 0;
		//maxNumber = (input.maxnumber) ? input.maxnumber : 0;

		if (input.required == 'true' || input.accessKey == 'true') {
			if (input.tagName.toLowerCase() == 'select') {

					if (input.options.length == 0 || !input[input.selectedIndex].value) {
						ok = false;
					}

			} else if (input.tagName.toLowerCase() == 'div') {

					if (input.innerText.length == 0 ) {
						ok = false;
					}
					if (!ok) {
						var my_LABEL_JS_PLEASECOMPLETEFIELD = LABEL_JS_PLEASECOMPLETEFIELD.replace(/\@\@FIELD\@\@/,"'"+input.title+"'");
						alert(my_LABEL_JS_PLEASECOMPLETEFIELD);
						try {
							input.focus();
						} catch (e) {}
						return false;
					}

			} else {

					if (!input.value || input.value.length < minLength) {
						ok = false;
					}
					if (!ok) {

						var my_LABEL_JS_PLEASECOMPLETEFIELD = LABEL_JS_PLEASECOMPLETEFIELD.replace(/\@\@FIELD\@\@/,"'"+input.title+"'");
						alert(my_LABEL_JS_PLEASECOMPLETEFIELD);
						try {
							input.focus();
						} catch (e) {}
						return false;
					}

					if (ok) {
						if (input.name.toLowerCase().indexOf('time') > -1) {
							if (!isTime(input.value)) {
								alert(LABEL_JS_BADSYNTAX_TIME);
								return false;
							}
							/*if (!(input.value.match(/([0-9]{2}):([0-9]{2})/))) {
								alert(LABEL_JS_BADSYNTAX_TIME);
								try {
									input.focus();
								} catch (e) {}
								return false;
							}

							var timePair = input.value.split(':');
							var timeHour = timePair[0];
							var timeMins = timePair[1];
							if ( (parseInt(timeHour) > 23) || (parseInt(timeMins) > 59)) {
								alert(LABEL_JS_BADSYNTAX_TIME);
								try {
									input.focus();
								} catch (e) {}
								return false;
							}*/
	
						} else if (input.name.toLowerCase().indexOf('postcode') > -1) {
	
							if (!(input.value.match(/([0-9]{4})([A-Za-z]{2})/))) {
								alert(LABEL_JS_BADSYNTAX_POSTCODE);
								try {
									input.focus();
								} catch (e) {}
								return false;
							}
							if (input.getAttribute('alt') != 'syntaxOnly') {
								if (!(FV_IsPostcode(input.value))) {
									alert(LABEL_JS_UNKNOWN_POSTCODE);
									try {
										input.focus();
									} catch (e) {}
									return false;
								}
							}
							input.value = input.value.toUpperCase();
	
						} else if (input.name.indexOf('mail') > -1) {
	
							if ( !FV_IsEmail(input.value) ) {
								var my_LABEL_JS_FIXFIELD = LABEL_JS_FIXFIELD.replace(/\@\@FIELD\@\@/,"'"+input.title+"'");
								alert(my_LABEL_JS_FIXFIELD);
								try {
									input.focus();
								} catch (e) {}
								return false;
							}
	
						}
					} else {

						ok = true;

					}

			}
			
			if (input.confirm && input.confirm != '') {

				if (input.value != document.getElementById(input.confirm).value) {
					alert(document.getElementById(input.confirm).title + ' and ' + input.title + ' do not match.');
					return false;
				}

			}
		}

		if (input.checkmax == 'true') { //check maximum quantity available per article
			if ( parseInt(input.value) > parseInt(input.maxavailable + input.srcavailable) ) {
				ok = false;
			}
			if (!ok) {
				var my_LABEL_JS_PLEASECOMPLETEFIELD = "Warning! Maximum of "+(parseInt(input.maxavailable) + parseInt(input.srcavailable))+" can be entered."; //LABEL_JS_PLEASECOMPLETEFIELD.replace(/\@\@FIELD\@\@/,"'"+input.title+"'");
				alert(my_LABEL_JS_PLEASECOMPLETEFIELD);
				try {
					input.focus();
				} catch (e) {}
				return false;
			}
		}
	}
	return true;

	//	execute the content of onSubmit attribute (from var)
	eval(tmpFormOnSubmit);
	form.submit();
}


function assign_charCounterAlert(e)
{
	var obj = getEventSrc(e);
	return charCounterAlert(obj);
}

function charCounterAlert(obj)
{
	if (obj.getAttribute("maxlength"))
	{
		LABEL_JS_TOOMUCHCHARS = LABEL_JS_TOOMUCHCHARS.replace(/\@\@QTY\@\@/,obj.getAttribute("maxlength"));
		try {
			if (obj.value.length > obj.getAttribute("maxlength")) {
				obj.value = obj.value.substring(0, obj.getAttribute("maxlength"));
				alert(LABEL_JS_TOOMUCHCHARS);
			} else {
				//cntfield.value = maxlimit - field.value.length;
			}
			}
		catch(e) {
			if (obj.innerText.length > obj.getAttribute("maxlength")) {
				obj.innerText = obj.innerText.substring(0, obj.getAttribute("maxlength"));
				alert(LABEL_JS_TOOMUCHCHARS);
			} else {
				//cntfield.value = maxlimit - field.value.length;
			}
		}
	}
}


// Focuses on the first <input> or <select> field in the specified form
function focusForm(formId) {
	var elements = (formId) ? document.getElementById(formId).getElementsByTagName("*") : document.getElementsByTagName("*");
	for (var i = 0; i < elements.length; i++) {
		var input = elements[i];
		if (input.tagName.toLowerCase() == 'input' || input.tagName.toLowerCase() == 'select') {
			input.focus();
			break;
		}
	}
	return true;
}

function fill_sameAsCustomerAddress(obj,src,targ) {
	if (!obj.checked)
	{
		document.getElementById(targ+"Straat").value = "";
		document.getElementById(targ+"Huisnummer").value = "";
		document.getElementById(targ+"Postcode").value = "";
		document.getElementById(targ+"Plaats").value = "";
		document.getElementById(targ+"CountryId").value = "nl";
	} 
	else
	{
		document.getElementById(targ+"Straat").value = document.getElementById(src+"Straat").value;
		document.getElementById(targ+"Huisnummer").value = document.getElementById(src+"Huisnummer").value;
		document.getElementById(targ+"Postcode").value = document.getElementById(src+"Postcode").value;
		document.getElementById(targ+"Plaats").value = document.getElementById(src+"Plaats").value;
		document.getElementById(targ+"CountryId").value = document.getElementById(src+"CountryId").value;
	}
} // function

// Object Contact
function Contact()
{
	this.id = null; //id
	this.fullName = null; //fullName;
	this.email = null; //email;
	this.phone = null; //phone;
	this.gsm = null; //gsm;
	this.fax = null; //fax;
	this.isMain = null; //isMain;
	return this;
}

// Object Depot
function Depot()
{
	this.id=null; // = id
	this.name=null; // = name;
	this.street=null; // = street;
	this.housenr=null; // = housenr;
	this.postcode=null; // = postcode;
	this.city=null; // = city;
	this.countryId=null; // = countryId;
	return this;
}

function valTime(time) {
// timeregex pattern = 01:59PM or 1:59PM
// 13:59PM and 13:59 are invalid
	//var timeregex=/^(\d|(0\d|10|11|12)):([0-5]+[0-9])*$/;
	var timeregex=/^(\d|(\d\d)):([0-5]+[0-9])$/;
	return time.match(timeregex);
}



