lang = {
	newTaskFlashColor: '#ffffaa',
	editTaskFlashColor: '#bbffaa',
	errorFlashColor: '#ff0000',

	actionNote: 'note',
	actionEdit: 'modificar',
	actionDelete: 'borrar',
	taskDate: function(date) {
		return 'added at '+date;
	},
	confirmDelete: 'Estas Seguro',
	actionNoteSave: 'save',
	actionNoteCancel: 'cancel'
};

//Global vars
var filterCompl = 0;
var sortOrder;
var searchTimer;
var filterSearch = '';



function loadTasks()
{
	//setAjaxErrorTrigger();
	//if(filterSearch) search = '&s='+encodeURIComponent(filterSearch);
	 search = '';
	nocache = '&rnd='+Math.random();
	$.getJSON('carritos.php?loadTasks&compl='+filterCompl+search+nocache, function(json){
		//resetAjaxErrorTrigger();
		//$('#total').html(json.total);
		var tasks = '';
		    //var datos=eval("(" + json.list + ")");
			
		$('#totales').html(json.total);	
		$('#totales2').html(json.total);	
		$.each(json.list, function(i,item){
			tasks += prepareTaskStr(item.id, item.title);
		});
		$('#grilla tbody').html(tasks);
		$('#span_cantidad').html('<h1>'+json.total+'<h1>');
		//if(filterCompl) showhide($('#compl_hide'),$('#compl_show'));
		//else showhide($('#compl_show'),$('#compl_hide'));
	});
}


function prepareTaskStr(id,title,date,compl,note,noteText,prio,title2,title3)
{
	id = parseInt(id);
	compl = parseInt(compl);
	prio = parseInt(prio);
	return '<tr id="taskrow_'+id+'">'+ '<td>' + id + '</td>'+ '<td>' + title + '</td><td>'+ '<a href="#" onClick="return deleteTask('+id+')"><img src="images/delete.png" width=16 height=16 border=0/></a>'+  '</td>'+'</tr>';
}

function submitNewTask(form)
{  alert(form);
	if(form.task.value == '') return false;
	setAjaxErrorTrigger()
	nocache = '&rnd='+Math.random();
	$.post('ajax.php?newTask'+nocache, { title: form.task.value, title2: form.task2.value, title3: form.task3.value}, function(json){
		resetAjaxErrorTrigger();
		if(!parseInt(json.total)) return;
		$('#total').text( parseInt($('#total').text()) + parseInt(json.total) );
		var item = json.list[0];
		form.task.value = '';
		form.task2.value = '';
		form.task3.value = '';
		$('#grilla tbody').append(prepareTaskStr(item.id, item.title, item.date, item.compl, item.note, item.noteText, item.prio,item.title2,item.title3));
		$('#taskrow_'+item.id).effect("highlight", {color:lang.newTaskFlashColor}, 2000);
	}, 'json');
	return false;
}

function setAjaxErrorTrigger()
{
	resetAjaxErrorTrigger();
	$("#msg").ajaxError(function(event, request, settings){
		flashError('Oops, error');
	});
}

function flashError(str)
{
	$("#msg").text(str);
	$("#msg").css('display','block');
	$("#loading").hide();
	$("#msg").effect("highlight", {color:lang.errorFlashColor}, 3000);
}

function resetAjaxErrorTrigger()
{
	$("#msg").css('display','none');
	$("#msg").unbind('ajaxError');
}

function deleteTaskd(id)
{
	if(!confirm(lang.confirmDelete)) {
		return false;
	}
	nocache = '&rnd='+Math.random();
	$.getJSON('ajax.php?deleteTask='+id+nocache, function(json){
		if(!parseInt(json.total)) return;
		$('#total').text( parseInt($('#total').text()) - 1 );
		var item = json.list[0];
		taskOrder.splice($.inArray(id,taskOrder), 1);
		$('#grilla tbody'+item.id).fadeOut('normal', function(){ $(this).remove() });
		if(!taskList[id].compl && changeTaskCnt(taskList[id].dueClass, -1)) refreshTaskCnt();
		delete taskList[id];
	});
	flag.tagsChanged = true;
	return false;
}

function deleteTask1e(cliente_id){
		var msg = confirm("Desea eliminar este producto  de su cotizaci\xf3n?")
		if ( msg ) {
			
					
				
			
		}
		return false;
	}


function  deleteTask(cliente_id){
		var msg = confirm("Desea eliminar este producto  de su cotizaci\xf3n?")
		if ( msg ) {
			$.ajax({
				url: 'eliminar.php',
				type: "GET",
				data: "id="+cliente_id,
				success: function(datos){
				$('#taskrow_'+cliente_id).remove();
				//loadTasks();
				$('#totales').html(datos);	
				$('#totales2').html(datos);	
				alert('El producto a sido eliminado de su cotizaci\xf3n');
				}
			});
		}
		return false;
	}



function toggleTaskNote(id)
{
	aArea = '#tasknotearea'+id;
	if($(aArea).css('display') == 'none')
	{
		$('#taskrow_'+id+'>div.task-note-block').removeClass('hidden');
		$(aArea).css('display', 'block');
		$('#tasknote'+id).css('display', 'none');
		$('#notetext'+id).focus();
	} else {
		cancelTaskNote(id)
	}
	return false;
}

function cancelTaskNote(id)
{
	$('#tasknotearea'+id).css('display', 'none');
	$('#tasknote'+id).css('display', 'block');
	if($('#tasknote'+id).text() == '') {
		$('#taskrow_'+id+'>div.task-note-block').addClass('hidden');
	}
	return false;
}

function saveTaskNote(id)
{
	setAjaxErrorTrigger()
	nocache = '&rnd='+Math.random();
	$.post('ajax.php?editNote='+id+nocache, {note: $('#notetext'+id).val()}, function(json){
		resetAjaxErrorTrigger();
		if(!parseInt(json.total)) return;
		var item = json.list[0];
		$('#tasknote'+item.id+'>span').html(item.note);
		$('#notetext'+item.id).val(item.noteText);
		cancelTaskNote(item.id);
	}, 'json');
	return false;
}

function editTask(id)
{
	
  $.getJSON("pagina1.php",{dni:id},llegadaDatos); 
  return false;
}

function cancelEdit()
{
	document.edittask.task.value = ''; //?
	document.edittask.note.value = ''; //?
	$('#page_taskedit').hide();
	$('#page_tasks').show();
	return false;
}

function saveTask(form)
{
	setAjaxErrorTrigger();
	nocache = '&rnd='+Math.random();
	$.post('ajax.php?editTask='+form.id.value+nocache, { title: form.task.value, note:form.note.value, prio:form.prio.value }, function(json){
		resetAjaxErrorTrigger();
		if(!parseInt(json.total)) return;
		var item = json.list[0];
		$('#taskrow_'+item.id).replaceWith(prepareTaskStr(item.id, item.title, item.date, item.compl, item.note, item.noteText, item.prio));
		$('#taskrow_'+item.id).effect("highlight", {color:lang.editTaskFlashColor}, 'normal');
		if(item.note == '') $('#taskrow_'+item.id+'>div.task-note-block').addClass('hidden');
		else $('#taskrow_'+item.id+'>div.task-note-block').removeClass('hidden');
		cancelEdit();
	}, 'json');
	return false;
}

function showhide(a,b)
{
	a.show();
	b.hide();
}

function filterCompleted(a)
{
	if(a) filterCompl = 1;
	else filterCompl = 0;
	loadTasks();
	return false;
}

function sortStart(event,ui)
{
	// remember initial order before sorting
	sortOrder = $(this).sortable('toArray');
}

function orderChanged(event,ui)
{
	n = $(this).sortable('toArray');
	// remove possible empty id's
	for(i=0; i<sortOrder.length; i++) {
		if(sortOrder[i] == '') { sortOrder.splice(i,1); i--; }
	}
	changed = false;
	for(i=0; i<n.length; i++)
	{
		if(n[i] != sortOrder[i]) {
			changed = true;
			break;
		}
	}
	if(!changed) return;
	// make assoc from array for easy index
	var h0 = new Array();
	for(j=0; j<sortOrder.length; j++) {
		if(sortOrder[j] != '') h0[sortOrder[j]] = j;
	}
	var h1 = new Array();
	for(j=0; j<n.length; j++) {
		if(n[j] != '') h1[n[j]] = j;
	}
	// prepare param string 
	var s = '';
	for(j in h0)
	{
		diff = h1[j] - h0[j];
		if(diff != 0) {
			a = j.split('_');
			s += a[1] +'='+ diff+ '&';
		}
	}
	setAjaxErrorTrigger();
	nocache = '&rnd='+Math.random();
	$.post('ajax.php?changeOrder'+nocache, { order: s }, function(json){
		resetAjaxErrorTrigger();
	}, 'json');
}

function timerSearch()
{
	clearTimeout(searchTimer);
	searchTimer = setTimeout("searchTasks()", 500);
}

function searchTasks()
{
	filterSearch = $('#search').val();
	$('#searchbarkeyword').text(filterSearch);
	if(filterSearch != '') $('#searchbar').fadeIn('fast');
	else $('#searchbar').fadeOut('fast');
	loadTasks();
	return false;
}

function tabSelected(event, ui)
{
	// reload tasks when we return to task tab (from search tab)
	if(ui.index == 0 && filterSearch != '') {
		filterSearch = '';
		$('#searchbarkeyword').text('');
		$('#searchbar').hide();
		loadTasks();
	}
}

function llegadaDatos(datos)
{   
$("#task").attr("value",""+datos.nombre);
$("#task2").attr("value",""+datos.apellido);
$("#task3").attr("value",""+datos.direccion);


//alert("Nombre:"+datos.nombre+"<br>"+"Apellido:"+datos.apellido+"<br>"
  //                      +"Direccion:"+datos.direccion);
}
