// Constantes (para parametrizacion)

// Salida de mensajes de error
var sHeader				= '';
var sErrorLineStart		= '-- ';
var sErrorLineEnd		= ' --';

// Atributos
var sAtrValidacion			= 'fvValidation';
var sAtrEmpty						= 'fvAllowEmpty';
var sAtrMatcher					= 'fvMatchWith';
var sAtrMin							= 'fvMin';
var sAtrMax							= 'fvMax';
var sAtrFriend					= 'fvFriend';
var sAtrEmptyValue			= 'fvEmptyValue';
var sAtrEmptyIf					= 'fvEmptyIf';
var sAtrEmptyIfValue		= 'fvEmptyIfValue';

var sAtrMsgEmpty				= 'fvOnEmpty';
var sAtrMsgError				= 'fvOnError';
var sAtrMsgMismatch			= 'fvOnMismatch';

// Tipos de validaciones para <input type="text">
var sVtAny							= 'any';
var sVtAlphaNumeric			= 'alphanum';
var sVtAlpha						= 'alpha';
var sVtUnsignedInteger	= 'unsigned';
var sVtInteger					= 'integer';
var sVtUnsignedFloat		= 'unsigned_float';
var sVtFloat						= 'float';
var sVtEmail						= 'email';
var sVtURL							= 'url';
var sVtDate							= 'date';

// Tipos de validaciones para <input type="radio">
var sVtAtLeastOne				= "selectone"

// Tipos de validaciones para <input type="checkbox">
var sVtRange						= "range"

// Tipos de validaciones para <select>
var sVtSelect						= "select"

function fvValidateForm(frmValidar)
{
	var sError = sHeader;
	if (sError != '') sError += '\n';
	var bValid = true;
	var bControlValid;
	var sValidacion;
	var bAllowEmpty;
	var control;
	
	// Recorro los controles del formulario enviado para validar cada uno
	for (i=0;i<frmValidar.elements.length;i++)
	{
		control = frmValidar.elements.item(i);
		sValidacion = control.getAttribute(sAtrValidacion);
		
		// Si el elemento actual tiene validacion y el elemento esta habilitado, la ejecuto
	
		if (sValidacion != null && !control.disabled)
		{
			// Dependiendo del tipo de control me fijo que validaciones pueden ser...
			
			
			
			if (control.tagName == 'INPUT')
			{
				sType = control.getAttribute('type').toUpperCase();
				
				
				if (sType=='TEXT' || sType=='PASSWORD')
				{
					bControlValid = true;
					// INPUT type="text" o type="password"
					if (control.getAttribute(sAtrEmptyIf)==null)
						bAllowEmpty = (control.getAttribute(sAtrEmpty)=='true' && isWhitespace(control.value));
					else
						bAllowEmpty = (!isChecked(frmValidar, control.getAttribute(sAtrEmptyIf), control.getAttribute(sAtrEmptyIfValue)) && isWhitespace(control.value));
					if (!bAllowEmpty)
					{
						// Si no pasa que es vacio permitiendo vacio, valido...
						if (isWhitespace(control.value))
						{
							// Vacio?
							sError += sErrorLineStart + control.getAttribute(sAtrMsgEmpty) + sErrorLineEnd + '\n';
							bControlValid = false;
						}
						else if (
										(sValidacion == sVtAlphaNumeric && !isAlphanumeric(control.value)) ||	
										(sValidacion == sVtAlpha && !isAlphabetic(control.value)) ||	
										(sValidacion == sVtUnsignedInteger && !isInteger(control.value)) ||
										(sValidacion == sVtInteger && !isSignedInteger(control.value)) ||
										(sValidacion == sVtUnsignedFloat && !isFloat(control.value)) ||
										(sValidacion == sVtFloat && !isSignedFloat(control.value)) ||
										(sValidacion == sVtEmail && !isEmail(control.value)) ||
										(sValidacion == sVtURL && !isURL(control.value)) ||
										(sValidacion == sVtDate && !strIsDate(control.value))
										)
						{
							// Error
							sError += sErrorLineStart + control.getAttribute(sAtrMsgError) + sErrorLineEnd + '\n';
							bControlValid = false;
						}
						
						// Si la validacion para este control es valida y tengo que matchear con otro control, me fijo...
						if (bControlValid && control.getAttribute(sAtrMatcher) != null)
						{
							// Validacion normalmente para INPUT type="password"
							if (control.value != formControl(frmValidar,control.getAttribute(sAtrMatcher)).value)
							{
								// Error
								sError += sErrorLineStart + control.getAttribute(sAtrMsgMismatch) + sErrorLineEnd + '\n';
								bValid = false;
							}
						}
						else
							bValid = bValid && bControlValid;
					}
				}
				else if (sType=='RADIO')
				{
					// INPUT type="radio"
					
					if (sValidacion == sVtAtLeastOne)
					{
						// Al menos uno seleccionado (todos deben tener el mismo nombre)
						// Recorro todos y me fijo...
						bControlValid = false;
						
						if (control.getAttribute(sAtrEmptyIf)!=null)
							bControlValid = !isChecked(frmValidar, control.getAttribute(sAtrEmptyIf), control.getAttribute(sAtrEmptyIfValue));
						
						for (j=0;j<frmValidar.elements.length;j++)
						{
							if (isRadio(frmValidar.elements.item(j)) && frmValidar.elements.item(j).id == control.id)
								bControlValid = bControlValid || frmValidar.elements.item(j).checked;
						}
						
						if (!bControlValid)
						{
							// Error
							sError += sErrorLineStart + control.getAttribute(sAtrMsgError) + sErrorLineEnd + '\n';
							bValid = false;
						}
					}
				}
				else if (sType=='CHECKBOX')
				{
					if (sValidacion == sVtRange)
					{
						// Rango de seleccionados
						var iCantidadSeleccionados=0;
						var iMin = control.getAttribute(sAtrMin);
						var iMax = control.getAttribute(sAtrMax);
						if (iMax==null)
							iMax=0;
							
						if (control.getAttribute(sAtrEmptyIf)!=null)
							bAllowEmpty = !isChecked(frmValidar, control.getAttribute(sAtrEmptyIf), control.getAttribute(sAtrEmptyIfValue));
						else
							bAllowEmpty = false;
						
						for (j=0;j<frmValidar.elements.length;j++)
						{
							if (isCheckBox(frmValidar.elements.item(j)) && frmValidar.elements.item(j).id == control.id)
								iCantidadSeleccionados += (frmValidar.elements.item(j).checked?1:0);
						}
						
						if ( ((iCantidadSeleccionados < parseInt(iMin)) || ((iCantidadSeleccionados > parseInt(iMax)) && (iMax>0))) && !bAllowEmpty)
						{
							var bFriendOK;
							
							if (control.getAttribute(sAtrFriend) == null)
								bFriendOK = false;
							else
								bFriendOK = !isWhitespace(formControl(frmValidar,control.getAttribute(sAtrFriend)).value);
								
							if (!bFriendOK)
							{
								// Error
								sError += sErrorLineStart + control.getAttribute(sAtrMsgError) + sErrorLineEnd + '\n';
								bValid = false;
							}
						}
					}
				}
				else if (sType == 'HIDDEN')
				{
					
					if (isWhitespace(control.value))
					{
						// Error
						sError += sErrorLineStart + control.getAttribute(sAtrMsgEmpty) + sErrorLineEnd + '\n';
						bValid = false;	
					}
			
				}
				
			}
			else if (control.tagName == 'SELECT')
			{
				// SELECT
				if (sValidacion == sVtSelect)
				{
					// Que haya algun valor seleccionado
					if (control.value == control.getAttribute(sAtrEmptyValue))
					{
						// Error
						sError += sErrorLineStart + control.getAttribute(sAtrMsgError) + sErrorLineEnd + '\n';
						bValid = false;	
					}
				}				
			}
			else if (control.tagName == 'TEXTAREA')
			{
				// TEXTAREA
				
				// Me fijo que no sea vacio
				if (isWhitespace(control.value))
				{
					// Error
					sError += sErrorLineStart + control.getAttribute(sAtrMsgEmpty) + sErrorLineEnd + '\n';
					bValid = false;	
				}
			}
			
			
		}
	}
	
	if (!bValid)
		alert(sError);
		
	return bValid;
}

function isRadio(control)
{
	if (control.tagName == 'INPUT')
		return control.getAttribute('type').toUpperCase()=='RADIO';
	else
		return false;
}

function isCheckBox(control)
{
	if (control.tagName == 'INPUT')
		return control.getAttribute('type').toUpperCase()=='CHECKBOX';
	else
		return false;
}

function formControl(form,sControl)
{
	var control = null;
	for (h=0;h<form.elements.length;h++)
	{
		if (form.elements.item(h).id == sControl)
		{
			control = form.elements.item(h)
			break;
		}
	}
	
	return control;
}

function isChecked(theForm, sControl, theValue)
{
	if (sControl != null)
	{
		var bChecked = false;
		var bAnyChecked = false;
		for (z=0;z<theForm.elements.length;z++)
		{
			ctrl = theForm.elements.item(z);
			if (ctrl.tagName=='INPUT' && (ctrl.getAttribute('type').toUpperCase()=='RADIO' || ctrl.getAttribute('type').toUpperCase()=='CHECKBOX') && ctrl.id==sControl)
			{
				if (ctrl.checked)
					bAnyChecked = true;
				bChecked = bChecked || (ctrl.checked && ctrl.value==theValue);
			}
		}
		if (!bAnyChecked)
			return false;
		else
			return bChecked;
	}
	else
		return false;
}