function change(color){
	var el=event.srcElement
	if ((el.tagName=="INPUT"&&el.type=="submit") || 
		(el.tagName=="INPUT"&&el.type=="reset") ||
		(el.tagName=="INPUT"&&el.type=="button") )
	event.srcElement.style.backgroundColor=color
}
/*
    A generalized function that accepts a form name as a parameter
    and checks for mandatory field values and also whether the input fields 
    have valid values for 
            numeric ,
            alphabetic ,
            alphanumeric ,
            phone ,
            ssn ,
            date,
            time    fields.
            
    To use this Javascript the HTML that uses this js must have input tags defined as 
    
   : <input type='text' name='text0' 
                [value='value0'] 
                [mandatory='true/false'] 
                [date='true/false'] 
                [time='true/false'] 
                [alphabetic='true/false'] 
                [alphanumeric='true/false'] 
                [numeric='true/false']
                [phone='true/false']
                [ssn='true/false']
                [displayname='displayname0'] >
                
    if the optional fields are not defined (eg., if date='true' is not a part of input tag) 
    it is the same as giving its value as 'false'(eg., date='false') in both: the cases the 
    field is not checked for.           
*/


function validatePage()
{
   var pformname = document.forms[0].getAttribute("name");
   return validateForm(pformname);
}

function validateForm(pformname)
{
    var formElements = document.getElementsByTagName("INPUT");


    
    var valid=true;
    
    for(i=0;i<formElements.length;i++)
    {

        var formname = null;
        if(formElements[i].form != null) 
            formname = formElements[i].form.getAttribute("name");
        
        
        if((formname!=null)&&(formname==pformname))
        {

            var allattributes = formElements[i];
        
           
            var tagname=allattributes.getAttribute("name");
            var tagtype=allattributes.getAttribute("type");
            var numericitem = allattributes.getAttribute("numeric");
            if(numericitem != null)
            {
            	if(numericitem != "true" && numericitem != "false")
            	{
            		numericitem == null;
            	}
            }
            var alphabeticitem = allattributes.getAttribute("alphabetic");
            if(alphabeticitem != null)
            {
            	if(alphabeticitem != "true" && alphabeticitem != "false")
            	{
            		alphabeticitem == null;
            	}
            }
            var alphanumitem = allattributes.getAttribute("alphanumeric");
            if(alphanumitem != null)
            {
            	if(alphanumitem != "true" && alphanumitem != "false")
            	{
            		alphanumitem == null;
            	}
            }
            var dateitem = allattributes.getAttribute("date");
            if(dateitem != null)
            {
            	if(dateitem != "true" && dateitem != "false")
            	{
            		dateitem == null;
            	}
            }
            var timeitem = allattributes.getAttribute("time");
            if(timeitem != null)
            {
                if(timeitem != "true" && timeitem != "false")
                {
                    timeitem == null;
                }
            }
            var phoneitem = allattributes.getAttribute("phone");
            if(phoneitem != null)
            {
            	if(phoneitem != "true" && phoneitem != "false")
            	{
            		phoneitem == null;
            	}
            }
			var emailitem = allattributes.getAttribute("email");
            if(emailitem != null)
            {
            	if(emailitem != "true" && emailitem != "false")
            	{
            		emailitem == null;
            	}
            }
            var ssnitem = allattributes.getAttribute("ssn");
            if(ssnitem != null)
            {
            	if(ssnitem != "true" && ssnitem != "false")
            	{
            		ssnitem == null;
            	}
            }
            var zipcodeitem = allattributes.getAttribute("zipcode");
            if(zipcodeitem != null)
            {
                if(zipcodeitem != "true" && zipcodeitem != "false")
                {
                    zipcodeitem == null;
                }
            }
            var valueitem = allattributes.getAttribute("value");
            var dispnameitem = allattributes.getAttribute("displayname");
            if(dispnameitem == null)
            {
            	dispnameitem = "Field";
            }
            var maxlengthitem = allattributes.getAttribute("maxlength");
			var reqlengthitem = allattributes.getAttribute("reqlength");
			var mandatoryitem = allattributes.getAttribute("mandatory");
            if(mandatoryitem != null)
            {
            	if(mandatoryitem != "true" && mandatoryitem != "false")
            	{
            		mandatoryitem == null;
            	}
            }

            if(tagtype.toUpperCase() == "TEXT" || tagtype.toUpperCase() == "PASSWORD")
            {
                if(mandatoryitem == "true")
                {
                    if(isNull(valueitem))
                    {
                        alert(dispnameitem+" cannot be Empty");
                        valid=false;
                        formElements[i].focus();
                        break;
                    }
				if(reqlengthitem != null){	
						if(valueitem.length < reqlengthitem)   
						{
						 	alert(dispnameitem+" must be more than or equal to "+reqlengthitem+" characters");
	                        valid=false;
	                        formElements[i].focus();
	                        break;
						}
	                }
    			}
                if(!isNull(valueitem))
                {
                    if(maxlengthitem != null)
		   			 {
                        if(valueitem.length > maxlengthitem)
	    	        		{
                            alert(dispnameitem+" must be lesser than or equal to "+maxlengthitem+" characters");
                            valid=false;
                            formElements[i].select();
                            break;
	        				}
		    			}	        	        
	                
                    if(numericitem == "true")
                    {
                        if(isNaN(valueitem))
                        {
                            alert(dispnameitem+" must be Numeric");
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }

                    if(alphabeticitem == "true")
                    {
                        if(!isAlphabetic(valueitem))
                        {
                            alert("Invalid "+dispnameitem);
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }
    
                    if(alphanumitem == "true")
                    {
                        if(!isAlphaNumeric(valueitem))
                        {
                            alert("Invalid "+dispnameitem);
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }
                    
                    if(dateitem == "true") 
                    {
                        if(!checkDate(valueitem))
                        {
                            alert(dispnameitem+' should be a Valid Date');
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }
                    
                    if(timeitem == "true") 
                    {
                        if(!isValidTime(valueitem))
                        {
                            alert(dispnameitem+' should be a Valid Time');
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }
                    if(phoneitem == "true")
                    {
                        if(!isValidPhone(valueitem))
                        {
                            alert(dispnameitem+" should be a Valid Phone Number");
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                     }
					 if( emailitem == "true")
                    {
                        if(!isEmail(valueitem))
                        {
                            alert(dispnameitem+" should be a Valid Email");
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                     }
                    
                    if(ssnitem == "true") 
                    {   
                        if(!isValidSSN(valueitem))
                        {
                            alert(dispnameitem+" should be a Valid SSN (XXX-XX-XXXX)");
                            valid=false;
                            formElements[i].select();
                            break;
                        }
                    }                   
                    if(zipcodeitem == "true")
                    {
                        if(!isValidZipCode(valueitem))
		       			 {
			   				alert(dispnameitem+" should be a 5 Digit Numeric Value");
			   				valid=false;
			    			formElements[i].select();
			   				break;
                        }
                    
                    }
                    valueitem = isCharNumber(valueitem);	
					allattributes.setAttribute("value",valueitem);
	            }
            }
        }            
    }
   
    if(valid)
    {
	    var formElements = document.getElementsByTagName("TEXTAREA");
	    for(i=0;i<formElements.length;i++)
	    {
	        var formname = null;
	        
	        if( formElements[i].form != null) 
	            formname = formElements[i].form.getAttribute("name");
	            
	        if((formname!=null)&&(formname==pformname))
	        {
	            	
	            var allattributes = formElements[i];
	            var tarealength=allattributes.getAttribute("maxlength");
	            var dispnameitem = allattributes.getAttribute("displayname");
                var valueitem = allattributes.getAttribute("value");
	            var mandatoryitem = allattributes.getAttribute("mandatory");
		        if(mandatoryitem != null)
		        {
		        	if(mandatoryitem != "true" && mandatoryitem != "false")
		        	{
		        		mandatoryitem == null;
		        	}
		        }
 	
	            if(dispnameitem == null)
	            {
	            	dispnameitem = "Field";
	            }
	
	            if(mandatoryitem == "true")
	            {
	            	if(isNull(valueitem))
	            	{
					alert(dispnameitem+" cannot be Empty");
					valid=false;
					formElements[i].focus();
					break;
	               	}
	            }
	            
	            if(!isNull(valueitem))
	            {
	            	if((tarealength != null)&&(valueitem.length > tarealength))
	            	{
					alert(dispnameitem+" must be lesser than or equals to "+tarealength+" characters");
					valid=false;
					formElements[i].focus();
					break;
	            	}
	            	else
	            	{
				valueitem = isCharNumber(valueitem);	
				allattributes.setAttribute("value",valueitem);
	            	}
	            }
	       }            
	   }   
   }
   if(valid)
    {
	    var formElements = document.getElementsByTagName("SELECT");
	    for(i=0;i<formElements.length;i++)
	    {
	        var formname = null;
	        
	        if( formElements[i].form != null) 
	            formname = formElements[i].form.getAttribute("name");
	            
	        if((formname!=null)&&(formname==pformname))
	        {
	            	
	            var allattributes = formElements[i];
	            var dispnameitem = allattributes.getAttribute("displayname");
               
			    if ((allattributes.selectedIndex) > 0 ) 
				{
					 var valueitem = allattributes.selectedIndex;
				}
				else 
				{
					var valueitem = 0;
				}	
			  	 
	            var mandatoryitem = allattributes.getAttribute("mandatory");
		        
								
				if(mandatoryitem != null)
		        {
		        	if(mandatoryitem != "true" && mandatoryitem != "false")
		        	{
		        		mandatoryitem == null;
		        	}
		        }
 	
	            if(dispnameitem == null)
	            {
	            	dispnameitem = "Field";
	            }
	
	            if(mandatoryitem == "true")
	            {
	            	if(valueitem == 0)
	            	{
					alert(dispnameitem+" must be selected");
					valid=false;
					formElements[i].focus();
					break;
	               	}
	            }
	      }            
	   }   
   }

   return valid;
}

/*
    Checks whether the value passed is alpha Numeric, i.e., it 
    contains alphabets, numbers and spaces only as part of it.
*/

function isAlphaNumeric(val) 
{
  var values = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,/\' ";
  for (var i=0; i < val.length; i++) 
    if (values.indexOf(val.charAt(i)) < 0) 
        return false;
  return true;
}

/*
    Checks whether the value passed is alphabetic, i.e., it 
    contains alphabets and/or spaces only as part of it.
*/

function isAlphabetic(val) 
{
  var values = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-' ";
  for (var i=0; i < val.length; i++) 
    if (values.indexOf(val.charAt(i)) < 0) 
        return false;
  return true;
}

/*
    Checks whether the value passed is not null and 
    has atleast one character other than space.
*/

function isNull(inputValue)
{
    if((inputValue==null)||trim(inputValue)=='')
        return true;
    else
        return false;
}

/*
    Checks whether the value passed has atleast 
    one character other than space.
*/

function trim(strText) { 
   while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
   while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
   return strText;
} 

/*
    Checks whether phone number is valid.
    phone is allowed to have numeric data 
    and '-' as part of its value
*/

function isValidPhone(val)
{
  var values = "1234567890-()";
  for (var i=0; i < val.length; i++) 
    if (values.indexOf(val.charAt(i)) < 0) 
       return false;
  return true;
}


/* isEmail (STRING s [, BOOLEAN emptyOK])

	Email address must be of form a@b.c -- in other words:
	there must be at least one character before the @
	there must be at least one character before and after the .
	the characters @ and . are both required
*/
function isEmail (s)
{   
     // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


/*
    Checks whether SSN is valid.
    SSN is allowed to have numeric data 
    and '-' as part of its value and is only of type
    	XXX-XX-XXXX
*/

function isValidSSN(val)
{
 var values = "1234567890";
  if(val.length != 11)
  {
	return false;
  }
  else
  {
  	if((val.charAt(3)!='-') || 
  	   (val.charAt(6)!='-')) 
  	{
  	  		return false;
  	}
 	else
  	{	
		for (var i=0; i < 3; i++) 
			if (values.indexOf(val.charAt(i)) < 0) 
	       		return false;
		for (var i=4; i < 6; i++) 
			if (values.indexOf(val.charAt(i)) < 0) 
	       		return false;
		for (var i=7; i < 12; i++) 
			if (values.indexOf(val.charAt(i)) < 0) 
	       		return false;
	}
    return true;
   }
}

/*
    Checks whether ZipCode is valid.
    ZipCode is allowed to have 5 Digit Numeric data 
    
*/
function isValidZipCode(val) {
   if(isNaN(val) || val.length < 5) {
   	return false;
   }
   else {
   	return true;
   }
}

/*
    Checks whether Time is valid.
    Time is allowed to have numeric data 
    and ':' as part of its value and is only of type
        HH:MM
*/

function isValidTime(val)
{
  if(val.length != 5)
  {
    return false;
  }
  else
  {
    if(val.charAt(2)!=':')
    {
            return false;
    }
    else
    {   
    	var hour=val.substr(0,2);
    	var min=val.substr(3,2);
    	
    	if(!validHour(hour))
    	{
    		return false;
    	}
    
    	if(!validMinute(min))
    	{
    		return false;
    	}
    }
    return true;
   }
}

/*
	Checks whether the input is a valid Hour
*/

function validHour(hour){
    if(hour != "" && hour > 0 && hour < 13) {
        return true;
    }
    else {
        return false;
    }
}

/*
	Checks whether the input is a valid Minute
*/

function validMinute(minute){
    if(minute != "" && minute >= 0 && minute < 60) {
        return true;
    }
    else {
        return false;
    }

}

/**********************************************************************/
/*Function name :isDigit(theDigit) */
/*Usage of this function :test for an digit */
/*Input parameter required:thedata=string for test whether is digit */
/*Return value :if is digit,return true */
/* else return false */
/**********************************************************************/
function isDigit(theDigit)
{
    var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;

    for (j = 0; j < digitArray.length; j++)
    {
        if (theDigit == digitArray[j])
            return true
    }
    return false
}

/*************************************************************************/
/*Function name :isPositiveInteger(theString) */
/*Usage of this function :test for an +ve integer */
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value :if is +ve integer,return true */
/* else return false */
/*function require :isDigit */
/*************************************************************************/

function isPositiveInteger(theString)
{
    var theData = new String(theString)

    if (!isDigit(theData.charAt(0)))
        if (!(theData.charAt(0)== '+'))
            return false
    
    for (var i = 1; i < theData.length; i++)
        if (!isDigit(theData.charAt(i)))
            return false
    return true
}

/**********************************************************************/
/*Function name :isDate(s,f) */
/*Usage of this function :To check s is a valid format */
/*Input parameter required:s=input string */
/* f=input string format */
/* =1,in mm/dd/yyyy format */
/* else in dd/mm/yyyy */
/*Return value :if is a valid date return 1 */
/* else return 0 */
/*Function required :isPositiveInteger() */
/**********************************************************************/
function isDate(s,f)
{
    var a1=s.split("/");
    var a2=s.split("-");
    var e=true;
    if ((a1.length!=3) && (a2.length!=3))
    {
        e=false;
    }
    else
    {
        if (a1.length==3)
            var na=a1;
        if (a2.length==3)
            var na=a2;
        if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
        { 
            if (f==1)
            {
                var d=na[1],m=na[0];
            }
            else
            {
                var d=na[0],m=na[1];
            }
            var y=na[2];
            if ((e) && (y<1000))
                e=false
            if (e)
            {
                v=new Date(m+"/"+d+"/"+y);
                if (v.getMonth()!=m-1)
                    e=false;
            }
        }
        else
        {
            e=false;
        }
    }
    return e
}

function checkDate(v)
{
    if(isDate(v,0))     //2nd Parameter -> 1 for mm/dd/yyyy and 0 for dd/mm/yyyy
        return true;
    else
        return false;
}

function isCharNumber(val) 
{
  var digits = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  var strnew="";
  for (var i=0; i < val.length; i++) 
  {
    if (digits.indexOf(val.charAt(i)) < 0) 
    { 
	strnew += replace(val.charAt(i));
    }
    else
    {
	strnew += val.charAt(i);
    }
  }
  return strnew;
}


function replace(string1) 
{
// Replaces text with by in string
    array1 = new Array(32);
    array2 = new Array(32);
    array1 = ["'"];
    array2 = ["''"];
    for(j=0;j<34;j++) 
    {
        if(string1 == array1[j])
        {
            string1 = array2[j];
        }
    }
    return string1;
}


