function calcTabs() {
	$('.tab_container div:not(:first)').hide();
	$('.ui-tabs-nav li a:first').addClass('active');
	$('.tab_container div:first').addClass('active');
	
	$('.ui-tabs-nav li a').click(function(){
		$('.ui-tabs-nav li a.active').removeClass('active');
		$(this).addClass('active');
		
		$('.tab_container div.active').hide();
		$('.tab_container div.active').removeClass('active');
		
		var myPanel = $(this).map(function() { 
		  return this.hash || null; 
		}).get().join(',');
		
		$('.tab_container '+myPanel).show();
		$('.tab_container '+myPanel).addClass('active');
		
		return false;
	})	
}

function get_results(formtype){
	
	if(formtype == 'buying'){
		var owntype = $('#buyingForm input[name=owntype]:checked').val();
		var houseprice = $('#buyingForm input#house_price_buy').val();
		var datachain = 'ajaxLoad=' + formtype + '&owntype=' + owntype + '&input=' + houseprice;
		console.log(datachain);
		if(houseprice == '£' || !houseprice){
			$('#buyingForm .error').fadeIn(1000);
			$('#house_price_buy').addClass('invalid');
		}
		else{
			$.ajax({
					type: "POST",
					url: "/module/acms_conveyancingCalculator",
					data: datachain,
					cache: false,
					async: false,
					success: function(html){
   						var arr = html.split("||");
						for(i=0; i<arr.length; i++){
							var arr2 = arr[i].split("-");
							
							if(arr2[0] == 'reference_number'){
								$('.costsTableSelling td.'+arr2[0]).html(arr2[1]);
								var theRef = arr2[1];
							}
							
							$('.costsTableBuying td.'+arr2[0]).html('&pound;'+arr2[1]);
			
							//alert('.costsTableBuying td.'+arr2[0]);
					
							houseprice = houseprice.replace(/[^0-9]/,'');
					
							if(parseInt(houseprice) > 999999){
								$('.costsTableBuying td.legal_fee').html('<a href="/module/acms_enquiry">Contact us</a>');
							}
					
							
							//alert(arr);
							
							$('#buying .accept_estimate').attr({ href: "/module/acms_estimateEnquiry?enquiryForm_ref="+theRef});
							$('#buyingForm').animate({ height: "460px" }, 500);
							$('#house_price_buy').removeClass('invalid');
							$('.error').hide();
							$('.costsTableBuying').fadeIn(1000);
							$('#buying .estimate_links').fadeIn(1000);
						}
  					}

			}).responseText;
		}
	}
	else if(formtype == 'selling'){
		var owntype = $('#sellingForm input[name=owntype]:checked').val();
		var houseprice = $('#sellingForm input#house_price_sale').val();
		var datachain = 'ajaxLoad=' + formtype + '&owntype=' + owntype + '&input=' + houseprice;
		
		if(houseprice == '£' || !houseprice){
			$('#sellingForm .error').fadeIn(1000);
			$('#house_price_sale').addClass('invalid');
		}
		else{
			$.ajax({
					type: "POST",
					url: "/module/acms_conveyancingCalculator",
					data: datachain,
					cache: false,
					async: false,
					success: function(html){
   						var arr = html.split("||");
						for(i=0; i<arr.length; i++){
							var arr2 = arr[i].split("-");
							
							if(arr2[0] == 'reference_number'){
								$('.costsTableSelling td.'+arr2[0]).html(arr2[1]);
								var theRef = arr2[1];
							}
							else {
								$('.costsTableSelling td.'+arr2[0]).html('&pound;'+arr2[1]);
							}
							
							houseprice = houseprice.replace(/[^0-9]/,'');
							
							if(houseprice > 999999){
								$('.costsTableSelling td.legal_fee').html('<a href="/module/acms_enquiry">Contact us</a>');
							}
							
							//alert(arr);
							
							$('#selling .accept_estimate').attr({ href: "/module/acms_estimateEnquiry?enquiryForm_ref="+theRef});
							$('#sellingForm').animate({ height: "310px" }, 500);
							$('#house_price_sale').removeClass('invalid');
							$('.error').hide();
							$('.costsTableSelling').fadeIn(1000);
							$('#selling .estimate_links').fadeIn(1000);
						}
  					}

			}).responseText;
		}
	}
	else if(formtype == 'dual'){
		var owntype_purchase = $('#dualForm input[name=owntype_purchase]:checked').val();
		var owntype_sale = $('#dualForm input[name=owntype_sale]:checked').val();
		
		var houseprice_purchase = $('#dualForm input#house_price_dual_purchase').val();
		var houseprice_sale = $('#dualForm input#house_price_dual_sale').val();
		
		var datachain = 'ajaxLoad=' + formtype + '&owntype_purchase=' + owntype_purchase + '&owntype_sale=' + owntype_sale + '&input_purchase=' + houseprice_purchase + '&input_sale=' + houseprice_sale;
		
		if(houseprice_purchase == '£' || !houseprice_purchase){
			$('#dualForm .error').fadeIn(1000);
			$('#house_price_dual_purchase').addClass('invalid');
		}
		else if(houseprice_sale == '£' || !houseprice_sale){
			$('#dualForm .error').fadeIn(1000);
			$('#house_price_dual_sale').addClass('invalid');
		}
		else{
			$.ajax({
					type: "POST",
					url: "/module/acms_conveyancingCalculator",
					data: datachain,
					cache: false,
					async: false,
					success: function(html){
   						var arr = html.split("||");
						for(i=0; i<arr.length; i++){
							var arr2 = arr[i].split("-");
							
							if(arr2[0] == 'reference_number'){
								$('.costsTableDual td.'+arr2[0]).html(arr2[1]);
								var theRef = arr2[1];
							}
							else {
								$('.costsTableDual td.'+arr2[0]).html('&pound;'+arr2[1]);
							}
							
							houseprice_purchase = houseprice_purchase.replace(/[^0-9]/,'');
							houseprice_sale = houseprice_sale.replace(/[^0-9]/,'');
							
							if(houseprice_purchase > 999999){
								$('.costsTableDual td.purchase_legal_fee').html('<a href="/module/acms_enquiry">Contact us</a>');
							}
							
							if(houseprice_sale > 999999){
								$('.costsTableDual td.sale_legal_fee').html('<a href="/module/acms_enquiry">Contact us</a>');
							}
							
							//alert(arr);
							
							$('#dual .accept_estimate').attr({ href: "/module/acms_estimateEnquiry?enquiryForm_ref="+theRef});
							$('#dualForm').animate({ height: "645px" }, 500);
							$('#house_price_dual_purchase').removeClass('invalid');
							$('#house_price_dual_sale').removeClass('invalid');
							$('.error').hide();
							$('.costsTableDual').fadeIn(1000);
							$('#dual .estimate_links').fadeIn(1000);
						}
  					}

			}).responseText;
		}
	}
	else if(formtype == 'remortgage'){
		var owntype = $('#remortgageForm input[name=owntype]:checked').val();
		var houseprice = $('#remortgageForm input#house_price_remortgage').val();
		var datachain = 'ajaxLoad=' + formtype + '&owntype=freehold&input=' + houseprice;
		
		if(houseprice == '£' || !houseprice){
			$('#remortgageForm .error').fadeIn(1000);
			$('#house_price_remortgage').addClass('invalid');
		}
		else{
			$.ajax({
					type: "POST",
					url: "/module/acms_conveyancingCalculator",
					data: datachain,
					cache: false,
					async: false,
					success: function(html){
   						var arr = html.split("||");
						for(i=0; i<arr.length; i++){
							var arr2 = arr[i].split("-");
							
							if(arr2[0] == 'reference_number'){
								$('.costsTableRemortgage td.'+arr2[0]).html(arr2[1]);
								var theRef = arr2[1];
							}
							else {
								$('.costsTableRemortgage td.'+arr2[0]).html('&pound;'+arr2[1]);
							}
							
							houseprice = houseprice.replace(/[^0-9]/,'');
							
							if(houseprice > 999999){
								$('.costsTableRemortgage td.legal_fee').html('<a href="/module/acms_enquiry">Contact us</a>');
							}
							
							//alert(arr);
							
							$('#remortgage .accept_estimate').attr({ href: "/module/acms_estimateEnquiry?enquiryForm_ref="+theRef});
							$('#remortgageForm').animate({ height: "375px" }, 500);
							$('#house_price_remortgage').removeClass('invalid');
							$('.error').hide();
							$('.costsTableRemortgage').fadeIn(1000);
							$('#remortgage .estimate_links').fadeIn(1000);
						}
  					}

			}).responseText;
		}
	}
}

function printPage(){
	window.print();
}

function renderCalculator(){
	$.ajax({
			url: "/modules/acms_conveyancingCalculator/templates/calculator.php",
			cache: false,
			success: function(html){
				$("#calculator_container").html(html);
				/*$("#calculator_container #tabs").tabs();*/
				
				$('#house_price_buy').keyup(function(event){
					if (event.keyCode == 13) {
						var formId = $(this).parent().parent().parent().attr('id');
						get_results(formId);
					}
				});
				
				$('.tab_container a.submit').click(function(){
					var formId = $(this).parent().parent().attr('id');
					delayedResults(formId);
				})

				$('a.print_estimate').click(function(){ printPage(); })
				
				$('a.new_estimate').click(function(){ renderCalculator(); })
				
				$("a[rel^=\'prettyPopin\']").prettyPopin({});
				
				calcTabs();
				
				$('.resultTable a').click(function(){
					$('html').scrollTo({ top:0 }, 'slow');
				});

			}
	});
}

var delayedResultsTimeout;

function delayedResults(formId){
	clearTimeout(delayedResultsTimeout);
	delayedResultsTimeout = setTimeout('get_results(\''+formId+'\')', 0);
}








