
	var username_error = true;
	var student_name_error = true;
	var parent_name_error = true;
	var parent_email_error = true;
	var mobile_prefix_error = true;
	var mobile_error = true;
	var school_name_error = true;
	var std_id_error = true;
	var password_error = true;
	var address_error = true;
	var country_error = true;
	var district_error = true;
	var state_error = true;
	var taluka_error = true;
	var city_error = true;
	var pin_code_error = true;
	var terms_error = true;


	function username_check()
	{
		var username = $('#username').val();
		if (username.length == '') 
		{
			$('#username_check').show();
			$('#username_check').html('The username field is required');
			username_error = false;
		}
		else
		{
			$.ajax({
	            url: base_url+'home/check_username_available', // point to server-side controller method
	            data: {username:username},
	            type: 'post',
	            dataType:'json',
	            success: function (response) {

	                if(response == 0)
                    {
                       	$('#username_check').show();
                        $('#username_check').html('<span style="color:green;">Available</span>');
                        username_error = true;
                    }
                    else
                    {
                       	$('#username_check').show();
                        $('#username_check').html('This username is already exist.');
                        username_error = false;
                    }
	            },
	            
	        });
		}
	}


	function student_name_check()
	{
		var student_name = $('#student_name').val();
		if (student_name.length == '') 
		{
			$('#student_name_check').show();
			$('#student_name_check').html('The student name field is required');
			student_name_error = false;
		}
		else
		{
			$('#student_name_check').hide();
			student_name_error = true;
		}
	}


	function parent_name_check()
	{
		var parent_name = $('#parent_name').val();
		if (parent_name.length == '') 
		{
			$('#parent_name_check').show();
			$('#parent_name_check').html('The parent name field is required');
			parent_name_error = false;
		}
		else
		{
			$('#parent_name_check').hide();
			parent_name_error = true;
		}
	}

	function parent_email_check()
	{
		var mailformat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var parent_email = $('#parent_email').val();
        if (parent_email.length == '') 
        {
            $('#parent_email_check').show();
            $('#parent_email_check').html('The email field is required');
            parent_email_error = false;
        }
        else
        {
            if(mailformat.test(parent_email) == false)
	        {
	            $('#parent_email_check').show();
	            $('#parent_email_check').html('Please Enter Valid Email Format.');
	            parent_email_error = false;
	        }
	        else
	        {
	            $.ajax({
	                url: base_url+'home/check_email_available', // point to server-side controller method
	                data: {parent_email:parent_email},
	                type: 'post',
	                dataType:'json',
	                success: function (response) {

	                    if(response == 0)
	                    {
	                        $('#parent_email_check').show();
	                        $('#parent_email_check').html('<span style="color:green;">Available</span>');
	                        parent_email_error = true;
	                    }
	                    else
	                    {
	                        $('#parent_email_check').show();
	                        $('#parent_email_check').html('This Email is already exist.');
	                        parent_email_error = false;
	                    }
	                },
	                
	            }); 
	        } 
        }          

	}


	function mobile_prefix_check()
	{
		var mobile_prefix = $('#mobile_prefix').val();
		if (mobile_prefix.length == '') 
		{
			$('#mobile_prefix_check').show();
			$('#mobile_prefix_check').html('The mobile prefix field is required');
			mobile_prefix_error = false;
		}
		else
		{
			$('#mobile_prefix_check').hide();
			mobile_prefix_error = true;
		}
	}


	function mobile_check()
	{
		var mobile = $('#mobile').val();
		if (mobile.length == '') 
		{
			$('#mobile_check').show();
			$('#mobile_check').html('The mobile field is required');
			mobile_error = false;
		}
		else
		{
			$.ajax({
	            url: base_url+'home/check_mobile_available', // point to server-side controller method
	            data: {mobile:mobile},
	            type: 'post',
	            dataType:'json',
	            success: function (response) {
	                
	                if(response == 0)
                    {
                        $('#mobile_check').show();
                        $('#mobile_check').html('<span style="color:green;">Available</span>');
                        mobile_error = true;
                    }
                    else
                    {
                        $('#mobile_check').show();
                        $('#mobile_check').html('You are already registered,<a href="'+base_url+'student-olympiad-login">Click here to login</a>');
                        mobile_error = false;
                    }
	            },
	            
	        });
		}
	}


	function school_name_check()
	{
		var school_name = $('#school_name').val();
		if (school_name.length == '') 
		{
			$('#school_name_check').show();
			$('#school_name_check').html('The school name field is required');
			school_name_error = false;
		}
		else
		{
			$('#school_name_check').hide();
			school_name_error = true;
		}
	}

	function std_id_check()
	{
		var std_id = $('#std_id').val();
		if (std_id.length == '') 
		{
			$('#std_id_check').show();
			$('#std_id_check').html('The standard field is required');
			std_id_error = false;
		}
		else
		{
			$('#std_id_check').hide();
			std_id_error = true;
		}
	}

	function password_check()
	{
		var password = $('#password').val();
		if (password.length == '') 
		{
			$('#password_check').show();
			$('#password_check').html('The password field is required');
			password_error = false;	
		}
		else
		{
			$('#password_check').hide();
			password_error = true;	
		}
	}

	function address_check()
	{
		var address = $('#address').val();
		if (address.length == '') 
		{
			$('#address_check').show();
			$('#address_check').html('The address field is required');
			address_error = false;
		}
		else
		{
			$('#address_check').hide();
			address_error = true;
		}
	}

	function country_check()
	{
		var country = $('#country').val();
		if (country.length == '') 
		{
			$('#country_check').show();
			$('#country_check').html('The country field is required');
			country_error = false;
		}
		else
		{
			$('#country_check').hide();
			country_error = true;
		}
	}

	function district_check()
	{
		var district = $('#district').val();
		if (district.length == '') 
		{
			$('#district_check').show();
			$('#district_check').html('The district field is required');
			district_error = false;
		}
		else
		{
			$('#district_check').hide();
			district_error = true;
		}
	}

	function state_check()
	{
		var state = $('#state').val();
		if (state.length == '') 
		{
			$('#state_check').show();
			$('#state_check').html('The state field is required');
			state_error = false;
		}
		else
		{
			$('#state_check').hide();
			state_error = true;
		}
	}

	function taluka_check()
	{
		var taluka = $('#taluka').val();
		if (taluka.length == '') 
		{
			$('#taluka_check').show();
			$('#taluka_check').html('The taluka field is required');
			taluka_error = false;		}
		else
		{
			$('#taluka_check').hide();
			taluka_error = true;		
		}
	}


	function city_check()
	{
		var city = $('#city').val();
		if (city.length == '') 
		{
			$('#city_check').show();
			$('#city_check').html('The city field is required');
			city_error = false;
		}
		else
		{
			$('#city_check').hide();
			city_error = true;
		}
	}
	
	function pin_code_check()
	{
		var pin_code = $('#pin_code').val();
		if (pin_code.length == '') 
		{
			$('#pin_code_check').show();
			$('#pin_code_check').html('The pin code field is required');
			pin_code_error = false;
		}
		else
		{
			$('#pin_code_check').hide();
			pin_code_error = true;
		}
	}

	function terms_check()
	{
		// var terms = $('#terms').val();
		if($('#terms').is(":checked"))
        {
            $('#terms_check').hide();
			terms_error = true;
        }
        else
        {
            $('#terms_check').show();
			$('#terms_check').html('The terms and conditions field is required');
			terms_error = false;
        }
	}

// -------------------------------------------------------------------------------


	function validateForm() 
	{
		// username_check();
		student_name_check();
		parent_name_check();
		parent_email_check();
		mobile_prefix_check();
		mobile_check();
		school_name_check();
		std_id_check();
		password_check();
		address_check();
		country_check();
		district_check();
		state_check();
		taluka_check();
		city_check();
		pin_code_check();
		terms_check();

		if ((student_name_error == true) && (parent_name_error == true) && 
			(parent_email_error == true) && (mobile_prefix_error == true) && 
			(mobile_error == true) && (school_name_error == true) && (std_id_error == true) && 
			(password_error == true) && (address_error == true)&& (country_error == true) && 
			(district_error == true) && (state_error == true) && (taluka_error == true) && 
			(city_error == true) && (pin_code_error == true) && (terms_error == true)) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
// -------------------------------------------------------------------------------



$(document).ready(function($) {


// check_monthly_amount(parseFloat($('#monthly_individual_sub_total').val())); 


	// $('#username_check').hide();
	$('#student_name_check').hide();
	$('#parent_name_check').hide();
	$('#parent_email_check').hide();
	$('#mobile_prefix_check').hide();
	$('#mobile_check').hide();
	$('#school_name_check').hide();
	$('#std_id_check').hide();
	$('#password_check').hide();
	$('#address_check').hide();
	$('#country_check').hide();
	$('#district_check').hide();
	$('#state_check').hide();
	$('#taluka_check').hide();
	$('#city_check').hide();
	$('#pin_code_check').hide();
	$('#terms_check').hide();
// -------------------------------------------------------------------------------
	
	// $("#username").keyup(function () {
	// 	username_check();
 //    });

	$("#student_name").keyup(function () {
		student_name_check();
    });
	
	$("#parent_name").keyup(function () {
		parent_name_check();
    });
	
	$("#parent_email").keyup(function () {
		parent_email_check();
    });

	$("#mobile_prefix").change(function () {
		mobile_prefix_check();
    });

	
	$("#mobile").keypress(function (e) {
         //if the letter is not digit then display error and don't type anything
         if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            //display error message
            $('#mobile_check').show();
            $("#mobile_check").html("Digits Only").show().fadeOut("slow");
            return false;
        }
    });

	$("#mobile").keyup(function () {
		mobile_check();
    });

	$("#school_name").keyup(function () {
		school_name_check();
    });

	$("#std_id").change(function () {
		std_id_check();
    });

	$("#password").keyup(function () {
		password_check();
    });
	
	$("#address").keyup(function () {
		address_check();
    });

	$("#country").change(function () {
		country_check();
    });

	$("#district").keyup(function () {
		district_check();
    });
	
	$("#state").keyup(function () {
		state_check();
    });
	
	$("#taluka").keyup(function () {
		taluka_check();
    });
	
	$("#city").keyup(function () {
		city_check();
    });
	
	$("#pin_code").keyup(function () {
		pin_code_check();
    });

    $("#terms").click(function () {
		terms_check();
    });



// code for check checkbox of subject

   // monthly exam individual subject 
	
	$(document).on('click','.checkbox_field1',function(){
        
        var amount = parseFloat($('#monthly_individual_sub_total').val());
        var charge = parseFloat($(this).attr("data-charges"));
        var result_amount = parseFloat($(this).attr("data-resultamount"));
        var display_result_amount = parseFloat($.trim($('#display_result_amount1').html().replace(",", "").replace("$", "")));
        
        var monthly_checkbox_count = parseInt($('#monthly_checkbox_count').val());

        if($(this).is(":checked"))
        {
            var total_amount = amount + charge;
            // console.log(total_amount);
            var final_result_amount = display_result_amount + result_amount;
            if ($('.checkbox_result_amount1').is(":checked")) 
            {
                total_amount = total_amount + result_amount;
            }

            var monthly_checkbox_count_add = monthly_checkbox_count + 1;
            $('#monthly_checkbox_count').val(monthly_checkbox_count_add);
            
        }
        else
        {
            var total_amount = amount - charge;
            var final_result_amount = display_result_amount - result_amount;
           
            if ($('.checkbox_result_amount1').is(":checked")) 
            {
                total_amount = total_amount - result_amount;
            }

            var monthly_checkbox_count_add = monthly_checkbox_count - 1;
            $('#monthly_checkbox_count').val(monthly_checkbox_count_add);
        }

        $('#display_result_amount1').text(final_result_amount.toFixed(2));

        // console.log(total_amount);
        check_monthly_amount(total_amount); 

    });
        

    $(document).on('click','.checkbox_result_amount1',function(){
       
        var display_result_amount = parseFloat($.trim($('#display_result_amount1').html().replace(",", "").replace("$", "")));
        var total_amount = parseFloat($('#monthly_individual_sub_total').val());

        if ($(this).is(":checked")) 
        {
            var final_amount = total_amount + display_result_amount;
        }
        else
        {
            var final_amount = total_amount - display_result_amount;
        }

        check_monthly_amount(final_amount);

    });


    $(document).on('change','#duration1',function(){
         
        var duration = $('#duration1').val();
        var service_id = $('#service_id1').val();
        
        if(duration != '' && service_id != '')
        {
            $.ajax({
                url: base_url+'payment/filter_products', // point to server-side controller method
                data: {
                    duration:duration,
                    service_id:service_id
                },
                type: 'post',
                dataType:'html',
                success: function (response) {
                    if(response)
                    {
                        $('#subject_list1').html(response);
                        $('#total_cost1').text('0.00');
                        $('#monthly_individual_sub_total').val('0.00');
                        $('#display_result_amount1').text('0.00');
                        check_monthly_amount(Number('0.00')); //give number bcoz of passing value 0.00 is not accept .toFixed(2) function 
                        
                    }
                },
            });
        }

    });



    // annual exam individual subject 

    $(document).on('click','.checkbox_field2',function(){
        
        var amount = parseFloat($('#annual_individual_sub_total').val());
        var charge = parseFloat($(this).attr("data-charges"));
        var result_amount = parseFloat($(this).attr("data-resultamount"));
        var display_result_amount = parseFloat($.trim($('#display_result_amount2').html().replace(",", "").replace("$", "")));
        
        var annual_checkbox_count = parseInt($('#annual_checkbox_count').val());

        if($(this).is(":checked"))
        {
            var total_amount = amount + charge;
            var final_result_amount = display_result_amount + result_amount;
            if ($('.checkbox_result_amount2').is(":checked")) 
            {
                total_amount = total_amount + result_amount;
            }

            var annual_checkbox_count_add = annual_checkbox_count + 1;
            $('#annual_checkbox_count').val(annual_checkbox_count_add);
        }
        else
        {
            var total_amount = amount - charge;
            var final_result_amount = display_result_amount - result_amount;
           
            if ($('.checkbox_result_amount2').is(":checked")) 
            {
                total_amount = total_amount - result_amount;
            }

            var annual_checkbox_count_add = annual_checkbox_count - 1;
            $('#annual_checkbox_count').val(annual_checkbox_count_add);
        }

        $('#display_result_amount2').text(final_result_amount.toFixed(2));

        check_annual_amount(total_amount); 

    });
        

    $(document).on('click','.checkbox_result_amount2',function(){
       
        var display_result_amount = parseFloat($.trim($('#display_result_amount2').html().replace(",", "").replace("$", "")));
        var total_amount = parseFloat($('#annual_individual_sub_total').val());

        if ($(this).is(":checked")) 
        {
            var final_amount = total_amount + display_result_amount;
        }
        else
        {
            var final_amount = total_amount - display_result_amount;
        }

        check_annual_amount(final_amount);

    });



    // book subject 

    $(document).on('click','.checkbox_field3',function(){
        
        var amount = parseFloat($('#book_sub_total').val());
        var charge = parseFloat($(this).attr("data-charges"));
        var book_checkbox_count = parseInt($('#book_checkbox_count').val());

        if($(this).is(":checked"))
        {
            var total_amount = amount + charge;
            var book_checkbox_count_add = book_checkbox_count + 1;
            $('#book_checkbox_count').val(book_checkbox_count_add);
        }
        else
        {
            var total_amount = amount - charge;
            var book_checkbox_count_add = book_checkbox_count - 1;
            $('#book_checkbox_count').val(book_checkbox_count_add);
        }

        $('#total_cost3').text(total_amount.toFixed(2));
        $('#book_sub_total').val(total_amount.toFixed(2));
        calculate_final_amount();
    });


    // question paper subject 

    $(document).on('click','.checkbox_field4',function(){
        
        var amount = parseFloat($('#question_paper_sub_total').val());
        var charge = parseFloat($(this).attr("data-charges"));
        var question_paper_checkbox_count = parseInt($('#question_paper_checkbox_count').val());

        if($(this).is(":checked"))
        {
            var total_amount = amount + charge;
            var question_paper_checkbox_count_add = question_paper_checkbox_count + 1;
            $('#question_paper_checkbox_count').val(question_paper_checkbox_count_add);
        }
        else
        {
            var total_amount = amount - charge;
            var question_paper_checkbox_count_add = question_paper_checkbox_count - 1;
            $('#question_paper_checkbox_count').val(question_paper_checkbox_count_add);
        }
        $('#total_cost4').text(total_amount.toFixed(2));
        $('#question_paper_sub_total').val(total_amount.toFixed(2));
        calculate_final_amount();
    });






});


	



// -------------------------------------------------------------------------------

    function check_monthly_amount(final_amount) 
    {
        // var total_amount = parseFloat($('#total_amount').val());
        console.log('final_amount = '+final_amount);
        $('#total_cost1').text(final_amount.toFixed(2));
        $('#monthly_individual_sub_total').val(final_amount.toFixed(2));
        calculate_final_amount();
    }

    function check_annual_amount(final_amount) 
    {
        // var total_amount = parseFloat($('#total_amount').val());
        $('#total_cost2').text(final_amount.toFixed(2));
        $('#annual_individual_sub_total').val(final_amount.toFixed(2));
        calculate_final_amount();
    }

    function calculate_final_amount()
    {
    	var monthly_total = parseFloat($('#monthly_individual_sub_total').val());
    	var annual_total = parseFloat($('#annual_individual_sub_total').val());
    	var book_total = parseFloat($('#book_sub_total').val());
    	var question_paper_total = parseFloat($('#question_paper_sub_total').val());
    	

    	var total = monthly_total + annual_total + book_total + question_paper_total;
    	console.log('total = '+total);

    	
    	var book_plus_question_paper_count = parseInt($('#book_checkbox_count').val()) + parseInt($('#question_paper_checkbox_count').val());
    	
		var currency = $('#currency').val();
		
    	if (book_plus_question_paper_count == 0) 
    	{
    		var delivery_charge = 0;
    	}
    	else if (book_plus_question_paper_count <= 3) 
    	{
    		if (currency == 'INR') 
    		{
				var delivery_charge = 70;
    		}
    		else
    		{
    			var delivery_charge = 2;
    		}
    		
    	}
    	else if (book_plus_question_paper_count <= 6) 
    	{
    		if (currency == 'INR') 
    		{
				var delivery_charge = 100;
    		}
    		else
    		{
    			var delivery_charge = 3;
    		}
    	}
    	else
    	{
    		if (currency == 'INR') 
    		{
				var delivery_charge = 140;
    		}
    		else
    		{
    			var delivery_charge = 4;
    		}
    	}
    	
    	var final_total_amount = total + delivery_charge;
    	
    	if (total <= 0) 
    	{
    		$('#final_total_cost').text(total.toFixed(2));
    		$('#final_total_amount').val(total.toFixed(2));
    		$('#submitbtn').attr('disabled',true);
    	}
    	else
    	{
    		$('#final_total_cost').text(final_total_amount.toFixed(2));
    		$('#final_total_amount').val(final_total_amount.toFixed(2));
    		$('#submitbtn').attr('disabled',false);
    	}

    	var coupon_status = $('#coupon_status').val();
        if (coupon_status == 'yes') 
        {
        	apply_coupon_ajax();
        }
    }
// -------------------------------------------------------------------------------

    $(document).on('click','#apply_coupon',function(){
        apply_coupon_ajax();
    });


    function apply_coupon_ajax()
    {
    	var duration1 = $('#duration1').val();
    	var duration2 = $('#duration2').val();
        var coupon_code = $('#coupon_code').val();


        if(coupon_code != '')
        {
        	if (duration1 == duration2) 
	        {
	        	var duration = duration1;
	        }
	        else 
	        {
	        	var monthly_checkbox_count = 0;
	        	$('.checkbox_field1:checkbox:checked').each(function(){
				    monthly_checkbox_count++;
				});

	        	$('#monthly_checkbox_count').val(monthly_checkbox_count);

				var annual_checkbox_count = 0;
	        	$('.checkbox_field2:checkbox:checked').each(function(){
				    annual_checkbox_count++;
				});

	        	$('#annual_checkbox_count').val(annual_checkbox_count);
	        	
				if (monthly_checkbox_count > 0 && annual_checkbox_count == 0) 
				{
					var duration = duration1;
				}
				else if (annual_checkbox_count > 0 && monthly_checkbox_count == 0) 
				{
					var duration = duration2;
				}
				else
				{
					var duration = '';
				}

	        }



         	if(duration != '')
	        {

	            $.ajax({
	                url: base_url+'payment/apply_coupon_from_signup_ajax', // point to server-side controller method
	                data: {
	                    duration:duration,
	                    coupon_code:coupon_code
	                },
	                type: 'post',
	                // dataType:'json',
	                success: function (response) {
	                    if(response)
	                    {
	                        var result = JSON.parse(response);
	                        if (result.status == 'true') 
	                        {
	                        	// calculate monthly
	                        	charges_array1 = [];
	                        	result_amount_array1 = [];

	                        	$('.checkbox_field1:checkbox:checked').each(function(){
								    charges_array1.push(parseFloat($(this).attr("data-charges")));
								    result_amount_array1.push(parseFloat($(this).attr("data-resultamount")));
								});

								var total_amount1 = charges_array1.reduce(function(a, b){
															        return a + b;
															    }, 0);

								if ($('.checkbox_result_amount1').is(":checked")) 
					            {
					                var total_result_amount1 = result_amount_array1.reduce(function(a, b){
																			        return a + b;
																			    }, 0);
					                total_amount1 += total_result_amount1;
					            }

					            // calculate annual
					            charges_array2 = [];
	                        	result_amount_array2 = [];

	                        	$('.checkbox_field2:checkbox:checked').each(function(){
								    charges_array2.push(parseFloat($(this).attr("data-charges")));
								    result_amount_array2.push(parseFloat($(this).attr("data-resultamount")));
								});

								var total_amount2 = charges_array2.reduce(function(a, b){
															        return a + b;
															    }, 0);

								if ($('.checkbox_result_amount2').is(":checked")) 
					            {
					                var total_result_amount2 = result_amount_array2.reduce(function(a, b){
																			        return a + b;
																			    }, 0);
					                total_amount2 += total_result_amount2;
					            }



					            var total_amount = total_amount1 + total_amount2;
	                            var discount = result.discount;
	                            var discount_amount = (total_amount * discount)/100;
	                            var final_amount = total_amount - discount_amount;



	                            // +++++++++++++++++++++++++++++++++++++++
	                            // calculate book
					            charges_array3 = [];
					            $('.checkbox_field3:checkbox:checked').each(function(){
								    charges_array3.push(parseFloat($(this).attr("data-charges")));
								});
					            var total_amount3 = charges_array3.reduce(function(a, b){
															        return a + b;
															    }, 0);

					            // calculate question paper
					            charges_array4 = [];
					            $('.checkbox_field4:checkbox:checked').each(function(){
								    charges_array4.push(parseFloat($(this).attr("data-charges")));
								});
					            var total_amount4 = charges_array4.reduce(function(a, b){
															        return a + b;
															    }, 0);
	                     	


	                     		var grand_final_amount = final_amount + total_amount3 + total_amount4;
	                     		// +++++++++++++++++++++++++++++++++++++++


	                            $('#final_total_cost').text(grand_final_amount.toFixed(2));
	                            $('#final_total_amount').val(grand_final_amount.toFixed(2));

	                            $('#apply_coupon').hide();
	                            // $('#cancel_coupon').show();
	                            $('#coupon_code_error').text('');
	                            $('#coupon_code').attr('readonly', true);
	                            $('#is_applied').html('');
	                            $('#coupon_status').val('yes');
	                            $('#is_applied').html('<b style="color:green;">coupon applied <i class="fas fa-check-circle"></i></b>');
	                        }
	                        else
	                        {
	                        	$('#is_applied').html('');
					        	$('#apply_coupon').show();
					        	$('#coupon_code').attr('readonly', false);
	                            $('#coupon_status').val('no');
	                            $('#coupon_code_error').text('Invalid coupon code.');
	                        }
	                    }
	                },
	            });
	        }
	        else
	        {
	        	$('#is_applied').html('');
	        	$('#apply_coupon').show();
	        	$('#coupon_code').attr('readonly', false);
	        	$('#coupon_status').val('no');
	        	$('#coupon_code_error').text('Opps! coupon not applied.');
	        }
        }
        else
        {
        	$('#coupon_code_error').text('Coupon code is required.');
        }
        






        
       
    }



