function Validator_GetValue(id)
{
    var control = document.getElementById(id);
    return (control != null) ? control.value : "";
}

function Validator_GetAttribute(id, attribute)
{
    var control = document.getElementById(id);

    return (control != null) ? control.getAttribute(attribute) : "";
}

function Validator_SetAttribute(id, attribute, value)
{
    var control = document.getElementById(id);

    if (control != null)
    {
        control.setAttribute(attribute, value);
    }
}

function Validator_Trim(s)
{
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function Validator_GetErrorMessage(id)
{
    return Validator_GetAttribute(id, "ErrorMessage");
}

function Validator_SetErrorMessage(id, value)
{
    return Validator_SetAttribute(id, "ErrorMessage", value);
}

function Validator_GetControlValue(ValidatorID)
{
        //get control value
        var CntID = Validator_GetAttribute(ValidatorID, "ControlToValidate");
        return Validator_GetValue(CntID);
}

function Validator_Notify(ValidatorID, Show)
{
    //get notification control
    var NotifyID = Validator_GetAttribute(ValidatorID, "NotifyID");

    if (NotifyID.length > 0)
    {
        //get the notification control
        NotifyControl = document.getElementById(NotifyID);

        //get NotifyTarget
        NotifyTarget = Validator_GetAttribute(ValidatorID, "NotifyTarget");

        //if NotifyTarget is set, we write error message into it
        if (NotifyTarget.length > 0)
        {
                var ErrorMessage = Validator_GetErrorMessage(ValidatorID);
                Validator_SetAttribute(NotifyID, NotifyTarget, ErrorMessage);
        }
        else
        {
                //we change display style
                NotifyControl.style.display = Show ? 'inline' : 'none';
        }
    }
}

function Validator_RequiredFieldValidate(ValidatorID)
{
        //get control value
        var CntValue = Validator_GetControlValue(ValidatorID);

        //get initial value
        var InitialValue = Validator_GetAttribute(ValidatorID, 'InitialValue');

        return (CntValue != InitialValue);
}

function Validator_RegularExpressionValidate(ValidatorID)
{
        //get control value
        var CntValue = Validator_GetControlValue(ValidatorID);

        //get RequiredField value
        RequiredField = Validator_GetAttribute(ValidatorID, 'IsRequiredField');

        //check required field
        if (RequiredField.toLowerCase() == 'false')
        {
               if (CntValue.length == 0)
               {
                        return true;
               }
        }

        //get validation expression
        var ValidationExpression = Validator_GetAttribute(ValidatorID, 'ValidationExpression');

        var rx = new RegExp(ValidationExpression);

        var matches = rx.exec(CntValue);

        return (matches != null && CntValue == matches[0]);
}

function Validator_ConvertInteger(CntValue)
{
        var exp = /^\s*[-\+]?\d+\s*$/;
        if (CntValue.match(exp) == null)
        {
            return null;
        }

        var num = parseInt(CntValue, 10);
        return (isNaN(num) ? null : num );
}

function Validator_IsInteger(CntValue)
{
        return (Validator_ConvertInteger(CntValue) == null ? false : true );
}

function Validator_ConvertDouble(DecimalChar, CntValue)
{
        var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + DecimalChar + "(\\d+))?\\s*$");
        var m = CntValue.match(exp);
        if (m == null)
        {
            return null;
        }

        var i;
        for (i = 0; i < m.length; i++ )
        {
            if (m[i] == null)
            {
                m[i] = "";
            }
        }

        var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];

        var num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num );
}

function Validator_IsDouble(DecimalChar, CntValue)
{
        return (Validator_ConvertDouble( Validator_Trim(DecimalChar), CntValue) == null ? false : true );
}

function Validator_ConvertValidate(ValidatorID)
{
        //get control value
        var CntValue = Validator_GetControlValue(ValidatorID);

        //get RequiredField value
        RequiredField = Validator_GetAttribute(ValidatorID, 'IsRequiredField');

        //check required field
        if (RequiredField.toLowerCase() == 'false')
        {
               if (CntValue.length == 0)
               {
                        return true;
               }
        }

        //get validator data type
        var ValidatorDataType = Validator_GetAttribute(ValidatorID, 'ValidatorDataType');

        switch (ValidatorDataType.toLowerCase())
        {
                case "integer" :
                        return Validator_IsInteger(CntValue);
                break;

                case "double" :
                        //get decimal separator
                        var DecimalSeparator = Validator_GetAttribute(ValidatorID, 'DecimalSeparator');
                        return Validator_IsDouble(DecimalSeparator, CntValue);
                break;

                default :
                        return false;
        }
}

function Validator_Convert(Value, DataType, DecimalChar)
{
        switch (DataType.toLowerCase())
        {
                case "integer" :
                        return Validator_ConvertInteger(Value);

                case "double" :
                        return Validator_ConvertDouble(DecimalChar, Value);

                default :
                        return Value;
        }
}

function Validator_Compare(CntValue, ValueToCompare, Operator, ValidatorDataType, DecimalChar)
{
        //convert operands
        if ((CntValue = Validator_Convert(CntValue, ValidatorDataType, DecimalChar)) == null)
        {
                return false;
        }

        if ((ValueToCompare = Validator_Convert(ValueToCompare, ValidatorDataType, DecimalChar)) == null)
        {
                return false;
        }

        switch (Operator.toLowerCase())
        {
             case "notequal":
             case "!=":
             case "<>":
                 return (CntValue != ValueToCompare);
             break;

             case "greaterthan":
             case ">":
                 return (CntValue > ValueToCompare);
             break;

             case "greaterthanequal":
             case ">=":
                 return (CntValue >= ValueToCompare);
             break;

             case "lessthan":
             case "<":
                 return (CntValue < ValueToCompare);
             break;

             case "lessthanequal":
             case "<=":
                 return (CntValue <= ValueToCompare);
             break;

             case "equal":
             case "=":
             case "==":
                 return (CntValue == ValueToCompare);
             break;

             default:
                 return false;
        }
}


function Validator_CompareValidate(ValidatorID)
{
       var ValueToCompare;

        //get control value
        var CntValue = Validator_GetControlValue(ValidatorID);

        //get RequiredField value
        RequiredField = Validator_GetAttribute(ValidatorID, 'IsRequiredField');

        //check required field
        if (RequiredField.toLowerCase() == 'false')
        {
               if (CntValue.length == 0)
               {
                        return true;
               }
        }

        //get validator data type
        var ValidatorDataType = Validator_GetAttribute(ValidatorID, 'ValidatorDataType');

        //get operator
        var Operator = Validator_GetAttribute(ValidatorID, 'Operator');

        //get decimal char
        var DecimalSeparator = Validator_GetAttribute(ValidatorID, 'DecimalSeparator');

        var ControlToCompare = Validator_GetAttribute(ValidatorID, 'ControlToCompare');
        if (ControlToCompare.length > 0)
        {
               ValueToCompare = Validator_GetValue(ControlToCompare);
        }
        else
        {
                ValueToCompare = Validator_GetAttribute(ValidatorID, 'ValueToCompare');
        }

        return Validator_Compare(CntValue, ValueToCompare, Operator, ValidatorDataType, DecimalSeparator);
}

function Validator_RangeValidate(ValidatorID)
{
        //get control value
        var CntValue = Validator_GetControlValue(ValidatorID);

        //get RequiredField value
        RequiredField = Validator_GetAttribute(ValidatorID, 'IsRequiredField');

        //check required field
        if (RequiredField.toLowerCase() == 'false')
        {
               if (CntValue.length == 0)
               {
                        return true;
               }
        }

        //get maximum and minimum value
        var MaximumValue = Validator_GetAttribute(ValidatorID, 'MaximumValue');
        var MinimumValue = Validator_GetAttribute(ValidatorID, 'MinimumValue');

        //get validator data type
        var ValidatorDataType = Validator_GetAttribute(ValidatorID, 'ValidatorDataType');

        //get decimal char
        var DecimalSeparator = Validator_GetAttribute(ValidatorID, 'DecimalSeparator');

        return Validator_Compare(CntValue, MinimumValue, ">=", ValidatorDataType, DecimalSeparator) && Validator_Compare(CntValue, MaximumValue, "<=", ValidatorDataType, DecimalSeparator);
}