$().ready(function() {
	payType();
	disNewPayment();
	showAllCards();
	validator();
});


// page defaults

// disable payment amount for new card if payment type set to min percent
	var payType = function()
	{
		$('#newPaymentType').bind("change", function()
			{
			if (this.value == 2) { $('#newPayment').attr("disabled","true") }
			else { $('#newPayment').removeAttr("disabled") }
			}
		);
	}

// disable field if JS enabled
	var disNewPayment = function()
	{
	$('#newPayment').attr("disabled","true");
	}

// show all cards
	var showAllCards = function()
	{
		$('img.allCards').bind("click", function() {
			$('#resultText,#cardtable_a,#cardinfo_b,#cardtable_b').html('');
			$("#cardtable_b").load("/com/card-guide-table.html?categoryType=catstudent");
			return false;
		});
	}

// validate the form
	var validator = function()
	{
	var errorText = "An error occured while processing your form. Please try again.";
	var buttonSrc = "/assets/images/button/search-card.png";
	var buttonSrcActive = "/assets/images/button/search-card-wait.png";
	var container = $('div.msgtext');
	$('#w').validate
	(
	{
		onblur: false,
		submitHandler: function(form)
			{
			$('.subbut').attr("src",buttonSrcActive).attr("disabled","true");
			$('#messages').hide();
			$('#resultText,#cardtable_a,#cardinfo_b,#cardtable_b').html('');
			$('div.msgtext li').remove();
			$('#subtype').val('ajax');
			$(form).ajaxSubmit
				(
				{
				url: '/com/studentCardProcess.html',
				dataType: 'json',
				error: function()
					{
						$('.subbut').attr("src",buttonSrc).removeAttr("disabled");
						alert(errorText)
					},
				success: function(responseText,statusText)
					{
						$('.subbut').attr("src",buttonSrc).removeAttr("disabled");
						if(!statusText || statusText != "success")
							{
							alert(errorText);
							}
						else
							{
							if (typeof responseText.SUCCESS == undefined || responseText.SUCCESS != 1)
								if (responseText.ERRORARRAY[0] != '' && responseText.ERRORARRAY[0] != undefined)
									{
										var errorMessageBlock = '';
										$.each( responseText.ERRORARRAY, function(i,n)
											{
											errorMessageBlock = errorMessageBlock+'<li>'+responseText.ERRORARRAY[i]+'</li>';
											} );
										$('#messages,#msgtext,#msgtext ul').show();
										$('#msgtext ul').prepend(errorMessageBlock);
									}
								else
									{
										$('#messages').show();
										$('#msgtext').show().html(errorText);
									}
							else if(responseText.SUCCESS == 1)
								{
								var displayDisclaimer = false;
								var resultBlock = '';
								if (responseText.EXISTINGCARDINTEREST > 0)
									{
									var existingYearsdata = '';
									var calcMonthlyPayment = '';
									if (responseText.EXISTINGCARDYEARS == 1)
										{
										existingYearsdata = responseText.EXISTINGCARDYEARS+' year and ';
										}
									else if (responseText.EXISTINGCARDYEARS > 1)
										{
										existingYearsdata = responseText.EXISTINGCARDYEARS+' years and ';
										}
									if ($('#existingPaymentType').val() == 1)
										{
										calcMonthlyPayment = '&pound;'+$('#existingPayment').val();
										}
									else
										{
										calcMonthlyPayment = $('#existingPayment').val()+'%';
										}
									resultBlock = resultBlock+'<p class="sub-title">Your current '+$('#existingInterestRate').val()+'% APR card would take you:</p><p><b>'+existingYearsdata+responseText.EXISTINGCARDMONTHS+' months</b> and cost you <b>&pound;'+responseText.EXISTINGCARDINTEREST+' in interest</b> to pay off <b><font color="red">&pound;'+$('#existingAmountOwed').val()+'</font> at '+calcMonthlyPayment+' per month</b>*</p>';
									displayDisclaimer = true;
									}
								if (responseText.EXISTINGCARDINTEREST > 0 && responseText.NEWCARDINTEREST > responseText.EXISTINGCARDINTEREST)
									{
									resultBlock = resultBlock+'<p class="sub-title">There are no better cards available</p>';
									}
								else if (responseText.EXISTINGCARDINTEREST == 0 || responseText.NEWCARDINTEREST <= responseText.EXISTINGCARDINTEREST)
									{
									var newYearsdata = '';
									var calcMonthlyPayment = '';
									if (responseText.NEWCARDYEARS == 1)
										{
										newYearsdata = responseText.NEWCARDYEARS+' year and ';
										}
									else if (responseText.NEWCARDYEARS > 1)
										{
										newYearsdata = responseText.NEWCARDYEARS+' years and ';
										}
									if (responseText.NEWCARDPAYMENTTYPE == 1)
										{
										calcMonthlyPayment = '&pound;'+responseText.NEWCARDPAYMENT;
										}
									else
										{
										calcMonthlyPayment = responseText.NEWCARDPAYMENT+'%';
										}
									resultBlock = resultBlock+'<p class="sub-title">Our recommended '+responseText.NEWCARDAPR+'% APR card would take you:</p><p><b>'+newYearsdata+responseText.NEWCARDMONTHS+' months</b> and cost you <b>&pound;'+responseText.NEWCARDINTEREST+' in interest</b> to pay off <b><font color="red">&pound;'+responseText.NEWCARDAMOUNTOWED+'</font> at '+calcMonthlyPayment+' per month</b></p>';
									$("#cardtable_a").load("/com/card-guide-table.html?carduuid="+responseText.NEWCARDDATA);
									}
								if (responseText.EXISTINGCARDBT == 1)
									{
									var btcardText = '<p class="sub-title">Another consideration:</p><p>Your existing monthly payment will pay off your balance in less than 9 months. Maybe consider a balance transfer card and pay the card off without interest charges.</p>';
									$('#cardinfo_b').show().html(btcardText);
									$("#cardtable_b").load("/com/card-guide-table.html?carduuid="+responseText.EXISTINGCARDBTDATA);
									}
								if (displayDisclaimer)
									{
									$("#card_disclaimer").html('* These figures may be affected by the minimum monthly payment on your card.');
									};
								$('#messages').show();$('#resultText').show().html(resultBlock);
								}
							else
								{ $('#messages').show();$('#resultText').show().html(errorText); }
							}
					}
				}
				);
			},
		focusInvalid: false,
		rules: {
				existingInterestRate: { required: function() { return $("[@name=existingAmountOwed]").is(":filled") || $("[@name=existingPayment]").is(":filled") }, number: true, rangeValue: [1,100] },
				existingAmountOwed: { required: function() { return $("[@name=existingInterestRate]").is(":filled") || $("[@name=existingPayment]").is(":filled") }, number: true, rangeValue: [1,50000] },
				existingPayment: { required: function() { return $("[@name=existingInterestRate]").is(":filled") || $("[@name=existingAmountOwed]").is(":filled") }, number: true},
				newAmountOwed: { number: true, rangeValue: [1,500] },
				newPayment: { required: function() { return $("[@name=newPaymentType]").val() == 1 }, number: true}
				},
		messages: {
				existingInterestRate: "Please enter a valid percentage for your existing cards interest rate (APR).",
				existingAmountOwed: "Please enter a valid amount owed on your existing card (between 1 and 50000).",
				existingPayment: "Please enter a valid amount for the monthly payments made on your existing card.",
				newAmountOwed: "&pound;500 is the maximum credit limit for a student credit card. Please enter a valid amount between 1 and 500.",
				newPayment: "Please enter a valid amount for the expected monthly payment on your new card."
				},
		errorContainer: container,
		errorLabelContainer: $("ul", container),
		errorElement: "li"
	}
	);
	}
