var ajax_response = {
		'ERROR_UNKNOWN_REQUEST': '-1',
		'ERROR_OK': '0',
		'ERROR_MISSING_INPUT': '1',
		'ERROR_INVALID_INPUT': '2',
		'ERROR_EOF': '-1'
};

var privileges = {
		'NONE': 'NONE',
		'READWRITE': 'CAN_READ_WRITE',
		'READ': 'CAN_READ'
};
var EVENT = {
		'PRODUCT_LOAD_START':'product_load_start',
		'PRODUCT_LOADING':'product_loading',
		'PRODUCT_LOADED':'product_loaded',
		'PAPER_LOAD_START':'paper_load_start',
		'PAPER_LOADING':'paper_loading',
		'PAPER_LOADED':'paper_loaded'
}
var device_list_init_handler = null;
function get_paper_list(){
	if (typeof(p)== undefined){
		if(p.finishing_task_editor)
			finishing_task_editor_create_init();
		alert('warning: undefined p');
		return false;
	}
	if($(p.paper_ui_object_selector).attr("tagName") != "SELECT")
	{
		if($(p.paper_ui_object_selector).val() == 0 || $(p.paper_ui_object_selector).val() == ''){
			$(p.paper_ui_object_selector).parent().parent().hide();
		}
		if(p.finishing_task_editor)
			finishing_task_editor_create_init();
		return false;
	}
	var productID = $(p.product_ui_object_selector).val();
	var deviceID = $(p.device_ui_object_selector).val() || 1;
	$(p.paper_ui_object_selector).trigger(EVENT.PAPER_LOADING);
	var ui_enable = p.ui_enable||false;
	$.get('/etc/shared/ajax_page.php',
		  {'request':'get_papers', 'productID':productID, 'deviceID':deviceID, 'paper_ui_user':p.paper_ui_user, 'publicEnabled':'true', 'ui_enable': ui_enable, 'artID': p.artID||false},
		  function(data){
			if(data.result == ajax_response.ERROR_OK){
				var $parent =  $(p.paper_ui_object_selector).closest('td');
				var $obj_name = $(p.paper_ui_object_selector).attr('name');
				switch(data.type){
					default:
					case privileges.NONE:
					case privileges.READWRITE:
						$(p.paper_ui_object_selector).empty();
						var dataprovider = data.response;
						var selectedPaper = "";
						if(dataprovider.length == 0){
							$(p.paper_ui_object_selector).parent().parent().hide();
						}else{
							$(p.paper_ui_object_selector).parent().parent().show();
						}
						$.each(dataprovider, function(i, info){
							if(p.paper_ui_user != "" && p.paper_ui_user == info.paperID){
								selectedPaper = info.paperID;
								$(p.paper_ui_object_selector).attr('desc', info.description);
							}else if(info.isDefault == 'Y' && selectedPaper == ""){
								selectedPaper = info.paperID;
								$(p.paper_ui_object_selector).attr('desc', info.description);
							}
							var selectedFlag="";
							if(selectedPaper == info.paperID){
								selectedFlag = ' selected="selected" ';
							}
							var noption = '<option value="'+info.paperID+'"' + selectedFlag + '>'+info.paperID+' : '+info.description+'</option>';
							$(p.paper_ui_object_selector).append(noption);
							});
						
						$(p.paper_ui_object_selector).unbind("change");
						$(p.paper_ui_object_selector).bind("change", function(){
							
							adjust_coating();
						});
						break;
					case privileges.READ:
						var dataprovider = data.response;
						var desc = '';
						var selectedpaper = 0;
						$.each(dataprovider, function(i, info){
							if(info.isDefault == 'Y'){
								selectedpaper = info.paperID;
								desc = info.description;
							}else if(p.paper_ui_user != "" && p.paper_ui_user == info.paperID){
								selectedpaper = info.paperID;
								desc = info.description;
								return false;
							}
						});
						if(selectedpaper == 0 &&dataprovider.length >= 1){
							selectedpaper = dataprovider[0].paperID;
							desc = dataprovider[0].description;
						}
						$(p.paper_ui_object_selector).after(selectedpaper + ' - ' + desc);
						break;
				}
				$(p.paper_ui_object_selector).trigger(EVENT.PAPER_LOADED);
				if(p.finishing_task_editor)
					finishing_task_editor_create_init();
				adjust_coating();
			}else{
				$(p.paper_ui_object_selector).trigger(EVENT.PAPER_LOADED);
// Make it a silent error
/* 				alert(data.message); */
			}  
		  },
		  'json');
}

function get_device_list(){
	if(!$(p.device_ui_object_selector)[0])
		return false;
	
	var productID = $(p.product_ui_object_selector).val();
	$(p.device_ui_object_selector).empty();
	
	if($(p.device_ui_object_selector).attr("tagName") != "SELECT")
	{
		if(device_list_init_handler && typeof(device_list_init_handler) == 'function'){
			device_list_init_handler();
		}
		return;
	}
	$(p.device_ui_object_selector).append('<option value="0">loading......</option>');
	$.get('/etc/shared/ajax_page.php',
		 {'request':'get_devices','productID':productID},
			  function(data){
				if(data.result == ajax_response.ERROR_OK){
					$(p.device_ui_object_selector).empty();
					var dataprovider = data.response;
					var selectedDevice = '';
					$.each(dataprovider, function(i, info){
						
						if(p.device_ui_edit && $.inArray(info.deviceID,p.device_ui_edit.option) == -1)
						{
							return true;
						}
						
						if(p.device_ui_user != "")
						{
							if(p.device_ui_user == info.deviceID)
							{
								var option = '<option value="'+info.deviceID+'" selected="selected">'+info.deviceName+'</option>';
							}else{
								var option = '<option value="'+info.deviceID+'">'+info.deviceName+'</option>';
							}
						}else if(p.device_ui_edit){
							if(p.device_ui_edit['default'] == info.deviceID)
							{
								var option = '<option value="'+info.deviceID+'" selected="selected">'+info.deviceName+'</option>';
							}else{
								var option = '<option value="'+info.deviceID+'">'+info.deviceName+'</option>';
							}
						}else{
							if(info.isDefault == 'Y')
							{
								var option = '<option value="'+info.deviceID+'" selected="selected">'+info.deviceName+'</option>';
								
							}else{
								var option = '<option value="'+info.deviceID+'">'+info.deviceName+'</option>';
							}
						} 
						$(p.device_ui_object_selector).append(option);					
						
					});
	
					if(device_list_init_handler && typeof(device_list_init_handler) == 'function'){
						device_list_init_handler();
					}
					get_paper_list();
				}else{
					alert(data.message);
				}  
			  },
			  'json');
}

function adjust_coating()
{
	if(p.finishing_task_editor)
		finishing_task_editor_adjust_coating(p.finishing_task_editor,$(p.paper_ui_object_selector).val());
}

function set_custom()
{
	$(p.product_ui_object_selector).val('CUSTOM');
	$(p.product_ui_object_selector).trigger('change');
}

function showSubMenu(obj, sub_obj, selectedValue){
	if ($(obj) == null) return false;
	$(sub_obj).hide();
	$(sub_obj).empty();
	if($(obj).val() == 'none'){
		return false;
	}
	$.post('/etc/shared/finishing.options.php',
		  {'request':'RETRIVE','methodID':$(obj).val()}, 
		  function(data){
			  if(data.status == 0){
				  if(data.options.length > 0){
					  $(sub_obj).empty();//fix bug receive response nearly same time
					  $(sub_obj).show();
					  for(var i=0; i<data.options.length; i++){
						  $(sub_obj).append('<option value="'+data.options[i].optionID+'">'+data.options[i].optionDescription+'</option>"');
					  }
					  if(selectedValue != 'none'){
						  $(sub_obj).val(selectedValue);
					  }
				  }
			  }else{
				  alert('An error occured (' + data.status + ')');
			  }
		  },'json');
}

function lib_estimate_js_addEventListener(){
	$(p.product_ui_object_selector).bind("change", function(){
		p.paper_ui_user = '';
		p.device_ui_user = '';
	});
	$(p.device_ui_object_selector).bind("change", function(){
		p.device_ui_user = '';
		get_paper_list();
	});
}
