
var roomnumglobal = 0;
var pricetypes;

var preloaddone = false;

$(document).ready(function() {

  Shadowbox.init( {'enableKeys':false} );



  $( "#categories" ).change(function () {

    var category = $( "#categories" ).val();
    $.getJSON(
      "price_calc.d2?method=jsoncategory&id=" + category,
      function( data ){

        //városok visszatöltése
        $( "#cities" ).empty();
        $( "#hotels" ).empty();
        $( "#promotions" ).empty();
        $( "#paymentmethods" ).empty();
        $( "#roomnum" ).empty();
        $( "#promotionshow" ).empty();
        $( "#showroom" ).empty();
        $( '#newroom' ).attr("disabled", true);
        $( '#deleteroom' ).attr("disabled", true);
        $( "#number" ).css("display","none");
        $( '#calculate' ).attr("disabled", true);
        $( '#booking' ).attr("disabled", true);
        $( '#calculatesendmail' ).attr("disabled", true);
        roomnumglobal = 0;
        $.each( data.cities, function( i, item ){
          var option   = document.createElement("option");
          option.value = item.city_url_format;
          option.text  = item.city;
          var cities = document.getElementById("cities");

          try{
            cities.add( option, null );
          } catch ( e ) {
            cities.add( option );
          }
        });

        if ( pre_city_url_format != '' && ! preloaddone ) {
          $( "#cities option[value='" + pre_city_url_format + "']").attr("selected", true);
          $( "#cities" ).trigger('change');
        } else if ( data.cities.length == 1 ){
          $( "#cities" ).trigger('change');
        }
		
		if (data.hotels){
			$.each( data.hotels, function( i, item ){
			  var option   = document.createElement("option");
			  option.value = item.id;
			  option.text  = item.name;
			  var sel = document.getElementById("hotels");

			  try{
				sel.add( option, null );
			  } catch ( e ) {
				sel.add( option );
			  }
			});


			if ( pre_serviceid != 0 && ! preloaddone ) {
			  $( "#hotels option[value='" + pre_serviceid + "']").attr("selected", true);
			  $( "#hotels" ).trigger('change');
			} 
			
			else if ( data.hotels.length == 1 ){
			  $( "#hotels" ).trigger('change');
			}
		
		}
		
      }
    );



  });


  $( "#cities" ).change(function () {

    var a        = $( "#cities" ).val();
    var category = $( "#categories" ).val();
	
	if( category == 0)
	  alert('Kérjük válasszon egy kategóriát!');
	
    $.getJSON(
      "price_calc.d2?method=jsoncity&city_url_format=" + a + "&categoryid=" + category,
      function( data ){

        //hotelek betöltése
        $( "#hotels" ).empty();
        $( "#promotions" ).empty();
        $( "#paymentmethods" ).empty();
        $( "#roomnum" ).empty();
        $( "#showroom" ).empty();
        $( "#promotionshow" ).empty();
        $( '#newroom' ).attr("disabled", true);
        $( '#deleteroom' ).attr("disabled", true);
        $( "#number" ).css("display","none");
        $( '#calculate' ).attr("disabled", true);
        $( '#booking' ).attr("disabled", true);
        $( '#calculatesendmail' ).attr("disabled", true);
        roomnumglobal = 0;
        $.each( data.hotels, function( i, item ){
          var option   = document.createElement("option");
          option.value = item.id;
          option.text  = item.name;
          var sel = document.getElementById("hotels");

          try{
            sel.add( option, null );
          } catch ( e ) {
            sel.add( option );
          }
        });


        if ( pre_serviceid != 0 && ! preloaddone ) {
          $( "#hotels option[value='" + pre_serviceid + "']").attr("selected", true);
          $( "#hotels" ).trigger('change');
        } else if ( data.hotels.length == 1 ){
          $( "#hotels" ).trigger('change');
        }

      }
    );
  });


  $( "#hotels" ).change(function () {

    var a        = $( "#hotels" ).val();
    $.getJSON(
      "price_calc.d2?method=jsonhotel&serviceid=" + a,
      function( data ){
		getPrevMonth( a ); 
		
		
		
		
        //hotelek betöltése
        $( "#promotions" ).empty();
        $( "#paymentmethods" ).empty();
        $( "#roomnum" ).empty();
        $( "#showroom" ).empty();
        $( "#promotionshow" ).empty();
        $( '#newroom' ).attr("disabled", true);
        $( '#deleteroom' ).attr("disabled", true);
        $( "#number" ).css("display","none");
        $( '#calculate' ).attr("disabled", true);
        $( '#booking' ).attr("disabled", true);
        $( '#calculatesendmail' ).attr("disabled", true);
        roomnumglobal = 0;

        $.each( data.promotions, function( i, item ){
          var option   = document.createElement("option");
          option.value = item.id;
          option.text  = item.name;
          var sel = document.getElementById("promotions");

          try{
            sel.add( option, null );
          } catch ( e ) {
            sel.add( option );
          }
        });

        if ( pre_promotionid != 0 && ! preloaddone ) {
          $( "#promotions option[value='" + pre_promotionid + "']").attr("selected", true);
          $( "#promotions" ).trigger('change');
          preloaddone = true ;
        } else if ( data.promotions.length == 1 ){
          $( "#promotions" ).trigger('change');
        }

      }
    );
  });

  $( "#promotions" ).change(function () {
    var a        = $( "#promotions" ).val();

    $.getJSON(
      "price_calc.d2?method=jsonpromotion&promotionid=" + a,
      function( data ){

        //hotelek betöltése
        $( "#paymentmethods" ).empty();
        $( "#roomnum" ).empty();
        $( "#showroom" ).empty();
        $( "#promotionshow" ).empty();
        $( '#newroom' ).removeAttr("disabled");
        $( '#deleteroom' ).removeAttr("disabled");
        $( "#number" ).css("display","none");

        $( '#booking' ).removeAttr("disabled");
        //$( '#calculate' ).attr("disabled", true);
        $( '#calculatesendmail' ).attr("disabled", true);
        roomnumglobal = 0;
        $.each( data.paymentmethods, function( i, item ){
          var option   = document.createElement("option");
          option.value = item.id;
          option.text  = item.name;
          var sel = document.getElementById("paymentmethods");

          try{
            sel.add( option, null );
          } catch ( e ) {
            sel.add( option );
          }
        });
        $( "#promotionshow" ).html( data.promotionhtml );
        pricetypes = data.pricetypes;

        if ( data.paymentmethods.length == 1 ){
          $( "#paymentmethods" ).trigger('change');
        }
        $("#number").show();


    var newdiv =  $("#toclone").clone().attr( "id", "room_" + ++roomnumglobal ).show();
    $("#showroom").append( newdiv );

    $("#room_" + roomnumglobal + " .toclonetitle").text(roomnumglobal +'. szoba');

    $.each( pricetypes, function( i, item ){

      $("#room_" + roomnumglobal + " .roomtype").append("<option value='"+item.id+"'>"+item.name+"</option>");

    });

    $("#room_" + roomnumglobal + " .roomtype").attr("name", "pricetype[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabed").attr("name", "extrabed[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabedage").attr("name", "extrabedage[" + roomnumglobal +"]");

    $( '#calculate' ).removeAttr("disabled");
    $( '#calculatesendmail' ).removeAttr("disabled");
      }
    );

  });

  $( "#deleteroom" ).click(function () {

    var a        = $( "#promotions" ).val();
    $.getJSON(
      "price_calc.d2?method=jsonpromotion&promotionid=" + a,
      function( data ){

        //hotelek betöltése
        $( "#paymentmethods" ).empty();
        $( "#roomnum" ).empty();
        $( "#showroom" ).empty();
        $( "#promotionshow" ).empty();
        $( '#newroom' ).removeAttr("disabled");
        $( '#deleteroom' ).removeAttr("disabled");
        $( "#number" ).css("display","none");
        $( '#booking' ).removeAttr("disabled");
        //$( '#calculate' ).attr("disabled", true);
        $( '#calculatesendmail' ).attr("disabled", true);
        roomnumglobal = 0;
        $.each( data.paymentmethods, function( i, item ){
          var option   = document.createElement("option");
          option.value = item.id;
          option.text  = item.name;
          var sel = document.getElementById("paymentmethods");

          try{
            sel.add( option, null );
          } catch ( e ) {
            sel.add( option );
          }
        });
        $( "#promotionshow" ).html( data.promotionhtml );
        pricetypes = data.pricetypes;

        if ( data.paymentmethods.length == 1 ){
          $( "#paymentmethods" ).trigger('change');
        }
        $("#number").show();


    var newdiv =  $("#toclone").clone().attr( "id", "room_" + ++roomnumglobal ).show();
    $("#showroom").append( newdiv );

    $("#room_" + roomnumglobal + " .toclonetitle").text(roomnumglobal +'. szoba');

    $.each( pricetypes, function( i, item ){

      $("#room_" + roomnumglobal + " .roomtype").append("<option value='"+item.id+"'>"+item.name+"</option>");

    });

    $("#room_" + roomnumglobal + " .roomtype").attr("name", "pricetype[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabed").attr("name", "extrabed[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabedage").attr("name", "extrabedage[" + roomnumglobal +"]");

    $( '#calculate' ).removeAttr("disabled");
    $( '#calculatesendmail' ).removeAttr("disabled");
      }
    );

  });

  $( "#newroom" ).click(function () {
   $("#number").show();


    var newdiv =  $("#toclone").clone().attr( "id", "room_" + ++roomnumglobal ).show();
    $("#showroom").append( newdiv );

    $("#room_" + roomnumglobal + " .toclonetitle").text(roomnumglobal +'. szoba');

    $.each( pricetypes, function( i, item ){

      $("#room_" + roomnumglobal + " .roomtype").append("<option value='"+item.id+"'>"+item.name+"</option>");

    });

    $("#room_" + roomnumglobal + " .roomtype").attr("name", "pricetype[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabed").attr("name", "extrabed[" + roomnumglobal +"]");
    $("#room_" + roomnumglobal + " .extrabedage").attr("name", "extrabedage[" + roomnumglobal +"]");

    $( '#calculate' ).removeAttr("disabled");
    $( '#calculatesendmail' ).removeAttr("disabled");
  });

  $("#calculatesendmail").click(function () {

    $('#sendmail').val(1);
    var ser = $("#pricecalcform").serialize();

    $.post("price_calc.d2", ser, function( data ){
        Shadowbox.open({
          player:     'html',
          content:    data,
          height:     350,
          width:      400,
          resizeDuration : 0.01,
          fadeDuration : 0.01
        });
      },
      'html'
    );


  });


/*
 $("#booking").click(function () {
  var a        = $( "#hotels" ).val();
  var b        = $( "#promotions" ).val();
  var c        = $( "#datefrom" ).val();
  var d        = $( "#dateto" ).val();


   var ser = $("#pricecalcform").serialize();

 $.post("services.d2?target=details&booking=1", ser, "json");
    //alert(ser);
   // location.href = "services.d2?target=details&booking=1&id=" + a +"&datefrom=" + c +"&dateto=" + d +"&promotionid=" + b +"#inputfields";
  });
*/
  $("#calculate").click(function () {

    $('#sendmail').val(0);
    var ser = $("#pricecalcform").serialize();

    $.post("price_calc.d2", ser, function( data ){
        Shadowbox.open({
          player:     'html',
          content:    data,
          height:     300,
          width:      400,
          resizeDuration : 0.01,
          fadeDuration : 0.01
        });
      },
      'html'
    );

  });


// ha be kell tölteni valamit
if ( pre_categoryid >0 ){

  $( "#categories option[value=" + pre_categoryid + "]").attr("selected", true);
  $( "#categories" ).trigger('change');


}

});


