// JavaScript Document
function getHostName(){
	var domainName="";
	var hostName=window.location.hostname;
	if(hostName=='10.10.10.30'){domainName='http://10.10.10.30/cartrade/'}
	else if(hostName=='localhost'){domainName='http://localhost/cartrade/'}
	else if(hostName.indexOf('jil.in')>-1){domainName='http://'+hostName+'/cartrade/'}
	else{if(window.location.protocol=='https:'){domainName='https://'+hostName+'/'}
	else{domainName='http://'+hostName+'/'}}
	return domainName
}

/*--------------- Sell Your Car Start -------------------*/
$.validator.addMethod("phoneRegex", function(value, element) {
        return this.optional(element) || /^([7-9]{1})([0-9]?)+$/i.test(value);
    }, "Phone number must starting with 9, 8 or 7 only");


	$("form#sell_your_car").validate({
		// Specify the validation rules
		rules: {
			name: "required",
			phone: {
					required: true,
					number: true,
					minlength: 10, 
					maxlength: 10,
					phoneRegex: true,
			},
			email: {
                required: true,
				email: true
            },
			make: "required",
			model: "required"
			
		}
		
		
	});

/*-------------------------------------------------*/


/*---------------  frm_service, contact us -------------------*/


	$("form#frm_contact_us").validate({
		// Specify the validation rules
		rules: {
			showroom: "required",
			name: "required",
			phone: {
					required: true,
					number: true,
					minlength: 10, 
					maxlength: 10,
					phoneRegex: true,
				 	},
			email: {
                email: true
            }
		}
	});
	
/*--------------------------------------------------------*/


/*---------------  Car search -------------------*/
$("form#search_car_frm").validate({
	rules: {
		brand: "required",
	},
	submitHandler: function(form) {
		document.search_car_frm.submit();
		return false;
	}
});
	
/*--------------------------------------------------------*/


/*----------------------- Insurance ----------------------*/

	$("form#insurance_frm").validate({
		rules: {
			showroom: "required",
			category: "required",
			mst_brand_list: "required",
			model: "required",
			mfg_year: {
                required: true,
				number: true,
				maxlength: 4
            },
			name: "required",
			email: {
                required: true,
				email: true
            },
			phone: {
					required: true,
					number: true,
					minlength: 10, 
					maxlength: 10,
					phoneRegex: true,
			}			
		}
	});

	
	$("form#career_frm").validate({
		rules: {
			name: "required",
			phone: {
					required: true,
					number: true,
					minlength: 10, 
					maxlength: 10,
					phoneRegex: true,
			}			
		}
	});
	

	
	$("#drop_down").change(function(){
		var test=$("#drop_down").val();
		if(test=="new"){
			$("div.insuance_date").hide();
		}else if(test=="renewal"){
			$("div.insuance_date").show();
		}
	});
	
	// GET MODEL LIST 
	$("form#insurance_frm select#mst_brand_list").change(function(){
		var url 		= getHostName()+"loan/ajx_model_list";
		var brand_id 	= $(this).val();
		var myData 		= {'brand_id': brand_id};
		$.post(url, myData, function(data){
			//alert(data);
			$("form#insurance_frm select#model").html(data);
			return false;
		});
	});
	
/*--------------------------------------------------------*/

/*------------------------- LOAN CALCULATE YOUR EMI -------------------------------*/


var appendthis =  ("<div class='modal-overlay js-modal-close'></div>");

	$('a[data-modal-id]').click(function(e) {
		e.preventDefault();
    $("body").append(appendthis);
    $(".modal-overlay").fadeTo(500, 0.7);
    //$(".js-modalbox").fadeIn(500);
		var modalBox = $(this).attr('data-modal-id');
		$('#'+modalBox).fadeIn($(this).data());
	});  
  
  
$(".js-modal-close, .modal-overlay").click(function() {
    $(".modal-box, .modal-overlay").fadeOut(500, function() {
        $(".modal-overlay").remove();
    });
 
});
 
$(window).resize(function() {
    $(".modal-box").css({
        top: ($(window).height() - $(".modal-box").outerHeight()) / 2,
        left: ($(window).width() - $(".modal-box").outerWidth()) / 2
    });
});
 
$(window).resize();
 
// RANGE SLIDER 
    $( "#slider-range-min" ).slider({
      range: "min",
      value: 0,
      min: 0,
      max: 1000000,
      slide: function( event, ui ) {
        $( "#amount" ).val(  ui.value );
		
		var principal 	= $( "#amount" ).val();
	   	var rate 		= $( "#amount1" ).val();
	   	var months 		= $( "#amount2" ).val();
	   	simple_interest(principal, rate, months);
	   
      }
    });
    $( "#amount" ).val( $( "#slider-range-min" ).slider( "value" ) );



    $( "#slider-range-min1" ).slider({
      range: "min",
      value: 8,
      min:8,
      max: 26,
      slide: function( event, ui ) {
        $( "#amount1" ).val(  ui.value )  ;
		var principal 	= $( "#amount" ).val();
	   	var rate 		= $( "#amount1" ).val();
	   	var months 		= $( "#amount2" ).val();
	   	simple_interest(principal, rate, months);
      }
    });
    $( "#amount1" ).val(  $( "#slider-range-min1" ) .slider( "value" ) );



    $( "#slider-range-min2" ).slider({
      range: "min",
      value: 12,
      min: 12,
      max: 108,
      slide: function( event, ui ) {
        $( "#amount2" ).val(  ui.value )  ;
		var principal 	= $( "#amount" ).val();
	   	var rate 		= $( "#amount1" ).val();
	   	var months 		= $( "#amount2" ).val();
	   	simple_interest(principal, rate, months);
		
      }
    });
    $( "#amount2" ).val(  $( "#slider-range-min2" ) .slider( "value" ) );


/*--------------------------------------------------------*/

/*------------------------ MODEL, VERSION FROM AJAX --------------------*/
// get model list
$("select#brand_name").change(function(){
	var url 		= getHostName()+"loan/ajx_brand_model";
	var brand_id 	= $(this).val();
	var myData 		= {'brand_id': brand_id};
	$.post(url, myData, function(data){
		//alert(data);
		$("div#brand_model_name").html(data);
		return false;
	});
});
	
// get version list
$(document).on("change", "select#brand_model", function(){
	var url 				= getHostName()+"loan/ajx_brand_version";
	var brand_model_id 		= $(this).val();
	var myData 				= {'brand_model_id': brand_model_id};
	$.post(url, myData, function(data){
		//alert(data);
		$("div#brand_model_version").html(data);
		return false;
	});
});


/*--------------------------------------------------------*/

/*------------------------ CALCULATE YOUR EMI --------------------------------*/

function simple_interest(principal, rate, months){
	var r = rate/(12*100); 
	var emi = (principal * r * Math.pow((1+r),months))/(Math.pow((1+r),months)-1); 
	var final_si = Math.round(emi); 
	var payable_amt = Math.round( emi * months ) ; 
	
	$("div.calculate_month_right span.rubee_icon").html('<i class="fa fa-rupee rubee_icon"></i>'+final_si);
	$("div.total_amount").html('<p>Total Loan Amount: <i class="fa fa-rupee"></i> '+principal+' / Payable Amount: <i class="fa fa-rupee"></i> '+payable_amt+ '</p>');
	$( "input[name ='principal']" ).val(principal);
	$( "input[name ='rate']" ).val(rate);
	$( "input[name ='months']" ).val(months);
	
	// for popup box
	$("div.calculate_month_right1 span.rubee_icon").html('<i class="fa fa-rupee rubee_icon"></i>'+final_si);
	$("div.total_amount2.total_amount1").html('<p>Total Loan Amount: <i class="fa fa-rupee"></i> '+principal+' / Payable Amount: <i class="fa fa-rupee"></i> '+payable_amt+ ' </p>');
	
}

// APPLY LOAN
$("button#apply_loan").click(function(){
	
	// validation 
	if($("select#brand_name").val() == ''){
		$("select#brand_name").addClass('error');
		$("select#brand_name").focus();
		return false;
	}
	
	if($("select#brand_model").val() == ''){
		$("select#brand_model").addClass('error');
		$("select#brand_model").focus();
		return false;
	}
	
	if($("select#brand_version").val() == ''){
		$("select#brand_version").addClass('error');
		$("select#brand_version").focus();
		return false;
	}
	
	// check for loan amount selected or not
	var loan_amt = $("span.rubee_icon").text();
	if( parseInt(loan_amt) < 1){
		alert("Please select loan amount");
		return false;
	}
	
	$("input[name='brand_name1']").val($("#brand_name option:selected").text());
	$("input[name='brand_model1']").val($("#brand_model option:selected").text());
	$("input[name='brand_version1']").val($("#brand_version option:selected").text());
	
	$("input[name='brand_name2']").val($("#brand_name option:selected").val());
	$("input[name='brand_model2']").val($("#brand_model option:selected").val());
	$("input[name='brand_version2']").val($("#brand_version option:selected").val());

	//alert($("#brand_name option:selected").text());

	
	$('#popup3').fadeIn($(this).data());	// popup loan form	
});

// SUBMIT LOAN DETAIL
$(document).on("click", "div#popup3 button#loan_data_submit", function(){
	if($("input#name").val() == ''){
		$("inputt#name").addClass('error');
		$("input#name").focus();
		return false;
	}
	
	if($("input#phone").val() == ''){
		$("input#phone").addClass('error');
		$("input#phone").focus();
		return false;
	}
	
	if($("input#email").val() == ''){
		$("input#email").addClass('error');
		$("input#email").focus();
		return false;
	}

});

/*--------------- FOR DEALER SECTION -------------------*/



// BOOK A SERVICES
//frm_service
/*$(document).ready(function(){
	$("form#frm_service").find("button").click(function(){
		alert('ok');
		var baseurl = getHostName()+"mailer/book_service";
		
		var form_data =$('form#frm_service').serialize();
		$.post(baseurl, form_data, function(data){
			window.location.href = getHostName()+"services";
			return false;
			
		});
	
	});
});*/

/*alert(data);
return false;*/
/*if(data == 'Y'){
	window.location.href = getHostName()+"services";
	return false;
}else{
	$("p#msg").html("Please enter correct login detail");
	return false;
}*/



/*------------------------ Car search --------------------------------*/
/*
$(document).ready(function(){
	$("form#frm_service").find("button").click(function(){
		alert('ok');
		var baseurl = getHostName()+"mailer/book_service";
		
		var form_data =$('form#frm_service').serialize();
		$.post(baseurl, form_data, function(data){
			window.location.href = getHostName()+"services";
			return false;
			
		});
	
	});
});*/


