﻿var errors = new Array();
var errorFields = new Array();
var fields = new Array();

function ValidateData(validateCommand, obj, fieldName)
{
    var equals = validateCommand.search("=");
    var command = "";
    var commandValue = "";
    var testError = true;
    var multiple = obj.search(",");
    var objID = "";
    var objID2 = "";
    var objValue ="";
    
    if (equals >=0)
    {
        command = validateCommand.substring(0,equals);
        commandValue = validateCommand.substr(equals + 1);
    }
    else
    {
        command = validateCommand;
    }
    
    if (multiple >=0)
    {
        objID = obj.substring(0,multiple);
        objID2 = obj.substr(multiple + 1);
        
        objValue = document.getElementById(objID);   
    }
    else
    {
        objID = obj;
        objValue = document.getElementById(objID);    
    }
    
    
    objValue.value = leftTrim(objValue.value);
    objValue.value = rightTrim(objValue.value);
    
    switch(command)
    {
        case "REQ":
        case "REQUIRED":
        {
            if (eval(objValue.value.length) == 0)
            {
                errors.push(fieldName + " : " + required);
                testError = false;
            }
            break;
        }
        case "CONREQ":
        case "CONDREQUIRED":
        {
            if (eval(document.getElementById(objID).value.length) > 0)
                if (eval(document.getElementById(objID2).value.length) == 0)
                {
                    errors.push(fieldName + " : " + required);
                    testError = false;
                }
            break;
        }
        case "LEN":
        case "LENGTH":
        {
            if (objValue.value.length != 0)
            {
                if (eval(objValue.value.length) != eval(commandValue))
                {
                    errors.push(fieldName + " : " + length + commandValue);
                    testError = false;
                }
            }
            break;
        }
        case "CONEML":
        case "CONDEMAIL":
        {
            if (eval(document.getElementById(objID).value.length) > 0 || eval(document.getElementById(objID2).value.length) > 0)
                if (!validateEmail(document.getElementById(objID2).value))
                {
                    errors.push(fieldName + " : " + condemail);
                    testError = false;
                }
            break;
        }
        case "NUM":
        case "NUMERIC":
        {
            if (objValue.value.search("[^0-9]") >= 0)
            {
                errors.push(fieldName + " : " + numeric);
                testError = false;
            }
            break;
        }
        case "ALNUM":
        case "ALPHANUMERIC":
        {
            if (objValue.value.search("[^A-Za-z0-9]") >= 0)
            {
                errors.push(fieldName + " : " + alphanumeric);
                testError = false;
            }
            break;
        }
        case "MAXLEN":
        case "MAXLENGTH":
        {
            if (eval(objValue.value.length) > eval(commandValue))
            {
                errors.push(fieldName + " : " + maxlength + commandValue);
                testError = false;
            }
            break;
        }
        case "MINLEN":
        case "MINLENGTH":
        {
            if (eval(objValue.value.length) < eval(commandValue))
            {
                errors.push(fieldName + " : " + minlength + commandValue);
                testError = false;
            }
            break;
        }
        case "EML":
        case "EMAIL":
        {
            if (!validateEmail(objValue.value))
            {
                errors.push(fieldName + " : " + email);
                testError = false;
            }
            break;
        }
        case "DT":
        case "DATE":
        {
            break;                
        }
        case "ALPHA":
        case "ALPHABETIC":
        {
            if (objValue.value.search("[^A-Za-z]") >= 0)
            {
                errors.push(fieldName + " : " + alphabetic);
                testError = false;
            }
            break;
        }
        case "LT":
        case "LESSTHAN":
        {
            if (isNaN(objValue.value))
            {
                errors.push(fieldName + " : " + numeric);
                testError = false;
            }
            else
            {
                if (eval(objValue.value) >= eval(commandValue))
                {
                    errors.push(fieldName + " : " + lessthan + commandValue);
                    testError = false;
                }
            }
            break;
        }
        case "GT":
        case "GREATERTHAN":
        {
            if (isNaN(objValue.value))
            {
                errors.push(fieldName + " : " + numeric);
                testError = false;
            }
            else
            {
                if (eval(objValue.value) <= eval(commandValue))
                {
                    errors.push(fieldName + " : " + greaterthan + commandValue);
                    testError = false;
                }
            }
            break;
        }
        case "REGEXP":
        {
            if (objValue.value.length > 0)
            {
                if (!objValue.value.match(cmdValue))
                {
                   errors.push(fieldName + " : " + regexp);
                   testError = false 
                }
            }
            break;
        }
        case "COMP":
        case "COMPARE":
        {
            if (!(document.getElementById(objID).value === document.getElementById(objID2).value))
            {
                errors.push(fieldName + " : " + compare);
                testError = false;
            }
            break;
        }
        case "MY":
        case "MONEY":
        {
            if (checkNumeric(document.getElementById(objID),",","."))
            {
                errors.push(fieldName + " : " + money);
                testError = false;
            }
            break;
        }
        case "URL":
        {
            if (!isUrl(objValue.value))
            {
                errors.push(fieldName + " : " + url);
                testError = false;
            }
            break;
        }
        case "CHECKED":
        {
            if (!(document.getElementById(objID).checked))
            {
                errors.push(fieldName + " : " + agree);
                testError = false;
            }
            break;
        }
    }
    
    if (!testError)
    {
        errorFields.push(objID);
    }
    
    fields.push(objID);
}

function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function rightTrim(sString) 
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}



function validateEmail(strValue) {
    var objRegExp  = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(([a-zA-Z]{2,4}))$/;
    //check for valid email
    return objRegExp.test(strValue);
}

function BuildErrorDiv(divName)
{
    var errorDiv = document.getElementById(divName);
        
    if (errors.length > 0)
    {
        errorDiv.style.display = "block";
        html = "<span><strong>We are sorry, but we are unable to process your request for the following reasons:</strong></span><ul>";
        for (i = 0; i < errors.length; i++)
        {
            if(errors[i] != "")
                html += "<li>" + errors[i] + "</li>";
        }
        html += "</ul>";
        errorDiv.innerHTML = html;
        
        return false;
    }
    
    return true;
}

function BuildAlertWindow()
{        
    if (errors.length > 0)
    {
        var errorMsg = "We are sorry, but we are unable to process your request for the following reasons: \n\n";
        for (i = 0; i < errors.length; i++)
        {
            if(errors[i] != "")
                errorMsg+= errors[i] + "\n";
        }        
        alert(errorMsg);
        return false;
    }
    return true;
}

function ShowErrorSigns()
{
    for (i=0;i<fields.length;i++)
    {
        if (document.getElementById("err_" + fields[i]))
            document.getElementById("err_" + fields[i]).style.display = "none";
    }
    
    for(i = 0; i < errorFields.length; i++)
    {
        if (document.getElementById("err_" + errorFields[i]))
        {
            document.getElementById("err_" + errorFields[i]).style.display = "block";
            
            //document.getElementById("img_" + errorFields[i]).alt = errors[i];
            //document.getElementById("img_" + errorFields[i]).title = errors[i];
        }
    }
}

function checkNumeric(objName,comma,period)
{
    var checkOK = "0123456789" + comma + period;
    var checkStr = objName;
    
    var allValid = true;
    var decPoints = 0;
    var allNum = "";
    
    for (i = 0;  i < checkStr.value.length;  i++)
    {
        ch = checkStr.value.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
        if (ch != ",")
           allNum += ch;
    }
    if (!allValid)
    {	
        return (false);
    }
}

function isUrl(val) {
    if (val == "")
        return true;
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(val);
}
