// JavaScript Document

function removeLeadingAndTrailingChar (inputString, removeChar) 
{
	var returnString = inputString;
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

function displayErrors(textval) {
	$('#dialogcontainer').empty().append('<div id="dialog" title="Error with contact form details"></div>');
	$('#dialog').empty().append(textval);
	$(function() {
		$("#dialog").dialog({
			bgiframe: true,
			modal: true,
			width: 500,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
	});	
}

function sendForm(form) {
	var str = $(form).serialize();
	var errors = '';
	var retbtn = '';
	var icon = '<div style="height:20px;"><span class="ui-icon ui-icon-alert" style="float:left; margin-right:5px;"></span>';
	
	form.full_name.value = removeLeadingAndTrailingChar(form.full_name.value,' ');
	form.email.value = removeLeadingAndTrailingChar(form.email.value,' ');
	form.telephone.value = removeLeadingAndTrailingChar(form.telephone.value,' ');
	form.enquiry.value = removeLeadingAndTrailingChar(form.enquiry.value,' ');

	if (form.full_name.value.length == 0) {
		errors += icon + 'Please enter your name.</div>';
		if (retbtn == '') retbtn = 'full_name';
	}
	
	if (form.email.value.length == 0 && form.telephone.value.length == 0) {
		errors += icon + 'Please enter either a contact email address or phone number.</div>';
		if (retbtn == '') retbtn = 'email';
	}
	
	if (form.enquiry.value.length == 0) {
		errors += icon + 'Please enter details of your enquiry.</div>';
		if (retbtn == '') retbtn = 'enquiry';
	}
	
	if (errors.length > 0) {
		displayErrors(errors);
		$('#submit_btn').recover();
		$('#' + retbtn).focus();
		return false;
	}
	document.getElementById('submit').disabled = true;
	document.getElementById('submit').value = 'Sending... Please Wait...';
	$.ajax({
		type: "POST",
		url: "/email.php",
		dataType: "json",
		data: str,
		success: function(msg){
			if (msg.mailsent) {
				htmltext = '<h3 class="centered" style="margin-top:25%;">Contact Form Sent, Thanks!</h3>';
			} else {
				htmltext = '<h3 class="centered" style="margin-top:25%;">An error occurred when sending contact form.<br />Please try again later.</h3>';
			}
			$('#contactformcontainer').animate({ height:'0%' },250, 'easeInOutSine',function(){
				$(this).html(htmltext);
				sIFR.replace(normaltypeface, {
					  selector: 'h3.centered',
					  css: [
					  '.sIFR-root { font-size:24px; font-weight:normal; color:#28356B; text-align:center;  }'
					  ],	  
					  wmode: 'transparent',
					  ratios: [8, 1.41, 11, 1.31, 15, 1.29, 16, 1.28, 24, 1.26, 27, 1.24, 33, 1.23, 36, 1.22, 37, 1.23, 41, 1.22, 42, 1.23, 48, 1.22, 52, 1.21, 53, 1.22, 80, 1.21, 83, 1.2, 85, 1.21, 88, 1.2, 90, 1.21, 1.2],
					  onReplacement: function(f) { 
							$('#contactformcontainer').animate({ height:'100px',easing:'easeInOutSine'},750);
					  }
				});
			});
		}
	});
}

$(document).ready(function(){
	$('#subject').focus();
});

