// <![CDATA[
/*
* Copyright: 2005 - this.year() SI Works Internet Solutions
* If you have come across this page, its cause you are snooping through code, and are generally a developer as such
* If you would like to use some of the code on this page, please simply email support@siworks.co.za and ask permission
* it would be much appreciated, as we have worked very hard on these func.tions() and scri.pts()
* 
* Note: All functions below are to make sure that we are sticking to standards and make sure that 
* all data that is put in the database is safe and clean
*
* This script validates the contact us form
* @page contact.form.js
* @version 2.1
* @author Greg Shiers, Jarratt Ingram (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - 2007
*/
function initialize ( ) {
	// Attatch event listeners to buttons using attatchEventListener()
	// First check if the element exsists, then add the event listener if it is
	if ( $('contact_form') ) { attachEventListener (  $('contact_form') , "submit" , validate  , false); }	
}
/* 
* Setting the below we can turn certain field validations on or off by setting
* true for on and false for off
*/
function validate( event ){
	// Turn JS validation on or off;
	var validate = true;
	// Make sure we have an event within this function
	if (typeof event == "undefined") {
    	event = window.event;
	}
	if ( validate ) {
	var focus_el = null, 
	    msg = '', 
	    prefix = "Your contact form has had some errors:\n";
	var contact_name 		= true,
		contact_phone 		= true,
		contact_emaill 		= true,
		contact_message		= true;
	
	$('contact_name').value 		= $('contact_name').value.trim();
	$('contact_phone').value 		= $('contact_phone').value.trim();
	$('contact_emaill').value 		= $('contact_emaill').value.trim();
	$('contact_message').value 		= $('contact_message').value.trim();
		
	// Make sure that the title is selected
	if ( contact_name && validation.empty ( $('contact_name') ) ) {
		msg += error[0];
		focus_el = focus_el || $('contact_name');
	}
	// Make sure that the telephone number is filled in
	if ( contact_phone && !validation.phone_number ( $('contact_phone') ) ) {
		msg += error[1];
		focus_el = focus_el || $('contact_phone');
	}
	// Make sure that the email address is filled in
	if ( contact_emaill && validation.empty ( $('contact_emaill') ) ) {
		msg += error[3];
		focus_el = focus_el || $('contact_emaill');
	}
	// If the email address is filled in
	if ( !validation.empty ( $('contact_emaill') ) ) {
		// Make sure that the email address is valid
		if ( !validation.email ( $('contact_emaill') ) ) {
			msg += error[4];
			focus_el = focus_el || $('contact_emaill');
		}
	}
	// Make sure that the title is selected
	if ( contact_message && validation.empty ( $('contact_message') ) ) {
		msg += error[5];
		focus_el = focus_el || $('contact_message');
	}
	// Script has found some one or more errors, now we run the alert
	if ( msg != '' ) {
			// Add the errors to the innerHTML
			$('validate').innerHTML = "<p class=\"error\"><a href=\"/\" onclick=\"clearInnerHtml('validate', false);return false;\">x</a>" + msg + "</p>";
			if ( focus_el.focus ) {
				focus_el.focus();
			}
			// Scroll tp the top of the page
			scrollToPagetop();
			// Stop the default action
			stopDefaultAction( event );
		}
		else {
			$('validate').innerHTML = '';
			scrollToPagetop();
		}
	}
}

addLoadListener ( initialize );

// ]]>