/**
 * @author Andy
 */

/* begin document ready */
$(document).ready(function() {
	
	
	// attach hide and shows.
	$('.hideShow').each(function() {
		//alert(this.name);
	});


	
	// add calculated cost of number of tables.
	if ($(".book,#tables")[0]) {
		$("#noTables").after('<div class="row"><label for="jscost">Total Cost of Booking: </label><input id="jscost" class="tiny" disabled="disabled" name="jscost" value="£700"></input></div>');
		// set initial value
		$("#jscost")[0].value = "£" + ($("#tables")[0].value * 700)+ " + VAT";
		// set value on change
		$(".book,#tables").change( function() {
			$("#jscost")[0].value = "£" + ($("#tables")[0].value * 700) + " + VAT";
		});		
	}
	
	// hide the optional fields on the booking form.	
	var booking = $('.book,#diff');
	if (booking[0]) {

		// hide if ticked
		if(booking[0].checked) {
			$('.hide').hide();
		}
		// set toggle.
		$('#diff').click(function() {
			$('.hide').slideToggle();		
		});

	}
	
	

	// ajax for the contact page.
    $('#name').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);   
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_name&name=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#name').parent().removeClass('error');
					  } else {
					  	$('#name').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	
    $('#payname').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);   
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_payname&payname=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#payname').parent().removeClass('error');
					  } else {
					  	$('#payname').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	
    $('#companyname').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);   
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_companyname&companyname=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#companyname').parent().removeClass('error');
					  } else {
					  	$('#companyname').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	
    $('#jobtitle').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);   
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_jobtitle&jobtitle=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#jobtitle').parent().removeClass('error');
					  } else {
					  	$('#jobtitle').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});

    $('#payjobtitle').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);   
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_payjobtitle&payjobtitle=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#payjobtitle').parent().removeClass('error');
					  } else {
					  	$('#payjobtitle').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	
	// ajax for the contact page.
    $('#phone').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_phone&phone=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#phone').parent().removeClass('error');
					  } else {
					  	$('#phone').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	$('#payphone').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_payphone&payphone=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#payphone').parent().removeClass('error');
					  } else {
					  	$('#payphone').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
	
	// ajax for the contact page.
    $('#from').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);            
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_email&from=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#from').parent().removeClass('error');
					  } else {
					  	$('#from').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});
    $('#payfrom').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);            
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_payemail&payfrom=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#payfrom').parent().removeClass('error');
					  } else {
					  	$('#payfrom').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});	
	
	// ajax for the contact page.
    $('#message').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);         
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_message&message=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#message').parent().removeClass('error');
					  } else {
					  	$('#message').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});	
    $('#invoiceadd').blur(function () {
        var t = this; 
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);         
            this.timer = setTimeout(function () {
            	$.ajax({
                	url: 'index.php',
                    data: 'action=check_invoiceadd&invoiceadd=' + t.value,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
					  if (j.ok) {
					  	$('#invoiceadd').parent().removeClass('error');
					  } else {
					  	$('#invoiceadd').parent().addClass('error');
					  }	 
                    }
                });
            }, 200);
            this.lastValue = this.value;
       	}
	});	
	// customised #link slide to's
	function filterPath(string) {
		return string
		.replace(/^\//,'')
		.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
		.replace(/\/$/,'');
  	}
	
	var locationPath = filterPath(location.pathname);
	
	if (locationPath.indexOf("to/") != -1) {
		var target = locationPath.replace('to/','');
		$(target)
		$('html, body').animate({scrollTop: $(target).offset().top}, 400, function(){
				location.hash = target;
				// if the link is to a form element focus it!
				$(target).focus(); 
		});
		
	}
	
	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if (  locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
			var $target = $(this.hash), target = this.hash;
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					$('html, body').animate({scrollTop: targetOffset}, 400, function() {
						location.hash = target;
						// if the link is to a form element focus it!
						$(target).focus(); 
    				});
  				});
   			}
		}
	});
});
/* end document ready */


