function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {oldonload();}
      func();
    }
  }
}


wf.showAlertOnError = false;
wf.functionName_formValidation = "myCustomValidation";

function myCustomValidation (evt) {
	
	var formElement = wf.utilities.getSrcElement(evt); 
	
	if(wf.formValidation(evt)) {
		if(formElement.id == "frm_discuss") {
			new Ajax.Updater('result', '/sendmail.php',{

			onLoading:function(request){sendmail()},
			onComplete:function(request){handelrequest()},parameters:Form.serialize(document.forms['frm_discuss']), insertion:Insertion.Bottom, asynchronous:true});
			return wf.utilities.XBrowserPreventEventDefault(evt);
		}
		
		if(formElement.id == "commentform") {
			// grab the comment form default action
			var com_action = $('commentform').action;

			new Ajax.Request(com_action,{

			onLoading:function(request){sendcomment()},
			onSuccess:function(request){handelcomrequest()},parameters:Form.serialize(document.forms['commentform']), asynchronous:true});
			return wf.utilities.XBrowserPreventEventDefault(evt);
		}
		
		if(formElement.id == "frm_mailing") {
			new Ajax.Updater('result', '/send_mailing_list.php',{

			onLoading:function(request){sendmail()},
			onComplete:function(request){handelmailing()},parameters:Form.serialize(document.forms['frm_mailing']), insertion:Insertion.Bottom, asynchronous:true});
			return wf.utilities.XBrowserPreventEventDefault(evt);
		}
}	
}
// // process the validation
// function formCallback(result, form) {
// 	
// 	// if the discuss form is valid process the ajax updater
// 	if(result == true && form.id =="frm_discuss") {
// 	
// 		new Ajax.Updater('result', '/sendmail.php',{
// 							
// 		onLoading:function(request){sendmail()},
// 		onComplete:function(request){handelrequest()},parameters:Form.serialize(document.forms['frm_discuss']), insertion:Insertion.Bottom, asynchronous:true});		
// }
// 	
// 	// if the comment form is valid proccess the ajax request
// 	if(result == true && form.id =="comment_form") {
// 	
// 		// grab the comment form default action
// 		var com_action = $('comment_form').action;
// 		
// 		new Ajax.Request(com_action,{
// 							
// 		onLoading:function(request){sendcomment()},
// 		onSuccess:function(request){handelcomrequest()},parameters:Form.serialize(document.forms['comment_form']), asynchronous:true});
// 		// onSuccess:function(request)
// 		// onComplete:function(request){handelrequest()},parameters: { $F('form_discuss').serialize()}, insertion:Insertion.Bottom, asynchronous:true});	
// 	}
// }

function sendmail() {
	//Make the Progress Bar Appear
	new Effect.Appear('progress');
	// $('frm_discuss').disable();
}

function sendcomment() {
	//Make the Progress Bar Appear
	new Effect.Appear('com_progress');
	// $('frm_discuss').disable();
}

function handelrequest() {
	new Effect.Fade('progress', {delay: 3.0});
	new Effect.BlindUp('frm_discuss', {delay: 0.0});
	new Effect.Appear('result', {delay: 4.0});
	// $('form_switch').onclick = function() {  return resetForm(); } // setup direction of scroll
	
	$$('#form_switch').each ( function(element) {
			
			// set a click event on the links
			Event.observe(element, 'click', function(event) {
				Event.stop(event);
				resetForm();
            }); // event observe
		}
	);
	
	if ($('show_form')) {
		$('show_form').hide();
	}
	if ($('hide_form')) {
	$('hide_form').hide();
	}
}

function handelmailing() {
	new Effect.Fade('progress', {delay: 3.0});
	new Effect.BlindUp('frm_mailing', {delay: 0.0});
	new Effect.Appear('result', {delay: 4.0});
	// $('form_switch').onclick = function() {  return resetForm(); } // setup direction of scroll
}

function handelcomrequest() {
	new Effect.Fade('com_progress', {delay: 3.0});
	new Effect.BlindUp('commentform', {delay: 0.0});
	
	
	$('com_result').update('<h4>Thank you!</h4><p>Your comments are greatly appreciated.</p><p><strong><em>Note:</em></strong> All comments are moderated, so please allow up to 24 hours for your comment to be visible on our website.</p><p class="button"><a href="#contact" id="com_form_switch">Click here to add another comment</a></p>');
	new Effect.Appear('com_result', {delay: 4.0});
	// $('form_switch').onclick = function() {  return resetForm(); } // setup direction of scroll

	$$('#com_form_switch').each ( function(element) {
			
			// set a click event on the links
			Event.observe(element, 'click', function(event) {
				Event.stop(event);
				com_resetForm();
            }); // event observe
		}
	);
}


function resetForm() {
	$('frm_discuss').enable();
	$('frm_discuss').reset();
	
	$('result').update('');
	new Effect.Fade('result');
	new Effect.BlindDown('frm_discuss');
	
	if ($('hide_form')) {
	$('hide_form').show();
	}
}

function com_resetForm() {
	$('commentform').enable();
	$('commentform').reset();
	
	$('com_result').update('');
	new Effect.Fade('com_result');
	new Effect.BlindDown('commentform');
	
}

function prepareLinks(){	
	
	$$('#frm_discuss').each ( function(element) {
			// set a click event on the links
			Event.observe(element, 'submit', function(event) {
				Event.stop(event);
				
		    })
		}
		);
		
	$$('#commentform').each ( function(element) {
				// set a click event on the links
				Event.observe(element, 'submit', function(event) {
					Event.stop(event);

			   })
		}
	);
	
	$$('#show_form').each ( function(element) {
			
			// set a click event on the links
			Event.observe(element, 'click', function(event) {
				Event.stop(event);
				new Effect.BlindDown('commentform');
				$('show_form').hide();
				$('hide_form').show();
            }); // event observe
		}
	);
	
	
	$$('#hide_form').each ( function(element) {
			
			// set a click event on the links
			Event.observe(element, 'click', function(event) {
				Event.stop(event);
				new Effect.BlindUp('commentform');
				$('show_form').show();
				$('hide_form').hide();
				
            }); // event observe
		}
	);
	
	$$('ul#imagegallery li a.preview').each ( function(element) {
			
			// set a click event on the links
			Event.observe(element, 'click', function(event) {
				Event.stop(event);
				par = element.id;
				new Ajax.Updater('image_preview', '/press_area/loader/'+par,{

				onLoading:function(request){$('image_preview').hide(), $('press_progress').show(), $('space_rocket').show()},
				onComplete:function(request){$('image_preview').show(),$('press_progress').hide(), $('space_rocket').hide()},
				method: 'get',
				// parameters: { id: par}, 
				 
				asynchronous:true
				});
					
				
            }); // event observe
		}
	);
}



addLoadEvent(prepareLinks);