// mapletonhillmedia js file

	var sDivOn = "arttherapy";
	var oldid;

	// turns divs on and off
	function ToggleDiv(psID, psOffID){
		var psDisplay;
		var poElement = document.getElementById(psID);	
		var psDisplay2;
		var poElement2 = document.getElementById(psOffID);

		psDisplay2 = (poElement.style.display == "block" || poElement.style.display == "normal") ? "none" : "none";		
		poElement2.style.display = psDisplay2

		psDisplay = (poElement.style.display == "block" || poElement.style.display == "normal") ? "none" : "block";				
		poElement.style.display = psDisplay
		
		sDivOn = psID;
	}
	
 // A utility function that returns true if a string contains only whitespace characters
 function isblank(s)
 {
  for (var i = 0; i < s.length; i++) {
   var c = s.charAt(i);
   if ((c != ' ') && (c != '\n') && (c != '')){return false};
  }
  return true;
 }
 
 // This is a function that performs form verification. It is invoked from the onSubmit event handler. 
 // The handler should return whatever value this function returns
 function verify(f)
 {
  var msg;
  var empty_fields = "";
  var errors = "";
  var focusNumber = -1;
  
  // Loop through the elements of the form, looking for all Text and Textarea elements that don't
  // have an "optional" property defined. Then check for fields that are empty and make a list of them.
  // Also, if any of these elements have a "min" or "max" property defined, verify that they are numbers
  // and are in the right range. If the element has a "numeric" property defined, verify that it is a number
  // but don't check it's range. Put together error messages for fields that are wrong.
  
  for(var i = 0; i < f.length; i++) {
   var e = f.elements[i];
   if (((e.type == "text") || (e.type == "textarea") || (e.type == "select-one")) && !e.optional) {
    //First check if the field is empty
    if ((e.value == null) || (e.value == "") || isblank(e.value)) {
     empty_fields += "\n          " + e.name;
		if(focusNumber == -1){
			focusNumber = i;
		}
     continue;
    }
    
    //Now check for fields that are suppossed to be numeric
    if (e.numeric || (e.min != null) || (e.max != null) || (e.name.search("Numeric") != -1)) {
     var v = parseFloat(e.value);
     var re_numeric = /^\d+$/;
     
     if (isNaN(v) || !re_numeric.exec(e.value) ||
      ((e.min != null) && (v < e.min)) ||
      ((e.max != null) && (v > e.max))) {
      errors += "- The field " + e.name + " must be a number";
      if (e.min != null)
       errors += " that is greater than " + e.min;
      if (e.max != null && e.min != null)
       errors +=" and less than " + e.max;
      else if (e.max != null)
       errors +=" that is less than " + e.max;
      errors += ".\n";
		if(focusNumber == -1){
			focusNumber = i;
		}
     }
    }
    
    //Now Check to make sure all the date fields are correct
    if (e.date)
    { 
		var re_date = /^(\d{1,2})\/(\d{1,2})\/(\d{4})+/;
		var firstSlash = e.value.indexOf("/");
		var secondSlash = e.value.lastIndexOf("/");
		var month = Number(e.value.substring(0,firstSlash));
		var day = Number(e.value.substring(firstSlash + 1,secondSlash));
		var year = Number(e.value.substring(secondSlash + 1,secondSlash + 7));	
		if ((!re_date.exec(e.value)) || (day<0) || (day>31) || (month<0) || (month>12) || (year<0) || (year>9999))
		{
			errors += "- The field " + e.name + " must be a valid date (ex: 1/1/2007)\n";	
			if(focusNumber == -1)
			{
				focusNumber = i;
			}
		}
		
		// months with 30 days 
		if (month==4 || month==6 || month==9 || month==11) 
		{ 
		    if (day>30)
		    {
		    	errors += "- The field " + e.name + " must have a valid day\n"; 
				if(focusNumber == -1)
				{
					focusNumber = i;
				}
			}
		} 
		// february, leap year 
		if (month==2) 
		{   
		    if (day>29) 
		    {
				errors += "- The field " + e.name + " must have a valid day\n"; 
				if(focusNumber == -1)
				{
					focusNumber = i;
				}
			}
		    if ((day==29) && ((Number(year) % 4) != 0)) 
		    {
				errors += "- The field " + e.name + " must have a valid day\n"; 
				if(focusNumber == -1)
				{
					focusNumber = i;
				}
			}
		} 			
	}
   
   //Now check to make sure the money fields are correct
   if (e.money || (e.name.search("Money") != -1))
   {
	var money = e.value;
	
		if (isNaN(money))
		{
			errors += "- The field " + e.name + " must be a valid money value.\n";
			if(focusNumber == -1) {
				focusNumber = i;
			}
		}
   }
   
   }
  }
  // Now if there were any errors, display the messages, and return false to prevent the form
  // from being submitted. Otherwise, return true.
  if (!empty_fields && !errors) return true;
  
  msg  = "____________________________________________________ _\n\n";
  msg += "The form was not submitted because of the following error(s).\n";
  msg += "Please correct these error(s) and re-submit.\n";
  msg += "____________________________________________________ _\n\n";
  
  if (empty_fields) {
   msg += "- The following required field(s) are empty:"
     + empty_fields + "\n";
   if (errors) msg += "\n";
  }
  msg += errors;
  alert(msg);
  f.elements[focusNumber].focus();
  return false;
 }	