
countryId = 0;
areaId = 0;
cityId = 0;
checkedServiceDates = [];
orderAllow = false;
userAgree = false;

    function loadCountries(sel)
    {
        $.getJSON("index.php?module=ajaxm&action=getCountriesList", '' ,
        function(json){
            $('#edit-order-country option').remove();
            $('#edit-order-country').append('<option value="">-- Выберите страну --</option>')

            $.each(json, function()
            {
                $('#edit-order-country').append('<option value="'+this.id+'"'+(this.id==countryId?' selected':'')+'>'+this.name+'</option>')
            }
            );

            loadRegions($('#edit-order-country').val());
        });
    }

    function loadRegions(country_id)
    {
        $.getJSON("index.php?module=ajaxm&action=getRegionsList", { country_id: country_id },
        function(json){
            $('#edit-order-region option').remove();
            $('#edit-order-region').append('<option value="">-- Выберите область --</option>')

            $.each(json, function()
            {
                $('#edit-order-region').append('<option value="'+this.id+'"'+(this.id==areaId?' selected':'')+'>'+this.name+'</option>');
            }
            );

            loadCities($('#edit-order-region').val());
        });
    }

    function loadCities(region_id)
    {
        $.getJSON("index.php?module=ajaxm&action=getCitiesList", { region_id: region_id },
        function(json){
            $('#edit-order-city option').remove();
            $('#edit-order-city').append('<option value="">-- Выберите город --</option>')

            $.each(json, function()
            {
                $('#edit-order-city').append('<option value="'+this.id+'"'+(this.id==cityId?' selected':'')+'>'+this.name+'</option>');
            }
            );

        });
    }

function popupWindow(mypage, myname, w, h, scroll,resizable) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',toolbars=1,scrollbars='+scroll+',resizable='+(resizable==0?'no':'yes')
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function checkServiceDates(){
	$("#order-settlement-part :select").attr("disabled", "disabled");
    $("#order-settlement-part :textarea").attr("disabled", "disabled");
	orderDenySubmit();	
	  tmp_array=$('#edit-order-category-bulk').val().split(", ");
      cat_id = tmp_array[0]; 
      tour_id = tmp_array[1];

        //$('#buttonSubmit').attr({disabled: 'disabled'});
//        $.getJSON("index.php?module=ajaxm&action=checkServiceDates", { categoryId: $('#edit-order-category-room').val(), dateFrom: $('#edit-order-day-in').val(), dateTo: $('#edit-order-day-out').val() },
        $.getJSON("index.php?module=ajaxm&action=checkServiceDates", { tourId: tour_id, categoryId: cat_id, dateFrom: $('#edit-order-day-in').val(), dateTo: $('#edit-order-day-out').val() },
        function(json){
          checkedServiceDates=json.Errors;
          txt='';
          for(i=0; i<checkedServiceDates.length; i++){
            txt+=checkedServiceDates[i]+' ';
          }
          if(txt!=''){
            //alert(txt);
        	  var tr = $("#edit-order-days").parent().parent().parent();
        	  tr.removeClass("row-grey").addClass("row-pink");
        	  //MessageWindow("Ошибка заполнения формы", txt);
        	  $('#free-spaces-result').text("");
        	  $('#free-spaces-result').append("<div style='float:left; vertical-align:bottom; margin-right:5px;'><img id='img-spaces-result' src='images/gz_conflict.png'/></div>");
        	  $('#free-spaces-result').append("<div style='vertical-align:top;'>Введенные даты заезда и выезда противоречат графику заезда.</div><br />");
        	  sendErrorMail("Дата", txt);
        	  checkedServiceDates = [];
            orderAllowSubmit();

            //return false;
          }
          else{
            checkedServiceDates = [];
            makeCoupon();
            
            $('#free-spaces-result').text("");
      	  $('#free-spaces-result').append("<div style='float:left; vertical-align:bottom; margin-right:5px;'><img id='img-spaces-result' src='images/enough_places.png'/></div>");
      	  $('#free-spaces-result').append("<div style='vertical-align:top;'>Введенные даты заезда и выезда не противоречат графику заезда.</div><br />");
            
            $("#order-settlement-part :select").removeAttr("disabled");
            $("#order-settlement-part :textarea").removeAttr("disabled");
			orderAllowSubmit();
			//if(checkForm()){$('#laspi_order_form').submit();}
            //else{$('#buttonSubmit').removeAttr("disabled");}
          }
          
          checkFreeSpaces();
          
        });

}		    
function checkServiceDates2(){
		
		tmp_array=$('#edit-order-category-bulk').val().split(", ");
		cat_id = tmp_array[0]; 
		tour_id = tmp_array[1];
        //$('#buttonSubmit').attr({disabled: 'disabled'});
        //$.getJSON("index.php?module=ajaxm&action=checkServiceDates", { categoryId: $('#edit-order-category-room').val(), dateFrom: $('#edit-order-day-in').val(), dateTo: $('#edit-order-day-out').val() },
		$.getJSON("index.php?module=ajaxm&action=checkServiceDates", { tourId: tour_id, categoryId: cat_id, dateFrom: $('#edit-order-day-in').val(), dateTo: $('#edit-order-day-out').val() },
        function(json){
          checkedServiceDates=json.Errors;
          txt='';
          for(i=0; i<checkedServiceDates.length; i++){
            txt+=checkedServiceDates[i]+' ';
          }
          if(txt!=''){
        	  EditOrderMessage("Ошибка заполнения формы", txt);
        	
            checkedServiceDates = [];
            //$('#buttonSubmit').removeAttr("disabled");

            return false;
          }
          else{
            checkedServiceDates = [];        
            if(checkForm()){$('#new_edit_order').submit();}
            else{
            //$('#buttonSubmit').removeAttr("disabled");
            }
          }          
        });
        
        
        

}

function EditOrderMessage(title, message_text)
{
var div = $("<div id=\"diag\" title=\""+title+"\" >"+message_text+"</div>");
	
	div.dialog({dialogClass: 'alert', buttons:{"OK":function(){ div.dialog("close");}}});  
	div.bind('dialogafterclose', function(event, ui) {
    	
    	$(".alert.ui-widget-content").css("border","none");
    });
	
	$(".alert.ui-dialog").css("width","300px");
	$(".alert.ui-dialog").css("padding-bottom","20px");
	$(".alert.ui-widget-header").css("border","0");	    		
	$(".alert .ui-dialog-buttonpane").css("border","0");
	$(".alert .ui-resizable-se").css("display","none");
	$(".alert .ui-dialog-titlebar").addClass("w-error-bg-title");
	$(".alert.ui-dialog-content").addClass("w-error-p");
	$(".alert.ui-dialog-content").css("min-height","52px");
	$(".alert.ui-dialog-content").css("padding","15px 20px 0");
	$(".alert.ui-dialog-content").css("border","0");	
	$(".alert .ui-dialog-buttonpane button").css("background","transparent url(/images/button-newprop-2.gif) no-repeat scroll center top");
	$(".alert .ui-dialog-buttonpane button").css("border","0");
	$(".alert .ui-dialog-buttonpane button").css("color","#FFFFFF");	
	$(".alert .ui-dialog-buttonpane button").css("width","120px");    	
	$(".alert .ui-dialog-buttonpane button").css("margin","10px 75px 0");

}


function orderAllowSubmit(){
	orderAllow = true;
	if(userAgree){
		$('#buttonSubmit').removeAttr("disabled");
		$("#buttonSubmit").removeClass("disabled");
	}
	
}

function orderDenySubmit(){
	orderAllow = false;
	$('#buttonSubmit').attr({disabled: 'disabled'});
	$('#buttonSubmit').addClass("disabled");
}
