// Effects for Sleek admin skin
$(function(){
		$("p.notice span").click(function() {  // Whenever someone clicks the litte X on the notice message do the following
			$('p.notice').slideUp('slow');     // Fade out the whole paragraph
			$('p.notice span').hide('fast');   // And hide the X 
			return false;
		});
		$("p.info span").click(function() {
			$('p.info').slideUp('slow');
			$('p.info span').hide('fast');
			return false;
		});
		$("p.success span").click(function() {
			$('p.success').slideUp('slow');
			$('p.success span').hide('fast');
			return false;
		});
		$("p.error span").click(function() {
			$('p.error').slideUp('slow');
			$('p.error span').hide('fast');
			return false;
		});
		$("p.todoitem a.close").click(function() { // Whenever someone clicks on a the X in a To-do item do this:
			$(this).hide('fast');				   // Hide the X - FAST!
			$(this).parent().slideUp('slow');      // Slide Up the to-do item.
			return false;
		});	

		$("button.end").click(function() {
			return confirm("Desea finalizar esta tranzacción?") ? true : false;
		});
		
		$("select.products").change(function() { 
			$("input.quantity")[0].value = 1;
			if ($("input.mont")[0]!=undefined) $("input.mont")[0].value = 0;			
		});

		$("select.client_provider").change(function() { 
			client_provider(this);			
		});
		
		$("button.cancel").click(function() {
			var obj = $("select.client_provider")[0];
			obj.value = 0;
			client_provider(obj);
		});	
		
		function client_provider (obj) {
			var loading = $('select#client_provider');
			var select  = obj;
			
			loading.show();			
			$.post("../ajax/ajax-gral.php", { "params": 'create:'+select.value, "action": false},
			function (data) {
				if (data._ajax==undefined) {
					var message = (data.message!=undefined) ? data.message : data.errors;
					var action  = (data.message!=undefined) ? true : 'delete';
					if(confirm(message)) {
						$.post("../ajax/ajax-gral.php", { "params": 'create:'+select.value, "action": action}, 
						function(data) { 
							productsOptions(data);
							alert(data.message);
							loading.hide(); 							
						}, "json");					
					} else {
						selected (select, data.client_provider_id);
					}					
				} else {				
					productsOptions(data);
					alert(data.message)						
				}
				loading.hide();
			}, "json");	
		}
		
		function selected (select, id) {
			for(var i = 1; i<=select.options.length;i++) {
				if (select.options[i]!=undefined) {
					if (select.options[i].value==id) select.selectedIndex = i; 								
				}
			}
		}

		function productsOptions (data) {
			
			if (data.products!=undefined) {
				var html = '';
				for(var i = 0; i<=data.products.length;i++) {
					if (data.products[i]!=undefined) {
					html += '<option value="'+data.products[i].id+'">'+data.products[i].value+'</option>';
					}
				}					
				$('select.products')[0].innerHTML = html;
			}
		}
		
		$("button.add").click(function() {
			var loading 	= $('span#products');			
			var product 	= $("select.products")[0];
			var quantity 	= $("input.quantity")[0];
			var montValue	= '';
			
			if ($("input.mont")[0]!=undefined) { var mont = $("input.mont")[0]; montValue = ':'+mont.value; }
			
			loading.show();
			
			$.post("../ajax/ajax-gral.php", { "params": 'fatures:'+product.value+':'+quantity.value+montValue}, 
			function (data) {
				if (data.message==undefined) {	
					$("table#data tr#"+data.product_id+"").remove();
					$("div#list_facture").show();				
					var htmlInsert = '<tr id="'+data.product_id+'"><td>'+data.code+'</td><td>'+data.name+'</td><td>'+data.mont+'</td><td>'+data.quantity+'</td><td>'+data.total_one+'</td><td width="40" align="center"><a href="#" onclick="return actions(\'update:'+data.id+'\', false);"><img src="'+data.www_images+'/icon_edit.gif" alt="Edit" width="16" height="16" border="0" /></a>	&nbsp;&nbsp;<a href="#" class="form_list_delete" onclick="return actions(\'delete:'+data.id+'\', \'Está seguro de borrar este registro?\');"><img src="'+data.www_images+'/icon_delete.gif" alt="Delete" width="16" height="16" border="0" /></a></td> </tr>';					
					$(htmlInsert).insertAfter('table#data tr#one');
					$("table#data tr th.total")[0].innerHTML = data.total;
					
					quantity.value 	= 1;
					if ($("input.mont")[0]!=undefined) mont.value = 0;
					
				} else {
					alert(data.message);
				}
				
				loading.hide();
			});
		});				
});

function actions(params, question) {

	if ((question!='' && !confirm(question)))	return false;

	var loading = $('span#loading');
	loading.show()
	$.post("../ajax/ajax-gral.php", { "params": params },
   	function(data){
		if (data) {
			/** action:clase:method:params **/
			if (data.message==undefined) {				
				if (data.actions=='status') {
					$('tr#'+data.id)[0].className = (data.checked=='true') ? '' : 'status_false'			
				} else if (data.actions=='delete') {
			    	$('tr#'+data.id).remove();						
				} else if (data.actions=='update') {
					var select = $("select.products")[0];
					$("input.quantity")[0].value = data.quantity;
					if ($("input.mont")[0]!=undefined) $("input.mont")[0].value = data.mont;
						
					for(var i = 1; i<=select.options.length;i++) {
						if (select.options[i]!=undefined) {
							if (select.options[i].value==data.product_id) select.selectedIndex = i; 								
						}
					}		
				}
			} else {
				alert(data.message);
			}			
		}
		loading.hide();
	}, "json");
	return false;
} 




