function init() {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5) {
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	       try {
	               xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	       } catch (e) {
	               try {
	                       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	               } catch (E) {
	                       xmlhttp = false;
	               }
	       }
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
         return xmlhttp;
}
function checkUncheck(id, status) {
	var xmlhttp=init();

         var element = document.getElementById('todos');
         var image = document.getElementById(id);
         if (status == "notdone")
         	image.src = "images/CheckboxChecked.gif";
         else
         	image.src = "images/CheckboxUnchecked.gif";
         //element.innerHTML = 'Loading ...';
         xmlhttp.open('GET', 'home.php?id=' + id + '&status=' + status, true);

         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     element.innerHTML = xmlhttp.responseText;
                     
                     Sortable.create("todos_notdone", {  
                         onUpdate: function() { 
                             new Ajax.Request("home.php?ajax=true", {  
                                 method: "post",  
                                 parameters: { todos: Sortable.serialize("todos_notdone") }            
                             });  
                         }
                     });                     
             }
         };

         xmlhttp.send(null);
}

function moveUpAndDown(id, dir, in_category) {
	var xmlhttp=init();
	in_category = (in_category ? in_category : false);

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
         var element = document.getElementById('todos');

         //element.innerHTML = 'Loading ...';
         xmlhttp.open('GET', 'home.php?id=' + id + '&dir=' + dir + (in_category ? '&cat=' + in_category : ""), true);

         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     element.innerHTML = xmlhttp.responseText;
             }
         };

         xmlhttp.send(null);
}

function redate(file, id, todate) {
         var funcRef = redateit(file, id, todate);
	window.setTimeout(funcRef, 0);
}

function redateit(file, id, todate) {
	return (function() {
	        /* This inner function is to be executed with - setTimeout
	           - and when it is executed it can read, and act upon, the
	           parameters passed to the outer function:-
	        */
	         var xmlhttp=init();

	         var element = document.getElementById('todos');
	         xmlhttp.open('GET', file + '?id=' + id + '&redate=' + todate, true);

	         xmlhttp.onreadystatechange = function() {
	                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	                    //document.location.href
	                     element.innerHTML = xmlhttp.responseText;
	                 }
	         };

	         xmlhttp.send(null);
	});
}

// Pulls up the calendar for this event
function calendarRequest(file, type, eventDate, calendarDate, elementid, eid) {
         // Bring up, or refresh the calendars
//         	alert(file + " " + type + " " + eventDate + " " + calendarDate + " " + elementid);
//         alert('file=' + file + 'type=' + type + 'eventDate=' + eventDate + 'calendarDate=' + calendarDate + 'elementid=' + elementid);
	var xmlhttp=init();

         if (eid == undefined)
         	eid = 0;

         var element = document.getElementById(elementid);


         if (type == '')
	         xmlhttp.open('GET', file + '?ajax=blank', true);
         else if (type == 'redate')
	         xmlhttp.open('GET', file + '?ajax=blank', true);
         else if (file == 'new.php' || file == 'newr.php')
	         xmlhttp.open('GET', file + '?ajax=getcal' + '&edate=' + eventDate + '&cdate=' + calendarDate, true);
         else if (file.substring(0, 8) == 'home.php' || file.substring(0, 10) == 'search.php')
	         xmlhttp.open('GET', file + '&ajax=getcal' + '&edate=' + eventDate + '&cdate=' + calendarDate + '&element=' + elementid, true);
         else if (file == 'detail.php')
	         xmlhttp.open('GET', file + '?ajax=getcal' + '&edate=' + eventDate + '&cdate=' + calendarDate + '&eid=' + eid, true);

         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     if (type == 'forward')
                     	return;

                     element.innerHTML = xmlhttp.responseText;

                     // Only update the todo header if its new or an existing editable todo
                     if (file == 'new.php' || file == 'newr.php' || file == 'detail.php') {
	                     // Update the todo header with the new date
	                     var xmlhttpInner=init();
	                     var elementInner = document.getElementById('todoheader');
                              xmlhttpInner.open('GET', file + '?ajax=redate' + '&edate=' + eventDate + '&cdate=' + calendarDate + '&eid=' + eid, true);
                             
	                     xmlhttpInner.onreadystatechange = function() {
	                             if (xmlhttpInner.readyState == 4 && xmlhttpInner.status == 200) {
	                                 elementInner.innerHTML = xmlhttpInner.responseText;
                                          
                                          if (file == 'newr.php') {
                                              var xmlhttpDate=init();
                                              var elementDate = document.getElementById('futuredate');
                                              var when = document.getElementById('when-future');
                                              
                                              xmlhttpDate.open('GET', file + '?ajax=tododate' + '&edate=' + eventDate + '&cdate=' + calendarDate + '&future=' + (when.checked ? "true" : "false"), true); 
                                              
                                              xmlhttpDate.onreadystatechange = function() {
                                                      if (xmlhttpDate.readyState == 4 && xmlhttpDate.status == 200) {                                                   
                                                          elementDate.innerHTML = xmlhttpDate.responseText;
                                                      }
                                              };

                                              xmlhttpDate.send(null);                                  
                                          }                                                                                
	                             }
	                     };

                     	     xmlhttpInner.send(null);
                     }
                 }
         };
         xmlhttp.send(null);
}

function calendarRedate(file, id, todate) {
//        	alert(file + " " + id + " " + todate);
         var xmlhttp=init();

         var element = document.getElementById('todos');

         xmlhttp.open('GET', file + '?id=' + id + '&redate=' + todate, true);

         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     element.innerHTML = xmlhttp.responseText;
                     
					
					var ob = document.getElementsByTagName("script");
					for(var i=0; i<ob.length-1; i++){
						if(ob[i+1].text!=null) eval(ob[i+1].text);
					}
					
                     Sortable.create("todos_notdone", {  
                         onUpdate: function() { 
                             new Ajax.Request("home.php?ajax=true", {  
                                 method: "post",  
                                 parameters: { todos: Sortable.serialize("todos_notdone") }            
                             });  
                         }
                     });
                 }
         };

         xmlhttp.send(null);
}

/*
// Modifies the calendar page's evendate and calendardate with the selected date
function calendarselect(file, eventDate) {
	var xmlhttp=init();
         var element = document.getElementById('edate');
         element.value = eventDate;
         // try to enable ajax by calling index, which sets a cookie
         // if we have an ajax cookie we use ajax, otherwise we won't use ajax
         xmlhttp.open('GET', file + '?redate=true&edate=' + eventDate, true);

         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     element.innerHTML = xmlhttp.responseText;
                 }
         };

         xmlhttp.send(null);
}
*/

function setLongDate(id, date) {
	var xmlhttp=init();
         var element = document.getElementById(id);
         // try to enable ajax by calling index, which sets a cookie
         // if we have an ajax cookie we use ajax, otherwise we won't use ajax
         xmlhttp.open('GET', 'new.php?ajax=longdate&edate=' + date, true);

         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                 	element.innerHTML = xmlhttp.responseText;
                 }
         };


         xmlhttp.send(null);
}

function checkajax() {
	var xmlhttp=init();
         var element = document.getElementById('todos');
         // try to enable ajax by calling index, which sets a cookie
         // if we have an ajax cookie we use ajax, otherwise we won't use ajax
         xmlhttp.open('GET', 'index.php?ajax=true', true);

         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200);
         };


         xmlhttp.send(null);
}

/*
*  iPhone calendar
*/
function geticalendar(id, date) {
	var xmlhttp=init();
         var element = document.getElementById(id);
         
         xmlhttp.open('GET', 'iphone_calendar.php?date=' + date, true);

         
         xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                 	element.innerHTML = xmlhttp.responseText;
                 }
         };


         xmlhttp.send(null);	
}


function toggleCategory(id) {
	var xmlhttp=init();
	var element = document.getElementById(id);

	var cat = document.getElementById(id);
	var cat_img = document.getElementById(id + "_img");
	var open = "false";
	
	if (cat.style.display=='none') {
		open = "true";
		cat.style.display = 'block';
		cat_img.src = "images/down.gif";
	}
	else {
		cat.style.display = 'none';
		cat_img.src = "images/right.gif";
	}
	
	
	xmlhttp.open('GET', 'home.php?ajax=togglecat&cat=' + id + '&open=' + open, true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			;
		}
	};
	
	
	xmlhttp.send(null);
}