﻿var msgErr="";

function ValidateForm(arrCheckList, objDivId)
{
    i = 0;
    var strValidateString;
    var objVal;
    var strError;
    var arrVal;
    var IsErr = false;
    var retVal = true;
    msgErr="";
    if ( arrCheckList.length > 0)
    {
        for (i=0; i < arrCheckList.length; i++)
        {
            if (arrCheckList[i].length >= 1)
            {
             objVal = arrCheckList[i][0] 
            } 
            if (arrCheckList[i].length >= 2)
            {
             strValidateString = arrCheckList[i][1] 
            } 
            if (arrCheckList[i].length >= 3)
            {
             strError = arrCheckList[i][2] 
            } 
            if (validateInput(strValidateString,objVal,strError) == false)
            {
                stuff_error_msg(strError)
            }
        }
    }
    if (msgErr.length > 0)
    {
        showMessage(objDivId);
        retVal = false;
    }
    else
    {
        clearError(objDivId)
    }
    return retVal; 
}

function stuff_error_msg(strMsg)
{
    var i;
    if (msgErr.length > 0)
    {
        msgErr = msgErr + "<br/>" + strMsg; 
    }
    else
    {
        msgErr = strMsg;
    }
}

function showMessage(objDivId)
{
    document.getElementById(objDivId).innerHTML = "<span class='error_td'>" + msgErr + "</span>";   
}

function clearError(objDivId)
{
    document.getElementById(objDivId).innerHTML = "";   

}

function validateInput(strValidateStr,objValue,strError) 
{ 
    var ret = true;
    var epos = strValidateStr.search(":"); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
		   ret = TestRequiredInput(objValue,strError)
           break;             
         }//case required 
        case "dlrequired": 
         { 
		   ret = TestRequiredSelect(objValue,strError)
           break;             
         }//case required 
        case "filetype":
        {
            ret = filterFileType(objValue,cmdvalue, strError);
           break;             
        }
        case "maxlength": 
        case "maxlen": 
          { 
			 ret = TestMaxLen(objValue,cmdvalue,strError)
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
			 ret = TestMinLen(objValue,cmdvalue,strError)
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
				ret = TestInputType(objValue,"[^A-Za-z0-9]",strError, 
						objValue.name+": Only alpha-numeric characters allowed ");
				break; 
           }
        case "alnum_s": 
        case "alphanumeric_space": 
           { 
				ret = TestInputType(objValue,"[^A-Za-z0-9\\s]",strError, 
						objValue.name+": Only alpha-numeric characters and space allowed ");
				break; 
           }		   
        case "num": 
        case "numeric": 
           { 
                ret = TestInputType(objValue,"[^0-9]",strError, 
						objValue.name+": Only digits allowed ");
                break;               
           }
        case "dec": 
        case "decimal": 
           { 
                ret = TestInputType(objValue,"[^0-9\\.]",strError, 
						objValue.name+": Only numbers allowed ");
                break;               
           }
        case "alphabetic": 
        case "alpha": 
           { 
                ret = TestInputType(objValue,"[^A-Za-z]",strError, 
						objValue.name+": Only alphabetic characters allowed ");
                break; 
           }
        case "alphabetic_space": 
        case "alpha_s": 
           { 
                ret = TestInputType(objValue,"[^A-Za-z\\s]",strError, 
						objValue.name+": Only alphabetic characters and space allowed ");
                break; 
           }
        case "email": 
          { 
			   ret = TestEmail(objValue,strError);
               break; 
          }
        case "date":
        {
            ret = validateDate(objValue);
            break;
        }
        case "lt": 
        case "lessthan": 
         { 
    	      ret = TestLessThan(objValue,cmdvalue,strError);
              break; 
         }
        case "gt": 
        case "greaterthan": 
         { 
			ret = TestGreaterThan(objValue,cmdvalue,strError);
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
			ret = TestRegExp(objValue,cmdvalue,strError);
           break; 
         }
        case "dontselect": 
         { 
			 ret = TestDontSelect(objValue,cmdvalue,strError)
             break; 
         }
		case "dontselectchk":
		{
			ret = TestDontSelectChk(objValue,cmdvalue,strError)
			break;
		}
		case "shouldselchk":
		{
			ret = TestShouldSelectChk(objValue,cmdvalue,strError)
			break;
		}
		case "selone_radio":
		{
			ret = TestSelectOneRadio(objValue,strError);
		    break;
		}
		case "compare":
			ret = TestEqual(objValue,cmdvalue,strError)
			break;
		case "futuredate":
		    ret = CheckForFutureDate(objValue,strError)
		    break; 
		case "custom":
		{
		    break;
		}
    }//switch 
	return ret;
}
