function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
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 formatphone(x){
var vphone = /^\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/
	if(vphone.test(x.value)){
		x.value=x.value.replace(vphone,'($1) $2-$3');
          return true;
	}
	else {
	       return false;	
	}
}



 function validateEmail(the_address)
{
var the_at = the_address.indexOf("@");
var the_last_at = the_address.lastIndexOf("@");
var the_dot = the_address.indexOf(".");
var the_last_dot = the_address.lastIndexOf(".");
var a_space = the_address.indexOf(" ");
var address_length = the_address.length;

var error_message = "";
// ensuring there is at least one @
if (the_at == -1)
{
error_message += "You are missing an @ symbol in your e-mail address.\n";
}
// ensuring the @ is not the first symbol
if (the_at == 0)
{
error_message += "You have nothing preceding the @ symbol in your e-mail address\n";
}
// ensuring there is not more than one @
if (the_at != the_last_at)
{
error_message += "You have more than one @ symbol in your e-mail address\n";
}
// ensuring you have a dot
if (the_dot == -1)
{
error_message += "You have no dot in your e-mail address.\n";
}
// ensuring you have at least one dot for type of location
if (the_last_dot < the_last_at)
{
error_message += "You do not have a dot after the @ to specify address type.\n"
}
// ensuring you have something between the @ and the last dot
if ((the_last_dot <= the_last_at + 1) && (the_last_dot > the_last_at))
{
error_message += "You do not have anything between the last @ and the last dot.\n";
}
// ensuring you do not have a dot at the very end of the address
if (the_last_dot == address_length - 1)
{
error_message += "You have a dot as the last character in the address.\n";
}
// ensuring you do not have any spaces in the address
if (a_space != -1)
{
error_message += "You have at least one space in the e-mail address.\n"
}
// giving feedback to the user about the validity of the address
if (error_message == "")
{
return true;
}
else
{
return false;
}
}





function formValidator(formName) { 

//set error message 
var message=''; 
//loop through form elements
var homephonenumblank = false;
var otherphonenumblank = false;
var warned = true;
for(i=0;i<formName.elements.length;i++) { 
//set field name

var formField=formName.elements[i]; 

switch(formName.elements[i].type) {

//text field
case "text": 
case "textarea":




if(formField.value == '') {
//if empty
  if (formField.name == 'home_phone') {
    homephonenumblank = true;
  }
  if (formField.name == 'other_phone') {
    otherphonenumblank = true;
  }
//and required = "yes"
if(formField.getAttribute('required') == 'yes') { 
//check to see if message object exists
if(formField.getAttribute('message')) { message= message + '\r\n' + formField.getAttribute('message') ; }
//display field name if message object doesn't exists
else { message= message + '\r\n' + formField.name + ' Required'; }
}
}
if (formField.name == 'email' && formField.value != '')
{
  if (!validateEmail(formField.value))    {
    message= message + '\r\n' + formField.getAttribute('message') ;      
  }
}

if (formField.name == 'home_phone' && formField.value != '')
{
  if (!formatphone(formField))    {
    message= message + '\r\n' + formField.getAttribute('message') ;
  }
}

if (formField.name == 'other_phone' && formField.value != '' && (homephonenumblank))
{
  if (!formatphone(formField))    {
    message= message + '\r\n' + formField.getAttribute('message') ;
  }
}


if (formField.name == 'dob' && formField.value != '')
{
  if (!isValidDate(formField.value,'U','A'))    {
    message= message + '\r\n' + formField.getAttribute('message') ;
  }
}

if (formField.name == 'appt_date' && formField.value != '')
{
  if (!isValidDate(formField.value,'U','A'))    {
    message= message + '\r\n' + formField.getAttribute('message') ;
  }
}

if (formField.name == 'new_date' && formField.value != '')
{
  if (!isValidDate(formField.value,'U','A'))    {
    message= message + '\r\n' + formField.getAttribute('message') ;
  }
}

if (homephonenumblank && otherphonenumblank && warned) {
   warned=false;
   message = message + '\r\n You must enter a valid home or work/cell phone number to continue, eg 617-555-1212';
}


break; 

//select menu 
case "select-one":
case "select-multiple":


//if empty
if(formField.value == '' || formField.value =='0') {
//and required = "yes"
if(formField.getAttribute('required') == 'yes') { 
//check to see if message object exists
if(formField.getAttribute('message')) { message= message + '\r\n' + formField.getAttribute('message') ; }
//display field name if message object doesn't exists
else { message= message + '\r\n' + formField.name + ' Required'; }

   
}

}


break; 

//radio button 
case "radio": 


var radioField=formField; 

//if checked
if(formField.checked == true)  { var radioCheck=true;  }


if(formField.required == "yes") { 


var radioMessage; 
var radioRequired=true; 

if(formField.message) { radioMessage = radioField.message ; } else { radioMessage= formField.name + ' Required'; }


} 

break; 

//check box 
case "checkbox": 

//if empty
if(formField.checked == false) { 
//and required = "yes"
if(formField.required == 'yes') { 
//check to see if message object exists
if(formField.message) { message= message + '\r\n' + formField.message ; }
//display field name if message object doesn't exists
else { message= message + '\r\n' + formField.name + ' Required'; }

   
}

}

if (formField.checked)
{
  //alert ('Checkbox ' + formField.name + ' is selected...and the element id is ' + i);
  full = i + 1;
  partial = i + 2;
  msg = "";
  ffname = formField.name;
  answer = ffname.indexOf('selected');
  if (answer != -1) {
//need to make sure that we are only looking at the camp checkboxes....
    msg = ffname.substr(0,answer-1) + '[amount][0]';
 //   tot_len = formName.elements[full].value;
    amount_val = formName.elements['event_array['+i+'][amount]'].length;  //doesn't work!!
        if ((!formName.elements['event_array[1][amount]'][0].checked) && (!formName.elements['event_array[1][amount]'][1].checked)) { //hardcoded method does work
      formField.focus();      
      alert(msg  +' You must select either a full payments or partial deposit if you wish to register for this camp' + formName.elements[i].name);
      return false;
    }
  }
//   if (!msg.checked) {
 
//if ((!formName.elements[full].checked) && (!formName.elements[partial].checked)) {
    
//    alert(msg + 'You must select between a full payment or partial deposit if you wish to register for this camp' + formName.elements[i].name);
//    return false;
//  }
//  if (!formName.elements[partial].checked) {
//    alert(msg + 'Checkbox ' + formField.name + ' is selected...and the deposit selected is partial ' + formName.elements[partial].name);
//  }
  
}    




break; 


}


}


//radio button check addition 
if(radioCheck != true && radioRequired == true) { 

message = message + '\r\n' + radioMessage; 

}


//display generic button 
if(message != '') { alert(message); return false; } else { return true; } 


}
