// JavaScript Document
$('document').ready(function(){
	$("#work_phone_number").mask("(99) 9999 9999");
	$("#mobile").mask("9999 999 999");
	$loadedForm	= $('form.form').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 changeProduct(value)
{
	if ( value == 'Small Ticket Finance' || value == 'Finance Lease' || value == 'Equipment Rental' ) {
		$(':input[name=deposit_trade_in]').val('0').attr('disabled',true);
//		$(':input[name=deposit_trade_in]').val('0').keypress(function(){return false});
	} else {
		$(':input[name=deposit_trade_in]').attr('disabled',false);
//		$(':input[name=deposit_trade_in]').keypress(function(){return true});
	}
	if ( value == 'Equipment Rental' || value == 'Small Ticket Finance' || value == 'Finance 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><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 goods?" 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><input type="text" value="" name="gst_credit" style="text-align:right;" id="gst_credit" size="7" class="txt" onblur="return changeGSTCredit(this.value);" /> ';
			$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 goods." 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('');
	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;
		$(':input[name=gst_credit]').val(Math.round($tpp/11));
		$(':input[name=gst_credit]').keypress(function(){return true});
	} else {
		$(':input[name=gst_credit]').val('').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]: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 ( $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 );
}

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