
	/* Load the validation xml file */
    var xmlVal		= new ActiveXObject("Microsoft.XMLDOM");
    xmlVal.async	= false;
	xmlVal.load('../include/script/validation.xml');

	function GetTypeByValidationType(sValidationType) {
		return GetValidationDataByValidationType(sValidationType, "type")
	} //GetTypeByValidationType

	function GetRegExByValidationType(sValidationType) {
		return GetValidationDataByValidationType(sValidationType, "regex")
	} //GetRegExByValidationType

	function GetErrorMessageByValidationType(sValidationType) {
		return GetValidationDataByValidationType(sValidationType, "errormessage")
	} //GetErrorMessageByValidationType
	
	function GetValidationDataByValidationType(sValidationType, sValidationDataType) {
		var aAttributes = xmlVal.documentElement.childNodes;
		for (var i=0; i<aAttributes.length; i++)
			if (aAttributes[i].selectSingleNode("type").text == sValidationType)
				return (aAttributes[i].selectSingleNode(sValidationDataType).text);
	} //GetValidationDataByValidationType

	/*=================================================================
	Validates all the elements of a form
	=================================================================*/
	function ValidateForm(f) {
		if (!f) f = document.forms[0];
		for (var i= 0; i < f.elements.length; i++)
			if (!ValidateControl(f.elements[i], true, true, true))
				return false;

		return true;
	} //ValidateForm

	/*=================================================================
	Validates a Control
	=================================================================*/
	function ValidateControl(o, bSetFocus, bShowMessage, bMainValidation) {
			if (!o) return true;
			if ((bMainValidation) && (o.notmainclientvalidation == "true")) return true;
			if (o.type=='checkbox') return true;
			if (o.mrequired == "true")
				if (!ValidateRequiredEntry(o, bSetFocus, bShowMessage)) return false;
			if ((o.mtype) && (Trim(o.value).length > 0))
				if (!ValidateEntry(o, o.mtype, bSetFocus, bShowMessage)) return false;
				
			return true;
	} //ValidateControl

	/*=================================================================
	Validates a field entry of a form
	=================================================================*/
	function ValidateEntry(entry, mtype, bSetFocus, bShowMessage) {
		var val;
		//defaults
		if (bShowMessage == null) bShowMessage = true;
		if (bSetFocus == null) bSetFocus = true;

		if (isObject(entry)) {
			entry.value = Trim(entry.value);
			val = entry.value;
			if (!mtype)
					mtype = entry.mtype;
			else if (Trim(mtype) == "")
					mtype = entry.mtype;
			}
		else {
			val			= entry;
			bSetFocus 	= null;
			}
		val = Trim(val);

		var regEx;
		var sMessage;
		// Look for the appropriate RegEx and the message 
		// in according to the prefix
		regEx = new RegExp(GetRegExByValidationType(mtype));
		sMessage = GetErrorMessageByValidationType(mtype);

		if (!regEx.test(val)) { // if syntax is valid
			DisplayValidationMessage(entry, bSetFocus, bShowMessage, sMessage);
			return false;
		} //regEx
		return true;


	} //ValidateEntry


	/*=================================================================
	Validates a required field entry of a form
	=================================================================*/
	function ValidateRequiredEntry(entry, bSetFocus, bShowMessage) {
		var val;
		var bValid = true;
		//defaults
		if (bShowMessage == null) bShowMessage = true;
		if (bSetFocus == null) bSetFocus = true;

		if (isObject(entry)) {
			entry.value = Trim(entry.value);
			val = entry.value;
			if (entry.type == "select-one")
				bValid = ((val != "-77") && (val != "-88")); // TODO: fix this
			}
		else {
			val			= Trim(entry);
			bSetFocus 	= null;
			}
			
		bValid = bValid && (val.length > 0);
		if (!bValid) { // if syntax is valid
			var sMessage = GetErrorMessageByValidationType("required");
			DisplayValidationMessage(entry, bSetFocus, bShowMessage, sMessage);
			return false;
		} //bValid
		return true;

	} //ValidateRequiredEntry


	/*=================================================================
	Validates a required field entry of a form
	=================================================================*/
	function ValidateFutureDate(entry, bSetFocus, bShowMessage) {
		var val;
		var bValid = true;
		//defaults
		if (bShowMessage == null) bShowMessage = true;
		if (bSetFocus == null) bSetFocus = true;

		if (isObject(entry)) {
			entry.value = Trim(entry.value);
			val = entry.value;
			}
		else {
			val			= Trim(entry);
			bSetFocus 	= null;
			}
		if (isNaN(Date.parse(val))) return false;
		var valAsDate = new Date(val);
		var curDate = new Date();
		bValid = (valAsDate > curDate);
		//bValid = bValid && (val.length > 0);
		
		if (!bValid) { // if syntax is valid
			var sMessage = GetErrorMessageByValidationType("future_date");
			DisplayValidationMessage(entry, bSetFocus, bShowMessage, sMessage);
			return false;
		} //bValid
		return true;

	} //ValidateFutureDate
	
	/*=================================================================
	Display an error message when validation
	=================================================================*/
	function DisplayValidationMessage(entry, bSetFocus, bShowMessage, sMessage, sLabel) {
		if (bShowMessage) {
			if (isObject(entry)) {		
				sLabel = getElementLabel(entry);
				}
			if (bSetFocus) {
				entry.className="formField1Error";
				if (sLabel == "") sFieldNameKey = " de '{0}'";
				else sFieldNameKey = "{0}";
				
				alert(sMessage.replace(sFieldNameKey, sLabel));
				entry.onfocus = null;
				entry.focus();
				}
			else
				alert(sLabel + sMessage);
			} //bShowMessage
		return false;
	} //DisplayValidationMessage

	/*=================================================================
	Set focus to a field with error
	=================================================================*/
	function GoToErrorField(entryID) {
				entry = document.getElementById(entryID);
				entry.className="formField1Error";
				entry.onfocus = null;
				entry.focus();
	}

	/*=================================================================
	Validates an Object
	=================================================================*/
	function isObject(a) {
		return (a && typeof a == 'object') || isFunction(a);
	}

	/*=================================================================
	Trims a text
	=================================================================*/
	function Trim(sText) {
		var newText = "";
		var ipos, inipos=0, endpos=0;
		
		
		// trim initial and end spaces, ends of line and car retuns
		for (ipos = 0; ipos < sText.length; ipos++) {
			if ((sText.charCodeAt(ipos) != 10)
					&& (sText.charCodeAt(ipos) != 13)
					&& (sText.charCodeAt(ipos) != 32) )
				{
				inipos=ipos;
				break;
				}

		}
		for (ipos = inipos; ipos < sText.length; ipos++)
			if ((sText.charCodeAt(ipos) != 10)
					&& (sText.charCodeAt(ipos) != 13)
					&& (sText.charCodeAt(ipos) != 32) )
				endpos = ipos+1;
		
		if (endpos >= inipos)
			newText = sText.substring(inipos, endpos);
		return newText;
		} //Trim


	/*=================================================================
	Get the description of the current input element
	==================================================================*/
	function getElementLabel(oItem) {
		var vds_name, collection, current_id, current_label;
		var index, vds_id;
		
		vds_name = '';
		
		//
		collection = document.getElementsByTagName('label');	
		try	{
			vds_id = oItem.getAttribute('id');
		} catch (er) {
			vds_id = null;
		}
		if (vds_id != null) {		
			var i = 0;
			if (collection.length) {

				//Search by for attribute
				for(i=0;i<collection.length;i++) {
					current_label = collection[i];
					var oForNode = current_label.getAttributeNode('for');
					if (oForNode) {
						current_id = oForNode.value;
						if (current_id==vds_id) {
							vds_name = current_label.innerText;
							break;
						}
					}
				}
				//if search by 'for' not found then search by name
				if (vds_name == '')
					//Search by name attribute
					for(i=0;i<collection.length;i++) {
						current_label = collection[i];
						current_id = current_label.getAttribute('name');			
						if (current_id==vds_id) {
							vds_name = current_label.innerText;
							break;
						}
					}
			} 
			
/*
				if(vds_name != '') {
					index = vds_name.indexOf(':');		
					if (index < 0) {
						vds_name = vds_name + ': ';
					} else {
						vds_name = vds_name + ' ';	
					}
				}
*/
			vds_name = vds_name.replace("\r\n", "");
		} //vds_id		
		return vds_name;
	} //getElementLabel

function checkMaxLength(oItem){
	var maxlength = oItem.getAttribute? parseInt(oItem.getAttribute("maxlength"), 10) : "";
	if (oItem.getAttribute && oItem.value.length > maxlength)
		oItem.value = oItem.value.substring(0, maxlength)
} //checkMaxLength


