var dlghtml = {};

function opendlg(url, params, buttons)
{
	$('#sysdlg').dialog('close');
	dlg_id = String(url).replace('/', '_');
	
	if(params==null)
		params={};
	
	if(buttons==null)
	{
		buttons={};
		buttons['Ok']=function(){$('#sysdlg').dialog('close');};;
	}
	else if(buttons['Отменить']==null)
	{
		buttons = $.extend({}, {'Отменить':function(){$('#sysdlg').dialog('close');}}, buttons);
	}
	
	params.dlg_id=dlg_id;
	
	if(dlghtml[dlg_id]==null)
		dlghtml[dlg_id]={};
	
	dlghtml[dlg_id].buttons = buttons;
	
	if(dlghtml[dlg_id]!=null && false)
	{
		//onLoadDlgHtml(dlghtml[dlg_id]);
	}
	else
	{	
		show_loader();
		$.get(
			url,
			params,
			onLoadDlgHtml
		)
	}
	
}

function onLoadDlgHtml(t)
{
	hide_loader();
	o=JSON.parse(t);
	//dlghtml[o.dlg_id]=t;
	//console.log(t.dlg_id);
	$('#sysdlg').html(o.body);
	
	$('#sysdlg').dialog({
                autoOpen: false,
                width: o.width,
                height: o.height,
                title: o.title,
				modal: true,
				resizable:true,
                buttons: dlghtml[dlg_id].buttons
            });	
	new InitializeForm ().init (['registration_form']);
	
	$('#sysdlg').dialog('open');
}

function onSaveClientProfile(url)
{
	$('#sysdlg').dialog('close');	
	show_loader();
	$.post(
		 url,
		 {
				email:		$('input[name="cli_email"]').val(),
				firstname:		$('input[name="firstname"]').val(),
				lastname:		$('input[name="lastname"]').val(),
				middlename:		$('input[name="middlename"]').val(),
				city:		$('input[name="city"]').val(),
				address:		$('input[name="address"]').val(),
				zip:		$('input[name="zip"]').val(),
				phone:		$('input[name="phone"]').val(),
				recipient:		$('input[name="recipient"]').val(),
				ship:	$('select[name="ship"]').val(),
				delivery_comm:		$('input[name="delivery_comm"]').val(),
				no_news:	($('input[name="no_news"]')[0].checked?0:1),
				sys_no_news:	($('input[name="sys_no_news"]')[0].checked?1:0),
				no_orders:	($('input[name="no_orders"]')[0].checked?1:0),
				no_orders_rem:	$('textarea[name="no_orders_rem"]').val(),
				id_region:	$('select[name="id_region"]').val(),
				ship_sum:		$('input[name="ship_sum"]').val()
		 },
		 function(t)
		 {
			 if(t=='ok')
			 {
				 hide_loader();
				 $('#sysdlg').dialog('close');
				 
			 }
			 else
			 {
				 onLoadDlgHtml(t);
			 }
		 }
	);
}

function ClientBlock(id)
{
    show_loader();
	$.post(
		'/sale/set-block-client',
		{
			id:id,
			act:1
		},
		function()
		{
			$('a[name="img_block_'+id+'"]').removeClass();
			$('a[name="img_block_'+id+'"]').addClass('unremove_ico');			
			$('a[name="img_block_'+id+'"]').each(function(i, value){
				value.onclick=function(e){ClientUnBlock(id);};
				value.title='Разблокировать';
			});			
			 hide_loader();
		}
	);
}

function ClientUnBlock(id)
{
	show_loader();
	$.post(
		'/sale/set-block-client',
		{
			id:id,
			act:0
		},
		function()
		{
			hide_loader();
			$('a[name="img_block_'+id+'"]').removeClass();
			$('a[name="img_block_'+id+'"]').addClass('remove_ico');
			$('a[name="img_block_'+id+'"]').each(function(i, value){
				value.onclick=function(e){ClientBlock(id);};
				value.title='Блокировать';
			})
		}
	);
}

//------------------------------------ Main Dlg ---------------------------------------------
function BaseMainDlg(){}
with(BaseMainDlg)
{
	prototype.default_buttons={};
	prototype.dlg_id = 'dlg_' + Math.floor(Math.random()*1000000);
	
	prototype.url = ''; 
	
	prototype.init = function(params)
	{		
		this.url = params.url || '';
		
		this.url_params = params.url_params || {};
		
		var self = this;
		this.default_buttons = {
			'Отменить': function(){ self.close_dlg();},
		}
		
		
		if(typeof(params.buttons) == 'object')
		{
			this.default_buttons = $.extend({}, this.default_buttons, params.buttons);
		}
		
		this.dlg = $('<div id="' + this.dlg_id + '"></div>');
		$('body').append(this.dlg);  
		this.dlg.dialog({
				autoOpen: false,
				title: params.title || '',	
				width: params.width || 650,
				modal: true,
				buttons: this.default_buttons,
				close: function(event, ui) {$('#'+self.dlg_id).remove();}
		});
	}
	
	prototype.show = function()
	{
		var self = this;
		
		this.dlg.dialog('open');
		
		if(this.url!='')
		{
			this.img = $('<center><img src="/images/preloader2.gif"/></center>');
			this.dlg.append(this.img);
			
			$.post(this.url, this.url_params, function(t){self.onGetDlgHtml(t);})
		}
	}
	
	prototype.close_dlg = function()
	{
		this.dlg.dialog('close');
	}
	
	prototype.onGetDlgHtml = function(t)
	{
		this.dlg.html(t);
		this.dlg.dialog('option', 'position', 'center');
	}
	
	prototype.getData = function()
	{
		var data = {};
		this.dlg.find(":input").each(
			function(index){
				if(jQuery(this).attr('type')=='checkbox')
				{
					data[jQuery(this).attr("name")]= (jQuery(this).is(':checked')?1:0);
				}								
				else if(jQuery(this).attr('type')!='file' && jQuery(this).attr('type')!='button')
				{
					data[jQuery(this).attr("name")]=jQuery(this).val();
				}
			}
		);
		
		return data;
	}
	
}


//------------------------------------ Checks -----------------------------------------------
function CheckCreateionDlg(){}
with(CheckCreateionDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Создание чека',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-create-check'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		$.post(
			'/sale/create-check', 
			this.getData()
		);
		this.close_dlg();
	}
}


function CheckAddOrderDlg(){}
with(CheckAddOrderDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Добавление в чек',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-add-to-check'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		$.post(
			'/sale/add-to-check', 
			this.getData()
		);
		this.close_dlg();
	}
}

function CheckEditDlg(){}
with(CheckEditDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Свойства чека',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-edit-check'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		show_loader();
		$.post(
			'/sale/set-check', 
			this.getData(),
			function(){
				window.location.href = window.location.href;
			}
		);
		this.close_dlg();
	}
}

function SetOrderStatusByCheckDlg(){}
with(SetOrderStatusByCheckDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Установить статус',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-set-status-check'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		show_loader();
		$.post(
			'/sale/set-order-status-by-check', 
			this.getData(),
			function(){
				hide_loader();
			}
		);
		this.close_dlg();
	}
}

//------------------------------------ Routes -----------------------------------------------
function RouteDlg(){}
with(RouteDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Редактирование маршрута',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-route'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		$.post(
			'/sale/set-route', 
			this.getData(),
			function(){
				window.location.href = window.location.href;
			}
		);
		this.close_dlg();
	}
}

function RouteItemDlg(){}
with(RouteItemDlg)
{
	prototype = new BaseMainDlg();
	
	prototype.init = function(params)
	{
		var self = this;
		var params_dlg = 
		{
			title:'Редактирование адреса',
			buttons : {
				'Ok':function(){self.onOk();}
			},
			url:'/sale/popup-route-item'
		}
		
		if(typeof(params)=='object')
			params_dlg = $.extend({}, params_dlg, params);
		
		BaseMainDlg.prototype.init.call(this, params_dlg);

		return this;
	}
	
	prototype.onOk = function()
	{
		$.post(
			'/sale/set-route-item', 
			this.getData(),
			function(t){
				if(t=='duplicate')
				{
					alert('Клиент уже есть в маршруте');
				}
				else
				{
					window.location.href = window.location.href;
				}
			}
		);
		this.close_dlg();
	}
}


