function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function tasksTimeout(uwid,auto_refresh_interval){
	window['xmlhttpTasks_'+uwid].abort();
	document.getElementById("tasks_"+uwid).innerHTML="<div class='error'>Connection has timed out <input type='button' class='ImagesRefresh' onclick='getTasks("+uwid+", "+auto_refresh_interval+")' /> <input type='button' class='ImagesSettings' onclick='showSettingsTasks("+uwid+")' /></div>";
}

function hideElement(id){
	document.getElementById(id).style.display="none";	
}

function showElement(id){
	document.getElementById(id).style.display="inline";	
}

function showSettingsTasks(uwid){
	document.getElementById("dimBackground").style.display="block";
	document.getElementById("settings").style.display="block";
	//document.getElementById("settings").innerHTML="settings";
	
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/showSettings.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("settings").innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("settings").innerHTML=window['xmlhttpTasks_'+uwid].responseText;
		}
	};
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
}

function hideSettingsTasks(){
	document.getElementById("settings").style.display="none";
	document.getElementById("dimBackground").style.display="none";
}

function updateFieldTasks(uwid,field_name,value){
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	value=encodeURIComponent(value);
	
	var url="widget_tasks/ajax/updateField.php";
	url=url+"?uwid="+uwid+"&field_name="+field_name+"&value="+value;
	url=url+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("update_field_status").innerHTML="<div class='load'></div>";
		}
		/*if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML=window['xmlhttpTasks_'+uwid].responseText;
			showSettingsTasks(uwid);
		}*/
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML="";//remove loading icon
			if(window['xmlhttpTasks_'+uwid].responseText==1){//if data was valid and settings have been saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "success");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
				$("#validate_"+field_name).delay(1000).fadeOut(1000);
				
				if(field_name=="auto_refresh"&&value==1){//if auto refresh has been enabled, change the onclick event of the exit button
					auto_refresh_interval=document.getElementById("auto_refresh_interval").value;
					document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsTasks(); getTasks("+uwid+", "+auto_refresh_interval+");");
				}
				if(field_name=="auto_refresh"&&value==0){//if auto refresh has been disabled, change the onclick event of the exit button
					document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsTasks(); getTasks("+uwid+", 0);");
				}
				if(field_name=="auto_refresh_interval"){//if auto refresh interval has changed...
					if(document.getElementById("auto_refresh_1").checked)//.and auto refresh is enabled, change the onclick event of the exit button
						document.getElementById("exit_"+uwid).setAttribute("onclick", "hideSettingsTasks(); getTasks("+uwid+", "+value+");");
				}
			}
			else if(window['xmlhttpTasks_'+uwid].responseText==0){//...else, if data was valid but settings could not be saved...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'Error';
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
			else{//...else, data was not valid...
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpTasks_'+uwid].responseText;
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
		}
	};
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
}

function clearTasks(uwid,which){
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/clearTasks.php";
	url=url+"?uwid="+uwid+"&which="+which;
	url=url+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("update_field_status").innerHTML="<div class='load'></div>";
		}
		/*if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML=window['xmlhttpTasks_'+uwid].responseText;
		}*/
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("update_field_status").innerHTML="";//remove loading icon
			if(window['xmlhttpTasks_'+uwid].responseText==1){//if data was valid and settings have been saved...
				document.getElementById("validate_clear_"+which+"_tasks").setAttribute("class", "success");
				document.getElementById("validate_clear_"+which+"_tasks").style.display="inline";
				document.getElementById("validate_clear_"+which+"_tasks").innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
				$("#validate_clear_"+which+"_tasks").delay(1000).fadeOut(1000);
			}
			else if(window['xmlhttpTasks_'+uwid].responseText==0){//...else, if data was valid but settings could not be saved...
				document.getElementById("validate_clear_"+which+"_tasks").setAttribute("class", "error");
				document.getElementById("validate_clear_"+which+"_tasks").style.display="inline";
				document.getElementById("validate_clear_"+which+"_tasks").innerHTML='<span class="pointer">&nbsp;</span>'+'Error';
				$("#validate_clear_"+which+"_tasks").delay(3000).fadeOut(1000);
			}
			else{//...else, data was not valid...
				document.getElementById("validate_clear_"+which+"_tasks").setAttribute("class", "error");
				document.getElementById("validate_clear_"+which+"_tasks").style.display="inline";
				document.getElementById("validate_clear_"+which+"_tasks").innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpTasks_'+uwid].responseText;
				$("#validate_clear_"+which+"_tasks").delay(3000).fadeOut(1000);
			}
		}
	};
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
}

function enableDisableAutoRefreshTasks(id,value){
	if(value==1)
		document.getElementById(id).removeAttribute("disabled");
	else
		if(value==0)
			document.getElementById(id).setAttribute("disabled", "disabled");
}

function getTasks(uwid,auto_refresh_interval){
	
	if(document.getElementById("tasks_add_form_"+uwid)!=null){//if tasks doesn't load for the first time, switch the add task form to display=none so as it won't appear together with the tasks.
		document.getElementById("tasks_add_form_"+uwid).style.display="none";
	}
	
	if(typeof(window['auto_refresh_tasks_'+uwid])!="undefined"){//if an auto-refresh timer is set, unset it and set another one (the same) again (down)
		clearTimeout(window['auto_refresh_tasks_'+uwid]);
	}
		
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/getTasks.php";
	url=url+"?uwid="+uwid+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("tasks_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			clearTimeout(window['tasks_'+uwid+'_timeout']);
			document.getElementById("tasks_"+uwid).innerHTML=window['xmlhttpTasks_'+uwid].responseText;
			if(auto_refresh_interval!=0)
				window['auto_refresh_tasks_'+uwid]=setTimeout('getTasks(' + uwid + ', ' + auto_refresh_interval + ')', (auto_refresh_interval*1000));//auto-refresh function	
		}
	}
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
	
	window['tasks_'+uwid+'_timeout']=setTimeout("tasksTimeout("+uwid+", "+auto_refresh_interval+")", 5000);
}

function hoverTask(id){
	document.getElementById("task_"+id).childNodes[3].childNodes[0].style.display="inline";	// childNodes[3] = <td> containing the button, childNodes[0] = the button
}

function removeHoverTask(id){
	document.getElementById("task_"+id).childNodes[3].childNodes[0].style.display="none";	
}

function addTaskForm(uwid,auto_refresh_interval){
	//alert(document.getElementById("tasks_tasks_"+uwid).style.display);
	//alert(document.getElementById("tasks_add_form_"+uwid).style.display);
	/*document.getElementById("tasks_deadline_calendar_"+uwid).removeAttribute("value");
	document.getElementById("tasks_deadline_hour_"+uwid).removeAttribute("value");
	document.getElementById("tasks_deadline_minute_"+uwid).removeAttribute("value");
	while (document.getElementById("calendar_"+uwid).hasChildNodes()) {
		document.getElementById("calendar_"+uwid).removeChild(document.getElementById("calendar_"+uwid).lastChild);
	}
	while (document.getElementById("time_"+uwid).hasChildNodes()) {
		document.getElementById("time_"+uwid).removeChild(document.getElementById("time_"+uwid).lastChild);
	}*/
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/addTaskForm.php";
	url=url+"?uwid="+uwid;
	url=url+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("tasks_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			clearTimeout(window['tasks_'+uwid+'_timeout']);
			document.getElementById("tasks_"+uwid).innerHTML=window['xmlhttpTasks_'+uwid].responseText;
			cdate=new Date();
			calendar(uwid, cdate.getDate(), (cdate.getMonth()+1), cdate.getFullYear());
			time(uwid, cdate.getHours(), cdate.getMinutes());
		}
	}
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
	
	window['tasks_'+uwid+'_timeout']=setTimeout("tasksTimeout("+uwid+", "+auto_refresh_interval+")", 5000);

	
	/*document.getElementById("tasks_tasks_"+uwid).style.display="none";
	document.getElementById("tasks_add_form_"+uwid).style.display="block";*/
}

/*function closeAddTaskForm(uwid){
	document.getElementById("tasks_add_form_"+uwid).style.display="none";
	document.getElementById("tasks_tasks_"+uwid).style.display="block";
}*/

function addTask(uwid){
	name=document.getElementById("tasks_name_"+uwid).value;
	calendar_date=document.getElementById("tasks_deadline_calendar_"+uwid).value;
	hour=document.getElementById("tasks_deadline_hour_"+uwid).value;
	hour*=3600;
	minute=document.getElementById("tasks_deadline_minute_"+uwid).value;
	minute*=60;
	
	deadline=(calendar_date*1+hour*1+minute*1);
	
	document.getElementById("tasks_add_form_"+uwid).style.display="none";
	document.getElementById("tasks_"+uwid).style.display="block";
	
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	name=encodeURIComponent(name);
	
	var url="widget_tasks/ajax/addTask.php";
	url=url+"?uwid="+uwid+"&name="+name+"&deadline="+deadline;
	url=url+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("tasks_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			document.getElementById("tasks_"+uwid).innerHTML=window['xmlhttpTasks_'+uwid].responseText;
		}
	}
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
}

function calendar(uwid,day,month,year){
	/*currDate=new Date;
	cYear=currDate.getDate();
	cMonth=currDate.getMonth();
		cMonth+=1;
	cYear=currDate.getFullYear();*/
	
	//alert("day"+day+"mothn"+month+"year"+year);
	
	var string='';
	
	if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
		var days=31;
	}
	else if(month==4||month==6||month==9||month==11){
		var days=30;
	}
	else if(month==2&&year%4==0){
		var days=29;
	}
	else if(month==2&&year%4!=0){
		var days=28;
	}
	
	firstDayDate=new Date(year, (month-1), 1);
	firstDay=firstDayDate.getDay();//gets selected month's first weekday. 0 for sunday, 1 for monday...
	if(firstDay==0)
		firstDay=7;
		
	if(month==1){
		prev_year="calendar("+uwid+", "+day+", "+month+", "+(year-1)+")";
		prev_month="calendar("+uwid+", "+day+", 12, "+(year-1)+")";
		next_month="calendar("+uwid+", "+day+", "+(month+1)+", "+year+")";
		next_year="calendar("+uwid+", "+day+", "+month+", "+(year+1)+")";
	}
	else if(month==12){
		prev_year="calendar("+uwid+", "+day+", "+month+", "+(year-1)+")";
		prev_month="calendar("+uwid+", "+day+", "+(month-1)+", "+year+")";
		next_month="calendar("+uwid+", "+day+", 1, "+(year+1)+")";
		next_year="calendar("+uwid+", "+day+", "+month+", "+(year+1)+")";
	}
	else{
		prev_year="calendar("+uwid+", "+day+", "+month+", "+(year-1)+")";
		prev_month="calendar("+uwid+", "+day+", "+(month-1)+", "+year+")";
		next_month="calendar("+uwid+", "+day+", "+(month+1)+", "+year+")";
		next_year="calendar("+uwid+", "+day+", "+month+", "+(year+1)+")";
	}
	
	//months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	months=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
	string+="<span id='tasks_calendar_header_"+uwid+"'><input type='button' class='ImagesBackPage' onclick='"+prev_month+"' /> "+months[month-1]+" <input type='button' class='ImagesNextPage' onclick='"+next_month+"' /> <input type='button' class='ImagesBackPage' onclick='"+prev_year+"' /> "+year+" <input type='button' class='ImagesNextPage' onclick='"+next_year+"' /></span>";
	//string+="<table><tr><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td><td>Su</td></tr>";// JS: 1 2 3 4 5 6 7
	string+="<table>";
	
	//alert("day"+day+"mothn"+month+"year"+year);
	//alert(days);
	//alert(firstDayDate);
	//alert(firstDay);
	
	
	sw1=0;//start from the correct weekday, but only at the beginning
	difw=(7-(firstDay-1));//end of week, when to go to next week; 7=number of days; firstDay-1=number of empty days before the first day; 7-firstDay-1=number of Days In First Week; 
	string+="<tr>";
	for(i=1;i<=days;i++){//loop through all days of the month
		if((i-difw)%7==1)//if day on cause should begin on new week, start the new week
			string+="</tr><tr>";//if other week is full, continue with next week
		if(sw1==0){//if first week, start from the correct weekday; else, continue writing days from monday to sunday
			for(j=1;j<=7;j++)
				if(j!=firstDay)
					string+="<td></td>";
				else
					break;
			sw1=1;
		}
		currentDay=new Date(year, (month-1), i);
		currentTime=currentDay.getTime()/1000;
		string+="<td><span id='calendar_"+currentTime+"_"+uwid+"' onclick='chooseDay("+uwid+", "+currentTime+")'>"+i+"</span></td>";
		if(i==day)
			checkCurrentDay=currentTime;//remember the span's id which will be boxed as the current day
	}
	string+="</tr>";
	string+="</table>";
	
	document.getElementById("calendar_"+uwid).innerHTML=string;
	document.getElementById("calendar_"+checkCurrentDay+"_"+uwid).setAttribute("class", "select");	//box the current day
	document.getElementById("tasks_deadline_calendar_"+uwid).value=checkCurrentDay;						//add current's day value in deadline hidden input
}

function time(uwid,hour,minute){
	string='';
	
	string+="<select name='time_hour_"+uwid+"' id='time_hour_"+uwid+"' style='uwidth:50px;' onchange='chooseHour("+uwid+")'>";
	for(i=0;i<=23;i++){
		if(i<10){
			string+="<option value='0"+i+"'";
			if(0+i==hour)
				string+=" selected='selected'";
			string+=">0"+i+"</option>";
		}
		else{
			string+="<option value='"+i+"'";
			if(i==hour)
				string+=" selected='selected'";
			string+=">"+i+"</option>";
		}
	}
	string+="</select>";
	
	string+="<select name='time_minute_"+uwid+"' id='time_minute_"+uwid+"' style='uwidth:50px;' onchange='chooseMinute("+uwid+")'>";
	for(i=0;i<=59;i++){
		if(i<10){
			string+="<option value='0"+i+"'";
			if(0+i==minute)
				string+=" selected='selected'";
			string+=">0"+i+"</option>";
		}
		else{
			string+="<option value='"+i+"'";
			if(i==minute)
				string+=" selected='selected'";
			string+=">"+i+"</option>";
		}
	}
	string+="</select>";
	
	document.getElementById("tasks_deadline_hour_"+uwid).value=hour;//set the current hour as default
	document.getElementById("tasks_deadline_minute_"+uwid).value=minute;//set the current minute as default
	document.getElementById("time_"+uwid).innerHTML=string;
}

function chooseDay(uwid,time){
	document.getElementById("tasks_deadline_calendar_"+uwid).value=time;
	for(i=0;i<document.getElementById("calendar_"+uwid).getElementsByTagName("span").length;i++)//when selecting a new day, deselect the other day selected previously
		if(document.getElementById("calendar_"+uwid).getElementsByTagName("span")[i].id=="calendar_"+time+"_"+uwid)
			document.getElementById("calendar_"+uwid).getElementsByTagName("span")[i].setAttribute("class", "select");
		else
			document.getElementById("calendar_"+uwid).getElementsByTagName("span")[i].removeAttribute("class");
}

function chooseHour(uwid){
	hour=document.getElementById("time_hour_"+uwid).options[document.getElementById("time_hour_"+uwid).selectedIndex].value;	
	document.getElementById("tasks_deadline_hour_"+uwid).value=hour;
}

function chooseMinute(uwid){
	minute=document.getElementById("time_minute_"+uwid).options[document.getElementById("time_minute_"+uwid).selectedIndex].value;	
	document.getElementById("tasks_deadline_minute_"+uwid).value=minute;
}

function deleteTask(uwid,id,passed_tasks,auto_refresh_interval,nof_items){
	
	window['xmlhttpTasks_'+uwid]=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid]==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/deleteTask.php";
	url=url+"?uwid="+uwid+"&id="+id;
	url=url+"&sid="+Math.random();
	window['xmlhttpTasks_'+uwid].onreadystatechange=function(){
		if (window['xmlhttpTasks_'+uwid].readyState!=4){
			document.getElementById("tasks_"+uwid).innerHTML="<div class='load'></div>";
		}
		if (window['xmlhttpTasks_'+uwid].readyState==4){
			if(window['xmlhttpTasks_'+uwid].responseText==1)
				getTasks(uwid,auto_refresh_interval);
			else
				document.getElementById("tasks_"+uwid).innerHTML=window['xmlhttpTasks_'+uwid].responseText;
		}
	}
	window['xmlhttpTasks_'+uwid].open("GET",url,true);
	window['xmlhttpTasks_'+uwid].send(null);
}

function elementInfoTasks(uwid,element){
	
	document.getElementById("validate_"+element).removeAttribute("class"); //remove previous errors (if any) and show the normal tooltip
	
	window['xmlhttpTasks_'+uwid+'_elementInfo']=GetXmlHttpObject();
	
	if (window['xmlhttpTasks_'+uwid+'_elementInfo']==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="widget_tasks/ajax/elementInfoTasks.php";
	url=url+"?"+element+"&sid="+Math.random();
	
	window['xmlhttpTasks_'+uwid+'_elementInfo'].open("GET",url,false);
	window['xmlhttpTasks_'+uwid+'_elementInfo'].send(null);
	
	document.getElementById("validate_"+element).style.display="inline";
	document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+window['xmlhttpTasks_'+uwid+'_elementInfo'].responseText;
	$("#validate_"+element).delay(3000).fadeOut(1000);
}
