var vlPageIsValid = true;
var ValidatorsArray = new Array();

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

function vlEvaluateRegEx(sText, sRegEx)
{
	if (vlTrim(sText) == "") return true;

	var rx = new RegExp(sRegEx,"i");
	var matches = rx.exec(sText);
	return (matches != null && sText == matches[0]);
}

function vlIsEmail(sText)
{
	return vlEvaluateRegEx(sText,"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
}

function vlIsInteger(sText)
{
	return vlEvaluateRegEx(sText,"^[0-9]+$");
}

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

function vlIsUrl(sText)
{
	return vlEvaluateRegEx(sText,"(http|https):\\/\\/[\\w]+(\\.[\\w]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");
}

function vlIsDate(sText, sFormat)
{
	/*switch (sFormat)
	{
		case "english":
        case "us":
			return vlEvaluateRegEx(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 vlEvaluateRegEx(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;
	}*/
	if (sText)
	var oDate = new DF_Date(sFormat);
	return (oDate.parsedate(sText) != null);
}

function vlIsTime(sText)
{
	return vlEvaluateRegEx(sText,"^([0-1][0-9]|2[0-3]):[0-5][0-9]$");
}

function vlIsIP(sText)
{
	return vlEvaluateRegEx(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 vlIsFileName(sText, sExtensions)
{
	//extensiones van separadas por |
	return (vlEvaluateRegEx(sText,"^(.+)\\.(" + sExtensions + ")$"));
}

function vlIsName(sText)
{
	return (vlEvaluateRegEx(sText,"^[0-9A-Za-z_]+$"));
}

function vlIsCuit(sText)
{
	if ( vlIsInteger(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 iProduct=0;
			for (i=0;i<sText.length-1;i++)
			{
				iProduct += (parseInt(sText.charAt(i)) * parseInt(arrMultipliers.charAt(i)));
			}
			var iMod = (iProduct % 11);
			if (iMod == parseInt(sText.charAt(10)))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
	  return false;
	}
}

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

function vlCommonValidator(sControlName,sMessage,sFormat,sParameter)
{
   this.type = "common";
   this.control = sControlName;
   this.message = sMessage;
   this.format = sFormat;
   this.parameter = sParameter;
}

function vlCompareValidator(sControlName,sMessage,sComparisson,sValue,sControlToCompare,sDataType,sDateFormat)
{
   this.type = "compare";
   this.control = sControlName;
   this.message = sMessage;
   this.comparisson = sComparisson;
   this.value = sValue;
   this.controltocompare = sControlToCompare;
   this.sdatatype = sDataType;
   this.dateformat = sDateFormat;
}

function vlRegularExpressionValidator(sControlName,sMessage,sRegEx)
{
   this.type = "regex";
   this.control = sControlName;
   this.message = sMessage;
   this.regex = sRegEx;
}

function vlFunctionValidator(sControlName,sMessage,sFunctionName)
{
   this.type = "function";
   this.control = sControlName;
   this.message = sMessage;
   this.functionname = sFunctionName;
}

function vlValidatorGetValue(sId)
{
    var control;
    control = document.getElementById(sId);
    if (control)
    {
		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 vlValidatorGetValueRecursive(control);
		}
    }
    return "";
}

function vlValidatorGetValueRecursive(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 = vlValidatorGetValueRecursive(control.children[i]);
        if (val != "") return val;
    }
    return "";
}

function vlValidateRequired(val)
{
   return (vlTrim(vlValidatorGetValue(val.control)) != "");
}

function vlValidateCommon(val)
{
    if (vlTrim(vlValidatorGetValue(val.control)) == "") return true;
    
    switch (val.format.toLowerCase())
    {
         case "email":
             return (vlIsEmail(vlValidatorGetValue(val.control)));
             break;
         case "number":
             return (vlIsNumber(vlValidatorGetValue(val.control), val.parameter));
             break;
         case "url":
             return (vlIsUrl(vlValidatorGetValue(val.control)));
             break;
         case "date":
             return (vlIsDate(vlValidatorGetValue(val.control), val.parameter));
             break;
         case "time":
             return (vlIsTime(vlValidatorGetValue(val.control)));
             break;
         case "ip":
             return (vlIsIP(vlValidatorGetValue(val.control)));
             break;
         case "filename":
             return (vlIsFileName(vlValidatorGetValue(val.control), val.parameter));
             break;
         case "name":
             return (vlIsName(vlValidatorGetValue(val.control), val.parameter));
             break;
         case "cuit":
             return (vlIsCuit(vlValidatorGetValue(val.control)));
             break;
         default:
             return (vlIsInteger(vlValidatorGetValue(val.control)));
    }
}

function vlValidateCompare(val)
{
    if (vlTrim(vlValidatorGetValue(val.control)) == "") return true;

    var value;
    var value1;
    if (val.controltocompare)
    {
		value1 = vlValidatorGetValue(val.controltocompare);
    }
    else
    {
		value1 = val.value;
    }
    switch (val.sdatatype.toLowerCase())
    {
        case "string":
           value = vlValidatorGetValue(val.control);
           //value1 = val.value;
           if (vlTrim(value) == "") return true;
           break;
        case "float":
           value = parseFloat(vlValidatorGetValue(val.control).replace(/[^\d]/g,"."));
           value1 = parseFloat(value1);
           if (isNaN(value) || isNaN(value1)) return false;
           break;
        case "date":
		   var d = new DF_Date(val.dateformat);
		   value = d.parsedate(vlValidatorGetValue(val.control));
		   value1 = d.parsedate(value1);
           break;
        default:
           value = parseInt(vlValidatorGetValue(val.control));
           value1 = parseInt(value1);
           if (isNaN(value) || isNaN(value1)) return false;
           break;
    }
    switch (val.comparisson.toLowerCase())
    {
         case "notequal":
            return (value != value1);
            break;
         case "greaterthan":
            return (value > value1);
            break;
         case "greaterthanequal":
            return (value >= value1);
            break;
         case "lessthan":
            return (value < value1);
            break;
         case "lessthanequal":
            return (value <= value1);
            break;
         case "equal":
            return (value == value1);
            break;
    }
}

function vlValidateFunction(val)
{
    var bReturn = true;
    eval("if (window." + val.functionname + ") bReturn = " + val.functionname + "(document.getElementById('" + val.control + "')); else bReturn = false;");
    return bReturn;
}

function vlValidateRegEx(val)
{
   return vlEvaluateRegEx(vlValidatorGetValue(val.control), val.regex);
}

function vlValidatePage()
{
   if (typeof(ValidatorsArray) == "undefined") return true;

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

   for (i=0;i<ValidatorsArray.length;i++)
   {
       val = ValidatorsArray[i];
       if (typeof(val.type) == "string")
       {
            switch (val.type.toLowerCase())
            {
                 case "required":
                     if (document.getElementById(val.control))
                         bReturn = vlValidateRequired(val);
                     else
                         bReturn = true;
                     break;
                 case "common":
                     if (document.getElementById(val.control))
                         bReturn = vlValidateCommon(val);
                     else
                         bReturn = true;
                     break;
                 case "compare":
                     if (document.getElementById(val.control))
                         bReturn = vlValidateCompare(val);
                     else
                         bReturn = true;
                     break;
                 case "function":
                     bReturn = vlValidateFunction(val);
                     break;
                 case "regex":
                     bReturn = vlValidateRegEx(val);
                     break;
            }
            if (!bReturn)
            {
                 bValidationOk = false;
                 if (sMessage != "") sMessage = sMessage + "\n";
                 sMessage = sMessage + "-" + val.message;
            }

       }
   }

   if (!bValidationOk) alert(sMessage);

   vlPageIsValid = bValidationOk;
   return bValidationOk;
}
