function validate(iDiv)
{
    var theDiv = document.getElementById(iDiv);
    theDiv.innerHTML = "<img src='validated.gif'>";
}

function validateField(iField, iType, iDiv)
{
    var valid = false;
    var theDiv = document.getElementById(iDiv);
    if (iType == "text")
    {
        if (!(iField.value == ''))
            valid = true;
    }
    else if (iType == "date")
    {
         var DayArray =new Array(31,28,31,30,31,30,31,31,30,31,30,31);
         var MonthArray = new Array(01,02,03,04,05,06,07,08,09,10,11,12);
         var thisYear = null;
         var thisMon = null;
         var thisDay = null;
         var today = null;
         var today_day = null;
         var today_month = null;
         var today_year = null;
        
         inpDate = iField.value;
         if (inpDate.length == 0 ) 
             valid = false;
        
         thisDay = inpDate.substr(3,2);
         thisMonth = inpDate.substr(0,2);
         thisYear = inpDate.substr(6,4);
         var filter=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
         if (! filter.test(inpDate))
             valid = false;

         /* Check Valid Month */
         var filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
         if (! filter.test(thisMonth))
             valid = false;
         /* Check For Leap Year */
         N=Number(thisYear);
         if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
         {
            DayArray[1]=29;
         }
        
          /* Check for valid days for month */
          for(var ctr=0; ctr<=11; ctr++)
          {
            if (MonthArray[ctr]==thisMonth)
            {
              if (thisDay<= DayArray[ctr] && thisDay >0 )
                   valid = true;
               else
                   valid = false;
            }
        }
    }

    
    if (valid)
    {
        theDiv.innerHTML = "<img src='validated.gif'>";
        return true;
    }
    else
    {
        theDiv.innerHTML = "<img src='required.gif'>";
        return false;
    }
}





function formatString(s) {
	var begin, end, temp, ret_val;
	var tick;
	
	tick = s.indexOf("'");
	if (tick > -1) {
		begin = s.substring(0,tick+1);
		temp = s.substring(tick + 1, s.length);
		end = formatString(temp);
		ret_val = begin + "'" + end;
	 }
	 else
		ret_val = s;
	return ret_val;
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function findChars (s, bag)

{   var i;

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (!(bag.indexOf(c) == -1)) 
			return true ;
    }

    return false;
}

function isNumber(theElement) { 
	var s = theElement.value;
	var validChars = "0123456789"; 
	var ch; 
	
	for (i=0; i < s.length; i++) { 
		ch = s.charAt(i); 
		if (validChars.indexOf(ch) == -1) { 
			alert("Please fill in a valid number!");
			theElement.focus();
			return false;	
		} 
	}	
	return true; 
} 


function isRadioChecked(theElement, anchor)
{
    for (var i=0; i < theElement.length; i++)
    {
       if (theElement[i].checked)
       {
           return true;
       }
    }
    alert("Please make a selection!");
    location.href="#" + anchor;
    return false;
}

    
    
function getRadioValue(theElement)
{
var rad_val = '';
for (var i=0; i < theElement.length; i++)
   {
   if (theElement[i].checked)
      {
       rad_val = theElement[i].value;
      }
   }
   return rad_val;
}

function get_checkbox_value(theElement)
{
	if(theElement.checked == true) 
		return 'on';
	else
		return 'off';
}



function isCheckboxNull(theElement)
{
	checked = theElement.checked;
	
	if (checked == false) {
		return true;	
	}
	else 
		return false;
}


/*  isNull(element, name) */

function isNull(theElement, name)
{	
	value = theElement.value;
	if (value == '') {
		alert("Please fill in the " + name + " field!");
		theElement.focus();
                //theElement.select();
		return true;
	}
	else
		return false;
}  



/* Test function for displaying a parameter      */
/* value in an alert box                         */
function msg(input) {
	alert('Value of parameter is '+ input);
}


function isTime(theElement)
{
 inpTime = theElement.value;
  if (inpTime.length == 0 ) 
  {
	alert("Please enter a time!");
	theElement.focus();
	theElement.select();
	return false;
  }
  var filter=/^[0-2][0-9]:[0-5][0-9]$/;
  if (! filter.test(inpTime))
  { 
  	 alert("Please enter Time in HH:MM Format!");
     theElement.focus();
     theElement.select();
     return false;
  }
  if (inpTime.substr(1,1) == ":")
  {
	if (inpTime.substr(0,1) == "0")
	{
  	 alert("Hour is less than 1, please correct.");
     theElement.focus();
     theElement.select();
     return false;
	}	
  }
  else
  {
	if (inpTime.substr(0,2) > 23)
	{
  	 alert("Hour is greater than 23, please correct.");
     theElement.focus();
     theElement.select();
     return false;
	}	
  }
  return true;
}
	




/* Check for Valid Date                          */
function isDate(theElement)
{
 var DayArray =new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 var MonthArray = new Array(01,02,03,04,05,06,07,08,09,10,11,12);
 var thisYear = null;
 var thisMon = null;
 var thisDay = null;
 var today = null;
 var today_day = null;
 var today_month = null;
 var today_year = null;

 inpDate = theElement.value;
  if (inpDate.length == 0 ) 
  {
	alert("Please enter a date!");
	theElement.focus();
	theElement.select();
	return false;
   }


  thisDay = inpDate.substr(3,2);
  thisMonth = inpDate.substr(0,2);
  thisYear = inpDate.substr(6,4);
 var filter=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  if (! filter.test(inpDate))
  { 
     alert("Please enter Date in MM/DD/YYYY Format!");
     theElement.focus();
     theElement.select();
     return false;
  }
  /* Check Valid Month */
  var filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
  if (! filter.test(thisMonth))
  {  
     alert("Please enter the Correct Month  (MM/DD/YYYY)!");
     theElement.focus();
     theElement.select();
     return false;
  }
  /* Check For Leap Year */
  N=Number(thisYear);
  if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
  {
    DayArray[1]=29;
  }

  /* Check for valid days for month */
  for(var ctr=0; ctr<=11; ctr++)
  {
   if (MonthArray[ctr]==thisMonth)
   {
      if (thisDay<= DayArray[ctr] && thisDay >0 )
           return true;
       else
       {  
           alert("Please enter a valid Day !");
           theElement.focus();
           theElement.select();
           return false;
       }
    }
   }
}


/* Check for Valid Date                          */
function isDateCanBeNull(theElement)
{
 var DayArray =new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 var MonthArray = new Array(01,02,03,04,05,06,07,08,09,10,11,12);
 var thisYear = null;
 var thisMon = null;
 var thisDay = null;
 var today = null;
 var today_day = null;
 var today_month = null;
 var today_year = null;

 inpDate = theElement.value;
  if (inpDate.length == 0 ) 
  {
	return true;
   }

  thisDay = inpDate.substr(3,2);
  thisMonth = inpDate.substr(0,2);
  thisYear = inpDate.substr(6,4);
 var filter=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  if (! filter.test(inpDate))
  { 
  	 alert("Please enter Date in MM/DD/YYYY Format!");
     theElement.focus();
     theElement.select();
     return false;
  }
  /* Check Valid Month */
  var filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
  if (! filter.test(thisMonth))
  {  
     alert("Please enter the Correct Month  (MM/DD/YYYY)!");
     theElement.focus();
     theElement.select();
     return false;
  }
  /* Check For Leap Year */
  N=Number(thisYear);
  if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
  {
    DayArray[1]=29;
  }

  /* Check for valid days for month */
  for(var ctr=0; ctr<=11; ctr++)
  {
   if (MonthArray[ctr]==thisMonth)
   {
      if (thisDay<= DayArray[ctr] && thisDay >0 )
           return true;
       else
       {  
           alert("Please enter a valid Day !");
           theElement.focus();
           theElement.select();
           return false;
       }
    }
   }
}


