// t2a contact
//

var contact = (function(){
  
  return {
	
    init: function(){
			$A($(document).getElementsByClassName('open-contact-form')).each(function(el){
			  el.observe('click', contact.showContactForm.bindAsEventListener(contact));
			});
			//$('nav-contact').observe('click', contact.showContactForm.bindAsEventListener(contact));
			/*if(!$('contact-link')) return;
		  $('contact-link').observe('click', this.showContactForm.bindAsEventListener(this));*/
			this.formEl = $('contact-form');
			this.button = $('contact-close-button');
			this.button.observe('click', this.closeForm.bindAsEventListener(this));
			$('contact-form-submit').observe('click', this.submitForm.bindAsEventListener(this));
		},
		
		showContactForm: function(){
			trackHit("contact");
			var arrayPageSize = this.getPageSize();
      $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		  new Effect.Appear($('overlay'), { duration: .3, from: 0.0, to: .8, afterFinish: this.initForm.bindAsEventListener(this) });
		},
		
		initForm: function(){
		  if(!this.formEl || !this.button) return;
			var el = this.formEl; // shorthand
			var pageSize = this.getPageSize();
			el.style.left = (pageSize[0] / 2) - (el.getWidth()/2)-100 +'px';
			el.show();

		},
		
		closeForm: function(){
		  this.formEl.hide();
			new Effect.Fade($('overlay'), { duration: .3, to: 0.0 });
		},
		
		validate: function(){
			var isValid = false;
		  var name = $('contact-first-name').value, email = $('contact-email').value, phone = $('contact-phone').value;
			if(name.length > 2 && email.indexOf('@' > 0) && phone.length > 7){
				isValid = true;
			}	else {
				alert('Please enter accurate name, phone and email information');
			}
			return isValid;
		},
		
		submitForm: function(){
			if(!this.validate()) return;
			trackHit("contact-submitted");

			var url = ROOT + "rpc/core/sendEnquiry";
			    params = $('contact-form-element').serialize();
			new Ajax.Request(url, { method: 'post', parameters: params, onSuccess: this.formSubmitted.bindAsEventListener(this)});
		},
		
		formSubmitted: function(o){
			
			AjaxForms.DisplayNotification("InfoMessage", "We thank you for taking the time to e-mail us. We shall endeavour to get a response back to you as soon as possible. Please be sure to check your email 'spam' folder periodically to ensure our response to you has not been misfiled there by your email provider.");
			/*
			if(o.responseText == 1){ 
		    alert('We have received your enquiry, we\'ll be in touch as soon as possible.');
			} else {
				alert('sending failed, please try again.');
			}*/
			this.closeForm();
		},
		
		
		
		
		
		
		
		// borrowed from lightbox!
		getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}


  }
})();

document.observe("dom:loaded", contact.init.bindAsEventListener(contact));



function sendEnquiry() {
	Tools.SetProgress("Sending enquiry...");
	AjaxForms.OnSuccess = sendEnquiry2;
	AjaxForms.SubmitForm();
}

function sendEnquiry2(returnValue) {
	AjaxForms.DisplayNotification("InfoMessage", "Your enquiry has been sent and we shall get back to you shortly. We thank you for taking the time to e-mail us. We shall endeavour to get a response back to you as soon as possible.");
	$("FormWrapper").style.display = "none";
}
