/* Cargar fichero XML usando AJAX */

/**/
/* Expresiones regulares */

/* Ej. <input name="fecha" type="text" id="fecha" title="Fecha" vtype="10" /> */

var arrRegex = new Array();
var arrMsg = new Array();  

arrRegex[1] = /^[áéíóúÁÉÍÓÚñÑÁÉÍÓÚÜÖÄËÏa-zA-Z ]+$/; //1 Letras
arrMsg[1] = "Letras";
       
arrRegex[2] = /^[0123456789]+$/; //2 Números
arrMsg[2] = "Números";

arrRegex[3] = /^[áéíóúÁÉÍÓÚñÑÁÉÍÓÚÜÖÄËÏ\-0-9a-zA-Z ]+$/; //3 Letras o Números
arrMsg[3] = "Letras y/o números";

arrRegex[4] = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; //4 Correo
arrMsg[4] = "Email (Ej. pepe@host.com)";

arrRegex[5] = /^.+$/; //5 No vacío
arrMsg[5] = "No vacío";

arrRegex[6] = /^([1-9]{1}[0-9]{0,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9xX]{1,3}){1}$/; //6 IP de clase C
arrMsg[6] = "Máscara de IP Clase C (ej. 10.50.50.x)";

arrRegex[7] = /^([1-9]{1}[0-9]{0,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9]{1}[0-9]{0,2}){1}$/; //7 Dirección IP
arrMsg[7] = "Dirección IP (ej. 10.31.7.200)";

arrRegex[8] = /^[-+]?[0-9]*\.?[0-9]+$/; //8 Número real
arrMsg[8] = "Número real (ej. 5.8)";

arrRegex[9] = /^[0-9]*\.?[0-9]+$/; //9 Precio
arrMsg[9] = "Precio (ej. 90.05)";

//arrRegex[10] = /^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/; //10 Fecha
arrRegex[10] = /^.+$/;
//arrMsg[10] = "Fecha (ej. 1983-01-21)";
arrMsg[10] = "Fecha (ej. 21/01/1983)";

arrRegex[11] = /^[0123456789\-]+$/; //2 Números
arrMsg[11] = "Números";

function Validar(mxForm) {

	if (typeof mxForm == 'object') {
		var objForm = mxForm;
	} else {
		var objForm = this.document.getElementById(mxForm);
	}

	var intCountElements = objForm.elements.length;
	var strMsgErrors = '';	

	for(i=0 ; i<intCountElements ; i++) {
	
		objElement = objForm.elements[i];
		
		var strName = objElement.name;
        var strValue = objElement.value;                             
		var intNumberValidation = objElement.getAttribute('vtype');
		var strTitle = objElement.getAttribute('title');

		if (strName != "") {
			
			if (strTitle == null) {
				strTitle = strName;
			}
			
			if (intNumberValidation != null) {
            
				var strRegex = arrRegex[intNumberValidation];
				var strMessage = arrMsg[intNumberValidation];
				
                if (strRegex != undefined) {                
                    if (strValue.search(strRegex) == -1) {                        
						if (typeof strMessage == 'undefined') {
							strMsgErrors = strMsgErrors + strTitle + ' contiene errores! \n';
						} else {
							strMsgErrors = strMsgErrors + strTitle + " debe ser de tipo '" + strMessage + "'\n\n";
						}						
                    }                
                }                 
				
			}
		}
	}
	
	if (strMsgErrors != "") {
		alert("Debe corregir los siguientes errores: \n" + "-------------------------------------------\n" + strMsgErrors);
		return false;
	}
	
	return true;
	
}