
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0]);
  			var day = parseInt(elems[1]);
 			var year = parseInt(elems[2]);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function addPostageandHandling(theform)
{
	// am looping through form to get number of items selected and charge appropriate postage
	// 2 or less items == 10.00
	// 3 - 10 items == 15.00
	// more than 10 or no state do not submit

	var postages = new Array();
	postages['The_Australian_Directory_of_Philanthropy_2008_2009'] = 1;
	postages['The_Australian_Directory_of_Philanthropy_Online'] = 0;
	postages['The_Australian_Directory_of_Philanthropy_Online-RENEWAL'] = 0;
	postages['Australian_Philanthropy_journal_subscription'] = 0;
	postages['Creative_Philanthropy'] = 1;
	postages['Conference_Proceedings'] = 1;
	postages['Youth_On_Board'] = 1;
	postages['Successful_Submission_Writing_for_Business_and_Non_Profit_Organizations'] = 1;
	postages['Working_on_Governance_and_Accountability'] = 1;
	postages['Working_with_Foundations_in_Australia'] = 1;
	postages['Monograph_2_Trusts_and_Foundations_in_Australia'] = 1;
	postages['Monograph_3_Australian_Philanthropy_Research_Papers_2000'] = 1;
	postages['Community_Foundation_Kit'] = 1;
	postages['Community_Foundation_Kit_Inserts'] = 1;
	
	var counter = 0;
	
	for ( i=0; i < theform.length; i++ )
	{
		var el = theform.elements[i];
		
		if ( el.type == 'text' && postages[el.name] )
		{
			if ( el.value != "" && el.value != null )
			{
				counter += parseInt(el.value) * parseInt(postages[el.name]);
			}
		}
	}

	if ( counter == 0 )
	{
		theform.Postage_and_Handling[0].value = '0';
		theform.Postage_and_Handling[1].value = '0.00';
	}
	if ( counter > 0 && counter <= 2 )
	{
		theform.Postage_and_Handling[0].value = '1';
		theform.Postage_and_Handling[1].value = '10.00';
		
	}
	if ( counter >= 3 && counter <= 10 )
	{
		theform.Postage_and_Handling[0].value = '1';
		theform.Postage_and_Handling[1].value = '15.00';
	}
	if ( counter > 10 )
	{
		return false;
	}

	return true;
}

function validateForm(theForm)
{
	// Customize these calls for your form

	if ( !addPostageandHandling(theForm) )
	{
		alert("Please contact Philanthropy Australia for the appropriate postage rates");
		return false;
	}
	
	if (!validRequired(theForm.Name,"Name"))
		return false;

	if ( theForm.The_Australian_Directory_of_Philanthropy_Online[0].value != "" ) {
		if (!validEmail(theForm.Email_Address,"Email Address",true))
			return false;
	} 
	if ( theForm.The_Australian_Directory_of_Philanthropy_2008_2009[0].value != "" ) {
		if (!validEmail(theForm.Email_Address,"Email Address",true))
			return false;
	}
	if ( theForm.Email_Address.value != "" ) {
		if (!validEmail(theForm.Email_Address,"Email Address",true))
			return false;
	}
	if ( theForm.value = "" ) {
		alert('No Fields have been filled in');
		return false;
	}

	return true;
}







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_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];}
}

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;
}

