/* 
 *this file containsn javascript functions used in itineries operations
 */

//

//this function helps to show the form for adding a new car package overview
function car_showAddCarOverviewForm()
{
       var action_file_path = "car/index.php";
       var actioncmd = "?add_car_overview_request=1"+
       "&&div_edit_car_box_id=div_add_new_car_box";

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

       $(function()
       {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#div_add_new_car_box").dialog({
			autoOpen: false,
			height: 610,
			width: 400,
			modal: true,
                        draggable: true,
                        resizable:false,
                        show:"fade",
                        hide:"fade"
		});

		$( "#btn_add_new_car" )
                .click(function(e)
                {
                        e.preventDefault();

                      $.ajax({
                                method: "get",url:url,data: "page",
                                beforeSend: function()
                                {
                                showSpinner('div_add_new_car_box');
                                },
                                complete: function()
                                {},
                                success: function(html)
                                {
                                  $("#div_add_new_car_box").html(html);
                                  $( "#div_add_new_car_box" ).dialog( "open" );
                                }
                     }); //close $.ajax()
                });
	});
}//car_showAddCarOverviewForm()




//this function helps to show the form for editing a tour package overview
function car_showEditCarOverviewForm(input)
{  //alert(input);
    var delim =',';
    var car_id=parseInt(getTokenAt(input,0,delim,3));
    var btn_edit_car_id=getTokenAt(input,1,delim,3);
    var div_edit_car_box_id=getTokenAt(input,2,delim,3);

   var action_file_path = "car/index.php";
   var actioncmd = "?edit_car_basic_info_request=1"+
                   "&&car_id="+car_id+
                   "&&div_edit_car_box_id="+div_edit_car_box_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

       $(function()
       {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#"+div_edit_car_box_id).dialog({
			autoOpen: false,
			height: 610,
			width: 400,
			modal: true,
                        draggable: true,
                        resizable:false,
                        show:"fade",
                        hide:"fade"
		});

		$( "#"+btn_edit_car_id )
                .click(function(e)
                {
                        e.preventDefault();
                        $( "#"+div_edit_car_box_id).dialog( "open" );

                      $.ajax({
                                method: "get",url:url,data: "page",
                                beforeSend: function()
                                {
                                showSpinner(div_edit_car_box_id);
                                },
                                complete: function()
                                {},
                                success: function(html)
                                {
                                  $("#"+div_edit_car_box_id).html(html);
                                  $( "#"+div_edit_car_box_id).dialog( "open" );
                                }
                     }); //close $.ajax()
                });
	});
}//tour_showEditTourOverviewForm()
 


 //this function coordinate the saving of a car info which has just been
   //edited
      function car_editCarBasicInfo(input)
      {//alert(input);
        var delim =',';
        var missing_fields = "";

        var car_id =parseInt(getTokenAt(input,0,delim,14));
        var div_edit_car_box_id =getTokenAt(input,1,delim,14);
        var txt_car_model_id =getTokenAt(input,2,delim,14);
        var txt_car_make_id=getTokenAt(input,3,delim,14);
        var txt_car_type_id=getTokenAt(input,4,delim,14);
        var txt_car_color_id=getTokenAt(input,5,delim,14);
        var txt_car_engine_size_id=getTokenAt(input,6,delim,14);
        var select_car_seating_capacity_id=getTokenAt(input,7,delim,14);
        var select_car_offer_type_id=getTokenAt(input,8,delim,14);
        var txt_car_daily_rate_id=getTokenAt(input,9,delim,14);
        var selectday_id=getTokenAt(input,10,delim,14);
        var selectmonth_id=getTokenAt(input,11,delim,14);
        var selectyear_id=getTokenAt(input,12,delim,14);
        var function_cmd=getTokenAt(input,13,delim,14);

        var car_model = $("#"+txt_car_model_id).val();
        if(($('#'+txt_car_model_id).hasClass('ui-state-error'))||(!car_model))
         {
            $("#"+txt_car_model_id).addClass( "ui-state-error" );
            missing_fields = " Car Model ";
         }

        var car_make = $("#"+txt_car_make_id).val();
        if($('#'+txt_car_make_id).hasClass('ui-state-error'))
         {
            $("#"+txt_car_make_id).addClass( "ui-state-error" );
                missing_fields += ", Car Make";
         }

        var car_type = $("#"+txt_car_type_id).val();
        if(($('#'+txt_car_type_id).hasClass('ui-state-error'))||(!car_type))
         {
            $("#"+txt_car_type_id).addClass( "ui-state-error" );
                missing_fields += ", Car Type";
         }

        var car_seating_capacity = $("#"+select_car_seating_capacity_id).val();
        if(-1==car_seating_capacity)
            {
         //   $("#"+select_car_seating_capacity_id).addClass( "ui-state-error" );
                missing_fields += ", Seating Capacity";
            }

        var car_engine_size = $("#"+txt_car_engine_size_id).val();
        if(($('#'+txt_car_engine_size_id).hasClass('ui-state-error'))||(!car_engine_size))
         {
            $("#"+txt_car_engine_size_id).addClass( "ui-state-error" );
                missing_fields += ", Engine Size";
         }

        var car_color = $("#"+txt_car_color_id).val();
        if(($('#'+txt_car_color_id).hasClass('ui-state-error'))||(!car_color))
         {
            $("#"+txt_car_color_id).addClass( "ui-state-error" );
                missing_fields += ", Car Colour";
         }


        var car_offer_type = $("#"+select_car_offer_type_id).val();
        if(-1==car_offer_type)
            {
         //   $("#"+select_car_offer_type_id).addClass( "ui-state-error" );
                missing_fields += ", Car Offer Type";
            }

        var car_daily_rate = $("#"+txt_car_daily_rate_id).val();
        if($('#'+txt_car_daily_rate_id).hasClass('ui-state-error'))
         {
            $("#"+txt_car_daily_rate_id).addClass( "ui-state-error" );
                missing_fields += ",Rate/ Price";
         }
         
        var car_validity_day = document.getElementById(selectday_id).value;
        var car_validity_month = document.getElementById(selectmonth_id).value;
        var car_validity_year = document.getElementById(selectyear_id).value;

        if(missing_fields!="")
        {
            alert("Please provide "+missing_fields);
            return;
        }

          var action_file_path = "car/index.php";
          var actioncmd;

             if("ADD"==function_cmd)
               {
            actioncmd = "?add_car_overview_submit=1"+
            "&&txt_car_model="+car_model+
            "&&txt_car_make="+car_make+
            "&&txt_car_type="+car_type+
            "&&select_numbers="+car_seating_capacity+
            "&&txt_car_engine_size="+car_engine_size+
            "&&txt_car_color="+car_color+
            "&&car_offer_type="+car_offer_type+        
            "&&txt_car_daily_rate="+car_daily_rate+
            "&&selectday="+car_validity_day+
            "&&selectmonth="+car_validity_month+
            "&&selectyear="+car_validity_year;   
               }
             else
              {
            actioncmd = "?edit_car_basic_info_submit=1"+
            "&&txt_car_model="+car_model+
            "&&txt_car_make="+car_make+
            "&&txt_car_type="+car_type+
            "&&select_numbers="+car_seating_capacity+
            "&&txt_car_engine_size="+car_engine_size+
            "&&txt_car_color="+car_color+
            "&&car_offer_type="+car_offer_type+     
            "&&txt_car_daily_rate="+car_daily_rate+
            "&&selectday="+car_validity_day+
            "&&selectmonth="+car_validity_month+
            "&&selectyear="+car_validity_year+
            "&&car_id="+car_id;     
              }
    
            var url = action_file_path+actioncmd;
            url=url+"&sid="+Math.random();

            //alert(url);

           $.ajax({
                    method: "get",url:url,data: "page",
                    beforeSend: function()
                    {
                     // showSpinner(div_edit_car_box_id);
                    },
                    complete: function()
                    {},
                    success: function(html)
                    {
                      alert("Car "+function_cmd+"ed Successfully");
                      $( "#"+div_edit_car_box_id).dialog( "close" );
                       //view_car_listCars();
                       //$("#"+div_edit_car_box_id).html(html);
                    }
         });

      }//car_editCarBasicInfo()

 //this function helps to show the form for editing a car package description
 function car_showEditCarDescripForm(input)
 {
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));//object id
        var div_show_edit_car_descrip_id=getTokenAt(input,1,delim,2);//div id

         //url
       var action_file_path = "car/index.php";
       var actioncmd = "?edit_car_descrip_request=1"+
                       "&&car_id="+car_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

             //get the content to display using ajax
                $.ajax({
                    method: "get",url:url,data: "page",
                    beforeSend: function()
                    {
                     // showSpinner(div_show_edit_car_descrip_id);
                    }, 
                    complete: function()
                    {},
                    success: function(html)
                    {
                    $("#"+div_show_edit_car_descrip_id).html(html); //show the html inside .content div
                   }
         }); //close $.ajax()
 }//car_showEditCarDescripForm()
     

//this form cordinates showing a form for the user to confirm delete of car package
 function car_showConfirmDeleteForm(input)
{  //alert(input);
    var delim =',';
    var car_id=parseInt(getTokenAt(input,0,delim,3));
    var btn_delete_car_id=getTokenAt(input,1,delim,3);
    var div_delete_car_box_id=getTokenAt(input,2,delim,3);

    var action_file_path = "car/index.php";
    var actioncmd = "?show_confirm_delete_car_form_request=1"+
                     "&&car_id="+car_id+
                     "&&div_show_delete_car_id="+div_delete_car_box_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

       $(function()
       {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#"+div_delete_car_box_id).dialog({
			autoOpen: false,
			height: 140,
			width: 500,
			modal: true,
                        draggable: false,
                        resizable:false,
                        show:"fade",
                        hide:"fade"
		});

		$( "#"+btn_delete_car_id )
                .click(function(e)
                {
                        e.preventDefault();

                      $.ajax({
                                method: "get",url:url,data: "page",
                                beforeSend: function()
                                {
                                //    showSpinner(div_delete_car_box_id);
                                },
                                complete: function()
                                {},
                                success: function(html)
                                {
                                  $("#"+div_delete_car_box_id).html(html);
                                  $( "#"+div_delete_car_box_id).dialog( "open" );
                                }
                     }); //close $.ajax()
                });
	});
}//car_showConfirmDeleteForm()


        //this function cordinates deleting of a car from the database
  function car_deleteCar(input)
      {    
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));
        var div_show_delete_car_id=getTokenAt(input,1,delim,2);
        
       var action_file_path = "car/index.php";
       var actioncmd = "?delete_car_request=1"+
                        "&&car_id="+car_id+
                        "&&div_show_delete_car_id="+div_show_delete_car_id;

            var url = action_file_path+actioncmd;
            url=url+"&sid="+Math.random();

      //get the content to display using ajax
        $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
               showSpinner(div_show_delete_car_id);
            },
            complete: function()
            {
            },
            success: function(html)
            {
            $( "#"+div_show_delete_car_id).dialog( "close" );
            alert("Car Deleted Successfully");
            view_car_listCars();

            }
     }); 
      }//car_deleteCar()


    //this function helps to show the form for editing a car terms and conditions
    function car_showEditCarHireTermsConditionsForm(div_edit_car_hire_terms_conditions)
 {  
                 //url
               var action_file_path = "car/index.php";
               var actioncmd = "?edit_car_hire_terms_conditions_request=1"+
                               "&&div_edit_car_hire_terms_conditions="+div_edit_car_hire_terms_conditions;

               var url = action_file_path+actioncmd;
               url=url+"&sid="+Math.random();

              //get the content to display using ajax
                $.ajax({
                    method: "get",url:url,data: "page",
                    beforeSend: function()
                    {
                    //    showSpinner(div_edit_car_hire_terms_conditions);
                    },
                    complete: function()
                    {},
                    success: function(html)
                    {
                    $("#"+div_edit_car_hire_terms_conditions).html(html);
                    }
         }); //close $.ajax()
 }//car_showEditCarHireTermsConditionsForm()

         

//this function helps to show the form for editing notes
  function car_showEditCarNotesForm(input)
 {
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));//object id
        var div_show_edit_car_notes_id=getTokenAt(input,1,delim,2);//div id
        
         //url
       var action_file_path = "car/index.php";
       var actioncmd = "?edit_car_notes_request=1"+
                       "&&car_id="+car_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

 //get the content to display using ajax
        $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
             // showSpinner(div_show_edit_car_notes_id);
            },
            complete: function()
            {},
            success: function(html)
            {
            $("#"+div_show_edit_car_notes_id).html(html); //show the html inside .content div
            }
     }); //close $.ajax()
 }//car_showEditCarNotesForm()

////////////////////////////////////////////////////////////////////////////////

//this function helps to show the form for editing car price includes
 function car_showEditCarPriceIncludesForm(input)
 {
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));//object id
        var div_show_edit_car_price_includes_id=getTokenAt(input,1,delim,2);//div id

         //url
       var action_file_path = "car/index.php";
       var actioncmd = "?edit_car_price_includes_request=1"+
                       "&&car_id="+car_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();

      //get the content to display using ajax
        $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
            //  showSpinner(div_show_edit_car_price_includes_id);
            },
            complete: function(){}, 
            success: function(html)
            {
            $("#"+div_show_edit_car_price_includes_id).html(html);
           }
     }); //close $.ajax()
 }//car_showEditCarPriceIncludesForm()

   
//this function helps to show the form for editing car price excludes
 function car_showEditCarPriceExcludesForm(input)
 {
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));//object id
        var div_show_edit_car_price_excludes_id=getTokenAt(input,1,delim,2);//div id

         //url
       var action_file_path = "car/index.php";
       var actioncmd = "?edit_car_price_excludes_request=1"+
                       "&&car_id="+car_id;

       var url = action_file_path+actioncmd;
       url=url+"&sid="+Math.random();


      //get the content to display using ajax
        $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
            //  showSpinner(div_show_edit_car_price_excludes_id);
            }, 
            complete: function(){},
            success: function(html)
            {
            $("#"+div_show_edit_car_price_excludes_id).html(html); //show the html inside .content div
            }
     }); //close $.ajax()
 }//car_showEditCarPriceExcludesForm()

    


//this function helps to select a tour package to be made the current promotion
function car_setCurrentPromotionCar(input)
{
        var delim =',';
        var car_id=parseInt(getTokenAt(input,0,delim,2));
        var div_set_current_promotion_car=getTokenAt(input,1,delim,2);

        var action_file_path = "car/index.php";
        var actioncmd = "?set_current_promotion_car_request=1"+
                        "&&car_id="+car_id;

        var url = action_file_path+actioncmd;
        url=url+"&sid="+Math.random();

        //get the content to display using ajax
        $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
              showSpinner(div_set_current_promotion_car);
            },
            complete: function()
            {
            },
            success: function(html)
            {
            $("#"+div_set_current_promotion_car).html(html);
            $("#"+div_set_current_promotion_car).hide("slow");
            }
     }); //close $.ajax()
}//car_setCurrentPromotionCar()

