//**************************************************************//
//		sasktel_utils.js				//
// contains javascript to handle general javascript functions	//
//**************************************************************//
//Change Log:							//
//**************************************************************//
//20060111tz - initial write					//
//**************************************************************//	


//timer function
//implements a countdown that displays in a given element and show a given message when the timer reaches zero
//inputs:
//		minutes - the number of minutes to start the timer at
//		seconds - the number of seconds to start the timer at
//		element - the html element to display the timer text, a span tag is recommmended
//		message - the message to be displayed when the timer hits zero
function timer(minutes,seconds,element,message)
{
	if(seconds > 59) seconds = 59;
	if(seconds < 0) seconds = 0;
	if(minutes < 0) minutes = 0;
	
	if(message == null || message == "") message = "The timer has expired.";
	
	time = minutes + ":";
	if (seconds < 10) time += "0";
	time += seconds;
	
	if(element && element.innerHTML)
	{
		element.innerHTML = time;
	}

	if (seconds == 0 && minutes == 0)
	{
		alert(message);
		return;
	}
	if (seconds == 0)
	{
		minutes--;
		seconds = 59;
	}
	else
	{
		seconds--;
	}
	setTimeout(function(){timer(minutes,seconds,element);},1000);
}

//function checkLength 
//checks length of a textfield and stops input once the given length is reached
//will display the current character length in a given element
//inputs:
//		field		- the field to check
//		length 		- the maximun charcacters to allow
//		counterElement	- the element to display the counter text in (span reccommended)
//		e		- the event which contains the last key pressed (stored in the variable called event when called inline) 
function checkLength(field,length,counterElement,e)
{
        if (!e) var e = window.event;
        
        var key = null;
	if(e)
	{
        	if (e.keyCode) key = e.keyCode;
		else if (e.which) key = e.which;
	}
	//control keys
        if ((key==null) || (key==0) || (key==8) ||
           (key==9) ||  (key==27) || (key==37) ||
	   (key==38) || (key==39) || (key==40) || (key==46))
        {
                if(counterElement != null) counterElement.innerHTML = field.value.length;
                return true;
        }

        maxLength = length;
        if(field.value.length == maxLength)
        {
                if(counterElement != null) counterElement.innerHTML = field.value.length;
                return false;
        }
        if(field.value.length > maxLength)
        {
                field.value = field.value.substring(0,maxLength);
                alert("You have exceeded the maximum length for this field. Your text has been truncated!");
                if(counterElement != null) counterElement.innerHTML = field.value.length;
                return false;
        }
        if(counterElement != null) counterElement.innerHTML = field.value.length;
        return true;
}


//function addCheckLengthHandler
//adds the check length function as an event handler to all neccessary events for the specified field
//inputs:
//              field           - the field to add the handlers to
//              length          - the maximun charcacters to allow
//              counterElement  - the element to display the counter text in (span reccommended)
//              e               - the event which contains the last key pressed
function addCheckLengthHandler(field,length,counter,e)
{
	checkLength(field,length,counter,e);
	field.onkeypress=function(e) { return checkLength(field,length,counter,e); };
	field.onkeyup=function(e) { return checkLength(field,length,counter,e); };
	field.onfocus=function(e) { return checkLength(field,length,counter,e); };
	field.onblur=function(e) { return checkLength(field,length,counter,e); };
}



//function addLoadEvent
//adds and event handler to run code when a page loads, keeps any existing handlers as well
//inputs:
//	func - the function to run on page load
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//generic function to add an event listener to an object
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}


//onlynumbers function
//limits input into a text field to only digits
//checks the last key pressed and returns true or false depending on if it is a number
//inputs:
//		myfield - input field to check
//		e	- the event which contains the last key pressed (stored in the variable called event)
function onlynumbers(myfield, e)
{

	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	    (key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;

	else
	   return false;
}

//this function will retrieve the element with the given id
//and either hide or show it by setting the display attribute
//if the element is currently hidden it will be shown and vice versa
function toggleDisplay(elementID)
{
	var element = document.getElementById(elementID);

	if(element)
	{
		if(element.style.display == 'none')
			element.style.display = '';
		else
			element.style.display = 'none';
	}
}

//This function returns the current date and time in a string format
function getDate()
{
	var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var now = new Date();
	var month = months[now.getMonth()];
	var date = now.getDate();
	var year = now.getYear();
	var rawHours = now.getHours();
	var rawMinutes = now.getMinutes();
	var amOrPm = (rawHours <= 11) ? "am" : "pm";
	var hours = (rawHours > 12) ? rawHours - 12 : rawHours;
	var minutes = (rawMinutes < 10) ? "0" + rawMinutes : rawMinutes;
	if (year < 100)
	{
		year = year + 2000;
	}
	else
	{
		if (year < 2000)
		{
			year = year + 1900;
		}
	}
	return month + " " + date + ", " + year + " - " + hours + ":" + minutes + " " + amOrPm;
}

