$(document).ready(function(){
	$('#tab_cont').hide();
	callGraph(); 
	$('#3').removeClass('odd').addClass('lgreen');
	 $('#colourGreen').css('background', '#6CBE47');
	
	 $('#interest').css('color', '#000');
	if($('#amount').val()==200000 && $('#rate').val()==6.50260 && $('#aPeriod').val()==6 && $('#iTerm').val()==5){
		
			$('#amount').val(0);
			$('#rate').val(0);
			$('#aPeriod').val(0);
			$('#iTerm').val(0);	
			$('#iMonthTerm').val(0);
	
	}
	
	 });


function select_graph(id){
	graphChange(id);
	if(id==2) {
		$('#'+id).removeClass('odd').addClass('lgreen');
		$('#3').removeClass('lgreen').addClass('odd');
		$('#4').removeClass('lgreen');
		$('#6').removeClass('lgreen').addClass('odd');
		
	}
	else if(id==3) {
		$('#'+id).removeClass('odd').addClass('lgreen');
		$('#2').removeClass('lgreen');
		$('#4').removeClass('lgreen');
		$('#6').removeClass('lgreen').addClass('odd');
	}
	else if(id==4) {
		$('#'+id).removeClass('odd').addClass('lgreen');
		$('#2').removeClass('lgreen');
		$('#3').removeClass('lgreen').addClass('odd');		
		$('#6').removeClass('lgreen').addClass('odd');
	}
	
	else if(id==6) {
		$('#'+id).removeClass('odd').addClass('lgreen');
		$('#2').removeClass('lgreen');
		$('#3').removeClass('lgreen').addClass('odd');
		$('#4').removeClass('lgreen');
		
		
	}
	
}
	
function validate(){	
	
	if($('#amount').val()==0 || $('#amount').val()==""){
	alert("Please Enter Mortgage Amount");
	return false;
	}else
	if($('#rate').val()==0 || $('#rate').val()==""){
	alert("Please Enter Interest Rate");
	return false;
	}else
	if($('#aPeriod').val()==0 && $('#iMonthTerm').val()==0){
	alert("Please Enter Amortization Period");
	return false;
	}else
	if($('#iTerm').val()==0 && $('#iTermMonth').val()==0){
	alert("Please Enter Interest Term");
	return false;
	}else{
		 $('#lightGreen').css('background', '#C6E1AE');
	return true;
	}
}

function graphChange(n){
	sideMenu(n);
	
	if(n==1){//monthly
		$(".jqplot-table-legend").hide();
		$("#list_biwkly").val($("#list_biwkly").val());
		$("#tab_id").val('Bi-Weekly');
		callGraph();
		
	}if(n==2){//Semi-Monthly
		$(".jqplot-table-legend").hide();
		$("#list_biwkly").val($("#list_bimnthly").val());
		$("#tab_id").val('Semi Monthly');
		callGraph();
		
		
		
	}if(n==3){//Bi-Weekly
		$(".jqplot-table-legend").hide();
		$("#list_biwkly").val($("#list_biwkly").val());
		$("#tab_id").val('Bi-Weekly');
		
		callGraph();
		
	}if(n==4){//Weekly
		$(".jqplot-table-legend").hide();
		$("#list_biwkly").val($("#list_wkly").val());
		$("#tab_id").val('Weekly');
		callGraph();
		
		
	}
	if(n==5){//Acceletrated Bi-Weekly
		$("#tab_id").val('');
		$("#list_biwkly").val($("#list_accbiwkly").val());
		$("#tab_id").val('Accelerated Bi-Weekly');
		callGraph();
		
		
	}
if(n==6){//Acceletrated Weekly
	$(".jqplot-table-legend").hide();
	$("#list_biwkly").val($("#list_accwkly").val());
	$("#tab_id").val('Accelerated Weekly');
	callGraph();
	
	
}

}

function sideMenu(n){	
	if(n==1){//monthly
		$("#inst_lab").html("MONTHLY PAYMENTS");
		$("#inst_val").html($("#m_inst_val").val());
		$("#tot_val").html($("#m_tot_val").val());
		$("#interest_val").html($("#m_interest_val").val());
		
	}if(n==2){//Semi-Monthly
		$("#inst_lab").html("S-MONTHLY PAYMENTS");
		$("#inst_val").html($("#sm_inst_val").val());
		$("#tot_val").html($("#sm_tot_val").val());
		$("#interest_val").html($("#sm_interest_val").val());
		
		
	}if(n==3){//Bi-Weekly
		$("#inst_lab").html("BI WEEKLY PAYMENTS");
		$("#inst_val").html($("#bw_inst_val").val());
		$("#tot_val").html($("#bw_tot_val").val());
		$("#interest_val").html($("#bw_interest_val").val());
		
		
	}if(n==4){//Weekly
		$("#inst_lab").html("WEEKLY PAYMENTS");
		$("#inst_val").html($("#w_inst_val").val());
		$("#tot_val").html($("#w_tot_val").val());
		$("#interest_val").html($("#w_interest_val").val());		
		
	}
	if(n==5){//Acceletrated Bi-Weekly
		$("#inst_lab").html("ACCBiWEEKLY PAYMENTS");
		$("#inst_val").html($("#abw_inst_val").val());
		$("#tot_val").html($("#abw_tot_val").val());
		$("#interest_val").html($("#abw_interest_val").val());		
	}
	if(n==6){//Acceletrated Weekly
	$("#inst_lab").html("ACC-WEEKLY PAYMENT");
	$("#inst_val").html($("#aw_inst_val").val());
	$("#tot_val").html($("#aw_tot_val").val());
	$("#interest_val").html($("#aw_interest_val").val());
	}
}

function loadAgain(){
	
	$("#amount").val($("#amount").val());
	$("#rate").val($("#rate").val());
	$("#aPeriod").val($("#aPeriod").val());
	$("#iTerm").val($("#iTerm").val());
	$("#aPeriodM").val($("#aPeriodM").val());
	$('#button1').trigger('click');
	
	
}
function showtab(n){
	if(n==1){
		
		$('#tab_cont').show();
		$('#twentyfiveYear').show();		
		$('#fiveYear').hide();
		
	}if(n==2){
		
		$('#tab_cont').show();
		$('#twentyfiveYear').hide();
		$('#fiveYear').show();	
		
	}
	
	
}



function callGraph(){
	
$.jqplot.config.enablePlugins = true;
	
    var listMth=($("#list_mthly").val()).split(" "); 
    var listBywkly= ($("#list_biwkly").val()).split(" "); 
   
	
	var loanTrm = $("#loan_term").val();

    var xtickYrly=$("#min_amrt_lt_yr").val()
    var xtick=$("#xaxis_ticks").val();//minimum among loan and amortization period
        
  

    
    

	
	var listOne = new Array();
	var listTwo = new Array();	
	
	
	var xaxisExpention = new Array();
	
	
	
	/*X-Axis*/
	var i=0;
	var q=1;//X-axis Values [1,3,5,...]
	for( i=0;i<xtickYrly;i++){
		
		listOne[i]=listMth[i];
		listTwo[i]=listBywkly[i];
		if (q%2 != 0) {//Check it is odd number,and omit even number only on x-expentions 
			//if (i<xtickYrly)
			xaxisExpention[i]=q;
		} else {
			xaxisExpention[i]=" ";//assign even numbers to null
		}
		q=q+1;
		

	}
	
	for (var r=1;r<3;r++){
		//xaxisExpention[i++]= xaxisExpention[i]+(r+1);
		xaxisExpention[i++]= " ";
	}
			
	line2 = listTwo; //Bi-Weekly	
	line1 = listOne; //Monthly
	
		
	plot3c = $.jqplot('chart3c', [line1, line2], {
	
		legend: {
        	show: true,
            location: 'sw',
			xoffset: -50,
			yoffset: -100
         },
         title: {
        	 text:"Mortgage Amortization Paydown",
        	 fontSize: "18px",
        	 textColor: "#095AA5",
        	 textAlign: "center"
         },

         seriesDefaults: {
             renderer: $.jqplot.BarRenderer,
             

             rendererOptions: {
        	 	barWidth: 9,
                 barPadding: 1,
                 barMargin: 16
             }
         },
         series: [{
        	 color:'#6cbe47',
             background:'#ffffff',
        	 label: 'Monthly'
         },
         {
        	 color:'#c6e1ae',
        	 label: $("#tab_id").val()
         }],
         axes: {
             xaxis: {
                 renderer: $.jqplot.CategoryAxisRenderer,
                 tickOptions:{formatString:'$%.0f'},
                 ticks: xaxisExpention
                 // ticks: [1, 2, 3, 4]
             },
             yaxis: {
            	// renderer: $.jqplot.LogAxisRenderer,
                // tickOptions:{formatString:'$%.2f'},
            	 tickOptions:{formatString:'$%.0f'},
            	 min: 0, max: $("#yaxis_ticks").val()*1.2, numberTicks:5
            
}
         },
         highlighter: {
             showMarker:true,
             tooltipAxes: 'both',
             //yvalues: 3,
             formatString:'<div class="tool_tip" ><ul><li>Year </li><li>%s Balance</li><li class="clr">Outstanding </li><li class="clr">$%s</li></ul></div>'
         },
         cursor:{zoom:true, showTooltip:true, clickReset:true}
     });	

}

