//DateTime Parser Method For Date Object
Date.prototype.parseDate = function(dt, format){
 var keys = { y:"setFullYear",
   m:"setMonth",
   d:"setDate",
   h:"setHours",
   n:"setMinutes",
   s:"setSeconds",
   i:"setMilliseconds"
 }
 var num, ci, meth;
 //document.write('\r\n<br><br>\r\n' + format + ' =>' + format.length + '<br>\r\n');
 //document.write(dt + ' =>' + dt.length + '<br>\r\n');
 for(var i=0,j=0,k; i<format.length; i++,j++){
  ci = format.charAt(i);
  if(keys[ci] != undefined){
  	//document.write(ci + " OK  ==> ");
   for(k=j; k<=dt.length && !isNaN(dt.charAt(k)); k++);
   if(k > dt.length)return;
   num = parseInt(dt.substring(j,k));
   	//document.write(num + " ==> ");
   meth = keys[ci];
   if(meth == "setMonth"){
    this.setMonth(num-1);
   }else{
    this[meth](num);
   }
   //document.write(meth + ' => ' + num + '<br>\r\n');
   j = k-1;
  } //else document.write(ci + " UNDEFINED<br>");

 }
}
//End of Method
function asc_sort_alarms (a, b) { 
  if (a.dateTime.getTime() > b.dateTime.getTime())
  	return 1; 
  if (a.dateTime.getTime() < b.dateTime.getTime())
  	return -1; 
  return 0;
} 
function desc_sort_alarms (a, b) { 
  if (a.dateTime.getTime() < b.dateTime.getTime())
  	return 1; 
  if (a.dateTime.getTime() > b.dateTime.getTime())
  	return -1; 
  return 0;
} 
function asc_sort_reminds (a, b) { 
  if (a.remindDateTime.getTime() > b.remindDateTime.getTime())
  	return 1; 
  if (a.remindDateTime.getTime() < b.remindDateTime.getTime())
  	return -1; 
  return 0;
} 
function desc_sort_reminds (a, b) { 
  if (a.remindDateTime.getTime() < b.remindDateTime.getTime())
  	return 1; 
  if (a.remindDateTime.getTime() > b.remindDateTime.getTime())
  	return -1; 
  return 0;
} 

function alarm(itemid, dt, rdt, t, vu) { 
	this.itemId = itemid;
	this.dateTime = new Date(); 
	this.dateTime.parseDate(dt,".y-m-d-h-n-s."); 
	this.remindDateTime = new Date(); 
	this.remindDateTime.parseDate(rdt,".y-m-d-h-n-s."); 
	this.eventTitle = t; 
	this.viewUrl = vu; 
} 
var alarms = new Array(); 
var reminds = new Array(); 
var hours, minutes, seconds;
var timer=null;

function sClock() {
  var d = new Date();
  hours=d.getHours();
  minutes=d.getMinutes();
  seconds=d.getSeconds()+1;
  if(timer){clearInterval(timer);timer=null;}
  timer=setInterval("work();",1000);
}
function twoDigit(_v) {
  if(_v<10)_v="0"+_v;
  return _v;
}
function refreshEvents(itemID,sec) {
	if (document.images) {
		switch (sec) {
		  case 0:
		    if (document.images['monthly'+itemID]) {document.images['monthly'+itemID].alt='-'; document.images['monthly'+itemID].src=img_EVENT_PASSED;} //'./DesktopModules/DesktopCalendar/images/EVENT_PASSED.gif'
		    if (document.images['weekly'+itemID]) {document.images['weekly'+itemID].alt='-'; document.images['weekly'+itemID].src=img_EVENT_PASSED;}
		    if (document.images['daily'+itemID]) {document.images['daily'+itemID].alt='-'; document.images['daily'+itemID].src=img_EVENT_PASSED;}
		    break;
		  case 1:
		    if (document.images['monthly'+itemID]) if (document.images['monthly'+itemID].alt!='-') document.images['monthly'+itemID].src=img_EVENT_ALARMED; //'./DesktopModules/DesktopCalendar/EVENT_ALARMED.gif';
		    if (document.images['weekly'+itemID]) if (document.images['weekly'+itemID].alt!='-') document.images['weekly'+itemID].src=img_EVENT_ALARMED;
		    if (document.images['daily'+itemID]) if (document.images['daily'+itemID].alt!='-') document.images['daily'+itemID].src=img_EVENT_ALARMED;
		    break;
		 }
	}
}
function work() {
  //if (!document.layers && !document.all && !document.getElementById) return;
  var runTime = new Date();
 /* var dn = " AM";
  var shours = hours;
  var sminutes = minutes;
  var sseconds = seconds;
  if (shours >= 12) {
    dn = " PM";
    shours-=12;
  }
  if (!shours) shours = 12;
  sminutes=twoDigit(sminutes);
  sseconds=twoDigit(sseconds);
  shours  =twoDigit(shours);
  movingtime = ""+ shours + ":" + sminutes +":"+sseconds+"" + dn;
  if (document.getElementById)
    document.getElementById("clockclock").innerHTML=movingtime;
  else if (document.layers)  {
    document.layers.clockclock.document.open();
    document.layers.clockclock.document.write(movingtime);
    document.layers.clockclock.document.close();
  }
  else if (document.all)
    clockclock.innerHTML = movingtime;*/
  if(++seconds>59)  {
    seconds=0;
    if(++minutes>59) {
      minutes=0;
      if(++hours>23) {
        hours=0;
      }
    }
  }
  if (alarms.length > 0) {
	  if    (  (runTime.getFullYear() == alarms[0].dateTime.getFullYear())
	  	   && (runTime.getMonth() == alarms[0].dateTime.getMonth())
	  	   && (runTime.getDate() == alarms[0].dateTime.getDate())
	  	   && (runTime.getHours() == alarms[0].dateTime.getHours())
	  	   && (runTime.getMinutes() == alarms[0].dateTime.getMinutes())
	  	   && (runTime.getSeconds() == alarms[0].dateTime.getSeconds())
	  	) {
	  	alert("ALARM!!!\r\n" + alarms[0].eventTitle);
	  	refreshEvents(alarms[0].itemId,0);
	  	alarms.shift();	
	  } else if (runTime > alarms[0].dateTime) {
	    refreshEvents(alarms[0].itemId,0);
	    alarms.shift();
	  }
  }
  if (reminds.length > 0) {
	  if    (  (runTime.getFullYear() == reminds[0].remindDateTime.getFullYear())
	  	   && (runTime.getMonth() == reminds[0].remindDateTime.getMonth())
	  	   && (runTime.getDate() == reminds[0].remindDateTime.getDate())
	  	   && (runTime.getHours() == reminds[0].remindDateTime.getHours())
	  	   && (runTime.getMinutes() == reminds[0].remindDateTime.getMinutes())
	  	   && (runTime.getSeconds() == reminds[0].remindDateTime.getSeconds())
	  	) {
	  	alert("REMIND!!!\r\n" + reminds[0].eventTitle);
	  	refreshEvents(reminds[0].itemId,1);
	  	alarms.shift();	
	  } else if (runTime > reminds[0].remindDateTime) {
	    refreshEvents(reminds[0].itemId,1);
	    reminds.shift();
	  }
  }
}

