
var oldLink = null;
var arrTriggerEventVariables = null;

function sprintf(){
/*
======================================================================
 sprintf()
======================================================================
 Purpose : format a string

 Author  : Antoine Hurkmans, January 2002
----------------------------------------------------------------------
 Parameters :
 you may figure this one out yourself (hint: www.php.net/sprintf)
----------------------------------------------------------------------
 Returns : a formatted string
----------------------------------------------------------------------
 Revision History :
 12-Feb-02 AH  - Added support for alternate padding char
 05-Feb-02 AH  - Fixed bug in display of decimal part for floats
 28-Jan-02 AH  - Initial Version
======================================================================
*/
	var iCount, iPadLength, aMatch, iMatchIndex = 1;
	var bAlignLeft, sPad, iWidth, iPrecision, sType;


	var aArgs = sprintf.arguments;
	if (aArgs.length < 2) return '';

	var sFormat = aArgs[0];
	var re = /%(-)?(0| |'.)?(\d+)?(\.\d*)?([bcdfosxX]{1})/; //'
	while (re.test(sFormat))
	{
		aMatch = re.exec(sFormat);
		bAlignLeft = (aMatch[1] == '-');
		sPad = (aMatch[2] == '' ? ' ' : aMatch[2]);
		if (sPad.substring(0, 1) == "'") sPad = sPad.substring(1);
		iWidth = (aMatch[3] > 0 ? parseInt(aMatch[3]) : 0);
		iPrecision = ((aMatch[4] != null && aMatch[4].length > 1) ? parseInt(aMatch[4].substring(1)) : 6);
		sType = aMatch[5];
		mArgument = (aArgs[iMatchIndex] != null ? aArgs[iMatchIndex] : '');
		++iMatchIndex;
		if (mArgument.toString().length)
		{
			if ('fbcdoxX'.indexOf(sType) != -1 && isNaN(mArgument)) mArgument = 0;
			switch (sType)
			{
				case 'f':	// floats
					var iPower = Math.pow(10, iPrecision);
					mArgument = (Math.round(parseFloat(mArgument) * iPower) / iPower).toString();
					var aFloatParts = mArgument.split('.');
					if (iPrecision > 0)
					{
						if (aFloatParts.length == 1) aFloatParts[1] = '';
						// pad with zeroes to precision
						for (iCount = aFloatParts[1].length; iCount < iPrecision; iCount++)
							aFloatParts[1] += '0';
						mArgument = aFloatParts[0] + '.' + aFloatParts[1];
					}
					else mArgument = aFloatParts[0];

					iPadLength = aFloatParts[0].length;
					break;
				case 'b':	// binary
					mArgument = parseInt(mArgument).toString(2);
					iPadLength = mArgument.length;
					break;
				case 'c':	// character
					mArgument = String.fromCharCode(parseInt(mArgument));
					break;
				case 'd':	// decimal
					mArgument = mArgument.toString();
					iPadLength = mArgument.length;
					break;
				case 'o':	// octal
					mArgument = parseInt(mArgument).toString(8);
					iPadLength = mArgument.length;
					break;
				case 'x':	// hexadecimal (lowercase)
					mArgument = parseInt(mArgument).toString(16);
					iPadLength = mArgument.length;
					break;
				case 'X':	// hexadecimal (uppercase)
					mArgument = parseInt(mArgument).toString(16).toUpperCase();
					iPadLength = mArgument.length;
					break;
				default:	// strings
					mArgument = mArgument.toString();
					iPadLength = mArgument.length;
			}

			if ('fbdoxX'.indexOf(sType) != -1)
			{
				// pad with padding-char to width
				if (bAlignLeft)
					for (iCount = iPadLength; iCount < iWidth; iCount++)
						mArgument += sPad;
				else
					for (iCount = iPadLength; iCount < iWidth; iCount++)
						mArgument = sPad + mArgument;
			}
		}
		sFormat = sFormat.replace(re, mArgument);
	}
	return sFormat;
}//end sprintf


// code to change the active stylesheet
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  if (cal.dateClicked){
    cal.sel.value = date; // just update the date in the input field.

    if(arrTriggerEventVariables != null){

		if(arrTriggerEventVariables.length > 0){
			var strParameters = '';
			for(var x=1; x < arrTriggerEventVariables.length; x++){
				strParameters += 'arrTriggerEventVariables[' + x + '],';
			}//end for
			if(strParameters != '') strParameters = strParameters.substr(0, strParameters.length -1);
			var theObj = eval(arrTriggerEventVariables[0] + "(" + strParameters + ")");
		}//endif

    }//endif

    cal.callCloseHandler(cal);
  }//endif
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
  cal.destroy();
  calendar = null;
}


// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime) {

	//if they pass in more then 3 parameters that means that the 4th parameter is the name of a function
	//and the remainder are parameters to that function
	// which will be called after the user choses a date.

	if(showCalendar.arguments.length > 3){

  		arrTriggerEventVariables = new Array();
  		for(var count=3; count < showCalendar.arguments.length; count++){
  			arrTriggerEventVariables.push(showCalendar.arguments[count]);
  		}//end for
  		//alert("arr length : " + arrTriggerEventVariables.length);
	}//endif

  var el = document.getElementById(id);
  //change all dashes to / so that date parser won't freak

  var objDate = new Date(Date.parse(el.value.replace(/-/g, "/")));

  if(el.value != '' && !isNaN(objDate.getTime())){
  	//el.value = sprintf("%04d-%02d-%02d", objDate.getFullYear(), (objDate.getMonth() + 1), objDate.getDate());
  	el.value = sprintf("%02d/%02d/%04d",  (objDate.getMonth() + 1), objDate.getDate(), objDate.getFullYear());
  }else if(el.value != ''){
  	objDate = new Date();
  	//el.value = sprintf("%04d-%02d-%02d", objDate.getFullYear(), (objDate.getMonth() + 1), objDate.getDate());
  	el.value = sprintf("%02d/%02d/%04d",  (objDate.getMonth() + 1), objDate.getDate(), objDate.getFullYear());
  }//endif

  if(typeof setFieldId == "string"){
  	objSetField = document.getElementById(setFieldId);
  	if(typeof setFieldValue == "string"){
		strSetFieldValue = setFieldValue;
	}
  }//endif

  if (calendar != null) {
    // we already have some calendar created
    calendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(false, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  calendar.setDateFormat(format);    // set the specified date format
  //calendar.setDateFormat("%m/%d/%Y");    // set the specified date format
  calendar.parseDate(el.value);      // try to parse the text in field
  calendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  //calendar.showAtElement(el.nextSibling, "Br");        // show the calendar
  calendar.showAtElement(el, "Br");        // show the calendar

  return false;
}

function showCalendarAt(id, format, showsTime, posX, posY) {
  var el = document.getElementById(id);
  if (calendar != null) {
    // we already have some calendar created
    calendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(false, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  calendar.setDateFormat(format);    // set the specified date format
  calendar.parseDate(el.value);      // try to parse the text in field
  calendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
 calendar.showAt(posX, posY);

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(true, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}

// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
	document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
	document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
	if (document.layers) {
		// When the page scrolls in Netscape, the event's mouse position
		// reflects the absolute position on the screen. innerHight/Width
		// is the position from the top/left of the screen that the user is
		// looking at. pageX/YOffset is the amount that the user has
		// scrolled into the page. So the values will be in relation to
		// each other as the total offsets into the page, no matter if
		// the user has scrolled or not.
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	} else if (document.all) {
		// When the page scrolls in IE, the event's mouse position
		// reflects the position from the top/left of the screen the
		// user is looking at. scrollLeft/Top is the amount the user
		// has scrolled into the page. clientWidth/Height is the height/
		// width of the current page the user is looking at. So, to be
		// consistent with Netscape (above), add the scroll offsets to
		// both so we end up with an absolute value on the page, no
		// matter if the user has scrolled or not.
		xMousePos = window.event.x+document.body.scrollLeft;
		yMousePos = window.event.y+document.body.scrollTop;
		xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
		yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	} else if (document.getElementById) {
		// Netscape 6 behaves the same as Netscape 4 in this regard
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}
}

function popUpCalendar(elementId){
	alert("xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax);
	//var openedWindow = window.open("test.html", "openedWindow", "height=170,width=200,resizable=yes");
	//openedWindow.window.test();
	//openedWindow.window.setOpenerData(elementId, "2004-04-05");
}//end popUpCalendar
