// JavaScript Document

function fncValidateMiniForm (f) {

		var i;
		var form_validation = new Array();

		form_validation['name'] = new Array('Name', 1);
		form_validation['tel'] = new Array('Contact Tel No', 1,"telephone");
		form_validation['area'] = new Array('Email', 1,"email");

		var form_valid = F.Validate(f, form_validation);

		if (!form_valid) {
			return false;
		} else {
			return true;
		}
	}
	
/*
Ultimate Age calculator script- By JavaScript Kit (http://www.javascriptkit.com)
Over 200+ free scripts here!
Credit must stay intact for use
*/

var one_day=1000*60*60*24
var one_month=1000*60*60*24*30
var one_year=1000*60*60*24*30*12

function displayage(yr, mon, day, unit, decimal, round){
  var today=new Date();
  var pastdate=new Date(yr, mon-1, day);
  
  var countunit=unit;
  var decimals=decimal;
  var rounding=round;
  
  var finalunit=(countunit=="days") ? one_day : (countunit=="months")? one_month : one_year;
  decimals=(decimals <= 0)? 1 : decimals*10;
  
  if (unit!="years"){
  	if (rounding=="rounddown")
  		return(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals);
  	else
  		return(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals);
  	}
  else{
	var yearspast=today.getFullYear()-yr-1;
	var tail=(today.getMonth() > (mon-1) || today.getMonth() == mon-1 && today.getDate() >= day) ? 1 : 0;
	pastdate.setFullYear(today.getFullYear());
	pastdate2=new Date(today.getFullYear()-1, mon-1, day);
	tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals;
	return(yearspast+tail);
}
}	
	
$(document).ready(function() {
	
	$(".Error").live("change",function() {
			$(this).removeClass("Error");
			$(this).addClass("Error"); // ie7 frigg
	});

	$("input").click(function() {
		var  initialval =  $(this).attr("title");
		if ($(this).val()==initialval)
			$(this).val("");
	});
	$("input").blur(function() {
		var initialval =  $(this).attr("title");
		if ($(this).val()=="")
			$(this).val(initialval);
	});
	
	$("#logo").click (function() { window.location = "http://www.auto-money.co.uk"; });
	
	$('.click-applybtn').hover(function() {
		$(this).css('cursor','pointer');
	}, function() {
		$(this).css('cursor','auto');
	});
	$(".click-applybtn").click( function() {
		simpleRedirect("http://www.auto-money.co.uk/apply-now/");
	});
	
	// form handling
	          
			var callback = { 
				success:   showResponse,  // post-submit callback 
				dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
			}; 
			
			 

			 var redirect = { 
				success:   doRedirect,  // post-submit callback 		 
				dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
			}; 
	
			function showResponse(responseText, statusText, xhr, $form)  { 			 
				$(responseText.div).replaceWith("<p>"+responseText.response+"</p>");		
			} 
			
			function doRedirect(responseText, statusText, xhr, $form)  { 	
			//	alert("[TENG_BaseRef]"+responseText.response);	 
				window.location=responseText.response;
			} 
			
			function simpleRedirect(responseText)  { 	
			//	alert("[TENG_BaseRef]"+responseText.response);	 
				window.location=responseText;
			} 
			
			
            
			//$('#request_callback').ajaxForm(function(callback) {});

			$("#btn_introducerpack").click(function() { $("#introducer_pack").ajaxSubmit(callback); return false;});			
			$("#btn_applyonline").click(function() { $("#apply_online").ajaxSubmit(redirect); return false;});
			$("#submitrequest").click(function() { $("#appform").ajaxSubmit(callback); return false;});
			
			$("#btn_callback").click(function() 	{ 
					if ( $("#mfrm_name").val()!="Name" && $("#mfrm_telephone").val()!= "Telephone" ) {
							$("#request_callback").ajaxSubmit(callback); 
					}
					return false; 
			});
			
	$("#faq_q").change(function() {
		get_faq();
	});
	
	function get_faq() {
		var pid = $("#faq_q option:selected").val();
		var link = "http://www.auto-money.co.uk/hq/lib/core/modules/faq/ajax/";
		if (pid!="") {
			$.post(link + "get_data.php", { pid: pid },  
											function(data){ 
										 		
												// consider putting up a saving data panel
												if (data.status=="OK") {
													// reove the panel from here
													 var lsRegExp = /\+/g;
													var answer = unescape(data.faq_a).replace(lsRegExp, " ");

													$("#faq_a").html(answer);
							//						alert("SAVED");
												}
												 // Interact with returned data  
											 }, "json");
		}
	}
	
	if($("#faq_q")) { get_faq();}

});

