// 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 quote 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,' ');

	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 (errors.length > 0) {
		displayErrors(errors);
		return false;
	}
	
	$('#submit_quote').after('<span>Please wait.....</span>').hide();
	$('#submit_callback').after('<span>Please wait.....</span>').hide();
	
	$('body').animate({ scrollTop:0 }, 0, 'linear',function() {
		$('#left_container').animate({height:450},'slow');
		$('#right_container').animate({height:450},'slow');
		$('#content-left-nav').animate({height:450},'slow');
													   
	 });
	$.ajax({
		type: "POST",
		url: "/emailquote.php",
		dataType: "json",
		data: str,
		success: function(msg){
			if (msg.mailsent) {
				htmltext = '<h3 class="centered" style="margin-top:25%;">Quote Form Sent, Thanks!</h3>';
			} else {
				htmltext = '<h3 class="centered" style="margin-top:25%;">An error occurred when sending quote form.<br />Please try again later.</h3>';
			}
			$('#quoteformcontainer').animate({ height:'0%' },50, '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) { 
							$('#quoteformcontainer').animate({ height:'100px',easing:'easeInOutSine'},50);
					  }
				});
			});
		}
	});

	return false;
}

$(document).ready(function(){
	$('input:checkbox').checkbox({empty:'/global/images/empty.png'});						   
	$('select').selectbox();
	$('#full_name').focus();
});
