/*********************************************************
	Teleweb schedule script
	Developed by: Raymond Ko, AP ibm.com Production Services
	Date: 12-02-2004
	version: 1.2
**********************************************************/
//****EDITOR CHECKLIST*******/
//1. Check call centre status
//2. Please select one time zone only
//3. Update CallMeSchedule using local time.
//4. Update holidays schedule
/**************************************************/
//CHOOSE ONE ONLY !!
var status = true; // turn call centre on
//var status = false;// turn call centre off
/***************************************************/
	var days = 10;
	var holidays = new Array(days);
	holidays[0] = new Date(Date.parse("Jan 1,2004"));
	holidays[1] = new Date(Date.parse("Jan 1,2004"));
	holidays[2] = new Date(Date.parse("Jan 1,2004"));
	holidays[3] = new Date(Date.parse("Jan 1,2004"));
	holidays[4] = new Date(Date.parse("Jan 1,2004"));
	holidays[5] = new Date(Date.parse("Jan 1,2004"));
	holidays[6] = new Date(Date.parse("Jan 1,2004"));
	holidays[7] = new Date(Date.parse("Jan 1,2004"));
	holidays[8] = new Date(Date.parse("Jan 1,2004"));
	holidays[9] = new Date(Date.parse("Jan 1,2004"));
/***************************************************/
//new daylight saving for 2008 onward
var gmt = new Date;
//daylight saving finish
var lsm = new Date;
//daylight saving begin
var lso = new Date;
lsm.setMonth(3); // April
lsm.setDate(1);
var day = lsm.getDay();// day of week of 1st
lsm.setDate(1+(7-day+7)%7); // first Sunday of the month
lso.setMonth(9); // October
lso.setDate(1);
day = lso.getDay();
lso.setDate(1+(7-day+7)%7);
var dst = 0;
if (gmt < lsm || gmt >= lso) dst = 1; 
//For AUST timezone only, disable the following if the script is for other GEO.
/***************************************************/
if (dst){
	var timezone = 11; //AU SUMMWE - GMT + 11
}else{
	var timezone = 10; //AU SUMMWE - GMT + 10
}

/***************************************************/
//Pick one only for non AUST geo
//var timezone = -4; //US EST - GMT - 5
//var timezone = 8; //ASEAN,HK,CN and TW - GMT + 8
//var timezone =  9 //KR and JP - CMT + 9
/**************************************************/
	var CallMeSchedule = new Array();
	CallMeSchedule[0] = new Array(7);
    for (var i = 0; i < 7; i++)
    {
     CallMeSchedule[0][i] = new Array(2);
     }
     CallMeSchedule[0][0][0] = "1:00"; //Sunday start time
     CallMeSchedule[0][0][1] = "1:00"; //Sunday end time
     CallMeSchedule[0][1][0] = "8:30";//Monday start time
     CallMeSchedule[0][1][1] = "17:00";//Monday end time
     CallMeSchedule[0][2][0] = "8:30";//Tuesday start time
     CallMeSchedule[0][2][1] = "17:00";//Tuesday end time
     CallMeSchedule[0][3][0] = "8:30";//Wednesday start time
     CallMeSchedule[0][3][1] = "17:00";//Wednesday end time
     CallMeSchedule[0][4][0] = "8:30";//Thursday start time
     CallMeSchedule[0][4][1] = "17:00";//Thursday end time
     CallMeSchedule[0][5][0] = "8:30";//Friday start time
     CallMeSchedule[0][5][1] = "17:00";//Friday end time
     CallMeSchedule[0][6][0] = "1:00";//Saturday start time
     CallMeSchedule[0][6][1] = "1:00";//Saturday end time
/*************************************************************/

// Return the hour component of the time given in HH:MM format.
function get_hour( time )
{
   var end = time.indexOf( ":" );
   var hours = time.substring( 0, end );
   return hours;
}

// Return the minutes component of the time given in HH:MM format.
function get_minutes( time )
{
   var start = time.indexOf( ":" ) + 1;
   var end = time.length;
   var minutes = time.substring( start, end );
   return minutes;
}

function Check_Time( schedule )
{
   var visible = false;
   var date_obj = new Date();
   var dow = date_obj.getDay();
   var start_time = schedule[0][dow][0];
   var stop_time = schedule[0][dow][1];
   var avail = CheckVdnOpen( date_obj, start_time, stop_time );
   visible = (visible || avail);
   return visible;
}

function CheckVdnOpen( date, start, end )
{
   var available = false;
   var time_now = new Date();
   var offset = time_now.getTimezoneOffset();
   var isdayoff = false;
   // extract hour & minutes components of start & stop times
   var start_hour = get_hour( start );
   var start_min = get_minutes( start );
   var stop_hour = get_hour( end );
   var stop_min = get_minutes( end );
   // convert hours & minutes from String to Integer
   start_hour = parseInt( start_hour );
   start_min = parseInt( start_min );
   stop_hour = parseInt( stop_hour );
   stop_min = parseInt( stop_min );
    // convert hours & minutes into date object.
	var time_start = new Date();
	var time_end = new Date();
	time_start.setHours(start_hour-timezone);//
	time_start.setMinutes(start_min,00);
	time_end.setHours(stop_hour-timezone);
	time_end.setMinutes(stop_min,00);
	//covert current time base on user's time zone setting
	var time_new = new Date(time_now.getTime()+(offset*60*1000)); //timezone in milliseconds
	current_time = time_new.getTime();
	var start_time = time_start.getTime();
	var end_time = time_end.getTime();
	for(var i = 0; i < days; i++){
		var hdate = new Date(holidays[i]);	
		if(date.getMonth() == hdate.getMonth() && date.getDate() == hdate.getDate()) {
		isdayoff = true;
		}
	}

  //check if the schedule is available
  if (status == true){
  	if(isdayoff == false){
		if ( end_time > start_time ) {
      		if ( current_time >= start_time && current_time <= end_time ) available = true;
  		}
	}
  }
   return available;
}