// JavaScript Document
$('document').ready(function(){
	$("#work_phone_number").mask("(99) 9999 9999");
	$("#mobile").mask("9999 999 999");
	$loadedForm	= $('form.newform').html();
	$('table.oqtbl tr').each(function(){
		$('th:first,td:first',$(this)).css('border-left','0px solid');
	});
});

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function isInt( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;
  return objRegExp.test(strValue);
}

function pickLoanPurpose( value )
{
	switch (value) {
		case 'Business Use':
			$(':radio[name=type_of_finance]').each(function(){
				if ( $(this).val() == 'Consumer Loan' || $(this).val() == 'Personal Lease' ) {
					$(this).attr('disabled',true).attr('checked',false);
				} else {
					$(this).attr('disabled',false).attr('checked',false);
				}
			});
			break;
		case 'Private Use':
			$(':radio[name=type_of_finance]').each(function(){
				if ( $(this).val() == 'Chattel Mortgage' || $(this).val() == 'Commercial Hire Purchase' || $(this).val() == 'Finance Lease' ) {
					$(this).attr('disabled',true).attr('checked',false);
				} else {
					$(this).attr('disabled',false).attr('checked',false);
				}
			});
			break;
		case 'Unsure':
			$(':radio[name=type_of_finance]').attr('disabled',true).attr('checked',false);
			break;
		default:
			$(':radio[name=type_of_finance]').attr('disabled',false).attr('checked',false);
			break;
	}
	changeProduct('');
	$(':input[name=term]').val('');
	changeTerm('');
}

function changeProduct(value)
{
	if ( $(':input[name=loan_purpose]').val() == '' ) {
		$(':input[name=loan_purpose]').addClass('error').parents('tr').addClass('error');
		$(':radio[name=type_of_finance]').attr('checked',false);
		return false;
	}
	if ( value == 'Novated Lease' || value == 'Finance Lease' ) {
		$(':input[name=deposit_trade_in]').val('0').attr('disabled',true);
	} else {
		$(':input[name=deposit_trade_in]').attr('disabled',false);
	}
	if ( value == 'Novated Lease' || value == 'Finance Lease' || value == 'Personal Lease' ) {
		if ( !$('tr.added-field').length ) {
			$newfields		= '<tr class="added-field">';
			$newfields		+= '	<th><label for="gst"><span>*</span> Does GST apply?</label></th>';
			$newfields		+= '	<td>';
			$newfields		+= '		<select name="gst" id="gst" style="width:81px;" onchange="changeGST(this.value);">';
			$newfields		+= '			<option value="">&nbsp;</option>';
			$newfields		+= '			<option value="Yes">Yes</option>';
			$newfields		+= '			<option value="No">No</option>';
			$newfields		+= '			<option value="Unsure">Unsure</option>';
			$newfields		+= '		</select> ';
			$newfields		+= '		<img src="../images/help.gif" width="19" height="19" alt="Goods and Services Tax (GST) - Is GST included in the purchase price of the car?" class="cvtip" />';
			$newfields		+= '	</td>';
			$newfields		+= '</tr>';
			$newfields		+= '<tr class="added-field">';
			$newfields		+= '	<th><label for="gst_credit" class="unmandotary">GST input credit ($):</label></th>';
			$newfields		+= '	<td>';
			$newfields		+= '		<input type="text" value="" name="gst_credit" style="text-align:right;" id="gst_credit" size="7" onblur="return changeGSTCredit(this.value);" class="txt" /> ';
			$newfields		+= '		<img src="../images/help.gif" width="19" height="19" alt="GST Input Credit - The amount of GST included in the purchase price of the car. The maximum Input Credit that can be claimed is 1/11 of the Luxury Car Tax (LCT) threshold or $'+MAX_GST_INPUT_FULL+'." class="cvtip" />';
			$newfields		+= '	</td>';
			$newfields		+= '</tr>';
			$('tr#tpp').after($newfields);
			$('tr.added-field img.cvtip').cvTip();
		}
	} else {
		$('tr.added-field').remove();
	}
	$(':input[name=term]').val('');
	changeTerm('');
	changeGST($(':input[name=gst]').val());
}

function changeTotalPurchase(val)
{
	if ( val != '' && !isInt(val) ) {
		$(':input[name=total_purchase_price]').focus().addClass('error').parents('tr').addClass('error');
		return false;
	} else {
		$(':input[name=total_purchase_price]').removeClass('error').parents('tr').removeClass('error');
		if ( $(':input[name=gst]').length ) {
			changeGST($(':input[name=gst]').val());
			$gstVal		= $(':input[name=gst_credit]').val();
		} else {
			$gstVal		= 0;
		}
		updateAmount('');
	}
}

function updateDeposit(val)
{
	if ( val != '' ) {
		if ( !isInt(val) ) {
			$(':input[name=deposit_trade_in]').focus().addClass('error').parents('tr').addClass('error');;
			return false;
		} else {
			$(':input[name=deposit_trade_in]').removeClass('error').parents('tr').removeClass('error');;
			updateAmount('');
		}
	} else {
		$(':input[name=deposit_trade_in]').removeClass('error').parents('tr').removeClass('error');;
		updateAmount('');
	}
}

function changeGST(value)
{
	if (value=='No' || value == 'Unsure') {
		$(':input[name=gst_credit]').val(0).keypress(function(){return false});
	} else if ( value == 'Yes' ) {
		$tpp		= ($(':input[name=total_purchase_price]').val()!='') ? parseInt($(':input[name=total_purchase_price]').val()) : 0;
		if ( $tpp > MAX_GST_INPUT*11 ) {
			$(':input[name=gst_credit]').val(MAX_GST_INPUT);
		} else {
			$(':input[name=gst_credit]').val(Math.round($tpp/11));
		}
		$(':input[name=gst_credit]').keypress(function(){return true});
	} else {
		$(':input[name=gst_credit]').keypress(function(){return true});
	}
	updateAmount('');
}

function changeGSTCredit(val)
{
	$total_purchase_price	= $(':input[name=total_purchase_price]').val();
	if ( !isInt(val) || val > $total_purchase_price/11 ) {
		$(':input[name=gst_credit]').val( ($(':input[name=total_purchase_price]').val()/11 <= MAX_GST_INPUT) ? Math.round($(':input[name=total_purchase_price]').val()/11) : MAX_GST_INPUT );
	}
	updateAmount('');
}

function calculateFinance()
{
	$('.error').removeClass('error');
	$aError		= new Array();
	if ($(':input[name=loan_purpose]').val()=='') {
		$(':input[name=loan_purpose]').addClass('error').parents('tr').addClass('error');
		$aError.push("Please select Loan purpose.");
	}
	if ($(':radio[name=type_of_finance]:enabled').length && !$(':radio[name=type_of_finance]:checked').val()) {
		$(':radio[name=type_of_finance]').parents('tr').addClass('error');
		$aError.push("Please select Finance type.");
	}
	$total_purchase_price	= $(':input[name=total_purchase_price]').val();
	$deposit_trade_in		= $(':input[name=deposit_trade_in]').val();
	if ( $total_purchase_price == '' || !isInt($total_purchase_price) ) {
		$(':input[name=total_purchase_price]').addClass('error').parents('tr').addClass('error');
		$aError.push("Please enter Total purchase price.");
	}
	if ( $('tr.added-field').length && $(':input[name=gst]').val() == '' ) {
		$(':input[name=gst]').addClass('error').parents('tr').addClass('error');
		$aError.push("Please select Does GST apply?");
	}
	if ( $aError.length != 0 ) {
		return false;
	} else {
		$total_purchase_price	= ($total_purchase_price!='') ? parseInt($total_purchase_price) : 0;
		$deposit_trade_in		= ($deposit_trade_in!='') ? parseInt($deposit_trade_in) : 0;
		$gst_credit				= $(':input[name=gst_credit]').val();
		if ( $('tr.added-field').length && $(':input[name=gst]').val() == '' ) {
			$(':input[name=gst]').addClass('error').parents('tr').addClass('error');
			$aError.push("Please select Does GST apply?");
		}
		if ( ( $gst_credit != '' &&  $gst_credit != undefined ) && !isInt($gst_credit) ) {
			$(':input[name=gst_credit]').addClass('error').parents('tr').addClass('error');
			$aError.push("Please enter GST input credit.");
		}
		if ( $('tr.added-field').length && $(':input[name=gst]').val() == 'Yes' ) {
			$gst			= Math.round($(':input[name=gst_credit]').val());
		} else {
			$gst			= 0;
		}
		if ( $gst > MAX_GST_INPUT && $(':radio[name=type_of_finance]').size() == 6 ) { $gst = MAX_GST_INPUT;$(':input[name=gst_credit]').val(MAX_GST_INPUT); }
		$amount_financed	= parseInt($total_purchase_price) - $gst - parseInt($deposit_trade_in);
		if ( $amount_financed < 0 ) {
			$(':input[name=deposit_trade_in]').addClass('error').parents('tr').addClass('error');
			$aError.push("Please enter Deposit/Trade-in amount.");
		}
		if ( $aError.length != 0 ) {
			return false;
		} else {
			updateAmount($amount_financed);
		}
	}
}

function updateAmount( val )
{
	$(':input[name=amount_financed]').val( val );
	$('#amount_financed_show').val( val );
	if ( val == '' ) {
		$(':input[name=residual_balloon]').val('');
		changeBalloon('');
	} else {
		changeBalloon($(':input[name=residual_balloon]').val());
	}
}

function changeTerm(val)
{
	if ( val == '' ) {
		$(':input[name=residual_balloon]').html('<option value="">&nbsp;</option>');
		changeBalloon('');
		return false;
	}
	$type_of_finance		= $(':radio[name=type_of_finance]:checked').val();
	if ( $type_of_finance == undefined && $(':input[name=loan_purpose]').val() != 'Unsure' ) {
		alert( "Please select Loan Purpose." );
		$(':input[name=term]').val('');
		return false;
	} else {
		if ( $type_of_finance == 'Novated Lease' || $type_of_finance == 'Finance Lease' ) {
			switch ( val ) {
				case '1':
					$startPercent	= 55;
					$lastPercent	= 65;
					break;
				case '2':
					$startPercent	= 45;
					$lastPercent	= 55;
					break;
				case '3':
					$startPercent	= 35;
					$lastPercent	= 50;
					break;
				case '4':
					$startPercent	= 25;
					$lastPercent	= 40;
					break;
				case '5':
					$startPercent	= 20;
					$lastPercent	= 35;
					break;
			}
			$slbRB		= '<option value="">&nbsp;</option>';
//			$slbRB		+= '<option value="0">0.00</option>';
			for ( $i=$startPercent; $i<=$lastPercent; $i+=5 ) {
				$slbRB		+= '<option value="'+$i+'">'+$i+'.00</option>';
			}
			$slbRB		+= '<option value="Other">Other</option>';
			$(':input[name=residual_balloon]').html($slbRB);
		} else {
			switch ( val ) {
				case '1':
					$lastPercent	= 65;
					break;
				case '2':
					$lastPercent	= 55;
					break;
				case '3':
					$lastPercent	= 50;
					break;
				case '4':
					$lastPercent	= 45;
					break;
				case '5':
					$lastPercent	= 40;
					break;
			}
			$slbRB		= '<option value="">&nbsp;</option>';
			for ( $i=0; $i<=$lastPercent; $i+=5 ) {
				$slbRB		+= '<option value="'+$i+'">'+$i+'.00</option>';
			}
			$slbRB		+= '<option value="Other">Other</option>';
			$(':input[name=residual_balloon]').html($slbRB);
		}
		changeBalloon('');
	}
}

function changeBalloon( val )
{
	if ( val == '' ) {
		$(':input[name=balloon_residual]').attr('disabled',false).val('');
	} else {
		$amount_financed		= $(':input[name=amount_financed]').val();
		if ( $amount_financed == '' ) {
			$(':input[name=amount_financed_show]').addClass('error').parents('tr').addClass('error');
			$(':input[name=residual_balloon]').val('');
			return false;
		} else {
			if ( val == 'Other' ) {
				$(':input[name=balloon_residual]').attr('disabled',false).val('').focus();
			} else {
				$(':input[name=balloon_residual]').val(Math.round(val*$amount_financed/100)).attr('disabled',true)/*keypress(function(){return false;})*/;
			}
		}
	}
}

function doReset()
{
	$('form.newform').html($loadedForm);
	$("#work_phone_number").mask("(99) 9999 9999");
	$("#mobile").mask("9999 999 999");
	$('img.cvtip').cvTip();
}