function errorCheck(frm, confirmMsg) 
{
  var fields = document.getElementsByTagName('label')
  
  formValid = true;
  
  for (i=0;i<fields.length;i++)
  {
    thisField = fields[i]
    if (thisField.title == "Error Message" &&  thisField.form == frm )
    {
		
      //Do error check based on this input's attributes
      thisField.style.display = 'none';
      
      tmpValid = true;
      
      var arr = thisField.id.split(",");
      //for (j=0;i<arr.length;i++)
      //{
      //}
      tmpField = arr[0];
      
      //Only do error checking if the input field is not hidden and not disabled
      
      
          //Not multiple: normal check
          if (tmpField.indexOf("|") == -1)
          {            
            
            tmpInput = document.getElementById(tmpField)
            
            
            
            
            if (tmpInput.type == "checkbox")
            {
              tmpValue = '';
              tmpName = tmpInput.name;
              
              tmpChecks = document.getElementsByName(tmpName);
              
              for (j=0;j<tmpChecks.length;j++)
              {
                if (tmpChecks[j].checked)
                {
                  if (tmpValue.length > 0)
                    tmpValue += ", ";
                  tmpValue += tmpChecks[j].value;
                }
              }
            }
            else
              tmpValue = document.getElementById(tmpField).value;
            
           
            
            if  (tmpInput.style.display != 'none' && tmpInput.disabled != true)
            {
                  
                    //Required Check
                    if (tmpValid)
                    {
                      if (arr[1] == "true")
                        tmpValid = chkRequired(tmpValue)
                    }
                    
                    //Field Length Check
                    if (tmpValid)
                    {
                      tmpValid = chkLength(tmpValue,arr[2],arr[3]);
                    }
                    //Type: Numeric
                    if (tmpValid && arr.length>4)
                    {
                      if (arr[4]=="true")
                        tmpValid = chkNumeric(tmpValue)
                    }
                    
                    //Type: Email
                    if (tmpValid && arr.length>5)
                    {
                      if (arr[5]=="true")
                        tmpValid = chkEmail(tmpValue)
                    }
            
            
                if (!tmpValid)
                {
                thisField.style.display = 'inline';
                tmpInput.style.backgroundColor = '#FFCC99';
				if (tmpInput.type != 'textarea')
					tmpInput.style.backgroundImage = 'url(\'/images/inputbg_red.gif\')';
                }
                else
				{
                  tmpInput.style.backgroundColor = '#C2D3C0';
				  if (tmpInput.type != 'textarea')
					  tmpInput.style.backgroundImage = 'url(\'/images/buttonbg_green.gif\')';
				}
                
                if (!tmpValid)
                {
                  formValid = false;
                }
                
    
            }
            
          }
          //Multiple box: required check only..:
          else
          {
            arrInputs = tmpField.split('|');
            //alert(tmpField);
            //alert(arrInputs.length);
            for (j=0;j<arrInputs.length;j++)
            {
              //alert(arrInputs[j]);
              tmpInput = document.getElementById(arrInputs[j]);
              
              if  (tmpInput.style.display != 'none')
              {
              
                if (arr[1] == "true") //Required check
                {
                  if (tmpInput.value.length < 1)
                    tmpValid = false; 
                }
              
              
              
                if (!tmpValid)
                {
                thisField.style.display = 'inline';
                tmpInput.style.backgroundColor = '#FFCC99';
				tmpInput.style.backgroundImage = 'url(\'/images/inputbg_red.gif\')';
                }
                else
				{
                  tmpInput.style.backgroundColor = '#C2D3C0';
				  tmpInput.style.backgroundImage = 'url(\'/images/buttonbg_green.gif\')';
				}
                
                if (!tmpValid)
                {
                  formValid = false;
                }
      
              }
          }
      
        }   
    }
  }
  
  /*
  if (formValid)
  {
    if (confirmMsg)
      return confirm(confirmMsg);
    else
      return true;
  }
  else
    return false;
  */
  
  return formValid;

}

function chkRequired(val)
{
  if (val.length == 0)
    return false;
  else
    return true;
}

function chkLength(val, min, max)
{
  if (min == "0" && max == "0")
  {
    return true;
  }
  else if (max != "0")
  {
    if (val.length >= parseInt(min) && val.length <= parseInt(max))
      return true;
     else
      return false; 
  }
  else
  {
    if (val.length >= parseInt(min))
      return true;
    else
      return false;
  }
}

function chkNumeric(val)
{
  if (isNaN(parseFloat(val)))
    return false;
  else
    return true;
}

function chkEmail(val) {

		var at="@"
		var dot="."
		var lat=val.indexOf(at)
		var lstr=val.length
		var ldot=val.indexOf(dot)
		if (val.indexOf(at)==-1){
		   return false;
		}

		if (val.indexOf(at)==-1 || val.indexOf(at)==0 || val.indexOf(at)==lstr){
		   return false;
		}

		if (val.indexOf(dot)==-1 || val.indexOf(dot)==0 || val.indexOf(dot)==lstr){
		    return false;
		}

		 if (val.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (val.substring(lat-1,lat)==dot || val.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (val.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (val.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;				
	}



function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {

return false;
}

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range

return false;
}

if (day < 1 || day > 31) {

return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {

return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {

return false;
}
}
return true; // date is valid
}

function isPast(day, month, year)
{
  var now = new Date();
	now = now.getTime();

  var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();
	
	//var futureDate = (now < checkDate);
	var pastDate = (now >= checkDate);
	
	return pastDate;
	
}
