/*
	Used for submitting email addresses
*/

$(document).ready(function() {
    $('#date').datepicker();
	
	/* HIDE on page load--------------------------------*/
	$('#requestInspection').hide();
	$('#scheduledInspections').hide();
	$('#availablePermits').hide();
	$('#calculatorBuildingPermit').hide();

	/* SHOW on click	------------------------------------*/
	$("#requestAnInspection").click(function() {$("#requestAnInspection").hide();$('#requestInspection').slideDown();});
	$("#showInspections").click(function() {$('#scheduledInspections').slideDown();$('#showInspections').hide();});
	$("#showPermits").click(function() {$('#availablePermits').slideDown();$('#showPermits').hide();});
	$("#showBuildingPermitCalculator").click(function() {$('#calculatorBuildingPermit').slideDown();$('#showBuildingPermitCalculator').hide();});
	
	/* CLOSE on click	------------------------------------*/
	$("#closeInspectionRequest").click(function() {$('#requestInspection').slideUp();$("#requestAnInspection").show();});
	$("#closeCalculatorBuildingPermit").click(function() {$('#calculatorBuildingPermit').slideUp();$('#showBuildingPermitCalculator').show();});
	$("#closeInspections").click(function() {$('#scheduledInspections').slideUp();$('#showInspections').show();});
	$("#closePermits").click(function() {$('#availablePermits').slideUp();$('#showPermits').show();});
	
	
		/*  --------------------------------------------------------------------
			CALCULATE BUILDING PERMI FEE
		----------------------------------------------------------------------- */
	$("#calculate").click(function() {
		//Declare variables
		var totalValue=$('#totalValue').val();
		var buildingFee=0;
		var stateFee=0;
		var licenseFee=5;
		var finalFee=0;
		
		//Clean value
		var totalValue=totalValue.replace('$','');
		var totalValue=totalValue.replace(' ','');
		var totalValue=totalValue.replace(',','');
		var totalValue=parseFloat(totalValue);
		if(isNaN(totalValue)){totalValue=0;}
		
		//Calculate City fees
		if(totalValue > 1 && totalValue < 500.1){buildingFee=23.50;}
		else if (totalValue > 500 && totalValue < 2000.1){
			baseFee=23.5;
			calc=Math.ceil(((totalValue-500)/100))*3.05; //$3.05 for each $100, less $500
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 2000 && totalValue < 25000.1){
			baseFee=69.25;
			calc=Math.ceil(((totalValue-2000)/1000))*14; //$14 for each $1,000, less $2,000
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 25000 && totalValue < 50000.1){
			baseFee=391.25;
			calc=Math.ceil(((totalValue-25000)/1000))*10.10; //$10.10 for each $1,000, less $25,000
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 50000 && totalValue < 100000.1){
			baseFee=643.75;
			calc=Math.ceil(((totalValue-50000)/1000))*7; //$7 for each $1,000, less $50,000
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 100000 && totalValue < 500000.1){
			baseFee=993.75;
			calc=Math.ceil(((totalValue-100000)/1000))*5.65; //$5.65 for each $1,000, less $50,000
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 500000 && totalValue < 1000000.1){
			baseFee=3233.75;
			calc=Math.ceil(((totalValue-500000)/1000))*4.75; //$4.75 for each $1,000, less $50,000
			buildingFee=baseFee+calc;
		}
		else if (totalValue > 1000000){
			baseFee=5608.75;
			calc=Math.ceil(((totalValue-1000000)/1000))*3.65; //$3.65 for each $1,000, less $50,000
			buildingFee=baseFee+calc;
		}
		
		//Calculate State fees
		if(totalValue > 1 && totalValue < 1000.1){stateFee=0.50;}
		else if (totalValue > 1000 && totalValue < 1000000.1){stateFee=totalValue*0.0005;}
		else if (totalValue > 1000000 && totalValue < 2000000.1){
			stateBaseFee=500;
			stateCalc=((totalValue-1000000)*0.0004);
			stateFee=stateBaseFee+stateCalc;
		}		
		else if (totalValue > 2000000 && totalValue < 3000000.1){
			stateBaseFee=900;
			stateCalc=((totalValue-2000000)*0.0003);
			stateFee=stateBaseFee+stateCalc;
		}			
		else if (totalValue > 3000000 && totalValue < 4000000.1){
			stateBaseFee=1200;
			stateCalc=((totalValue-3000000)*0.0002);
			stateFee=stateBaseFee+stateCalc;
		}			
		else if (totalValue > 4000000 && totalValue < 5000000.1){
			stateBaseFee=1400;
			stateCalc=((totalValue-4000000)*0.0001);
			stateFee=stateBaseFee+stateCalc;
		}			
		else if (totalValue > 5000000){
			stateBaseFee=1500;
			stateCalc=((totalValue-5000000)*0.00005);
			stateFee=stateBaseFee+stateCalc;
		}			
		
		//Round all numbers
		var finalFee=buildingFee+stateFee+licenseFee;
		
		var buildingFee=buildingFee.toFixed(2);
		var stateFee=stateFee.toFixed(2);
		var finalFee=finalFee.toFixed(2);
		
		$.ajax({
			type: 'post',
			success: function()
			{
				//Output if successful
				$('#buildingPermitFee').val('$'+buildingFee);
				$('#stateFee').val('$'+stateFee);
				$('#licenseFee').val('$'+licenseFee);
				$('#estimatedFee').val('$'+finalFee);
			}
			});
			return false;
    });
	
	
		/*  --------------------------------------------------------------------
			INSPECTION REQUEST
			Get form contents and post to database
	----------------------------------------------------------------------- */
	$("#submitRequestInspection").click(function() {
        var who=$('#who').val();
		var phone=$('#phone').val();
		var email=$('#email').val();
		var address=$('#address').val();
		var permit=$('#permit').val();
		var type=$('#type').val();
		var date=$('#date').val();
		var time=$('#time :selected').text();
		var altTime=$('#altTime :selected').text();
		var comments=$('#comments').val();
		
		var data ='who='+who+'&phone='+phone+'&email='+email+'&address='+address+'&permit='+permit+'&type='+type+'&date='+date+'&time='+time+'&altTime='+altTime+'&comments='+comments;
			 //SEND DATA
			$.ajax({
			type: 'post',
			url: '../common/INSERT_request.php',
			data: data,
			success: function(msg)
			{
				//alert(msg);
				//Output if successful
				$('#form').fadeOut(2000);
				$('#newForm').fadeIn(2000);
				$('#newForm').html('<p>Thank you!</p>');
			}
			});
			$('#requestInspection').html('<p>Your request has been sent. You will receive an email confirming the inspection once it has been confirmed by the City. If you do not hear from the City before your inspection, please call the City Inspector at (651) 490-4691.</p>');
        return false;
    });
	
	
});
