/*
* UPDATE: Chris Cheshire, 28/07/2010, Implementation of new direct debit mandate functions and features
*/
$(function(){
	$("a#pgEdit").show();
	$(".pgHidden").show();
	$(".noticeText").not('#pgTemporaryWorkWarning, #pgOccupationWarning').hide();
	$(".redNote").hide();
	//$(".yellowNote").hide();
	$(".PGSummaryText").hide();
	$("div#PGStaticPrintLink").show();

	$("a#PGPolicyPrint").click(function(){
		window.open("https://secure.loanfinder.co.uk/application/paymentguard/includes/printablePGPolicy", "_blank", "width=1100, height=900, scrollbars=1, top=25, left=110");
		return false;
	});
	
	$(".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() === "yes")||($(".pgApplicationForm input[name=workingTime]:radio:checked").val() === "no") ){
		$(".pgApplicationForm #policySummary").hide();
		$(".pgApplicationForm #policySummaryBox").slideUp("slow");
		$(".pgApplicationForm .notEligHide").hide("slow");
}

//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();
var workingTime = $(".pgApplicationForm input[name=workingTime]: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 < "50")||(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 === "yes")||(workingTime === "no")){
				errors++;
			}else{
				calculateCustomPremium();
				$(".pgApplicationForm span#customCoverPremiumNote").show("slow");
			}
		}
		
		if($(this).val() !== "other"){
			$(".pgApplicationForm div.custCoverErr").hide("slow");
			
			if((lawful === "no") || (attendance === "yes")||(workingTime === "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();
		
		if ((sortCode != undefined)&&(accountNo != undefined)){
		bankCheckAjax(sortCode, accountNo, false);
		}
		
		//Direct debit elements
		$(".pgApplicationForm .ddCont").show();
		$(".pgApplicationForm #coverMe").hide();
		$(".pgApplicationForm .ddPay").slideUp("slow");
		$(".pgApplicationForm div#PGSummary").slideDown("slow");
		$(".pgApplicationForm .ddInfo").slideDown("slow");
		$(".pgApplicationForm #ddPayBox").slideDown("slow");
		
		//Credit Card elements
		$(".pgApplicationForm div.cardInfo").slideDown("slow");
		$(".pgApplicationForm div.ccCheck").slideDown("slow");
		
		$(".PGSummaryText").slideDown("slow");
		}
		//alert(errors);
	}

});	

//Do 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 workingTime = $(".pgApplicationForm input[name=workingTime]:radio:checked").val();
	var errors = 0;
	
	if((lawful === "no") || (attendance === "yes") || (workingTime === "no")){
		errors++
	
		}
	
	var radioName = $(this).attr("name");
	var radioVal = $(this).val();
	
	//if((radioName === "lawful")||(radioName === "attendance")||(radioName === "workingTime")){
	//	if((((radioName==="lawful")||(radioName==="workingTime"))&&(radioVal === "no"))||((radioName==="attendance")&&(radioVal === "yes"))){
	if(radioName === "lawful" && radioVal === "no"){
		//Customer is not eligible so hide relevant bits
		$("#pgNoThanks").css('top', '0px');
		$(".pgApplicationForm #policySummary").hide();
		$(".pgApplicationForm #policySummaryBox").slideUp("slow");
		$(".pgApplicationForm .notEligHide").hide("slow");
		$(".pgApplicationForm div.redNote").css("color","#fe0000");
		$(".pgApplicationForm div.redNote").css("font-weight","bold");
		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();
		$("#pgNoThanks").css('top', '-68px');		
	}
});

//Check for pre population
if($(".pgApplicationForm input.policyQs:radio:checked")){
	$(".pgApplicationForm input.policyQs:radio:checked").click();
}
	
	
//Set the edit button's click function
$('a#pgEdit').click(function() {
	$(".pgApplicationForm .ddCheck").slideUp("slow");
	$(".pgApplicationForm .ddPay").slideDown("slow");
	$(".pgApplicationForm #coverMe").hide();
	$(".pgApplicationForm .ddCont").show();
	return false;
	});	
	
//If there is a bank error, show the enter bank details form
if(($("p.badDD").length)||($("p.Bank").length)){
		$('a#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");
		}
		return false;
});

$('.pgApplicationForm input#custCoverText').blur(function(){
$("input#custCover").click();
});

//Function for if the enter button is used in ddPay
$(".pgApplicationForm").bind("keypress", function(e) {
		if(e.keyCode == 13){
			$('.pgApplicationForm .ddCont').click();
			return false;
			}else{
			return true;
			}
		});

});

/* 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){
						//console.log(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");
}
