var validateForm = true;
function chkFrm(cmp,tipo,required,param){
	var msgError;
	var obj;
	var nameCmp;
	var colorError;
	var formatFecha;
	var separateFecha;
	this.msgError = "";
	this.colorError = "#CC0000";
	this.colorOk = "#FFF";
	this.separateFecha = "/";
	this.formatFecha = "dd/mm/yyyy";
	this.campo = cmp;
	this.tipo=tipo;
	this.isRequired = required;
	this.param = param;	
	
	chkFrm.prototype.getCmp = function (cmp){
		var vnameCmp;
		vnameCmp = (cmp == null) ? this.nameCmp : cmp;
		if (document.getElementById(vnameCmp) == null) 
		{
			this.setError(texto["noCmp"]);
			return false;
		}else{
			return document.getElementById(vnameCmp);
		}
	}
	
	chkFrm.prototype.setNameCmp = function(value)
	{
		this.nameCmp = value;
		this.obj = this.getCmp();
	}
	
	chkFrm.prototype.setError = function (msg){
		this.msgError = msg;	
	}
	
	chkFrm.prototype.getError = function(){
		return this.msgError;
	}
	
	chkFrm.prototype.required = function (){
			if (this.obj != false)
			{
				if (this.obj.value.length > 0)
				{
					return true;
				}else{
					this.setError(texto["isRequired"]);
					return false;
				}
			}
	}
	
	chkFrm.prototype.isText = function(value){
			var value;
			if (value == null){
				value = this.obj.value;	
			}
			if (this.isRequired) 
			{
				if (!this.required()){return false;}
				if (this.param > 0){
					if (value.length < this.param){
						this.setError(texto["mincampo"].replace("#num#",this.param));
						return false;	
					}
				}
			}
			return true;
	}
	
	chkFrm.prototype.isCheck = function(){
		return (this.obj.checked);	
	}
	
	chkFrm.prototype.isEmail = function (){
			if (this.obj != false)
			{
				if (this.isRequired){
					if (!this.required()){return false;}
				}
				var mailres = true;            
				var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
				var txtEmail = this.obj.value;
				
				var arroba = txtEmail.indexOf("@",0);
				if ((txtEmail.lastIndexOf("@")) != arroba) arroba = -1;
				
				var punto = txtEmail.lastIndexOf(".");
				 for (var contador = 0 ; contador < txtEmail.length ; contador++){
					if (cadena.indexOf(txtEmail.substr(contador, 1),0) == -1){
						mailres = false;
						break;
				 }
				}
				if (txtEmail.length > 0){
					if ((arroba > 1) && (arroba + 1 < punto) && ((punto + 1) < (txtEmail.length)) && (mailres) && (txtEmail.indexOf("..",0) == -1))
					 mailres = true;
					else{
					 this.setError(texto["isEmail"]);
					 mailres = false;
					}
				}else{mailres = true;}
							
				return mailres; 
			}
	}
	
	chkFrm.prototype.isNumber = function (value){
			var cmpRet = true;
			if (this.obj != false)
			{
				if (this.isRequired){
					if (!(type = this.required())){
						return type;
					}
				}			
				var cadena = "1234567890";
				var numero;
				if (value == null)
				{
					numero = this.obj.value;
				}else{numero = value;}
	
				for (var contador = 0 ; contador < numero.length ; contador++){
					if (cadena.indexOf(numero.substr(contador, 1),0) == -1){
						this.setError(texto["isNumber"]);
						cmpRet = false;
						break;
					}
				}
				
				return cmpRet;
			}
			return false;
	}
	
	chkFrm.prototype.isFloat = function (nameCmp,Required,Decimal){
			var cmpRet = true;
			if (this.obj != false)
			{
				if (!(type = this.required())){
						return type;
				}
			}
			return false;
	}
	
	chkFrm.prototype.isEqual = function (cmp){
		if (this.param == null) return false;
		var aParam = this.param.split(",");
		if (aParam.length > 1)
		{
			cmp = aParam[0];
			this.param = aParam[1];
		}else{
			cmp = this.param;
			this.param = null;
		}
		if (this.isText())
		{
			var objCompare;
			objCompare = (cmp == null) ? this.getCmp(this.param) : this.getCmp(cmp);
			if (objCompare == null) return false;
			if (objCompare.value != this.obj.value)
			{
				objCompare.style.backgroundColor = this.colorError;
				this.setError(texto["noEqual"]);
				return false;
			}else{
				objCompare.style.backgroundColor = this.colorOk;
				return true;
			}
		}
		return false;
	}
	
	chkFrm.prototype.putError = function(){
			//this.obj.style.backgroundColor = "#CC0000"; //this.colorError;
			if (this.campo == "cn")
			{
				for (x=1;x<=4;x++)
				{
					document.getElementById("cn"+x).style.backgroundColor = this.colorError;	
				}
				var errorCmp = this.getCmp("cn_error");
			}else{
				if (this.tipo == "isCheck"){
					this.obj.parentNode.style.backgroundColor = this.colorError;
				}else{
					this.obj.style.backgroundColor = this.colorError;
				}
				var errorCmp = this.getCmp(this.campo+"_error");
			}
			if (errorCmp != null) errorCmp.innerHTML = this.getError();
	}
	chkFrm.prototype.quitError = function()
	{
		if (this.campo == "cn")
		{
			for (x=1;x<=4;x++)
			{
				document.getElementById("cn"+x).style.backgroundColor = this.colorOk;	
			}
			var errorCmp = this.getCmp("cn_error");
		}else{
			if (this.tipo == "isCheck"){
				this.obj.parentNode.style.backgroundColor = this.colorOk;
			}else{
				this.obj.style.backgroundColor = this.colorOk;
			}
			var errorCmp = this.getCmp(this.campo+"_error");
		}
		if (errorCmp != null) errorCmp.innerHTML = "";
	}//backgroundColor = "#FFF";this.obj.title = "";}
	
	chkFrm.prototype.isDate = function(){
		if (this.obj == false){return false;}
		var day,month,year;
		var dayMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		var valor = this.obj.value;
		if ((!this.isRequired) && (valor.length == 0)){return true;}
		var fmt = this.formatFecha;
		if (valor.length != fmt.length){this.setError(texto["isDate"]);return false;}
		var dt = valor.split(this.separateFecha);
		var pant = fmt.split(this.separateFecha);
		if (dt.length <= 0) {this.setError(texto["isDate"]);return false;}
		for (m = 0; m<dt.length; m++)
		{
			if (pant[m] == "dd")//Controla los dias
			{
				if (!this.isNumber(dt[m])) return false;
				day = parseInt(dt[m]);
				if ((day <= 0) && (day > 31)) {this.setError(texto["noDay"]); return false;}
			}else if (pant[m] == "mm"){ // controla los meses
				if (!this.isNumber(dt[m])) return false;
				month = parseInt(dt[m]);
				if ((month <= 0) && (month > 12)){this.setError(texto["noMonth"]);return false;}
			}else if (pant[m] == "yyyy"){ // controla el año
				if (!this.isNumber(dt[m])) return false;
				if (dt[m].length < 4){this.setError(texto["noYear"]);return false;}
				year = dt[m];
			}
		}
		if (year % 4 == 0) {dayMonth[1] = 29;}
		if (day > dayMonth[month-1]) {this.setError(texto["noDay"]);return false;}
		return true;
	}
	
	chkFrm.prototype.check = function(){
			var ret = false;
			if (this.campo == "cn"){
				var valor = "";
				for (x = 1; x<=4; x++)
				{
					valor += document.getElementById("cn"+x).value;	
				}
				if (valor.length < 20){this.setError(texto["isRequired"]); ret = false;}
				else{ret = this.isNumber(valor);}
			}else{
			this.setNameCmp(this.campo);
			switch (this.tipo){
				case "isTextEqual":{ret = this.isEqual();break;}
				case "isNumber":{ret = this.isNumber();break;}
				case "isEmail":{ret = this.isEmail();break;}
				case "isDate":{ret = this.isDate();break;}
				case "isCheck":{ret = this.isCheck();break;}
				default: {ret = this.isText();break;}	
			}
			}
			return ret;
	}
}
//Args tiene que ser el siguiente
// Obligatorio|por defecto isText| por defecto noRequired| opcional"
// "'campo|tipo|required|params@campo|tipo|required|params'"
// name_cmp|(isFloat|isNumber|isEmail|isDate|isTextEqual|isChecked)|(isRequired | notRequired)|(Number of Decimals | Cmp a comparar,min-length | min-length)
// 
function checkForm(args){
	if (!validateForm){return true;}
	var optRet = true;
	var listCmp = args.split("@");
	if (listCmp.length > 0)
	{
		for (countCmp = 0; countCmp < listCmp.length; countCmp++)
		{
			var aVal = listCmp[countCmp].split("|");
			var tipo = "isText";
			var tamano = aVal.length;
			var param = null;
			var isRequired = false;
			if (tamano > 0)
			{
				cmp = aVal[0];
				if (tamano > 1)
				{
					tipo = aVal[1];
					if ((tipo != "isFloat") && (tipo != "isNumber") && (tipo != "isEmail") && (tipo != "isDate") && (tipo != "isTextEqual") && (tipo != "isCheck"))
					{
						tipo = "isText";
					}
				}
				if (tamano > 2)
				{
					isRequired = (aVal[2] != "noRequired") ? true : false;
				}
				if (tamano > 3)
				{
					param = aVal[3];
				}
			}
			var objCheck = new chkFrm(cmp,tipo,isRequired,param);
			var ret = objCheck.check();
//			objCheck.setNameCmp(cmp);
//			objCheck.isRequired = isRequired;
//			if (param != null){objCheck.param = param;}
//			var ret = true;
/*			if (isRequired)
			{	objCheck.isRequired = true;
				if (tipo != "isCheck") ret = objCheck.required();
				else 
			}
			if ((ret) && ((tipo == "isFloat") || (tipo == "isNumber") || (tipo == "isEmail") || (tipo == "isDate") || (tipo == "isTextEqual")))
			if (tipo == "isText") ret = objCheck.isText();
			else if (tipo == "isTextEqual") ret = objCheck.isEqual();
			else if (tipo == "isNumber") ret = objCheck.isNumber();
			else if (tipo == "isEmail") ret = objCheck.isEmail();
			else if (tipo == "isDate") ret = objCheck.isDate();
			else if (tipo == "isCheck") ret = objCheck.isCheck();
			*/
			if (!ret)
			{
				optRet = false;
				objCheck.putError();
			}else{
				objCheck.quitError();
			}
			
		}
	}
	return optRet;
}
var swDelete = false;
function checkSubmitForm(accion,param){
	if (swDelete){
		return true;	
	}else{
		return checkForm(param);
	}
	return false;
}