jQuery.preloadImages = function() { for(var i = 0; i<arguments.length; i++) jQuery("<img>").attr("src", arguments[i]); }
$(document).ready( function() 
{ 
	$.preloadImages(	"Images/banner.jpg",
						"Images/goback.gif",
						"Images/sendform.gif"
					);	
	$('#apDiv22').click( function() { window.location.href='wfe_enquirySelect.php'; }); //go back
	$('#s_homepage').click( function() { window.location.href='index.php'; }); //home page	
	$('#apDiv14').click( function() //submit
	{ 
		if (check_form()) $('#wfe_Form').submit()
	}); 

	$('#wfe_notes').keyup( function(e)
	{
		var limitNum = 500;
		if ($('#wfe_notes').val().length > limitNum) $('#wfe_notes').val($('#wfe_notes').val().substring(0,limitNum)); 
			else $('#CountDown').val((limitNum - $('#wfe_notes').val().length)+' chars left');
	});
	$('#wfe_notes').keydown( function(e)
	{
		var limitNum = 500;
		if ($('#wfe_notes').val().length > limitNum) $('#wfe_notes').val($('#wfe_notes').val().substring(0,limitNum)); 
			else $('#CountDown').val((limitNum - $('#wfe_notes').val().length)+' chars left');
	});
	
	$('#wfe_forenames').focus( function() { hide_error('s2'); });	
	$('#wfe_name').focus( function() {  hide_error('s3'); $('#e_s').hide(); });	
	$('#wfe_house').focus( function() { hide_error('s5'); });
	$('#wfe_address1').focus( function() { hide_error('s6'); $('#e_a').hide(); });
	$('#wfe_town').focus( function() { hide_error('s9'); });
	$('#wfe_county').focus( function() { hide_error('s10'); });
	$('#wfe_postcode').focus( function() { hide_error('s11'); $('#e_p').hide(); });
	$('#wfe_telephone').focus( function() { hide_error('s12'); });
	if (FormId=='LBS') 
	{
		$('#wfe_finadvisor').focus( function() { hide_error('b2') });
		$('#wfe_branch').focus( function() { hide_error('b3') });
		$('#wfe_source_type').focus( function() { hide_error('b5') });
		$('#wfe_fsm_name').focus( function() { hide_error('b6') });
		$('#wfe_adm_name').focus( function() { hide_error('b7') });
	}
	if (FormId=='NBS') $('#wfe_reference').focus( function() { hide_error('b1'); });

	$('#wfe_Form').ajaxForm (
	{ 
		beforeSubmit: function(){ $('#apDiv14').hide(); $('#WaitBox').show(); },
		success: processXmlSave
	});	   
	
}); 
function processXmlSave(responseXML) 
{ 
    var mfeedback = $('feedback', responseXML).text(); 
    var mleadcode = $('leadcode', responseXML).text(); 	
	if ( mfeedback == 1) //success
	{ 
		$('#apDiv9').empty();
		$('#apDiv9').append('Thank you for your enquiry');
		$('#apDiv21').hide();
		$('#apDiv17').hide();			
		$('#apDiv19').hide();						
		for( i=1; i<17; i++) $('#s'+i).hide();
		for( i=1; i<8; i++) $('#b'+i).hide();			
		$('#s100').hide();
		$('#s_leadcode').empty();
		$('#s_leadcode').append('The Lead Code for this Appointment is: <strong>'+mleadcode+'</strong><br />Please quote this number in all correspondance.');
		$('#s_leadcode').show();						
		$('#s_text1').show();									
		$('#s_text').show();	
		$.scrollTo('#topBanner');		
	} 
	if ( mfeedback == 2 ) //person exists
	{ 
		$('#s3').height(50); $('#e_s').show();
		$('#s6').height(50); $('#e_a').show();
		$('#s11').height(50); $('#e_p').show();		
		$.scrollTo('#s3', '300');
		$('#WaitBox').hide();				
		$('#apDiv14').show();		
	} 
	if ( mfeedback == 0 ) //error
	{ 
		$.scrollTo('#s3', '300');
		$('#WaitBox').hide();				
		$('#apDiv14').show();		
	} 
}
function check_form() 
{ 
	stat = 1; 
	var scr = new Object();
	scr.value = 0;
	if (jQuery.trim($('#wfe_forenames').val())=='') { show_error('s2'); stat=0; wscroll(scr,'s2'); }
	if (jQuery.trim($('#wfe_name').val())=='') { show_error('s3'); stat=0; wscroll(scr,'s3'); }
	if (jQuery.trim($('#wfe_house').val())=='') { show_error('s5'); stat=0; wscroll(scr,'s5'); }
	if (jQuery.trim($('#wfe_address1').val())=='') { show_error('s6'); stat=0; wscroll(scr,'s6'); }
	if (jQuery.trim($('#wfe_town').val())=='') { show_error('s9'); stat=0; wscroll(scr,'s9'); }
	if (jQuery.trim($('#wfe_county').val())=='') { show_error('s10'); stat=0; wscroll(scr,'s10'); }
	if (jQuery.trim($('#wfe_postcode').val())=='') { show_error('s11'); stat=0; wscroll(scr,'s11'); }
	if (jQuery.trim($('#wfe_telephone').val())=='') { show_error('s12'); stat=0; wscroll(scr,'s12'); }
	if (FormId=='NBS') 
	{
		if (jQuery.trim($('#wfe_reference').val())=='') { show_error('b1'); stat=0; } 
		if (jQuery.trim($('#wfe_branch').val())=='0') { show_error('b3'); stat=0; } 
	}
	if (FormId=='LBS') 
	{ 
		if (jQuery.trim($('#wfe_finadvisor').val())=='0') { show_error('b2'); stat=0; } 
		if (jQuery.trim($('#wfe_branch').val())=='0') { show_error('b3'); stat=0; } 
		if (jQuery.trim($('#wfe_source_type').val())=='0') { show_error('b5'); stat=0; }
		if (jQuery.trim($('#wfe_fsm_name').val())=='0') { show_error('b6'); stat=0; }
		if (jQuery.trim($('#wfe_adm_name').val())=='0') { show_error('b7'); stat=0; }
	}
	return stat;
}

function wscroll(toscroll,div) 
{
	if (!toscroll.value)
	{
		toscroll.value = 1;
		$.scrollTo('#'+div, '300');
	}
}
function show_error(arg) { $('#'+arg).height(50); $('#l_'+arg).show(); }
function hide_error(arg) 
{ 
//	if($('#'+arg).height()==50) $('#'+arg).animate({ height: "35px" }, 200 );
	if($('#'+arg).height()==50) $('#'+arg).height(35);
	$('#l_'+arg).hide(); 
}