/*
* UPDATE: Chris Cheshire, 28/07/2010, Implementation of new direct debit mandate functions and features
*/
$(function(){
	$("input#pgEdit").show();
	$(".pgHidden").show();
	$(".noticeText").hide(); 
	$(".redNote").hide(); 
	$(".PGSummaryText").hide();	
	
	$(".pgApplicationForm div.redNote").css("padding","0");
	$(".pgApplicationForm div.redNote").css("line-height","15px");
	$(".pgApplicationForm div.redNote").css("border","none");
	$(".pgApplicationForm div.redNote").css("color","#fe0000");
	$(".pgApplicationForm div.redNote").css("font-weight","bold");
	
if(($(".pgApplicationForm input.lawful:radio:checked").val() === "no") || ($(".pgApplicationForm input[name=attendance]:radio:checked").val() === "no")){
		$(".pgApplicationForm #policySummary").hide();
		$(".pgApplicationForm #policySummaryBox").slideUp("slow");
		$(".pgApplicationForm .notEligHide").hide("slow");
		$("input#pgNoThanks").val("Continue");
}

//When a cover radio button is clicked
$("input[name=cover]").click(function(){
var errors;
errors = 0;
var custCoverText = ($(".pgApplicationForm #custCoverText").val());
var lawful = $(".pgApplicationForm input.lawful:radio:checked").val();
var attendance = $(".pgApplicationForm input[name=attendance]:radio:checked").val();
//If custom cover is selected but the length is less than 2 characters
 if (($(this).val() === "other")&&(custCoverText.length < 1))
 {
	paymentHide();
	paymentShow();
	$(".pgApplicationForm span#customCoverPremiumNote").hide("slow");
	$(".pgApplicationForm p.amountErr").hide("slow");
}else{
		//Custom cover is selected
		if($(this).val() === "other")
		{		
			var maxCover = $(".pgApplicationForm input#hiddenMax").attr("name");
			var minCover = $(".pgApplicationForm input#hiddenMin").attr("name");
			customCover = parseInt(custCoverText);
			maxCover = parseInt(maxCover);
			minCover = parseInt(minCover);
		if((customCover > maxCover)||(customCover < minCover)||(isNaN(customCover))){
			errors++;
			$(".pgApplicationForm div.custCoverErr").show("slow");
			$(".pgApplicationForm #custCoverPlease").hide("slow");
			$(".pgApplicationForm span#customCoverPremiumNote").hide("slow");
			$(".pgApplicationForm p.amountErr").hide("slow");
			
		}else if((lawful === "no") || (attendance === "no")){
		errors++;
		}
		else{
		calculateCustomPremium();
			$(".pgApplicationForm span#customCoverPremiumNote").show("slow");
		}
		}
		
		if($(this).val() !== "other"){
		if((lawful === "no") || (attendance === "no")){
		 errors++;
		}else{
		$(".pgApplicationForm #custCoverPlease").hide("slow");
		$(".pgApplicationForm div.custCoverErr").hide("slow");
		}
		}

		if (errors == 0 ){
		$(".pgApplicationForm #custCoverPlease").hide("slow");
		
		$(".pgApplicationForm div#premSummaryRight").slideDown("slow");
		
		//Populate the bank account name and address fields
		var sortCode = $(".pgApplicationForm #sortCode").val();
		var accountNo = $(".pgApplicationForm #accountNo").val();
		bankCheckAjax(sortCode, accountNo, false);
		
		//Direct debit elements
		$(".pgApplicationForm .ddCont").hide();
		$(".pgApplicationForm #coverMe").show();
		$(".pgApplicationForm .ddPay").slideUp("slow");
		$(".pgApplicationForm div#PGSummary").slideDown("slow");
		$(".pgApplicationForm .ddInfo").slideDown("slow");
		$(".pgApplicationForm .ddCheck").slideDown("slow");
		
		//Credit Card elements
		$(".pgApplicationForm div.cardInfo").slideDown("slow");
		$(".pgApplicationForm div.ccCheck").slideDown("slow");
		
		$(".PGSummaryText").slideDown("slow");
		}
		//alert(errors);
	}

});	

//Does click function on page load if already clicked
if($("input[name=cover]:radio:checked")){
		$("input[name=cover]:radio:checked").click();
	}
	
//Policy questions click function
$("input.policyQs").click(function(){
var lawful = $(".pgApplicationForm input.lawful:radio:checked").val();
var attendance = $(".pgApplicationForm input[name=attendance]:radio:checked").val();
var errors = 0;

if((lawful === "no") || (attendance === "no")){
	errors++
	}

var radioName = $(this).attr("name");
var radioVal = $(this).val();

if((radioName === "lawful")||(radioName === "attendance")){
	if(radioVal === "no") {
	
	//Customer is not eligible so hide relevant bits
		$(".pgApplicationForm #policySummary").hide();
		$(".pgApplicationForm #policySummaryBox").slideUp("slow");
		$(".pgApplicationForm .notEligHide").hide("slow");
		$(".pgApplicationForm div.redNote").css("color","#fe0000");
		$(".pgApplicationForm div.redNote").css("font-weight","bold");
		$("input#pgNoThanks").val("Continue");
		errors++;
	}
}

//If no 'illegal' radios are checked, then show rest of the page
if (errors == 0){
		$(".pgApplicationForm p.uneligErr").hide();
		$(".pgApplicationForm #policySummaryBox").slideDown("slow");
		$(".pgApplicationForm .notEligHide").show("slow");
		$(".pgApplicationForm #policySummary").show();
		$("input#pgNoThanks").val("Continue with no insurance");
	}
});
	
//Set the edit button's click function
$('input#pgEdit').click(function() {
	$(".pgApplicationForm .ddCheck").slideUp("slow");
	$(".pgApplicationForm .ddPay").slideDown("slow");
	$(".pgApplicationForm #coverMe").hide();
	$(".pgApplicationForm .ddCont").show();
	});	
	
//If there is a bank error, show the enter bank details form
if(($("p.badDD").length)||($("p.Bank").length)){
		$('input#pgEdit').click();
	}

		
//Set the function of the ddCont button
$('.pgApplicationForm .ddCont').click(function() {
	var accountName = $(".pgApplicationForm #accountName").val();
	var sortCode = $(".pgApplicationForm #sortCode").val();
	var accountNo = $(".pgApplicationForm #accountNo").val();
	
	var errors = checkddDetails(accountName, sortCode, accountNo);
	
		if (errors <1){
			bankCheckAjax(sortCode, accountNo, true, "paymentGuardApply");
		}
});

$('.pgApplicationForm input#custCoverText').blur(function(){
$("input#custCover").click();
});

});

/* Checks if min cover is selected */
function checkMinAmountSelected()
{
	/* Set has JS */
	document.getElementById('hasJS').value = 'yes';

	return true;
}

function changeCoverAmount()
{
	/* Manually set the cover amount */
	document.getElementById('mincover').checked=false;
	document.getElementById('medcover').checked=true;
	/* Update monthly premium */
	document.getElementById('monthlyPaymentForm').value = premium['med']; 
	/* Submit the form */
	document.forms["paymentguardform"].submit();
}

function calculatePremiums(pre,cover)
{
	var annualPayment = premium[pre] * 12;
	if($(".pgApplicationForm #annualPayment").length){ document.getElementById('annualPayment').innerHTML = annualPayment.toFixed(2); }
	if($(".pgApplicationForm #annualPaymentTop").length){ document.getElementById('annualPaymentTop').innerHTML = annualPayment.toFixed(2); }
	if($(".pgApplicationForm #mb").length){ document.getElementById('mb').innerHTML = cover; }
	if($(".pgApplicationForm #monthlyPayment").length){ document.getElementById('monthlyPayment').innerHTML = premium[pre]; }
	if($(".pgApplicationForm #monthlyPaymentTop").length){ document.getElementById('monthlyPaymentTop').innerHTML = premium[pre]; }
	if($(".pgApplicationForm #monthlyPaymentForm").length){ document.getElementById('monthlyPaymentForm').value = premium[pre]; }
	if($(".pgApplicationForm #monthlyPayment2").length){ document.getElementById('monthlyPayment2').innerHTML = premium[pre]; }
	if($(".pgApplicationForm #annualPayment2").length){ document.getElementById('annualPayment2').innerHTML = annualPayment.toFixed(2); }
	if($(".pgApplicationForm #monthlyPaymentdd").length){ document.getElementById('monthlyPaymentdd').innerHTML = premium[pre]; }
}

function calculateCustomPremium(){
var customCover = $(".pgApplicationForm #custCoverText").val();
	$.ajax({
					type:		"POST",
					url:		"/ajax/getCustomPGCover.php",
					dataType:	"json",
					cache:		false,
					data: "customCover="+customCover,
					success:	function(json){
						if (json === false){
								$(".pgApplicationForm #custCoverPlease").show("slow");
								$(".pgApplicationForm div#premSummaryRight").hide("slow");
								paymentHide();
								paymentShow();
						}else{
								coverPrem = json;
								$(".pgApplicationForm span#mb").html(customCover);
								$(".pgApplicationForm span#monthlyPayment").html(coverPrem);
								$(".pgApplicationForm span#monthlyPaymentTop").html(coverPrem);
								$(".pgApplicationForm span#monthlyPayment2").html(coverPrem);
								var annualPrem = (coverPrem * 12);
								annualPrem = (annualPrem.toFixed(2));
								$(".pgApplicationForm span#annualPaymentTop").html(annualPrem);
								$(".pgApplicationForm span#annualPayment").html(annualPrem);
								$(".pgApplicationForm span#annualPayment2").html(annualPrem);
								$(".pgApplicationForm span.custMonPrem").html(coverPrem);
								$(".pgApplicationForm span.custAnnPrem").html(annualPrem);
								$(".pgApplicationForm span#monthlyPaymentdd").html(coverPrem);
								$(".pgApplicationForm div.custCoverErr").hide("slow");
								document.getElementById('monthlyPaymentForm').value = coverPrem;
							}
						}
			});	
		}

function paymentHide(){
	$(".pgApplicationForm div.custCoverErr").hide("slow");
	$(".pgApplicationForm div#PGSummary").slideUp("slow");
	$(".pgApplicationForm .ddInfo").slideUp("slow");
	$(".pgApplicationForm .ddCheck").slideUp("slow");
	$(".pgApplicationForm .ddCont").hide();
	$(".pgApplicationForm div.cardInfo").slideUp("slow");
	$(".pgApplicationForm div.ccCheck").slideUp("slow");
}

function paymentShow(){
	$(".pgApplicationForm span#custCoverPlease").slideDown("slow");
	$(".PGSummaryText").slideUp("slow");
}