$(document).ready(function () {
	// tooltip
$('#partners img').tooltip({ 
    delay: 0, 
    fade: 250,
    track: "true",
    showURL: false, 
    bodyHandler: function() { 
        return $("<img/>").attr("src", this.src); 
    } 
});
	$('#teasers h2:first-child, #main h1:first-child').addClass("first");
	$(".hideme").hide();
	$("#consultantcall").click(function(){
		$("#applynow").fadeOut("fast",function(){
			$("#callme").fadeIn("normal");
			$('#warning-box').hide();
		});
	});
	$("#applyforfunding").click(function(){
		$("#callme").fadeOut("fast",function(){
			$("#applynow").fadeIn("normal");
			$('#warning-box').hide();
		});
	});
	$("#currentyhaveid_yes").click(function(){
		$("#existingdetails").fadeIn("normal")
	});
	$("#currentyhaveid_no").click(function(){
		$("#existingdetails").fadeOut("fast")
	});
	
	// validate form 1
	$('#formConsult').submit(function(){
		$('#warning-box').hide();
		var error = false;
		if(!validateInput('consult_name','')){
			error = true;			 
		}
		if(!validateInput('consult_business','')){
			error = true;			 
		}
		if(!validateInput('callme #email','email')){
			error = true;			 
		}
		if(error){
			$('html,body').animate({scrollTop: 455}, 1000, function(){$('#warning-box').fadeIn();});
			return false;
		}else{
			return true;
		}
	});
	
	// validate form 2
	$('#formFunding').submit(function(){
		$('#warning-box').hide();
		var error = false;
		if(!validateInput('name','')){
			error = true;			 
		}
		if(!validateInput('business','')){
			error = true;			 
		}
		if(!validateInput('business_address1','')){
			error = true;			 
		}
		if(!validateInput('business_address2','')){
			error = true;			 
		}
		if(!validateInput('postcode','')){
			error = true;			 
		}
		if(!validateInput('telephone','')){
			error = true;			 
		}
		if(!validateInput('applynow #email','email')){
			error = true;			 
		}
		if(!validateInput('fundingreq','')){
			error = true;			 
		}
		if(!validateInput('annualturnover','')){
			error = true;			 
		}
		if(!validateInput('debtorbalance','')){
			error = true;			 
		}
		if(!validateInput('businesssector','')){
			error = true;			 
		}
		if(!validateInput('approxcustomers','')){
			error = true;			 
		}
		if(!validateInput('approxinvoices','')){
			error = true;			 
		}
		if(!validateInput('starttrading','')){
			error = true;			 
		}
		if($("#currentyhaveid_yes").is(':checked')){
			if(!validateInput('currentlender','')){
				error = true;			 
			}
			if(!validateInput('bankersname','')){
				error = true;			 
			}
		}
		if(error){
			$('html,body').animate({scrollTop: 455}, 1000, function(){$('#warning-box').fadeIn();});
			return false;
		}else{
			return true;
		}
	});
	
	// update individual inputs
	$('#consult_name').keyup(function(){validateInput('consult_name','');});
	$('#consult_business').keyup(function(){validateInput('consult_business','');});
	$('#callme #email').keyup(function(){validateInput('#callme email','email');});
	$('#name').keyup(function(){validateInput('name','');});
	$('#business').keyup(function(){validateInput('business','');});
	$('#business_address1').keyup(function(){validateInput('business_address1','');});
	$('#business_address2').keyup(function(){validateInput('business_address2','');});
	$('#postcode').keyup(function(){validateInput('postcode','');});
	$('#telephone').keyup(function(){validateInput('telephone','');});
	$('#applynow #email').keyup(function(){validateInput('applynow #email','email');});
	$('#fundingreq').keyup(function(){validateInput('fundingreq','');});
	$('#annualturnover').keyup(function(){validateInput('annualturnover','');});
	$('#debtorbalance').keyup(function(){validateInput('debtorbalance','');});
	$('#businesssector').keyup(function(){validateInput('businesssector','');});
	$('#approxcustomers').keyup(function(){validateInput('approxcustomers','');});
	$('#approxinvoices').keyup(function(){validateInput('approxinvoices','');});
	$('#starttrading').keyup(function(){validateInput('starttrading','');});
	$('#currentlender').keyup(function(){validateInput('currentlender','');});
	$('#bankersname').keyup(function(){validateInput('bankersname','');});
	
	$('#consult_name').blur(function(){validateInput('consult_name','');});
	$('#consult_business').blur(function(){validateInput('consult_business','');});
	$('#callme #email').blur(function(){validateInput('callme #email','email');});
	$('#name').blur(function(){validateInput('name','');});
	$('#business').blur(function(){validateInput('business','');});
	$('#business_address1').blur(function(){validateInput('business_address1','');});
	$('#business_address2').blur(function(){validateInput('business_address2','');});
	$('#postcode').blur(function(){validateInput('postcode','');});
	$('#telephone').blur(function(){validateInput('telephone','');});
	$('#applynow #email').blur(function(){validateInput('applynow #email','email');});
	$('#fundingreq').blur(function(){validateInput('fundingreq','');});
	$('#annualturnover').blur(function(){validateInput('annualturnover','');});
	$('#debtorbalance').blur(function(){validateInput('debtorbalance','');});
	$('#businesssector').blur(function(){validateInput('businesssector','');});
	$('#approxcustomers').blur(function(){validateInput('approxcustomers','');});
	$('#approxinvoices').blur(function(){validateInput('approxinvoices','');});
	$('#starttrading').blur(function(){validateInput('starttrading','');});
	$('#currentlender').blur(function(){validateInput('currentlender','');});
	$('#bankersname').blur(function(){validateInput('bankersname','');});
	
	// contact form
	$('#contact_form').submit(function(){
		$('#warning-box').hide();
		var error = false;
		if(!validateInput('name','')){
			error = true;			 
		}
		if(!validateInput('email','email')){
			error = true;			 
		}
		if(!validateInput('message','')){
			error = true;			 
		}
		if(error){
			$('html,body').animate({scrollTop: 455}, 1000, function(){$('#warning-box').fadeIn();});
			return false;
		}else{
			return true;
		}
	});
	// update individual inputs
	$('#email').keyup(function(){validateInput('email','email');});
	$('#message').keyup(function(){validateInput('message','');});
	$('#email').blur(function(){validateInput('email','email');});
	$('#message').blur(function(){validateInput('message','');});
});
    

function validateInput(input, type){
	var error = false;
	switch(type){
		case "email":
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			if(!pattern.test($('#'+input).val())){
				error = true;
			}
			break;
		default:
			if($('#'+input).val()==""){error = true;}
			break;
	}
	if(error){
		$('#'+input).parent('li').addClass('warning');
		$('#'+input+':parent p').show();
		return false;
	}else{
		$('#'+input).parent('li').removeClass('warning');
		$('#'+input+':parent p').hide();
		return true;
	}
}
