
function chekeaform(formu) {
	if(formu.tittle.value.length == 0) {
		alert('Event title cant be empty');
		formu.tittle.focus();
		return false;
	}
	if(formu.tittle.value == 'Clocked In' || formu.tittle.value == 'Clocked Out') {
		alert('Forbidden Event Title');
		formu.tittle.focus();
		return false;
	}	
	
	if(formu.dateStart.value.length == 0) {
		alert('Start Date cant be empty');
		formu.dateStart.focus();
		return false;
	}											
	
	return validacheck(formu);
}

function chekeaformtask(formu) {
	if(document.getElementById('destList').options.length == 0 && formu.idTask.value.length == 0) {
		alert('Select some user from the list');
		//formu.idUserTo.focus();
		return false;
	}
	if(formu.dateExecFrom.value.length == 0) {
		alert('Start Date cant be empty');
		formu.dateExecFrom.focus();
		return false;
	}													
	if(formu.subject.value.length == 0) {
		alert('Task subject cant be empty');
		formu.subject.focus();
		return false;
	}	
	
	if(formu.dateExecTo.value.length != 0){						
		f1 = formu.dateExecFrom.value.split("-");
		f2 = formu.dateExecTo.value.split("-");
		d1 = new Date(f1[2], parseInt(f1[0])-1, parseInt(f1[1]));  //date end
		d2 = new Date(f2[2], parseInt(f2[0])-1, parseInt(f2[1]));	//date start
		if(d2 < d1){
			alert('Completed date is incorrect');
			return false;
		}
	}
	addSelectedItemsToParent();
	return true;
}

function validacheck(form) {
var datestart = "";
var dateend = "";
var d1 = "";
var d2 = "";

		for (i = 0; i < form.elements.length; i++) {		
			if (form.elements[i].type == "checkbox" && form.elements[i].name.substr(0,12) == "idUserEvents" ) {
				if (form.elements[i].checked)
					return true;
			}else if(form.elements[i].name == "dateStart"){
					datestart = form.elements[i].value;
					f2 = datestart.split("-");
			}else if(form.elements[i].name == "dateEnd"){
					if(form.stepDays.value != "0"){
						//alert(form.elements[i].value);
						dateend = form.elements[i].value;
						f1 = dateend.split("-");
											
						d1 = new Date(f1[2], parseInt(f1[0])-1, parseInt(f1[1]));  //date end
						d2 = new Date(f2[2], parseInt(f2[0])-1, parseInt(f2[1]));	//date start
	
						//alert('Start: ' + d2 + ' - End: ' + d1);
						if(d1 < d2){
							alert('Repeat Event date is incorrect');
							return false;
						}
					}
			}
			
		}
		alert('Select user(s)');
		return false;
}



var checkflag1 = "false";
function checka(form) {
	if (checkflag1 == "false") {
		for (i = 0; i < form.elements.length; i++) {		
			if (form.elements[i].type == "checkbox" && form.elements[i].name.substr(0,12) == "idUserEvents" ) {
				form.elements[i].checked = true; 
				if (form.elements[i].name.substr(0,18) != "idUserEventsGrupal") {form.elements[i].disabled = true;}
			}
		}
	checkflag1 = "true";
	} else {
		for (i = 0; i < form.elements.length; i++) {
			if (form.elements[i].type == "checkbox" && form.elements[i].name.substr(0,12) == "idUserEvents") {
				form.elements[i].checked = false; 
				if (form.elements[i].name.substr(0,18) != "idUserEventsGrupal") {form.elements[i].disabled = false;}				
			}
		}	
		checkflag1 = "false";
	}
}

function cierraTodo(){
	ventanas = getElementsByClassName('dhtmlgoodies_window');
	for (var i=0; i < ventanas.length; i++) {
		ventanas[i].style.display='none';
	}
	objOverlay = document.getElementById('overlay');
	//objOverlay.removeChild();???
	objOverlay.style.display = 'none';
	// make select boxes visible
	mostrarSelects();	
}

//function cierraTodoEditor(){
//	if (tinyMCE.getInstanceById('journaled'))
//	{
//		tinyMCE.execCommand('mceFocus', false, 'journaled');                    
//		tinyMCE.execCommand('mceRemoveControl', false, 'journaled');
//	}	
//	cierraTodo();	
//}



function cierraCasiTodo(){
	ventanas = getElementsByClassName('dhtmlgoodies_window');
	for (var i=0; i < ventanas.length; i++) {
		ventanas[i].style.display='none';
	}
	objOverlay = document.getElementById('overlay');
	objOverlay.style.display = 'none';
	// make select boxes visible	
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

var divId = 0;

function do_editEvent(id,hourAdd, dateStart, dateEnd,title) {
	divId = createNewWindow(580,350,(viewport.width/2)-(580/2),(viewport.height/2)-(350/2),title);
	document.getElementById('windowContent' + divId).innerHTML = '<p><br></p><p><br></p><div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;Loading content...</div>';
	x_editEvent(id, hourAdd, dateStart, dateEnd, do_newEvent_cb); 
}

function do_newEvent(id,hourAdd, dateStart, dateEnd,title) {
	divId = createNewWindow(580,350,(viewport.width/2)-(580/2),(viewport.height/2)-(350/2),title);
	document.getElementById('windowContent' + divId).innerHTML = '<p><br></p><p><br></p><div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;Loading content...</div>';	
	x_newEvent(id, hourAdd, dateStart, dateEnd, do_newEvent_cb); 
}

function do_newEvent_cb(z) {
  	document.getElementById('windowContent' + divId).innerHTML = z;	  
}

function do_showDeadline(idProject) {
	divId = createNewWindow(580,330,340,150,'Project Deadline');
	document.getElementById('windowContent' + divId).innerHTML = '<p><br></p><p><br></p><div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;Loading content...</div>';
	x_showDeadline(idProject, do_showDeadline_cb); 
}

function do_showDeadline_cb(z) {
	document.getElementById('windowContent' + divId).innerHTML = z;	  
}


function do_taskForm(idTask, cierra) {
	var title = "";
	if (idTask==""){
		title = "Create Task";
	}else{
		title = "Edit Task";		
	}
	divId = createNewWindow(600,340,340,150,title);
	document.getElementById('windowContent' + divId).innerHTML = '<p><br></p><p><br></p><div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;Loading content...</div>';
	x_taskformWindow(idTask, cierra, do_taskForm_cb); 
}

function do_taskForm_cb(z) {
	document.getElementById('windowContent' + divId).innerHTML = z;	 
	//fillInitialDestList(document.getElementById('member_list').innerHTML);
}

function refreshtk() {
	document.getElementById("tasksdiv").innerHTML = '<div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;</div>';	
	x_tasksStatus("",do_tasksStatus_cb);   
}

function deleteTkWindow(idtask){
	if(confirm("Delete current task?")) {
		x_deleteTkWindow(idtask,saveTkStatus_cb);
	}
}

/*
function do_tasksStatus_cb(z) {
	//document.getElementById("capaCalendario").style.display = 'block';
	var results = z.split("!#");
	document.getElementById("tasksdiv").innerHTML=results[1];
	document.getElementById("tasksdiv").style.height = results[0];
	collapse7= new animatedcollapse("tasksdiv", 500, false, "block");
	document.getElementById("tktitle").innerHTML="Tasks status";
}
*/
function todaytk() {
	//document.getElementById("tasksdiv").innerHTML = '<div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;</div>';	
	x_todaytk("",do_todaytk_cb);   
}

function do_todaytk_cb(z) {
	//document.getElementById("capaCalendario").style.display = 'block';
	//var results = z.split("!#");
	document.getElementById("tasksdiv").innerHTML=z;
	//document.getElementById("tasksdiv").style.height = results[0];
	collapse7= new animatedcollapse("tkdiv", 500, false, "block");
	document.getElementById("tktitle").innerHTML="Daily Tasks";
}

function do_saveTkNote(note,idTask){
	x_saveTkNote(idTask,note,saveTkNote_cb);
}

function saveTkNote_cb(z) {
	var results = z.split("#");
	document.getElementById("notes"+results[0]).innerHTML=results[1];
}

var list = null;
var DragDrop = null;
var windowTasks = "";
var entro = false;

function alltasks() {
	if(windowTasks!=""){
		ventanas = getElementsByClassName('dhtmlgoodies_window');
		for (var i=0; i < ventanas.length; i++) {
			if(windowTasks == ventanas[i].id){
				objOverlay = document.getElementById('overlay');
				objOverlay.style.display = 'block';				
				ventanas[i].style.display='block';
				entro = true;
				break;
			}
		}
	}else{
		var title = "Manage Tasks";
		divId = createNewWindow(790,450,340,50,title);
		windowTasks = "dhtml_goodies_id" + divId;
		document.getElementById('windowContent' + divId).innerHTML = '<p><br></p><p><br></p><div align=center><img align=middle src=\'images/loader333333.gif\'>&nbsp;&nbsp;&nbsp;&nbsp;Loading content...</div>';
		x_alltasks("", alltasks_cb); 		
	}
}

function alltasks_cb(z) {
	if (entro==false){
	document.getElementById('windowContent' + divId).innerHTML = z;	  
	
	list = document.getElementById("1prior");
	DragDrop.makeListContainer( list );
	list.onDragOver = function() { this.style["border"] = "2px dashed #000"; };
	list.onDragOut = function() {this.style["border"] = "1px solid white"; };
	
	list = document.getElementById("2prior");
	DragDrop.makeListContainer( list );
	list.onDragOver = function() { this.style["border"] = "2px dashed #000"; };
	list.onDragOut = function() {this.style["border"] = "1px solid white"; };
	
	list = document.getElementById("3prior");
	DragDrop.makeListContainer( list );
	list.onDragOver = function() { this.style["border"] = "2px dashed #000"; };
	list.onDragOut = function() {this.style["border"] = "1px solid white"; };	
	
	list = document.getElementById("completed");
	DragDrop.makeListContainer( list );
	list.onDragOver = function() { this.style["border"] = "2px dashed #000"; };
	list.onDragOut = function() {this.style["border"] = "1px solid white"; };	
	
	}
}

/*888888888888888888888888*/


function do_deleteEvent(id) {
	if(confirm("Are you sure?")) {
		x_deleteEvent(id, do_deleteEvent_cb); 
	}
}

function do_deleteEvent_cb(z) {
		//document.getElementById('windowContent' + divId).innerHTML = '<br><br>Event deleted';		
		top.do_changeSchedule(z,0);
		top.cierraTodo();
}

function calendarSetup(inputfield, triggerbutton) {
	Calendar.setup({"ifFormat":"%m-%d-%Y","daFormat":"%m-%d-%Y","firstDay":1,"showsTime":false,"showOthers":true,"timeFormat":12,"inputField":inputfield,"button":triggerbutton});
}

function new_Window(title,content,w,h) {
	var arrdim = getPageSize();
	divId = createNewWindow(w,h,(arrdim[2]/2)-(w/2),(arrdim[3]/2)-(h/2),title);
	document.getElementById('windowContent' + divId).innerHTML = content;	
}

function ocultar() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}	
}

function mostrarSelects() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

/*8888888888888888888888888888888888888888888888888888888888888*/

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function probar(){

	var objBody = document.getElementsByTagName("body").item(0);
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	objOverlay.setAttribute('id','overlay');
	//objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'block';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = (arrayPageSize[0] + 'px');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	
}

function contactAddEdit(idUser,dato,formu) {
	if (dato.length!=0 && formu.firstname.value.length==0 && formu.lastname.value.length==0){
		alert('Add contact Lastname or contact Firstname please');
	}else{
		x_contactAddEdit(idUser,dato,contactAddEdit_cb);
	}
}

function contactAddEdit_cb(z) {
	if (z == 'added') {
	alert('Contact Added');
	cierraTodo();
	}else{
		new_Window('Add Contact',z,680,440); 
	}
}

function convert(formu) {
	var datos = new Array(formu.elements.length);
	for(i=0; i<formu.elements.length; i++){
		datos[i]=formu.elements[i].value;
		//alert(i + " = " + datos[i]);
	}
    return datos;
}


/* ---------------------------------------------------------------------------------
ESTA FUNCION FUE REEMPLAZADA X JQUERY -->jQuery.inArray( value, array ) 
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
*/

// Add the selected items in the parent by calling method of parent
function addSelectedItemsToParent() {
	//self.opener.addToParentList(window.document.forms[0].destList,'{/literal}{$c}{literal}');
	//window.close();
//var destList = window.document.forms[0].destList; 
//var destListCSV = window.document.forms[0].destListCSV;
var destList = document.getElementById('destList');
var destListCSV = document.getElementById('destListCSV');
for(var i = 0; i < destList.options.length; i++) { 
	if (destList.options[i] != null) {
			if (i > 0) destListCSV.value = destListCSV.value + ',';
			destListCSV.value = destListCSV.value + destList.options[i].value;
	}		
}	
return true;
}
// Fill the selcted item list with the items already present in parent.
function fillInitialDestList(members) {
var destList = window.document.forms[0].destList; 
var srcList = window.document.forms[0].srcList;
var srcListSel = members.split(',');
for (var count = destList.options.length - 1; count >= 0; count--) {
		destList.options[count] = null;
}
var i = 0;
var j = 0;
while(i < srcList.options.length) { 
		if (srcList.options[i] != null) 
				//if (srcListSel.inArray(srcList.options[i].value)) {
				if (jQuery.inArray(srcList.options[i].value,srcListSel) != -1) {		
					destList.options[j] = new Option(srcList.options[i].text,srcList.options[i].value);
					j++;
				}
i++;					 
}	   
}
// Add the selected items from the source to destination list
function addSrcToDestList() {
//destList = window.document.forms[0].destList;
destList = document.getElementById('destList');
//srcList = window.document.forms[0].srcList; 
srcList = document.getElementById('srcList'); 
var len = destList.length;

for(var i = 0; i < srcList.length; i++) {
if ((srcList.options[i] != null) && (srcList.options[i].selected)) {
//Check if this value already exist in the destList or not
//if not then add it otherwise do not add it.
	var found = false;
	for(var count = 0; count < len; count++) {
	if (destList.options[count] != null) {
	if (srcList.options[i].text == destList.options[count].text) {
		found = true;
	break;
      }
   }
}
if (found != true) {
destList.options[len] = new Option(srcList.options[i].text,srcList.options[i].value); 
//alert(destList.options[len].value);
len++;
         }
      }
   }
}



// Deletes from the destination list.
function deleteFromDestList(havem) {	
var haveMilestones = havem.split(',');		
//var destList  = window.document.forms[0].destList;
var destList = document.getElementById('destList');
var len = destList.options.length;
for(var i = (len-1); i >= 0; i--) {
if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
	
		if (jQuery.inArray(destList.options[i].value,haveMilestones) != -1) {				
			alert('Can not remove users with assigned Milestones');
		}else{
			destList.options[i] = null;
		}
	}
   }
}

// Deletes from the destination list.
function deleteFromDestList() {	
//var destList  = window.document.forms[0].destList;
var destList = document.getElementById('destList');
var len = destList.options.length;
for(var i = (len-1); i >= 0; i--) {
if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
	
			destList.options[i] = null;
	}
   }
}


function do_initJournal(){
	x_postJournal("",postJournal_cb);
	document.getElementById("journal").value = "";
}

function do_postJournal(action){
	var ed = tinyMCE.get('journal');
	var texto = ed.getContent();
	if(action=="Save"){
		var idj = document.getElementById("idjournal").value;
		x_editJournal(texto,idj,postJournal_cb);
	}else{
		x_postJournal(texto,postJournal_cb);
		document.getElementById("journal").value = "";
	}
}

function postJournal_cb(z) {
	document.getElementById("journalresult").innerHTML=z;
}

function do_journalSearch(){
	var jkey = document.getElementById("jkey").value;
	var jmonth = document.getElementById("jmonth").value;
	var jday = document.getElementById("jday").value;	
	var jyear = document.getElementById("jyear").value;
	var jdate = jyear + '-' + jmonth + '-' + jday;
	x_journalSearch(jkey,jdate,postJournal_cb);
}

function changendT(ind){
	var endh = document.getElementById("endHour");
	if(ind!=16){
		endh.selectedIndex = ind+1;
	}else{
		endh.selectedIndex = 16;	
	}
}

function changendTf(ind){
	var starth = document.getElementById("startHour");
	if(ind < starth.selectedIndex){
		starth.selectedIndex = ind;
	}
}
/****************************************/
/******	VIEWPORT:: SCREEN TOOLS *********/
/****************************************/

viewport =
{
   getIECanvas: function ()
   {
      var canv = null;
      if (!window.opera && document.all && typeof document.body.clientWidth != "undefined")
      {
         var cm = document.compatMode && document.compatMode == "CSS1Compat";
         canv = cm ? document.documentElement : document.body;
      }
      return canv;
   },

   getWinWidth: function ()
   {
      var canv;
      if ( canv = this.getIECanvas() )
         this.width = canv.clientWidth;
      else
         this.width = window.innerWidth - 18;
   },

   getWinHeight: function ()
   {
      var canv;
      if (canv = this.getIECanvas())
         this.height = canv.clientHeight;
      else
         this.height = window.innerHeight - 18;
   },

   getScrollX: function ()
   {
      var canv;
      if (canv = this.getIECanvas())
         this.scrollX = canv.scrollLeft;
      else if (window.pageXOffset)
         this.scrollX = window.pageXOffset;
      else if (window.scrollX)
         this.scrollX = window.scrollX;
      else
         this.scrollX = 0;
   },

   getScrollY: function ()
   {
      var canv;
      if (canv = this.getIECanvas())
         this.scrollY = canv.scrollTop;
      else if (window.pageYOffset)
         this.scrollY = window.pageYOffset;
      else if (window.scrollY)
         this.scrollY = window.scrollY;
      else
         this.scrollY = 0;
   },

   getAll: function ()
   {
      this.getWinWidth();
      this.getWinHeight();
      this.getScrollX();
      this.getScrollY();
   }
};