﻿/* AR Intranet v1.0.0.0 [Christopher Díaz Pantoja] */

function loadProject(project){
	$.post("project.php", { project: project }, 
				function(data){ 
					$('#body').replaceWith(data);
				});
}



function editNote(note,project){
	$.post("edit_note.php", { note: note, project: project }, 
				function(data){ 
					$('#body_project').replaceWith(data);
				});
}



function editTask(task,project){
	$.post("edit_task.php", { task: task, project: project }, 
				function(data){ 
					$('#body_project').replaceWith(data);
				});
}




function deleteNote(note, project){
	
	ask = "¿Desea eliminar esta nota?";
	var res = confirm(ask);	
	if(res){
	 	$.post("scripts/deleteNote.php",{ note: note }, 
			function(data){ 
				loadSectionTargetOption('project_notes.php','body_project', project);
			});
	}
}


function deleteTask(task, project){
	
	ask = "¿Desea eliminar esta tarea?";
	var res = confirm(ask);	
	if(res){
	 	$.post("scripts/deleteTask.php",{ task: task }, 
			function(data){ 
				loadSectionTargetOption('project_task.php','body_project', project);
			});
	}
}



function addParticipant(project){
	
		var participants = new Array();
		
			$(":checked").each(function(i){
				participants[i] = this.value;
			});
			
			if(participants.length == 0){
				alert("Seleccione uno o más participantes");
			}
			else{
				$.post("scripts/addParticipant.php",{ participants: participants.join('-'), project: project}, 
				function(data){ 
					loadSectionTargetOption('project_participants.php','body_project', project);
				});
			}
			
	}
	
	
	
	
	
function deleteParticipant(participant, project){

		$.post("scripts/deleteParticipant.php",{ participant: participant, project: project}, 
			function(data){ 
				loadSectionTargetOption('project_participants.php','body_project', project);
		});
			
			
}



function viewItem(url,project,id,tar){
	
	script = "scripts/"+url;
	target = "#"+tar;
	
	$.post(script, { project: project, id: id }, 
				function(data){ 
					$(target).replaceWith(data);
					$(target).hide();
					$(target).slideDown(1000);
				});

}





function addNote(project,option,note){
	
		document.location.href = "#";
	
		$("#error_title").hide();
		$("#error_description").hide();
	
		if($("input[@name=title]").val()== "" || $("textarea[@name=description]").val()== ""){
			if($("input[@name=title]").val()== ""){
				
				message = getErrorString("empty_field", "title");
				$("#error_title").replaceWith(message);
				$("#error_title").fadeIn();
			}
			if($("textarea[@name=description]").val()== ""){
				
				message = getErrorString("empty_field", "description");
				$("#error_description").replaceWith(message);
				$("#error_description").fadeIn();
			}
		}
		else{
			
			option = (option == "update")?"updateNote.php":"addNote.php";
			
			var texto = $("textarea[@name=description]").val(); 
			var description = texto.replace(/\n/g, "[br]"); 
			
			$.post("scripts/"+option,{ title: $("input[@name=title]").val(), description: description, project: project, note: note}, 
			function(data){ 
				loadSectionTargetOption('project_notes.php','body_project', project);
			});
		}
			
			
}







function addTask(project,option,task){
	
		document.location.href = "#";
		count_errors = 0;
	
		$("#error_datetime").hide();
		$("#error_asigned").hide();
		$("#error_title").hide();
		$("#error_description").hide();
		
		if($("input[@name=date_begin]").val() == "" || $("input[@name=date_end]").val()== "" || $("input[@name=time_begin]").val()== "" || $("input[@name=time_end]").val()== ""){
				message = getErrorString("empty_field", "datetime");
				$("#error_datetime").replaceWith(message);
				$("#error_datetime").fadeIn();
				count_errors++;
		}
		else{
			
			fecha_inicio = new Date();
			bd = $("input[@name=date_begin]").val();
			date_begin = bd.split("-");
			fecha_inicio.setFullYear(date_begin[2], date_begin[1], date_begin[0]);
			
			
			fecha_fin = new Date();
			ed = $("input[@name=date_end]").val();
			date_end = ed.split("-");
			fecha_fin.setFullYear(date_end[2], date_end[1], date_end[0]);
			
			if(fecha_inicio > fecha_fin ){
				message = getErrorString("error_end_date", "datetime");
				$("#error_datetime").replaceWith(message);
				$("#error_datetime").fadeIn();
				count_errors++;
			}
		}
		
		if($("select[@name=asigned]").val()== "none" || $("select[@name=status]").val()== "none"){
				message = getErrorString("no_selected_option", "asigned");
				$("#error_asigned").replaceWith(message);
				$("#error_asigned").fadeIn();
				count_errors++;
		}
		
		timeB = $("input[@name=time_begin]").val(); timeB = timeB.split(":",2);
		timeE = $("input[@name=time_end]").val(); timeE = timeE.split(":",2);
		
		if(timeB[0].length != 2 || timeE[0].length != 2  || timeB[1].length != 2 || timeE[1].length != 2){
			message = getErrorString("malformed_time", "datetime");
			$("#error_datetime").replaceWith(message);
			$("#error_datetime").fadeIn();
			count_errors++;
		}
		
		
	
		if($("input[@name=title]").val()== "" || $("textarea[@name=description]").val()== ""){
			if($("input[@name=title]").val()== ""){
				
				message = getErrorString("empty_field", "title");
				$("#error_title").replaceWith(message);
				$("#error_title").fadeIn();
				count_errors++;
			}
			if($("textarea[@name=description]").val()== ""){
				
				message = getErrorString("empty_field", "description");
				$("#error_description").replaceWith(message);
				$("#error_description").fadeIn();
				count_errors++;
			}
		}
		
		
		
		
		if(count_errors == 0){
			
			datetimeBegin = date_begin[2]+"-"+date_begin[1]+"-"+date_begin[0]+" "+timeB[0]+":"+timeB[1]+":00";
			datetimeEnd = date_end[2]+"-"+date_end[1]+"-"+date_end[0]+" "+timeE[0]+":"+timeE[1]+":00";
			
			option = (option == "update")?"updateTask.php":"addTask.php";
			
			var texto = $("textarea[@name=description]").val(); 
			var description = texto.replace(/\n/g, "[br]"); 
			
			$.post("scripts/"+option,{ title: $("input[@name=title]").val(), description: description, project: project, task: task, datetimeBegin: datetimeBegin, datetimeEnd: datetimeEnd, status : $("select[@name=status]").val(), asigned : $("select[@name=asigned]").val() }, 
			function(data){ 
				loadSectionTargetOption('project_task.php','body_project', project);
			});
		}
			
			
}