// JavaScript Document



var f = {
	
	formId : 'contact-form',
	errorClass : 'field-with-errors',
	hide : true,
	
	positionForm: function(){
		var styles = {top:0, left:0};
		var w = window.getSize();
		var ws = window.getScroll();
		var c = $(f.formId).getParent();
		var cs = c.getSize();
		if(cs.y > w.y){
			styles.top = 20 + ws.y;
		}else{
			styles.top = ( (w.y - cs.y) / 2) + ws.y;
		}
		styles.left = ( (w.x - cs.x) / 2) + ws.x;
		c.setStyles(styles);
	},
	
	showForm: function(e){
		new Event(e).stop();
		var form = $(f.formId);
		var container = form.getParent();
		container.setOpacity(0);
		container.setStyle('display','block');
		f.positionForm();
		container.tween('opacity', 1);
	},
	
	hideForm: function(){
		var con = $(f.formId).getParent();
		con.tween('opacity', 0);
		con.setStyle.delay(1000, con, ['display', 'none']);
	},
	
	showAlert: function(txt){
		var c = $('form-alert');
		c.setOpacity(0);
		c.setStyle('display', 'block');
		c.getElement('#text p').set('html', txt);
		c.tween('opacity', 1);
		c.getElement('#close').addEvent('click', f.hideAlert);
	},
	
	hideAlert: function(){
		var con = $('form-alert');
		con.tween('opacity', 0);
		con.setStyle.delay(1000, con, ['display', 'none']);
		if(f.hide) f.hideForm();
	},
	
	resetValidation: function(){
		$$('.' + f.errorClass).each( function(el){
			el.removeClass(f.errorClass);
		});
	},
	
	validate: function(){
		var passed = true;
		$$('.required').each( function(el){
			if( el.value.trim() == "" ){
				el.getParent().addClass( f.errorClass );
				passed = false;
			}
		});
		if( !passed ){
			$('contact-form-info').addClass( f.errorClass );
		}
		$('budget_field').value = $('budget_field').value.replace('£', '');
		$('budget_field').value = $('budget_field').value.replace('$', '');
		return passed;
	},
	
	onSubmit: function(e){
		new Event(e).stop();
		f.resetValidation();
		if( f.validate() ){
			$(f.formId).set('send', {onSuccess:f.submitSuccess, onFailure:f.submitFail} );
			$(f.formId).send();
			$('contact-form-submit').setStyle('background', 'url(images/ajax-loader.gif) 10px 3px no-repeat');
		
		}
	},
	
	submitFail: function(xhr){
		f.hide = false;
		f.showAlert("<b>Error:</b> " + xhr.statusText);
		$(f.formId).getElement('.submit').setStyle('background', '#f1f2f5 url(images/send_btn.gif) left top no-repeat');
	},
	
	submitSuccess: function(txt){
		if( txt.contains('Error') ){
			f.hide = false;
			f.showAlert(txt);
			$('contact-form-submit').setStyle('background', '#f1f2f5 url(images/send_btn.gif) left top no-repeat');
		}else{
			f.hide = true;
			f.showAlert("Message sent.  You will receive confirmation from us shortly. <br> Please check your junk mail and add us to your safe list");
			$('contact-form-submit').setStyle('background', '#f1f2f5 url(images/send_btn.gif) left top no-repeat');
		}
	},
	
	init: function(){
		$(f.formId).addEvent('submit', f.onSubmit);
		$('close_btn').addEvent('click', f.resetValidation);
		$('close_btn').addEvent('click', f.hideForm);
		$('contact-form-link').addEvent('click', f.showForm);
	}
	

}

window.addEvent('domready', f.init);
