var _CAMPOS_OBLIGATORIOS = [];

/* AGREGO FORMULARIO POR FORMULARIO */
var temp = ['descargas_all','descargas_user','titulo'];
_CAMPOS_OBLIGATORIOS.push({nombre:"files",campos:temp});

//var temp = ['nombre','nombre_real','mail','mail_check','nombre_check','direccion','telefono'];
var temp = ['nombre','nombre_real','mail','mail_check','direccion','telefono'];
_CAMPOS_OBLIGATORIOS.push({nombre:"usuarios",campos:temp});

var temp = ['nombre'];
_CAMPOS_OBLIGATORIOS.push({nombre:"permisos",campos:temp});

var temp = ['nombre'];
_CAMPOS_OBLIGATORIOS.push({nombre:"grupos",campos:temp});

var temp = ['titulo','texto','usuarios||grupos||soporte==true'];
_CAMPOS_OBLIGATORIOS.push({nombre:"feedback",campos:temp});

var temp = ['nombre||mail||codigo'];
_CAMPOS_OBLIGATORIOS.push({nombre:"recover_pass",campos:temp});


//esto se podría mover a otro lado si tuviera más protos o cosas bien de bajo nivel
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }


var _ERRORES_CAMPOS =  [];
_ERRORES_CAMPOS.push({nombre:"nombre",texto:"El nombre de usuario ya está en uso o no es válido"});
_ERRORES_CAMPOS.push({nombre:"mail",texto:"El correo electrónico ya está en uso por otro usuario o no es válido"});

/*TODO!
//reemplazar todas las utilizaciones de trim() por empty() del objeto String en PROTOTYPE*/

function FORM_checkFormCampos(formID,formNAME)
{
	FORM = $(formID);
	
	var x = 0;
	var REFF = null;
	for(x = 0;x<_CAMPOS_OBLIGATORIOS.length;x++) if(_CAMPOS_OBLIGATORIOS[x].nombre == formNAME) REFF = _CAMPOS_OBLIGATORIOS[x].campos;
	
	for(x in REFF)
	{
		if(explode("||",REFF[x]).length > 1)
		{
			REFFx = explode("||",REFF[x]);
			var error = true;
			for(var z in REFFx)
			{
				var COMPARAR = null;
				var VALOR = null;
				if(explode("==",REFFx[z]).length > 1)
				{
					COMPARAR = explode("==",REFFx[z])[0];
					VALOR = explode("==",REFFx[z])[1];
				}
				else
				{
					COMPARAR = REFFx[z];
					VALOR = undefined;
				}
				
				var w;
				for(w in FORM.elements)
				{
					if(FORM.elements[w]) if(FORM.elements[w].name == COMPARAR)
					{
						if(VALOR == undefined)
						{
							var CAMPO = FORM.elements[w];
							if(CAMPO.value.trim() == "")
							{
								if(CAMPO.selectedIndex != undefined)
								{
									if(CAMPO.options[CAMPO.selectedIndex].value.trim() == "")
									{
										if(CAMPO.options[CAMPO.selectedIndex].text.trim() != "")
										{
											CAMPO.options[CAMPO.selectedIndex].value = CAMPO.options[CAMPO.selectedIndex].text;
											error = false;											
										}
									}
									else error = false;
								}							
							}
							else error = false;
						}
						else
						{
							var CAMPO = FORM.elements[w];
							if(CAMPO.value.trim() != VALOR)
							{
								if(CAMPO.selectedIndex != undefined)
								{
									if(CAMPO.options[CAMPO.selectedIndex].value.trim() != VALOR)
									{
										if(CAMPO.options[CAMPO.selectedIndex].text.trim() == VALOR)
										{
											CAMPO.options[CAMPO.selectedIndex].value = CAMPO.options[CAMPO.selectedIndex].text;											
											error = false;											
										}
									}
									else error = false;
								}							
							}
							else error = false
						}
					}
				}
			}
			if(error) return false;
		}
		else
		{
			var COMPARAR = null;
			var VALOR = null;
			if(explode("==",REFF[x]).length > 1)
			{
				COMPARAR = explode("==",REFF[x])[0];
				VALOR = explode("==",REFF[x])[1];
			}
			else
			{
				COMPARAR = REFF[x];
				VALOR = undefined;
			}
			
			var y;
			for(y in FORM.elements)
			{
				if(FORM.elements[y]) if(FORM.elements[y].name == COMPARAR)
				{
					var CAMPO = FORM.elements[y];
					if(VALOR == undefined)
					{
						if(CAMPO.value.trim() == "")
						{
							if(CAMPO.selectedIndex != undefined)
							{
								if(CAMPO.options[CAMPO.selectedIndex].value.trim() == "")
								{
									if(CAMPO.options[CAMPO.selectedIndex].text.trim() == "")
									{										
										return false;
									}
									else CAMPO.options[CAMPO.selectedIndex].value = CAMPO.options[CAMPO.selectedIndex].text;
								}
							}
							else
							{								
								return false;
							}
						}
						/*else
						{
							///CODIGO DE QUE EL CAMPO ESTÁ BIEN
						}*/
					}
					else
					{
						if(CAMPO.value.trim() != VALOR)
						{
							if(CAMPO.selectedIndex != undefined)
							{
								if(CAMPO.options[CAMPO.selectedIndex].value.trim() != VALOR)
								{
									if(CAMPO.options[CAMPO.selectedIndex].text.trim() != VALOR) return false;
									else CAMPO.options[CAMPO.selectedIndex].value = CAMPO.options[CAMPO.selectedIndex].text;
								}
							}
							else return false;
						}
						/*else
						{
							///CODIGO DE QUE EL CAMPO ESTÁ BIEN
						}*/
					}
				}
			}
		}
	}
	return true;
}