var V_ValidPage = true;

function V_GetElement(sName)
{
	if (document.getElementById)
	{
		return document.getElementById(sName);
	}
	if (document.all)
	{
		return document.all[sName];
	}
	eval("if (document" + sName + ")")
	{
		return eval ("document" + sName);
	}
	return null;
}

function V_trim(sText) {
    var m = sText.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function V_evaluateRE(sText, re)
{
	if (V_trim(sText) == "") return true;

	var rx = new RegExp(re,"i");
	var matches = rx.exec(sText);
	//alert(sText + " *** " + matches[0]);
	return (matches != null && sText == matches[0]);
}

function V_isEmail(sText)
{
	return V_evaluateRE(sText,"^([a-zA-Z0-9_\\-])([a-zA-Z0-9_\\-\\.]*)@(\\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}|((([a-zA-Z0-9\\-]+)\\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\])$");
}

function V_isInteger(sText)
{
	return V_evaluateRE(sText,"[0-9]+");
}

function V_isNumber(sText, sDecimalSeparator)
{
	return V_evaluateRE(sText,"\\-\\d{1,}\\" + sDecimalSeparator + "\\d{1,}|\\-\\d{1,}|\\d{1,}\\" + sDecimalSeparator + "\\d{1,}|\\d{1,}\\" + sDecimalSeparator + "\\d{1,}|\\d{1,}");
}

function V_isUrl(sText)
{
	switch (sText)
	{
		case "http://":
			return true;
			break;
		default:
			return V_evaluateRE(sText,"(http|https):\\/\\/[\\w]+(\\.[\\w]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");
			break;
	}
}

function V_isDate(sText, sSchema)
{
	switch (sSchema)
	{
		case "english":
		case "us":
			return V_evaluateRE(sText,"^(?:(?:(?:0?[13578]|1[02])(\\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\\/|-)(?:29|30)))(\\/|-)(?:[1-9]\\d\\d\\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\\/|-)(?:0?[1-9]|1\\d|2[0-8]))(\\/|-)(?:[1-9]\\d\\d\\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(0?2(\\/|-)29)(\\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\\d\\d)?(?:0[48]|[2468][048]|[13579][26]))$");
			break;
		default:
			return V_evaluateRE(sText,"^(?:(?:0?[1-9]|1\\d|2[0-8])(\\/|-)(?:0?[1-9]|1[0-2]))(\\/|-)(?:[1-9]\\d\\d\\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(?:(?:31(\\/|-)(?:0?[13578]|1[02]))|(?:(?:29|30)(\\/|-)(?:0?[1,3-9]|1[0-2])))(\\/|-)(?:[1-9]\\d\\d\\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(29(\\/|-)0?2)(\\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\\d\\d)?(?:0[48]|[2468][048]|[13579][26]))$");
			break;
	}
}

function V_isTime(sText)
{
	return V_evaluateRE(sText,"([0-1][0-9]|2[0-3]):[0-5][0-9]");
}

function V_isIP(sText)
{
	return V_evaluateRE(sText,"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
}

function V_isFile(sText,sExtensions)
{
	//extensiones van separadas por |
	//return (V_evaluateRE(sText,"^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[(\\w|\\-) ]*))+\\.(" + sExtensions + ")$") || V_evaluateRE(sText,"^((\\/\\w+)\\$?)?(\\/(\\w[(\\w|\\-) ]*))+\\.(" + sExtensions + ")$"));
	return (V_evaluateRE(sText,"^((\\W)|(\\w))+("+sExtensions+")$"));
}

function V_isCuitCuil(sText)
{
  if ( isInteger(sText) ) {
     if (sText.length<10) {
        return false;
     } else {
        if (sText.length==10) {
          sText=sText.substring(0.1) + '0' + sText.substring(2.9);
        }
        var arrMultipliers="6789456789";
        var iResult=0;
        for (i=0;i<sText.length-1;i++) {
           iResult+=(parseInt(sText.charAt(i))*parseInt(arrMultipliers.charAt(i)));
        }
        var iModulus=(iResult % 11);
        if (iModulus==parseInt(sText.charAt(10))) {
            return true;
        } else {
            return false;
        }
     }
  } else {
      return false;
  }
}

function V_RequiredValidator(sControl,sMessage)
{
   this.type = "required";
   this.control = sControl;
   this.message = sMessage;
}

function V_CommonValidator(sControl,sMessage,sFormat,sSchema,sOther)
{
   this.type = "common";
   this.control = sControl;
   this.message = sMessage;
   this.format = sFormat;
   this.schema = sSchema;
   this.other = sOther;
}

function V_CompareValidator(sControl,sMessage,sComparison,sValue,sDatatype)
{
   this.type = "compare";
   this.control = sControl;
   this.message = sMessage;
   this.comparison = sComparison;
   this.value = sValue;
   this.sdatatype = sDatatype;
}

function V_RegExpValidator(sControl,sMessage,sRegExp)
{
   this.type = "regexp";
   this.control = sControl;
   this.message = sMessage;
   this.regexp = sRegExp;
}

function V_FunctionValidator(sFunction,sMessage)
{
   this.type = "function";
   this.sfunction = sFunction;
   this.message = sMessage;
}

function V_ValidatorGetValue(id)
{
    var control;
    control = V_GetElement(id);
    if (typeof(control.value) == "string")
    {
        return control.value;
    }
    if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number")
    {
        var j;
        for (j=0; j < control.length; j++)
        {
            var inner = control[j];
            if (typeof(inner.value) == "string" && (inner.type != "radio" || inner.status == true))
            {
                return inner.value;
            }
        }
    }
    else 
    {
        return V_ValidatorGetRecursiveValue(control);
    }
    return "";
}

function V_ValidatorGetRecursiveValue(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.status == true))
    {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.children.length; i++)
    {
        val = V_ValidatorGetRecursiveValue(control.children[i]);
        if (val != "") return val;
    }
    return "";
}

function V_ValidateRequired(val)
{
   return (V_trim(V_ValidatorGetValue(val.control)) != "");
}

function V_ValidateCommon(val)
{
    switch (val.format.toLowerCase())
    {
         case "email":
             return (V_isEmail(V_ValidatorGetValue(val.control)));
             break;
         case "number":
             return (V_isNumber(V_ValidatorGetValue(val.control), ((val.schema == "us") ? "." : ",")));
             break;
         case "url":
             return (V_isUrl(V_ValidatorGetValue(val.control)));
             break;
         case "date":
             return (V_isDate(V_ValidatorGetValue(val.control), val.schema));
             break;
         case "time":
             return (V_isTime(V_ValidatorGetValue(val.control), val.schema));
             break;
         case "ip":
             return (V_isIP(V_ValidatorGetValue(val.control)));
             break;
         case "file":
             return (V_isFile(V_ValidatorGetValue(val.control), val.other));
             break;
         case "cuit":
             return (V_isCuitCuil(V_ValidatorGetValue(val.control)));
             break;
         default:
             return (V_isInteger(V_ValidatorGetValue(val.control)));
    }
}

function V_ValidateCompare(val)
{
    if (V_trim(V_ValidatorGetValue(val.control)) == "") return true;

    var sValue;
    var sValue1;
    switch (val.sdatatype.toLowerCase())
    {
        case "string":
           sValue = V_ValidatorGetValue(val.control);
           sValue1 = val.value;
           if (V_trim(sValue) == "") return true;
           break;
        case "float":
           sValue = parseFloat(V_ValidatorGetValue(val.control));
           sValue1 = parseFloat(val.value);
           if (isNaN(sValue) || isNaN(sValue1)) return false;
           break;
        default:
           sValue = parseInt(V_ValidatorGetValue(val.control));
           sValue1 = parseInt(val.value);
           if (isNaN(sValue) || isNaN(sValue1)) return false;
    }
    switch (val.comparison.toLowerCase())
    {
         case "notequal":
            return (sValue != sValue1);
            break;
         case "greaterthan":
            return (sValue > sValue1);
            break;
         case "greaterthanequal":
            return (sValue >= sValue1);
            break;
         case "lowerthan":
            return (sValue < sValue1);
            break;
         case "lowerthanequal":
            return (sValue <= sValue1);
            break;
         case "equal":
            return (sValue == sValue1);
            break;
    }
}

function V_ValidateFunction(val)
{
    var bReturn = true;
    eval("bReturn = " + val.sfunction + ";");
    return bReturn;
}

function V_ValidateRegExp(val)
{
   return V_evaluateRE(V_ValidatorGetValue(val.control), val.regexp);
}

function V_ValidatePage()
{
   if (typeof(ArrayValidators) == "undefined")
       return true;

   var i, val;
   var bValidationOk = true;
   var bReturn = false;
   var sMessage = "";

   for (i=0;i<ArrayValidators.length;i++)
   {
       val = ArrayValidators[i];
       if (typeof(val.type) == "string")
       {
            switch (val.type.toLowerCase())
            {
                 case "required":
                     if (V_GetElement(val.control))
                         bReturn = V_ValidateRequired(val);
                     else
                         bReturn = true;
                     break;
                 case "common":
                     if (V_GetElement(val.control))
                         bReturn = V_ValidateCommon(val);
                     else
                         bReturn = true;
                     break;
                 case "compare":
                     if (V_GetElement(val.control))
                         bReturn = V_ValidateCompare(val);
                     else
                         bReturn = true;
                     break;
                 case "function":
                     bReturn = V_ValidateFunction(val);
                     break;
                 case "regexp":
                     bReturn = V_ValidateRegExp(val);
                     break;
            }
            if (!bReturn)
            {
                 bValidationOk = false;
                 if (sMessage != "") sMessage = sMessage + "\n";
                 sMessage = sMessage + "- " + val.message;
            }

       }
   }

   if (!bValidationOk) alert(sMessage);

   V_ValidPage = bValidationOk;
   return bValidationOk;
}