//--Validación General
var aa_checkObjects	= new Array();
var as_errors		= "";
var ab_returnVal		= false;
var aa_language		= new Array();
aa_language["header"]	= "The Next error has ocurred:"
aa_language["start"]	= "->";
aa_language["field"]	= " Field ";
aa_language["require"]	= " is required";
aa_language["min"]		= ", must have at lest ";
aa_language["max"]		= ", no debe tener más de ";
aa_language["minmax"]	= " an no more than ";
aa_language["chars"]	= " characters";
aa_language["function"]	= " is required";
aa_language["num"]		= ", should be a number";
aa_language["email"]	= ", should be a valid email ";
aa_language["date"]		= ", Should be dd/mm/yyyy";
aa_language["file"]		= " is not valid";
aa_language["chbx"]		= ", Should be check at least 1";





function fdefine(n, type, Req, HTMLname, min, max, d) 
{
	var p;
	var i;
	var x;
	var indice;
	var elementos;
	var aa_control	= new Array();
	if (!d) d = document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) 
	{
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) 
	{
		x = fdefine(n, type, Req, HTMLname, min, max, d.layers[i].document);
		return x;       
	}

	eval("V_"+n+" = new fformResult(x, type, Req, HTMLname, min, max);");
	//alert (x.length);
	elementos = x.length
	if (elementos >0)
	{
		//alert (elementos)
		//aa_checkObjects[eval(aa_checkObjects.length)] = x
		//alert(aa_checkObjects[eval(aa_checkObjects.length)-1].length);
		//indice = eval(aa_checkObjects.length);
		//alert(indice);

		for (i=0; i < elementos;i++)
		{
			eval("V_"+n+" = new fformResult(x[i], type, Req, HTMLname, min, max);");
			aa_control[i] = eval("V_"+n);
		}
		aa_checkObjects[eval(aa_checkObjects.length)] = aa_control
		//alert (eval(aa_checkObjects.length));
	}
	else
	{
		aa_checkObjects[eval(aa_checkObjects.length)] = eval("V_"+n);
	}
}

function fformResult(form, type, Req, HTMLname, min, max) 
{
	this.form = form;
	this.type = type;
	this.Req = Req;
	this.HTMLname = HTMLname;
	this.min  = min;
	this.max  = max;
}
function fy2k(number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}

//Temporal
//var reason = '';

//Arma el formato fecha en un solo control
function fArmaFecha(ad_fecha)
{
	var as_Separador = "/";
	if (ad_fecha.value.length == 2 || ad_fecha.value.length == 5) {ad_fecha.value = ad_fecha.value+as_Separador;}
}

function fvalida(as_Accion) 
{
	var elementos;
	if (aa_checkObjects.length > 0) 
	{
		errorObject = "";
		for (i = 0; i < aa_checkObjects.length; i++) 
		{
			if (aa_checkObjects[i].length >0)
			{
				ai_items = aa_checkObjects[i].length;
				if (aa_checkObjects[i][0].type.toLowerCase() == "chbx")
				{
					var ai_Marcados = 0
					for (j = 0; j < ai_items; j++)
					{
						if (aa_checkObjects[i][j].form.checked)
						{ ++ai_Marcados; }
					}
					if (ai_Marcados == 0) { as_errors += aa_language['start'] + aa_language['field'] + aa_checkObjects[i][0].HTMLname + aa_language['require'] + aa_language["chbx"] + "\n";	}
					
				}
				else if (aa_checkObjects[i][0].type.toLowerCase() == "function")
				{
					if (!(fMacro(as_Accion)))
						{ 
							ab_returnVal = true;
							return;
						}
				}
				else if (aa_checkObjects[i][0].type.toLowerCase() == "num")
				{
					if (aa_checkObjects[i][0].Req == "N") 
					{
						aa_language["require"]="";
					}
					else 
					{
						aa_language["require"] = " is required";
					}
					for (j = 0; j < ai_items; j++)
					{
						if (aa_checkObjects[i][j].form.selected)
						{ 
							if (aa_checkObjects[i][j].form.value.length <= 0 || isNaN(aa_checkObjects[i][j].form.value))
								{ as_errors += aa_language['start'] + aa_language['field'] + aa_checkObjects[i][j].HTMLname + aa_language['require'] + "\n";} 
						}
					}
				}
			}
			else
			{

				validateObject = new Object();
				validateObject.form = aa_checkObjects[i].form;
				validateObject.HTMLname = aa_checkObjects[i].HTMLname;
				validateObject.val = aa_checkObjects[i].form.value;
				validateObject.len = aa_checkObjects[i].form.value.length;
				validateObject.min = aa_checkObjects[i].min;
				validateObject.max = aa_checkObjects[i].max;
				validateObject.type = aa_checkObjects[i].type.toLowerCase();
				validateObject.Req = aa_checkObjects[i].Req;
				if (validateObject.Req == null) {validateObject.Req = 'N';}
				else {validateObject.Req = validateObject.Req.toUpperCase();}
				if (validateObject.Req == "N" || validateObject.len > 0) 
					{
					aa_language["require"]="";
					}
				else 
					{
					aa_language["require"] = " is required";
					}
				if ((validateObject.Req=='S') || (validateObject.Req=='N' && validateObject.len > 0))
				{
					if (validateObject.type == "num" || validateObject.type == "string")
					{
						if ((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))){ as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['num'] + "\n";} 
						else if (validateObject.min == null && validateObject.max && validateObject.Req=='S' && validateObject.len == '') { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['max'] + validateObject.max+aa_language['chars'] + "\n";}
						else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['min'] + validateObject.min + aa_language['minmax'] + validateObject.max+aa_language['chars'] + "\n";}  
						else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['min'] + validateObject.min + aa_language['chars'] + "\n";} 
						else if (validateObject.max && !validateObject.min && (validateObject.len > validateObject.max)) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['max'] + validateObject.max + aa_language['chars'] + "\n";} 
						else if (!validateObject.min && !validateObject.max && validateObject.len <= 0) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + "\n";}
					}	
					if (validateObject.type == "function")
					{
						if (!(fMacro(as_Accion)))
							{ 
								ab_returnVal = true;
								return;
							}
					}	
					else if(validateObject.type == "chbx") 
					{
						if (validateObject.form.checked==false) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language["chbx"] + "\n";	}
					}	

					else if(validateObject.type == "email") 
					{
						//Revisa la existencia de "@" y ".". 
						// El tamaño debe ser >= 5 y el "." no  
						// debe preceder directamente o seguido de "@"
						if ((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") || (validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['email'] + "\n";	}
					}	
	
					else if(validateObject.type == "file") 
					{
						if (validateObject.len <= 0){ as_errors += aa_language['start'] + aa_language['field']+ validateObject.HTMLname  + aa_language['require'] + "\n";} 
						else
						{
							aa_extArray = new Array(".doc",".xls",".ppt",".pdf",".zip");
							ab_continuar = false;
							as_file = validateObject.val;
							while (as_file.indexOf("\\") !== -1)
							as_file = as_file.slice(as_file.indexOf("\\") + 1);
							//as_ext = as_file.slice(as_file.indexOf(".")).toLowerCase();
							var iLen = as_file.length;
							as_ext = as_file.substring(iLen, iLen - 4).toLowerCase();
							for (var i = 0; i < aa_extArray.length; i++) 
							{
								if (aa_extArray[i] == as_ext) { ab_continuar = true; break; }
							}	
							if (ab_continuar == false) 
							{ as_errors += aa_language['start'] + aa_language['field']+ validateObject.HTMLname  + aa_language['require'] + ", Extensión Inválida, solo están permitidas las sgts.:  " + (aa_extArray.join("  ")) + "\n";}
						}
					}	
					else if(validateObject.type == "date") 
					{
						// Chequea fecha valida
 							var as_elems = validateObject.val.split("/");
 							ai_result = (as_elems.length == 3); // Deben ser 3 componentes
							if ((validateObject.val.length !== 10) || ((validateObject.val.substring(2,3) !== "/") && (validateObject.val.substring(5,6) !== "/"))) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['date'] + "\n"; }
							else if (!ai_result){ as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['date'] + "\n"; }
							else 
							{
						        var as_dia  = parseInt(as_elems[0],10);
							    var as_mes = parseInt(as_elems[1],10);
						        var as_ani  = parseInt(as_elems[2],10);
							    var test = new Date(as_ani,as_mes-1,as_dia);
						        var as_tdia  = test.getDate();
								var as_tmes = test.getMonth();
					            var as_tani  = fy2k(test.getYear());
						        as_dia  = new Number(as_dia);
							    as_mes = new Number(as_mes);
						        as_ani  = new Number(as_ani);
						        if ((as_ani !== as_tani && (as_mes-1 !== as_tmes) && (as_dia !== as_tdia))) { as_errors += aa_language['start'] + aa_language['field'] + validateObject.HTMLname + aa_language['require'] + aa_language['date'] + "\n"; }
							}
					}
				}
			}	
	}
}
if (as_errors) {
alert(aa_language["header"].concat("\n" + as_errors));
as_errors = "";
ab_returnVal = false;
} else {
ab_returnVal = true;
   }
return ab_returnVal;
}
