/********************************************************************
commoncode.js
Modified By: JGA
Modification Date: 11-2-01
Changes Made: added the form validation code

Last Modified: 11-5-01
Changes made: added code for the pledge form validation
*********************************************************************/

// macromedia code
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/********************************************************************
validateFieldArray:
	This function requires all fields and comments
	returned by the validateForm function. It loops
	through the form fields and checks whether it's
	an input type of text or not.  If the user
	left the field blank, it alerts the user
	with the message that was specified in the
	ValidateForm function.
*********************************************************************/

function validateFieldArray( flds ){
 for (var i = 0; i < flds.length; i ++){
  if ( flds[i][2] == "text"){
	  if ( flds[i][0].value == "" ){
	   alert( "Please enter " + flds[i][1] );
	   flds[i][0].focus();
	   return false;
	  }
	  // if you want the fields validated for number of characters, uncomment this
	  // example: you want to make sure they don't put more that 30 keystrokes in any
	  // of the input fields
	  
	  /*else if (flds[i][0].value.length > 30){
	   alert( "Please limit " + flds[i][1] + " to 30 characters");
	   flds[i][0].focus();
	   return false;
	  }
	  */
  }
  else if ( flds[i][2] == "email" ){
	  var emailFilter=/.+@.+\..{2,3}/;
	  if ( flds[i][0].value == "" || !(emailFilter.test(flds[i][0].value))){
	   alert( "Please enter " + flds[i][1] );
	   flds[i][0].focus();
	   return false;
	  }
  }
  else if ( flds[i][2] == "select"){
	  if ( flds[i][0].selectedIndex == 0 ){
	   alert( "Please select " + flds[i][1] );
	   flds[i][0].focus();
	   return false;
	  }
  }
 }
 return true;
}

/*********************************************************************
ValidateForm:
	Requirement: 
	  1.) All forms fields that need validation
	  2.) the name of your form
	  
	This function allows you to add all of your form
	elements that you want validated to it.  
	Form fields are added like this:
	flds[flds.length] = [frm.YOUR_FIELD_NAME, "THE_TEXT_YOU_WANT_DISPLAYED", "INPUT_TYPE"];

	example:
	flds[flds.length] = [frm.FirstName, "your First Name", "text"];
*********************************************************************/
function ValidateFriend()
{
	// put the name of your form here
	var frm = document.tellFriend;
	
	var flds = new Array();
	
	// these fields get swapped out with the form field names in your form!!
	flds[flds.length] = [frm.recipient, "the recipients email address", "email"];
	flds[flds.length] = [frm.sender, "your email address", "email"];
	 
	return validateFieldArray( flds );
}

function ValidatePledge()
{
	// put the name of your form here
	var frm = document.pledge;
	
	var flds = new Array();
	
	// these fields get swapped out with the form field names in your form!!
	flds[flds.length] = [frm.amount, "the amount you wish to pledge", "text"];
	flds[flds.length] = [frm.name, "your name", "text"];
	flds[flds.length] = [frm.dayPhone, "your daytime phone number", "text"];
	flds[flds.length] = [frm.email, "your email address", "email"];
	 
	return validateFieldArray( flds );
}

function sendFriend()
{
	if (ValidateFriend())
	{
		document.tellFriend.send.value = 1;
		document.tellFriend.submit()
	}
}