// <![CDATA[
/*
* This validates the quicksearches in the rightbar
* @page validate.contact.js
* @version 1.0
* @author Greg Shiers, Jarratt Ingram
*/
/* 
* Setting the below we can turn certain field validations on or off by setting
* true for on and false for off
*/

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" , validateContact  , false); }	
}

/**
* Function to the adding of a notice to the system
* @param event
* @version 1.0
* @returns array
* @author Greg Shiers
*/
function validateContact ( 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 ) {	
		// /Set field vards to turn on or off (true = ON | false = OFF);
		var contact_name 		= true,
			contact_company		= true,
			contact_email		= true,
			contact_phone		= true,
			contact_message		= true;

		// Set the variables for the script
		var focus_el = null, 
		    msg = []
		/** validate full name field **/
		if ( contact_name && validation.empty ( $('contact_name') ) ) {
			msg += error[10];
			focus_el = focus_el || $('contact_name');
		}
		/** validate company name **/
		if ( contact_company && validation.empty ( $('contact_company') ) ) {
			msg += error[11];
			focus_el = focus_el || $('contact_company');
		}
		/** validate email field **/
		if ( contact_email && validation.empty ( $('contact_emaill') ) ) {
			msg += error[12];
			focus_el = focus_el || $('contact_emaill');
		}
		/** validate email is valid**/
		if ( !validation.empty ( $('contact_emaill') ) ) {
			if ( !validation.email ( $('contact_emaill') ) ) {
				msg += error[13];
				focus_el = focus_el || $('contact_emaill');
			}
		}
		/** validate telephone field **/
		if ( contact_name && validation.empty ( $('contact_phone') ) ) {
			msg += error[14];
			focus_el = focus_el || $('contact_phone');
		}
		/** validate telephone is valid**/
		if ( !validation.empty ( $('contact_phone') ) ) {
			if ( !validation.phone_number ( $('contact_phone') ) ) {
				msg += error[15];
				focus_el = focus_el || $('contact_phone');
			}
		}
		/** validate telephone field **/
		if ( contact_message && validation.empty ( $('contact_message') ) ) {
			msg += error[16];
			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 );
// ]]>