/* 
  We no longer include the script tags, so we can no longer include this file with shtml.
  We used to do this because IE 3.x did not support the SRC attribute of the <SCRIPT> tag. :-( 

  The function checkAvailabilityOfEmployee only works in JavaScript 1.2 or greater.
  If it is sourced in a pre-JavaScript 1.2 interpreter it will give errors

      File: employeeUnavailable.js
      Comments:	JavaScript code that implements the unavailable feature of AM pager interface.
      
      Last Modified:  2003-10-13
      Original Author:	Steven Conner
      Last Modified By:	Alex Hisen
*/
    
function getDayString(in_date) {		
    // getDay() returns 0-6 for day of week.
    switch (in_date.getUTCDay()) {		
	// Here we translate to appropriate char.
	case 0: return "U";
	case 1: return "M";
	case 2: return "T";
	case 3: return "W";
	case 4: return "H";
	case 5: return "F";
	case 6: return "S";
    }
}

function getDateString(in_date) {  
    // getDate() returns 1-31 for day of month.  
    //We want 01-31 so we pad single digits with a leading zero.
    return ((((in_date.getUTCDate()).toString()).length == 1) ? ('0' + (in_date.getUTCDate()).toString()) : ((in_date.getUTCDate()).toString()));
}

function getMonthString(in_date) {  
    // getMonth() returns 0-11 for month of year.  
    //We want 01-12 so we add 1 and pad single digits with a leading zero.
    return ((((in_date.getUTCMonth()+1).toString()).length == 1) ? ('0' + (in_date.getUTCMonth()+1).toString()) : ((in_date.getUTCMonth()+1).toString()));
}

function unavailableNow(in_date, start, end) {  
    // Is the present moment within unavailableRange? 
    return (((Date.parse(start) <= in_date.valueOf()) && (Date.parse(end) >= in_date.valueOf())) ? true : false);
}

function isIE() {
    //returns true if the browser is IE, false otherwise
    //alert ("isie: "+navigator.appName+" == Microsoft Internet Explorer : " + (navigator.appName == "Microsoft Internet Explorer"));
    return (navigator.appName == "Microsoft Internet Explorer");
}

function checkAvailabilityOfEmployee(checkBox, catagory, callingFunction) {
    // Comments:  Checks employee availability for a page and informs the user if necessary.
    var unavailableRange, unavailableMsg, unavailableStart, unavailableEnd;
    var monthString, results, pattern, empName, i = 1;
	//year, month, day, hours, minutes, seconds, ms
    var nowLocal = new Date();
	//var now = new Date(Date.UTC(nowLocal.getUTCFullYear(), nowLocal.getUTCMonth(), nowLocal.getUTCDate(), nowLocal.getUTCHours(), nowLocal.getUTCMinutes(), nowLocal.getUTCSeconds(), nowLocal.getUTCMilliseconds()));
	//-(1000*60*60*8));	
	var now = new Date();
    var empCode = checkBox.name.substr(9,12);  // Get employeeCode from name of checkbox
    
	//alert ("now (GMT): "+now.toUTCString());

    // if employee has no page_via_email value and therefore cannot receive a page via the form, then uncheck box and exit.
    if (eval("topframe.document.CheckForm.page_via_email_" + empCode + ".value") == "") {
	eval("topframe.document.CheckForm.selected_" + empCode + ".checked = false");
	return;
    }
    
    var tempvar = eval ("topframe.document.CheckForm.catagory_"+empCode+".value");
    //alert ("catagory: "+tempvar+" != "+catagory+" == "+(tempvar == catagory));
    if ((catagory != "all") &&  // if catagory is not appropriate, exit
	(eval("topframe.document.CheckForm.catagory_" + empCode + ".value") != catagory)) {
	return;
    }
    //alert ("made it here...")
    
    // if the box is unchecked then the user just unchecked it so we exit
    if ((callingFunction == "onclick") 
	&& (eval("topframe.document.CheckForm.selected_" + empCode + ".checked") != true)) {
	return;
	// if checkAll called us and the box is unchecked we check it and proceed
	
    } else if (callingFunction == "checkAll") {
	if (eval("topframe.document.CheckForm.selected_" + empCode + ".checked") != true) {  
	    eval("topframe.document.CheckForm.selected_" + empCode + ".checked = true");
	    // if the box is already checked then we exit
	} else {return;}
    }
	
    // Loop through all availability records till there aren't anymore
    while (eval("typeof topframe.document.CheckForm.unavailable_range_" + empCode + "_" + i) != "undefined") {
	eval("unavailableRange = topframe.document.CheckForm.unavailable_range_" + empCode + "_" + i + ".value");
	
	// Check to see if we are within unavailableRange and if so display message box
	
	var dataType = /([A-Za-z])/;  // regular expression
	
	// Determine what type of schedule we are dealing with and generate unavailableStart/unavailableEnd.
	// The comment following each "case label" contains an example of it's schedule type.
	// For details on the different schedule types see: 
	//   \\Devserver\Devserver-c\NaviSoft\Server\modules\tcl\oncall\oncall_dbspec.doc
	switch (dataType.exec(unavailableRange)[1]) {  // returns first alpha char in unavailableRange

	    case "R":   //  R|1999-01-27:1200;1999-01-30:1200
	    pattern = /(\d{4})-(\d{2})-(\d{2}):(\d{2})(\d{2});(\d{4})-(\d{2})-(\d{2}):(\d{2})(\d{2})/;
	    results = pattern.exec(unavailableRange);
	    unavailableStart = results[1]+"/"+results[2]+"/"+results[3]+" "+results[4]+":"+results[5];
	    unavailableEnd = results[6]+"/"+results[7]+"/"+results[8]+" "+results[9]+":"+results[10];
	    break;

	    case "D":   //  D|1200-1200
	    pattern = /(\d{2})(\d{2})-(\d{2})(\d{2})/;
	    results = pattern.exec(unavailableRange);
	    monthString = getMonthString(now);
	    unavailableStart = now.getUTCFullYear()+"/"+monthString+"/"+now.getUTCDate()+" "+results[1]+":"+results[2];
	    unavailableEnd = now.getUTCFullYear()+"/"+monthString+"/"+now.getUTCDate()+" "+results[3]+":"+results[4];
	    break;

	    case "W":   //  W|M:1200-2000;T:1200-2000;W:1200-2000
	    pattern = /([A-Za-z]):(\d{2})(\d{2})-(\d{2})(\d{2})/g;
	    monthString = getMonthString(now); //month/day/date String now contain data for *today*
	    dayString = getDayString(now);
	    dateString = getDateString(now);
	    // Navigator and IE each treat the lastIndex property differently so we have to have different
	    // code for each.  For details see the JavaScript book mentioned at the top of this file.
	    var stringToSearch = unavailableRange;
	    while ((results = pattern.exec(stringToSearch)) != null) {
		if (isIE()) {
		    stringToSearch = stringToSearch.substring(results.lastIndex,stringToSearch.length);
		}
		//should this run today?
		if (results[1] != dayString) {continue;}
		unavailableStart = now.getUTCFullYear()+"/"+monthString+"/"+dateString+" "+results[2]+":"+results[3];
		unavailableEnd = now.getUTCFullYear()+"/"+monthString+"/"+dateString+" "+results[4]+":"+results[5];
		if (unavailable(Now, unavailableStart, unavailableEnd)) {break;}
	    }
	    break;
	    case "M":   //  M|20:1200-1200;01:1100-2000;23:1200-1200
	    pattern = /(\d{2}):(\d{2})(\d{2})-(\d{2})(\d{2})/g;
	    monthString = getMonthString();
	    // Navigator and IE each treat the lastIndex property differently so we have to have different
	    // code for each.  For details see the JavaScript book mentioned at the top of this file.
	    var stringToSearch = unavailableRange;
	    while ((results = pattern.exec(stringToSearch)) != null) {
		if (isIE()) {
		    stringToSearch = stringToSearch.substring(results.lastIndex,stringToSearch.length);
		}
		unavailableStart = now.getFullYear()+"/"+monthString+"/"+results[1]+" "+results[2]+":"+results[3];
		unavailableEnd = now.getFullYear()+"/"+monthString+"/"+results[1]+" "+results[4]+":"+results[5];
		if (unavailableNow(now, unavailableStart, unavailableEnd)) {break;}
	    }			
	    break;
	    case "S":   //  S|1999-01-28:1200-1200
	    pattern = /(\d{4})-(\d{2})-(\d{2}):(\d{2})(\d{2})-(\d{2})(\d{2})/;
	    results = pattern.exec(unavailableRange);
	    unavailableStart = results[1]+"/"+results[2]+"/"+results[3]+" "+results[4]+":"+results[5];
	    unavailableEnd = results[1]+"/"+results[2]+"/"+results[3]+" "+results[6]+":"+results[7];
	    break;
	    default:
	    alert("Error in checkAvailability function.\n\nNotify system administrator.");
	}
	if (unavailableNow(now, unavailableStart, unavailableEnd)) {  
	    // If employee is unavailable for a page present user with a dialog.
	    
	    eval("unavailableMsg = topframe.document.CheckForm.unavailable_msg_" + empCode + "_" + i + ".value");
	    eval("empName = topframe.document.CheckForm.name_" + empCode + ".value");
	    
	    if (confirm('[From ' + empName + ']:\n\n' + unavailableMsg +   
			'\n\nIf you would like to send the page click OK,\n' +
			'otherwise click Cancel.')) {
			    eval("topframe.document.CheckForm.selected_" + empCode + ".checked = true"); 
			}
	    else {
		eval("topframe.document.CheckForm.selected_" + empCode + ".checked = false"); 
	    }
	    return;	// assuming there can be no overlapping unavailability ranges
	    // which should be enforced by the scheduling UI - we are done - exit the function
	}
	i++;
    }
    
    // If we get to the end, then the return above didn't execute and there is pager is availble
    // so lets check it
    eval("parent.frames[0].document.CheckForm.selected_" + empCode + ".checked = true");
    
} //end of function

