// Cookie function *********************************
function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain="+APPBAR_COOKIEDOMAIN;
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name, null,-1);
}

// Alarm checking functions *********************************
function CheckAlarm()
{
	var today=new Date();
	var nowtime=today.getHours()+":"+today.getMinutes();
	var todaydate=today.getFullYear()+''+today.getMonth()+''+today.getDate();
	var tmp="", i=0, hr, min, week, debug="";

	for(i=1; i<=3; i++)
	{
		if (today.getDay()==0) week="_sun";
		else if (today.getDay()==1) week="_mon";
		else if (today.getDay()==2) week="_tue";
		else if (today.getDay()==3) week="_wed";
		else if (today.getDay()==4) week="_thu";
		else if (today.getDay()==5) week="_fri";
		else if (today.getDay()==6) week="_sat";

    debug+="a"+i+week+") "+readCookie("a"+i+week)+", "+readCookie("a"+i+"_status")+", "+(readCookie("a"+i+"_hh")+":"+readCookie("a"+i+"_mm"))+"=="+nowtime+", "+readCookie("a"+i+"_alerted")+"<br>";
		if (readCookie("a"+i+week) && readCookie("a"+i+"_status")=="on" && (readCookie("a"+i+"_hh")+":"+readCookie("a"+i+"_mm"))==nowtime && readCookie("a"+i+"_alerted")!=todaydate)
		{
      debug+="---> Alarming<br>";
      createCookie("a"+i+"_alerted", todaydate, (30*6));
      Playa.doPlay();
			tmp=confirm("Alarm \n---------------------------------------\n"+readCookie("a"+i+"_name")+" - "+(readCookie("a"+i+"_hh")+":"+readCookie("a"+i+"_mm"))+"\n\nPress [OK] to dismiss, OR \nPress [CANCEL] to delay 2 mins.");
			if(tmp==false)
			{
				createCookie("a"+i+"_alerted", todaydate, (30*6));
				createCookie("a"+i+"_delayalert", today.getTime(), (30*6));
			}
      Playa.doStop();
		}

		if (readCookie("a"+i+"_delayalert")!=null)
		{
			if (today.getTime()-readCookie("a"+i+"_delayalert")>=120000)
			{
        eraseCookie("a"+i+"_delayalert");
        Playa.doPlay();
			  tmp=confirm("Reminder \n---------------------------------------\n"+readCookie("a"+i+"_name")+" - "+(readCookie("a"+i+"_hh")+":"+readCookie("a"+i+"_mm"))+"\n\nPress [OK] to dismiss, OR \nPress [CANCEL] to delay 2 mins.");
			  if(tmp==false)
			  {
				  createCookie("a"+i+"_delayalert", today.getTime(), (30*6));
			  }
        Playa.doStop();
			}
		}
	}
  //document.getElementById("debug").innerHTML=debug;
	return ;
}

function StartAlarm()
{
	if (document.all||document.getElementById)
	setInterval("CheckAlarm()", 5000)
	return ;
}

// Set Alarm functions *********************************
function PrintAlarms(prefix)
{
	var i;
  document.write('<table border="0" width="100%" cellpadding="2" cellspacing="0" style="color: #E1E1E1;">');
	document.write('<tr width="15px">');
	document.write('<td><input type="checkbox" name="'+prefix+'_status" id="'+prefix+'_status" onclick="setcookie_status(\''+prefix+'\');"/></td>');
	document.write('<td><input type="text" name="'+prefix+'_name" id="'+prefix+'_name" onkeyup="setcookie_all(\''+prefix+'_name\');" /></td>');
	document.write('<td>&nbsp;</td></tr><tr>');
	document.write('<td style="border-bottom: 1px solid #B5B6B5">&nbsp;</td>');
	document.write('<td style="border-bottom: 1px solid #B5B6B5">');
	document.write('<input type="checkbox" name="'+prefix+'_sun" id="'+prefix+'_sun" onclick="setcookie_all(\''+prefix+'_sun\');" /> Sun');
	document.write('<input type="checkbox" name="'+prefix+'_mon" id="'+prefix+'_mon" onclick="setcookie_all(\''+prefix+'_mon\');" /> Mon');
	document.write('<input type="checkbox" name="'+prefix+'_tue" id="'+prefix+'_tue" onclick="setcookie_all(\''+prefix+'_tue\');" /> Tue<br>');
	document.write('<input type="checkbox" name="'+prefix+'_web" id="'+prefix+'_wed" onclick="setcookie_all(\''+prefix+'_wed\');" /> Wed');
	document.write('<input type="checkbox" name="'+prefix+'_thu" id="'+prefix+'_thu" onclick="setcookie_all(\''+prefix+'_thu\');" /> Thu');
	document.write('<input type="checkbox" name="'+prefix+'_fri" id="'+prefix+'_fri" onclick="setcookie_all(\''+prefix+'_fri\');" /> Fri<br>');
	document.write('<input type="checkbox" name="'+prefix+'_sat" id="'+prefix+'_sat" onclick="setcookie_all(\''+prefix+'_sat\');" /> Sat');  
	document.write('</td>');
	document.write('<td width="85px" style="border-bottom: 1px solid #B5B6B5" valign="top">');
  document.write('<select name="'+prefix+'_hh" id="'+prefix+'_hh" onchange="setcookie_all(\''+prefix+'_hh\');" />');
	for(i=0; i<=23; i++)
  {
    if (i>=0 && i<=9)
    {
      document.write('<option value="0'+i+'">0'+i+'</option>');
    }
    else
    {
      document.write('<option value="'+i+'">'+i+'</option>');
    }
  }
  document.write('</select>');
  document.write(':');
  document.write('<select name="'+prefix+'_mm" id="'+prefix+'_mm" onchange="setcookie_all(\''+prefix+'_mm\');" />');
	for(i=0; i<=59; i++)
  {
    if (i>=0 && i<=9)
    {
      document.write('<option value="0'+i+'">0'+i+'</option>');
    }
    else
    {
      document.write('<option value="'+i+'">'+i+'</option>');
    }
  }
	document.write('</td>');
	document.write('</tr></table>');
	return null;
}

function setcookie_all(objname)
{ 
  var name=objname.substring((objname.indexOf("_")+1));
  
  if (name=="name" || name=="hh" || name=="mm")
  {
    if (document.getElementById(objname).value.length>0) { createCookie(objname, document.getElementById(objname).value, (30*6)); }
    else { eraseCookie(objname); }
  }
  else
  {
    if (document.getElementById(objname).checked)
    {
      createCookie(objname, document.getElementById(objname).value, (30*6));
    }
    else
    {
      eraseCookie(objname);
    }
  }
  eraseCookie(objname.substring(0, (objname.indexOf("_")+1))+"alerted");
  return null;
}
function setcookie_status(prefix)
{  
  // check status
  if (document.getElementById(prefix+"_status").checked) 
  {
    createCookie((prefix+"_status"), document.getElementById(prefix+"_status").value, (30*6));
  }
  else
  {
    eraseCookie((prefix+"_status"));
    eraseCookie((prefix+"_name"));
    eraseCookie((prefix+"_sun"));
    eraseCookie((prefix+"_mon"));
    eraseCookie((prefix+"_tue"));
    eraseCookie((prefix+"_wed"));
    eraseCookie((prefix+"_thu"));
    eraseCookie((prefix+"_fri"));
    eraseCookie((prefix+"_sat"));
    eraseCookie((prefix+"_hh"));
    eraseCookie((prefix+"_mm"));
    ResetAll(prefix);
  }
  eraseCookie((prefix+"_alerted"));
  return null;
}

function ResetAll(prefix)
{
  document.getElementById(prefix+"_status").checked=false;
  document.getElementById(prefix+"_name").value="";
  document.getElementById(prefix+"_sun").checked=false;
  document.getElementById(prefix+"_mon").checked=false;
  document.getElementById(prefix+"_tue").checked=false;
  document.getElementById(prefix+"_wed").checked=false;
  document.getElementById(prefix+"_thu").checked=false;
  document.getElementById(prefix+"_fri").checked=false;
  document.getElementById(prefix+"_sat").checked=false;
  document.getElementById(prefix+"_hh").options[0].selected=01;
  document.getElementById(prefix+"_mm").options[0].selected=01;
}

function SetValueByCookie()
{
  var i;
  for(i=1; i<=3; i++)
	{
    if (readCookie("a"+i+"_status")=="on") { document.getElementById("a"+i+"_status").checked=true; }
    if (readCookie("a"+i+"_name")!="") { document.getElementById("a"+i+"_name").value=readCookie("a"+i+"_name"); }
    if (readCookie("a"+i+"_sun")=="on") { document.getElementById("a"+i+"_sun").checked=true; }
    if (readCookie("a"+i+"_mon")=="on") { document.getElementById("a"+i+"_mon").checked=true; }
    if (readCookie("a"+i+"_tue")=="on") { document.getElementById("a"+i+"_tue").checked=true; }
    if (readCookie("a"+i+"_wed")=="on") { document.getElementById("a"+i+"_wed").checked=true; }
    if (readCookie("a"+i+"_thu")=="on") { document.getElementById("a"+i+"_thu").checked=true; }
    if (readCookie("a"+i+"_fri")=="on") { document.getElementById("a"+i+"_fri").checked=true; }
    if (readCookie("a"+i+"_sat")=="on") { document.getElementById("a"+i+"_sat").checked=true; }
    if (readCookie("a"+i+"_hh")!=null) { document.getElementById("a"+i+"_hh").selectedIndex=readCookie("a"+i+"_hh"); }
    if (readCookie("a"+i+"_mm")!=null) { document.getElementById("a"+i+"_mm").selectedIndex=readCookie("a"+i+"_mm"); }
  }
}
StartAlarm();