//	**********************************************************************************************
//	STANDARD JAVASCRIPT BROWSER EVENTS CAPTURE AND FORM PROCESSING
//
//	Include this javascript library into a page where you want to:
//
//	1. Catch cross browser events like onclick images etc (i.e. work in firefox and IE)
//	2. Where you have a form, and want to validate entries and submit
//
//	** NOTE **
//	You must have the <body> tag set to <body onload="init();" for this to work !!!!!!
//
//	**********************************************************************************************

//	Define your items/images etc (by ID) that you wish to event handle
	var eventHandlers = new Array('newsletterbutton');
	
//	Define the items (textboxes etc) that you wish to check validation for.  It is best to name the 
//	input fields (ID's) as the real names (i.e. not txtName etc) as when we email the form, we can
//	simply use the input box names ratherthan interpreting them.
//	** NOTE **
//	If you name an input box 'email' then the email address will be properly checked for format.
	//var checkitems = new Array("company","name","email","phone","bookkeeping","recruitment","employment","MYOB","quickbooks","setup","maintenance","cleanup","endofyear");
	var checkitems = new Array("email");
	
function init()
//
//	Setup the event handles and define actions
{
//	Next section sets up the handles for click and mouseover - you can add other
//	events if necessary
	var tmpObj;
	for (count=0;count<eventHandlers.length;count++)
	{
		tmpObj=window.document.getElementById(eventHandlers[count]);
		tmpObj.clicked = false;
		tmpObj.onclick = handleClick;
		tmpObj.onmouseover = handleMouseOver;
	}
}

function handleMouseOver()
//
//	Change the cursor on mouseover
{
	obj=window.document.getElementById(this.id);
	obj.style.cursor="pointer";
}
function handleClick()
//
//	This function handles the click event
{
	var oEvent = document.getElementById(this.id);
	var sError="";
	var iChk=0;

	switch(oEvent.id)
	{
		// Check the events
		case "newsletterbutton" :
			
			// Check if all the required fields have been entered
			for(c=0; c<checkitems.length;c++)
			{
				var oItem=document.getElementById(checkitems[c]);

				// Textboxes - and check if an email input has been used
				if(oItem.name=="email")
				{
					// This is the Regular Expression for an email address format
					// if this input box has an ID of 'email', check the regex for the format
					var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if(!filter.test(oItem.value))
					{
						sError=sError+"Email address is not in the correct format. ";
						oItem.style.background="#FFC1C1";
					}
				}
				
				// Check if any of the 'checkitems' have actually got anything entered
				if(oItem.value=="")
				{
					sError=sError+"You must enter something for " + oItem.name + ". ";
					oItem.style.background="#FFC1C1";
				}
				else
				{
					oItem.style.background="#FFFFFF";
				}
			}

		// Inform the user to correct the mandatory entries
		if(!sError=="")
		{
			alert(sError);
		}
		else
		// Post the values to send the email
		{
			alert("Thank you! Your email has been submitted to the All Stars Bookkeeping Team newsletter mailing list.");
			document.forms[0].submit();
		}
			
	}
	
}