$(document).ready(function(){

	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

    $("select[name=tip_transport]").change(function () {
      var tip_transport;
      $("select[name=tip_transport] option:selected").each(function () {
            tip_transport = $(this).val();
            if(tip_transport == 3 || tip_transport == 2)
            {
                $("select[name=subtip_transport]").hide();
            }
            else
            {
                $("select[name=subtip_transport]").show();
            }
            if(tip_transport == 1)
            {
				$("input[name=nr_containere]").removeAttr('disabled');
                $("input[name=numar_colete]").attr('disabled', true);
                $("input[name=lungime]").attr('disabled', true);
                $("input[name=latime]").attr('disabled', true);
                $("input[name=inaltime]").attr('disabled', true);
            }
            else
            {
				$("input[name=nr_containere]").attr('disabled', true);
                $("input[name=numar_colete]").removeAttr('disabled');
                $("input[name=lungime]").removeAttr('disabled');
                $("input[name=latime]").removeAttr('disabled');
                $("input[name=inaltime]").removeAttr('disabled');
            }

           $("select[name=destinatie]").trigger('change');
           $("select[name=origine]").trigger('change');
           
          });
    })
    .trigger('change');

    function numara_bife()
    {
        $(".bifa_solicita_cotatie").change(function () {
            var count = $(".bifa_solicita_cotatie:checkbox:checked").length;
            $("#nr_transp_bifati").html(count);
        })
        .trigger('change');
    }
    numara_bife();

    $(".ckb_ftransp").change(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var tip1 = 0, tip2 = 0, tip3 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }
        if($("input[name=cu_tranz_fin]").attr("checked") == true)
        {
            tip3 = 1;
        }

        $.ajax({
           type: "GET",
           url: url_base + "get_transportatori.php",
           data: "tip1="+tip1+"&tip2="+tip2+"&tip3="+tip3,
           success: function(msg){
             $(".box_oferte").html(msg);
             $("#nr_transp_bifati").html(0);
             numara_bife();
           }
        });
    });

    $(".ckb_atransp").change(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }
        
        var id_cotatie = $("input[name=id_cotatie]").val();

        $.ajax({
           type: "GET",
           url: url_base + "get_transp_care_au_aplicat.php",
           data: "id_cotatie="+id_cotatie+"&tip1="+tip1+"&tip2="+tip2,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });

	$(".ckb_deselect").change(function () {
		
		if($(this).attr("checked") == true)
		{
			$(".bifa_solicita_cotatie").attr("checked", false);
			$("#nr_transp_bifati").html(0);
		}
		else
		{
			$(".bifa_solicita_cotatie").attr("checked", true);
		}

	});

    $(".ordonare_a").click(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var ordonare = 0;
        if($(this).attr("id") == "pret_up")
        {
            ordonare = 3;
        }
        if($(this).attr("id") == "pret_down")
        {
            ordonare = 4;
        }

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }

        var id_cotatie = $("input[name=id_cotatie]").val();

        $.ajax({
           type: "GET",
           url: url_base + "get_transp_care_au_aplicat.php",
           data: "id_cotatie="+id_cotatie+"&tip1="+tip1+"&tip2="+tip2+"&ordonare="+ordonare,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });

//-----------------------------------------------------------------------------------------------// 

    $(".ckb_sotransp").change(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }

        $.ajax({
           type: "GET",
           url: url_base + "get_solicitari_oferta.php",
           data: "tip1="+tip1+"&tip2="+tip2,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });

    $(".ordonare_so").click(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var ordonare = 0;
        if($(this).attr("id") == "alf_up")
        {
            ordonare = 1;
        }
        if($(this).attr("id") == "alf_down")
        {
            ordonare = 2;
        }
        if($(this).attr("id") == "pret_up")
        {
            ordonare = 3;
        }
        if($(this).attr("id") == "pret_down")
        {
            ordonare = 4;
        }

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }

        $.ajax({
           type: "GET",
           url: url_base + "get_solicitari_oferta.php",
           data: "tip1="+tip1+"&tip2="+tip2+"&ordonare="+ordonare,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });
//-----------------------------------------------------------------------------------------------//

    $(".ckb_ftransp_cdet").change(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }
        var id_cotatie = $("input[name=id_cotatie]").val();

        $.ajax({
           type: "GET",
           url: url_base + "get_transportatori_of_primite.php",
           data: "id_cotatie="+id_cotatie+"&tip1="+tip1+"&tip2="+tip2,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });

    $(".ordonare").click(function () {

        $(".box_oferte").html('<img src="' + dir_template + '/img/ajax-loader.gif" width="220" style="float:left;padding:0px 0px 5px 5px;" height="19" />');

        var ordonare = 0;
        if($(this).attr("id") == "alf_up")
        {
            ordonare = 1;
        }
        if($(this).attr("id") == "alf_down")
        {
            ordonare = 2;
        }
        if($(this).attr("id") == "pret_up")
        {
            ordonare = 3;
        }
        if($(this).attr("id") == "pret_down")
        {
            ordonare = 4;
        }

        var tip1 = 0, tip2 = 0;
        if($("input[name=case_expeditie]").attr("checked") == true)
        {
            tip1 = 1;
        }
        if($("input[name=linii_maritime]").attr("checked") == true)
        {
            tip2 = 1;
        }

        var id_cotatie = $("input[name=id_cotatie]").val();

        $.ajax({
           type: "GET",
           url: url_base + "get_transportatori_of_primite.php",
           data: "id_cotatie="+id_cotatie+"&tip1="+tip1+"&tip2="+tip2+"&ordonare="+ordonare,
           success: function(msg){
             $(".box_oferte").html(msg);
           }
        });
    });

    $(".edit_user").click(function () {

        var id_user = $(this).attr("id");
        var idp = id_user.substr(5);

        $("#box_pt_editare_"+idp).animate({"height": "toggle"}, {duration: 1000});

    });

    $(".edit_user_btn").click(function () {

        var id_user = $(this).attr("id");
        var idp = id_user.substr(10);

        var pass1 = $("input[name=pass1_"+idp+"]").val();
        var pass2 = $("input[name=pass2_"+idp+"]").val();
		var email = $("input[name=email_"+idp+"]").val();

        $.ajax({
           type: "GET",
           url: url_base + "editeaza_admini_secundari.php",
           data: "id_user="+idp+"&pass1="+pass1+"&pass2="+pass2+"&email="+email,
           success: function(msg){
             $("#mesaj_"+idp).html(msg);
           }
        });

    });

    $(".delete_user").click(function () {

        var id_user = $(this).attr("id");
        var idp = id_user.substr(7);

        var name=confirm("Are you sure you want to delete this user?");
        if (name==true)
        {
            $.ajax({
               type: "GET",
               url: url_base + "sterge_admini_secundari.php",
               data: "id_user="+idp,
               success: function(msg){
                 $("#linie_admin_"+idp).remove();
                 alert(msg);
               }
            });
        }
    });


    $(".delete_ruta").click(function () {

        var id_user = $(this).attr("id");
        var idp = id_user.substr(7);

        var name=confirm("Are you sure you want to delete this route?");
        if (name==true)
        {
            $.ajax({
               type: "GET",
               url: url_base + "sterge_ruta.php",
               data: "id_ruta="+idp,
               success: function(msg){
                 $("#linie_admin_"+idp).remove();
                 alert(msg);
               }
            });
        }
    });
/*
    $("select[name=tara_plecare]").change(function () {
      var tara;
      $("select[name=tara_plecare] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=1",
               dataType: 'json',
               success: function(msg){
                 $("select[name=port_plecare]").empty();
                 $.each(msg, function(key, value)
                 {
                     $("select[name=port_plecare]").
                          append($("<option></option>").
                          attr("value",key).
                          text(value));
                 });
               }
            });
          });
    })
    .trigger('change');

    $("select[name=tara_destinatie]").change(function () {
      var tara;
      $("select[name=tara_destinatie] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=1",
               dataType: 'json',
               success: function(msg){
                 $("select[name=port_destinatie]").empty();
                 $.each(msg, function(key, value)
                 {
                     $("select[name=port_destinatie]").
                          append($("<option></option>").
                          attr("value",key).
                          text(value));
                 });
               }
            });
          });
    })
    .trigger('change');

    $("select[name=tara_plecare_aero]").change(function () {
      var tara;
      $("select[name=tara_plecare_aero] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=2",
               dataType: 'json',
               success: function(msg){
                 $("select[name=aeroport_plecare]").empty();
                 $.each(msg, function(key, value)
                 {
                     $("select[name=aeroport_plecare]").
                          append($("<option></option>").
                          attr("value",key).
                          text(value));
                 });
               }
            });
          });
    })
    .trigger('change');

    $("select[name=tara_destinatie_aero]").change(function () {
      var tara;
      $("select[name=tara_destinatie_aero] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=2",
               dataType: 'json',
               success: function(msg){
                 $("select[name=aeroport_destinatie]").empty();
                 $.each(msg, function(key, value)
                 {
                     $("select[name=aeroport_destinatie]").
                          append($("<option></option>").
                          attr("value",key).
                          text(value));
                 });
               }
            });
          });
    })
    .trigger('change');
*/

    $("select[name=origine]").change(function () {
      var tara;
      var tip = $("select[name=tip_transport] option:selected").val();
	  var loc_origine = getCookie("loc_origine");
      if(tip == 3)
      {
         tip = 2;
      }
      else
      {
          tip = 1;
      }
      $("select[name=origine] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip="+tip,
               dataType: 'json',
               success: function(msg){
                 $("select[name=loc_origine]").empty();
                 $.each(msg, function(key, value)
                 {
					 if(key == loc_origine)
					 {
						 $("select[name=loc_origine]").
							append($("<option></option>").
							attr("value",key).
							attr("selected","selected").
							text(value));
					 }
					 else
					 {
						 $("select[name=loc_origine]").
							append($("<option></option>").
							attr("value",key).
							text(value));
					 }
                     
                 });
               }
            });
          });
    })
    .trigger('change');

    $("select[name=destinatie]").change(function () {
      var tara;
      var tip = $("select[name=tip_transport] option:selected").val();
	  var loc_destinatie = getCookie("loc_destinatie");
      if(tip == 3)
      {
         tip = 2;
      }
      else
      {
          tip = 1;
      }
      $("select[name=destinatie] option:selected").each(function () {
            tara = $(this).val();
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip="+tip,
               dataType: 'json',
               success: function(msg){
                 $("select[name=loc_destinatie]").empty();
                 $.each(msg, function(key, value)
                 {
					 if(key == loc_destinatie)
					 {
						  $("select[name=loc_destinatie]").
							append($("<option></option>").
							attr("value",key).
							attr("selected","selected").
							text(value));
					 }
					 else
					 {
						 $("select[name=loc_destinatie]").
							append($("<option></option>").
							attr("value",key).
							text(value));
					 }
                 });
               }
            });
          });
    })
    .trigger('change');

    $("#tab1_despre").click(function () {
        $(".txt_tab1_despre").show();
        $(".txt_tab2_despre").hide();
        $(this).css("font-weight","bold");
        $(this).css("background-color","#3B5B8A");
        $(this).css("color","#fff");
        $("#tab2_despre").css("font-weight","normal");
        $("#tab2_despre").css("background","none");
        $("#tab2_despre").css("color","#000");
    });

    $("#tab2_despre").click(function () {
        $(".txt_tab1_despre").hide();
        $(".txt_tab2_despre").show();
        $(this).css("font-weight","bold");
        $(this).css("background-color","#3B5B8A");
        $(this).css("color","#fff");
        $("#tab1_despre").css("font-weight","normal");
        $("#tab1_despre").css("background","none");
        $("#tab1_despre").css("color","#000");
    });

    $(".btn_det_opinii").toggle(function() {
            $(this).text("Inapoi");
            $(this).prev(".opinie_dr").css('height', 'auto').css('overflow', 'visible');
    }, function() {
            $(this).text("Detalii");
            $(this).prev(".opinie_dr").css('height', '92px').css('overflow', 'hidden');
    });

    $("select[name=tara_ruta[]]").live("change", function () {
      var tara;
      /*$(this).each(function () {*/
            tara = $(this).val();
            var sel = $(this).next("select[name=port_ruta[]]");
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=1",
               dataType: 'json',
               success: function(msg){
                 sel.empty();
                 $.each(msg, function(key, value)
                 {
                     sel.
                          append($("<option></option>").
                          attr("value",key).
                          text(value));
                 });
               }
            });
         /*});*/
         return false;
    })
    .trigger('change');

    $("#sterge_ruta_maritima").live("click", function () {
        $(this).parent(".rand_rute").remove();
    });

    $("#adauga_ruta_maritima").click(function () {
        var html_maritim = $('.rand_nou_rm').html();
        $('.linie_rm').append("<div class='rand_rute'>"+html_maritim+"<a title='Sterge port' href='javascript:void(0);' id='sterge_ruta_maritima'></a></div>");
        $('select[name=tara_ruta[]]:last').trigger('change');
    });

    $("select[name=tara_ruta_aero[]]").live("change", function () {
      var tara;
      /*$(this).each(function () {*/
            tara = $(this).val();
            var sel = $(this).next("select[name=aeroport_ruta[]]");
            $.ajax({
               type: "GET",
               url: url_base + "get_porturi_aeroporturi.php",
               data: "tara="+tara+"&tip=2",
               dataType: 'json',
               success: function(msg){
                 sel.empty();
                 $.each(msg, function(key, value)
                 {
					 sel.
						  append($("<option></option>").
						  attr("value",key).
						  text(value));
                 });
               }
            });
         /*});*/
         return false;
    })
    .trigger('change');

    $("#sterge_ruta_aeriana").live("click", function () {
        $(this).parent(".rand_rute_aero").remove();
    });

    $("#adauga_ruta_aeriana").click(function () {
        var html_aero = $('.rand_nou_ra').html();
        $('.linie_ra').append("<div class='rand_rute_aero'>"+html_aero+"<a title='Sterge aeroport' href='javascript:void(0);' id='sterge_ruta_aeriana'></a></div>");
        $('select[name=tara_ruta_aero[]]:last').trigger('change');
    });

	
	$("select[name=subtip_transport]").change(function () {
	  var subtip_transport;
      $("select[name=subtip_transport] option:selected").each(function () {
			subtip_transport = $(this).val();
            if(subtip_transport == 4 || subtip_transport == 5 || subtip_transport == 6 || subtip_transport == 7)
            {
                $("input[name=lungime]").removeAttr('disabled');
                $("input[name=latime]").removeAttr('disabled');
                $("input[name=inaltime]").removeAttr('disabled');
            }
            else
            {
				$("input[name=lungime]").attr('disabled', true);
                $("input[name=latime]").attr('disabled', true);
                $("input[name=inaltime]").attr('disabled', true);
            }
          });
    })
    .trigger('change');

});
