// JavaScript Document
var domain = 'http://' + location.host + '/';
$(document).ready(function() {
	$.fn.clearOnFocus = function(){
	   return this.focus(function(){
		   var v = $(this).val();   
		   $(this).val( v === this.defaultValue ? '' : v );
	   }).blur(function(){
		   var v = $(this).val();
		   $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
	   });
	
	};
	$(document).ready(function() {
	$('input').clearOnFocus();
	$('textarea').clearOnFocus();
	});
	
	$('#submitForm').validate({
			submitHandler:function() {
				// set ajax submit options
				var frmOptions = {	
					success: function(responseText) 
					{						
						if (responseText == 'success') 
						{
							location.href=domain+'thank_you.php';
						} else if (responseText == 'reg_success') 
						{
							location.href=domain+'shop_confirm';
						} else if (responseText == 'find_staff_success') 
						{
							location.href=domain+'thank_you_find_staff.php';	
						} else if (responseText == 'applySuccess') 
						{
							location.href=domain+'thank_you_apply_for_job.php';
							} else if (responseText == 'find_job_alert') 
						{
							location.href=domain+'thank_you_job_alert.php';
						} else if (responseText == 'contact_success') 
						{
							location.href=domain+'thank_you_contact.php';
						} else if (responseText == 'tell_success') 
						{
							location.href=domain+'thank_you_friend.php';
						} else {	
							$("#error").html(responseText);
							$("#error").show();
						}
					}
				};
				// submit form using ajax
				$('#submitForm').ajaxSubmit(frmOptions);
			},
			
			errorPlacement:function(error, element) {				
				//$("#error").append('<br/>' +element.get(0).title + '<sup>*</sup>');
			}, 
			
			invalidHandler: function(form, validator) {
				var errors = validator.numberOfInvalids();
				  if (errors) {
					$("#error").html("Error: Please correct * marked fields!!");
					$("#error").show();
				  } else {
					$("#error").hide();
				  }
			}
 	});


	
	$('#submitForm2').validate({
			submitHandler:function() {
				// set ajax submit options
				var frmOptions = {	
					success: function(responseText) 
					{						
						if (responseText == 'success') 
						{
							location.href=domain+'thank_you.php';
						} else if (responseText == 'quote_success') 
						{
							location.href=domain+'thank_you.php';	
						} else {	
							$("#error2").html(responseText);
							$("#error2").show();
						}
					}
				};
				// submit form using ajax
				$('#submitForm2').ajaxSubmit(frmOptions);
			},
			
			errorPlacement:function(error, element) {				
				//$("#error").append('<br/>' +element.get(0).title + '<sup>*</sup>');
			}, 
			
			invalidHandler: function(form, validator) {
				var errors = validator.numberOfInvalids();
				  if (errors) {
					$("#error2").html("Error: Please correct * marked fields!!");
					$("#error2").show();
				  } else {
					$("#error2").hide();
				  }
			}
 	});
});
