$(function(){
	$('#documents').hide();
	$('#refinance_totals').hide();
	calculateTotal();
 
	$('#documentNo').click(function(){
		var documentNo = $('#documentNo').val();
		$('#documentsTable').remove();
		if(documentNo >0){
			var tableString = '<table id="documentsTable" cellspacing="5"> <tr><td>Charge to:</td><td>Type</td><td>Pages:</td></tr>';
			var buyerSellerString = '<select class="buyer_or_seller"><option>Buyer</option><option>Seller</option></select>';
			var typeString = '<select class="docType"><option>Deed</option><option>Mortgage</option>';
			typeString += '<option>Release</option><option>Power of Attorney</option><option>Assignment</option>';
			typeString += '<option>Trust Documentation</option></select>';
			var pageString = '<select class="pageNo">';
			for(var j=0; j<=24; j++){
				pageString+='<option>'+j+'</option>';
			}
			pageString+='</select>';
			for(var i=1; i<=documentNo; i++){
				tableString+='<tr><td>'+buyerSellerString+'</td><td>'+typeString+'</td><td>'+pageString+'</td></tr>';			
			}
			tableString +='</table>';
			$('#documents').append(tableString);
			$('#documents').fadeIn();
		}else{
			$('#buyer_recording_fees').html('$0.00');
			$('#seller_recording_fees').html('$0.00');
			$('#borrow_recording_fees').html('$0.00');
			calculateTotal();
		}
	});
	
	$('#type').click(function(){
		var type = $(this).val();
		if(type == 'Purchase'){
			$('#refinance_totals').hide();
			$('#salesPrice').show();
			$('#sales_pTag').show();
			$('#split').parent().show();
			$('#purchase_totals').show();
			$('#button_hide').show();
		}else{
			$('#purchase_totals').hide();
			$('#salesPrice').hide();
			$('#sales_pTag').hide();
			$('#split').parent().hide();
			$('#refinance_totals').show();
			$('#button_hide').hide();
		}
	
	});
	//This function is to split the cost of the title insurance 50/50
	$('#split').click(function(){
		var split = $('#split:checked').val();
		var newValue;
		var buyerOldVal = parseDollarPrice($('#buyer_title_insurance').html());
		var sellerOldVal = parseDollarPrice($('#seller_title_insurance').html());
		if(buyerOldVal > 0 && sellerOldVal > 0){
			if(split != null){
				//It was checked
				newValue = ((buyerOldVal+sellerOldVal)/2);
				$('#seller_title_insurance').html('$'+newValue.toFixed(2));
				$('#buyer_title_insurance').html('$'+newValue.toFixed(2));
				calculateTotal();
			}else{
				//They DO NOT want to split the cost
				$('#salesPrice').trigger('change');
				$('#loanAmount').trigger('change');
				calculateTotal();
			}
		}else{
			alert("Before you can split the title insurance, you must enter a sales price and a loan amount.");
			$(this).attr('checked', false);
		}
	
	});
	
	$('#insured_closing_letter').click(function(){
		var insured = $('#insured_closing_letter:checked').val();
		if(insured != null){
			$('#buyer_closing_letter').html("$25.00");
			$('#borrow_closing_letter').html('$25.00');
		}else{
			$('#buyer_closing_letter').html("$0.00");
			$('#borrow_closing_letter').html('$0.00');			
		}
		calculateTotal();
	});
	
	$('#salesPrice').change(function(){
		//This is so if the value is changed it unchecks the 50/50 split so the user can click it again to recalculate
		$('#split').attr('checked', false);
		var loanPrice = $(this).val();
		var intPrice = fixPrice(loanPrice);
		
		//This part is for the closing fee calculation
		if(intPrice <= 499999 && intPrice > 0){
			$('#buyer_closing_fee').html('$250.00');
			$('#seller_closing_fee').html('$250.00');
		}
		if(intPrice >=500000 && intPrice <= 999999){
			$('#buyer_closing_fee').html('$400.00');
			$('#seller_closing_fee').html('$400.00');
		}
		
		if(isNaN(intPrice)){
			$('#buyer_closing_fee').html('$0.00');
			$('#seller_closing_fee').html('$0.00');			
		}
		//This part is for the revenue stamp calculation
		var numOfStamps = Math.ceil(intPrice/1000);
		var revenueStamps = ((numOfStamps * 3.3)/2);
		if(isNaN(revenueStamps)){
			$('#buyer_revenue_stamps').html('$0.00');
			$('#seller_revenue_stamps').html('$0.00');
		}else{
			$('#buyer_revenue_stamps').html('$'+revenueStamps.toFixed(2));
			$('#seller_revenue_stamps').html('$'+revenueStamps.toFixed(2));
		}
		//This parts updates the table after doing the math on the loan price.
		if(intPrice <= 30000 && intPrice > 0){
			sellerPrice = 115.50;
			$('#seller_title_insurance').html("$"+sellerPrice.toFixed(2));
 
			
		}else if(intPrice > 30000 && intPrice <= 50000){
			price = 115.50 +(3.85 *(intPrice - 30000)/1000);
			sellerPrice = Math.round(price*100)/100;
			$('#seller_title_insurance').html("$"+sellerPrice.toFixed(2));
 
			
		}else if(intPrice > 50000 && intPrice <= 100000){
			price = 192.50 +(3.30 *(intPrice - 50000)/1000);
			sellerPrice = Math.round(price*100)/100;
			$('#seller_title_insurance').html("$"+sellerPrice.toFixed(2));

		}else if(intPrice > 100000){
			price = 357.50 +(2.20 *(intPrice - 100000)/1000);
			sellerPrice = Math.round(price*100)/100;
			$('#seller_title_insurance').html("$"+sellerPrice.toFixed(2));
 
		}else{
			$('#seller_title_insurance').html("$0.00");
		}
		calculateTotal();
	});
	
	$('#loanAmount').change(function(){
		//This is so if the value is changed it unchecks the 50/50 split so the user can click it again to recalculate
		$('#split').attr('checked', false);
		var salesPrice = $(this).val();
		var intPrice = fixPrice(salesPrice);
		
 
		//This is for the refinance closing fee
		if(intPrice <= 499999 && intPrice >0){
			$('#borrow_closing_fee').html('$250.00');
		}
		if(intPrice >=500000 && intPrice <= 999999){
			$('#borrow_closing_fee').html('$400.00');
		}
		if(isNaN(intPrice)){
			$('#borrow_closing_fee').html('$0.00');
		}
 
		//This parts updates the table after doing the math on the sales price.
		if(intPrice <= 30000 && intPrice > 0){
			buyerPrice = 82.50;
			$('#buyer_title_insurance').html("$"+buyerPrice.toFixed(2));
			$('#borrow_title_insurance').html("$"+buyerPrice.toFixed(2));
		}else if(intPrice > 30000 && intPrice <= 50000){
			price = 82.50 +(2.75 *(intPrice - 30000)/1000);
			buyerPrice = Math.round(price*100)/100;
			$('#buyer_title_insurance').html("$"+buyerPrice.toFixed(2));
			$('#borrow_title_insurance').html("$"+buyerPrice.toFixed(2));
		}else if(intPrice > 50000 && intPrice <= 100000){
			price = 137.50 +(2.20 *(intPrice - 50000)/1000);
			buyerPrice = Math.round(price*100)/100;
			$('#buyer_title_insurance').html("$"+buyerPrice.toFixed(2));
			$('#borrow_title_insurance').html("$"+buyerPrice.toFixed(2));			
		}else if(intPrice > 100000 && intPrice <= 500000){
			price = 247.50 +(2.00 *(intPrice - 100000)/1000);
			buyerPrice = Math.round(price*100)/100;
			$('#buyer_title_insurance').html("$"+buyerPrice.toFixed(2));
			$('#borrow_title_insurance').html("$"+buyerPrice.toFixed(2));
		}else if(intPrice > 50000){	
			price = 1047.50 +(1.50 *(intPrice -500000)/1000);
			buyerPrice = Math.round(price*100)/100;
			$('#buyer_title_insurance').html("$"+buyerPrice.toFixed(2));
			$('#borrow_title_insurance').html("$"+buyerPrice.toFixed(2));			
		}else{
			$('#buyer_title_insurance').html("$0.00");
			$('#borrow_title_insurance').html("$0.00");
		}
		calculateTotal();
	});	
 
	$('#documentsTable select').live('click',function(){
		var totalDocuments = $('#documentNo').val();
		var sellerRunningTotal = 0;		
		var buyerRunningTotal = 0;
		var borrowRunningTotal = 0;
		var borrowPriceTotal;
		var sellerPriceTotal;
		var buyerPriceTotal;
		$('#buyer_recording_fees').html('$0.00');
		$('#seller_recording_fees').html('$0.00');
		$('#borrow_recording_fees').html('$0.00');
		$('.pageNo').each(function(index){
			var pageNumber = $(this).val();
			var chargeTo = $(this).parent().parent().children().children('select.buyer_or_seller').val();
			var priceTotal;
			if(pageNumber > 0){
			//This if splits it up in to 2 different scenarios either there is only one document or more than one.
				if(totalDocuments == 1){
					if(pageNumber == 1){
						$('#borrow_recording_fees').html('$15.00');
						if(chargeTo == "Seller"){
							$('#seller_recording_fees').html('$15.00');
							$('#buyer_recording_fees').html('$0.00');
						}else{
							$('#buyer_recording_fees').html('$15.00');
							$('#seller_recording_fees').html('$0.00');	
						}
					}else{
						priceTotal = 15+((pageNumber-1) *5);
						$('#borrow_recording_fees').html('$'+priceTotal.toFixed(2));
						if(chargeTo == "Seller"){
							$('#seller_recording_fees').html('$'+priceTotal.toFixed(2));
							$('#buyer_recording_fees').html('$0.00');
						}else{
							$('#buyer_recording_fees').html('$'+priceTotal.toFixed(2));
							$('#seller_recording_fees').html('$0.00');	
						}
					}			
				}else{
					
					if(chargeTo == "Seller"){
					//This part is for the seller
						if(pageNumber ==1){
							sellerPriceTotal = sellerRunningTotal + 15;
						}else{
							sellerPriceTotal = (15+((pageNumber-1) *5)) +sellerRunningTotal;
						}
							sellerRunningTotal = sellerPriceTotal;
						$('#seller_recording_fees').html('$'+sellerPriceTotal.toFixed(2));
					}else{
					//This part is for the buyer
						if(pageNumber ==1){
							buyerPriceTotal = buyerRunningTotal + 15;
						}else{
							buyerPriceTotal = (15+((pageNumber-1) *5)) +buyerRunningTotal;
						}
							buyerRunningTotal = buyerPriceTotal;
						$('#buyer_recording_fees').html('$'+buyerPriceTotal.toFixed(2));						
					}
					//This part is for the borrower
					if(pageNumber ==1){
							borrowPriceTotal = borrowRunningTotal + 15;
						}else{
							borrowPriceTotal = (15+((pageNumber-1) *5)) +borrowRunningTotal;
						}
							borrowRunningTotal = borrowPriceTotal;
						$('#borrow_recording_fees').html('$'+borrowPriceTotal.toFixed(2));
				}
 
			}
		});
	calculateTotal();
	});
	
});//END OF JQUERY
 
function isEven(value) {
	if (value%2 == 0)
		return true;
	else
		return false;
}
 
function fixPrice(price){
	var intPrice;
	var stringPrice = "";

	var dollarSign = false;
	for(var i=0; i<=price.length; i++){		
		if(price.charAt(i) == "$"){
			dollarSign = true;	
		}
		if(price.charAt(i) != ","){
			stringPrice += price.charAt(i);		
		}
	}
	if(dollarSign){
		var fixedStringPrice = stringPrice.substr(1);
		intPrice = parseInt(fixedStringPrice);
	}else{
		intPrice = parseInt(stringPrice);
	}	
	return intPrice;
}
//This function fixes the val of the html and gets rid of the opening $ 
function parseDollarPrice(price){
	var fixedPrice = parseFloat(price.substring(1));
	return fixedPrice;
}
 
function calculateTotal(){
	var buyerTotal;
	var sellerTotal;
	var borrowTotal;
	
	var buyerClosingLetter = parseDollarPrice($('#buyer_closing_letter').html());
	var buyerTitleInsurance = parseDollarPrice($('#buyer_title_insurance').html());
	var buyerClosingFee = parseDollarPrice($('#buyer_closing_fee').html());
	var buyerTitleSearch = parseDollarPrice($('#buyer_title_search').html());
	var buyerRecordingFees = parseDollarPrice($('#buyer_recording_fees').html());
	var buyerRevenueStamps = parseDollarPrice($('#buyer_revenue_stamps').html());
	buyerTotal = buyerClosingLetter + buyerTitleInsurance + buyerClosingFee + buyerTitleSearch + buyerRecordingFees + buyerRevenueStamps;
	if(isNaN(buyerTotal)){
		$('#buyer_total').html('$0.00');
	}else{
		$('#buyer_total').html('$'+buyerTotal.toFixed(2));
	}
	
	var sellerClosingLetter = parseDollarPrice($('#seller_closing_letter').html());
	var sellerTitleInsurance = parseDollarPrice($('#seller_title_insurance').html());
	var sellerClosingFee = parseDollarPrice($('#seller_closing_fee').html());
	var sellerTitleSearch = parseDollarPrice($('#seller_title_search').html());
	var sellerRecordingFees = parseDollarPrice($('#seller_recording_fees').html());
	var sellerRevenueStamps = parseDollarPrice($('#seller_revenue_stamps').html());
	sellerTotal = sellerClosingLetter + sellerTitleInsurance + sellerClosingFee + sellerTitleSearch + sellerRecordingFees + sellerRevenueStamps;
	if(isNaN(sellerTotal)){
		$('#seller_total').html('$0.00');
	}else{
		$('#seller_total').html('$'+sellerTotal.toFixed(2));
	}
	
	var borrowClosingLetter = parseDollarPrice($('#borrow_closing_letter').html());
	var borrowTitleInsurance = parseDollarPrice($('#borrow_title_insurance').html());
	var borrowClosingFee = parseDollarPrice($('#borrow_closing_fee').html());
	var borrowTitleSearch = parseDollarPrice($('#borrow_title_search').html());
	var borrowRecordingFees = parseDollarPrice($('#borrow_recording_fees').html());
	borrowTotal = borrowClosingLetter + borrowTitleInsurance + borrowClosingFee + borrowTitleSearch + borrowRecordingFees;
	if(isNaN(borrowTotal)){
		$('#borrow_total').html('$0.00');
	}else{
		$('#borrow_total').html('$'+borrowTotal.toFixed(2));
	}
 
}
 
 
 
 
