﻿function $(Id){return document.getElementById(Id);}
function $N(Name){return document.getElementsByName(Name);}

 function ValidateInput(Input,Exp,Msg,isMandatory)
 {
        if(Input==null){return true;}
	        
	        
    	    
	        
            if(Input.value=="")
            {
                if(isMandatory)//When Mandatory Field is blank
                {
                    MsgBox("Please enter "+Msg);
                    SetInputMsg(Input,"Please enter "+Msg,"#FFEEFF");
                    Input.focus();
                    return false;
                }
                else//When Non Mandatory Field is blank
                {
                    return true;
                }
            }
                
             
	        
    	    var x=validRExpression(Input.value,Exp);
            if(x)
            {
                    SetInputMsg(Input,"","#FFFFFF");
                    return true;
            }
            else
            {	    	   
	                MsgBox("Please enter valid "+Msg);
                    SetInputMsg(Input,"Please enter valid "+Msg,"#FFEEFF");
	    	        Input.focus();
	                return false;
            }
    	    
	  
	  }
	  
 function ValidateDropdown(theField,fieldName)
{
 
    if(theField.selectedIndex==0)
    {
        MsgBox("Please select "+fieldName);
        SetInputMsg(theField,"Please select "+fieldName,"#FFEEFF");
        return false;
    }
    SetInputMsg(theField,"","#FFFFFF");
    return true;
}



function  ValidateRadio(Elems,Msg)
{
    
    var selected=false;
    for(var i=0;i<Elems.length;i++)
    {
        if(!Elems[i].disabled)
        {
            if(Elems[i].checked){selected=true;i=Elems.length+1;}
        }
    }
    //Notworking to do...
    if(!selected){MsgBox("Please select one of the "+Msg+".");SetInputMsg(Elems,"Please select one of the "+Msg,"#FFEEFF");}
    
    SetInputMsg(Elems,"","#FFFFFF");
    return selected;
     
 }
     
 function ValidateChkLst(Elems, Msg)
{
     
   var selected=false;
    for(var i=0;i<Elems.length;i++)
    {
        if(!Elems[i].disabled)
        {
            if(Elems[i].checked){selected=true;i=Elems.length+1;}
        }
    }
    //Notworking to do...
    if(!selected){MsgBox(Msg);SetInputMsg(Elems,Msg,"#FFEEFF");}
    
    SetInputMsg(Elems,"","#FFFFFF");
    return selected;
}





     
 function ValidateChkBox(Elem, Msg)
{
     
   var selected=false;
    
        if(!Elem.disabled)
        {
            if(Elem.checked){selected=true;}
        }
    
    //Notworking to do...
    if(!selected){MsgBox(Msg);SetInputMsg(Elem,Msg,"#FFEEFF");}
    
    SetInputMsg(Elem,"","#FFFFFF");
    return selected;
}





//Private Support Methods
function MsgBox(Msg)
{
alert(Msg);
//Message Box to be Designed.
}
function SetInputMsg(Input,Msg,color)  
{
    try
    {
        var CreateId=Input.id+"_"+"MsgTxt";
        var Elem=CreateSpan("ver13pxredbld",Msg);
        AppendChild(Input,Elem,CreateId); Input.style.background=color;      
     }
     catch(e){}
}
function CreateSpan(CSSClass,cont)
{
                            var Elem = document.createElement("span");
                            Elem.className=CSSClass;
                            Elem.appendChild(document.createElement("br"));
                            Elem.appendChild(document.createTextNode(cont));
                            return Elem;
}
function AppendChild(Input,Elem,Id)
{
                         
        var CElem=document.getElementById(Id);
        if(CElem==null)
        {
           Elem.setAttribute("id",Id);
           AppendControl(Input,Elem);  
                                          
        }
        else
        {
              CElem.innerHTML=Elem.innerHTML;
        
        }


}
function  AppendControl(Input,Elem)
{      
    if(Input.parentNode!=null){Input.parentNode.appendChild(Elem);}
    
}
  

function validRExpression(Value,Exp)
{
    //Declare regular expression
     var RegEx = new RegExp(Exp);
     //Match Value to Regular Expression
     //Match method returns a boolean value
      if (Value.match(RegEx))
      {
        return true;
      }
      else
      {
        
        return false;
      }

}



