moneySymbol = "$"
decimalSymbol = ".";

if (english)
{
	lang = "";
	arrDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	arrMonths = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
}
else
{
	lang = "_fr";
	arrDays = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");
	arrMonths = new Array("", "janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre");
}

//---------------------------------------------------------------------------
// Write a string representing a sum of money
function writeMoneyStr(money)
{
	document.write(getMoneyStr(money));
}

//---------------------------------------------------------------------------
// Get a string representing a sum of money
function getMoneyStr(money)
{
	if (english)
		return moneySymbol + get2Decimals(money);	

	return get2Decimals(money) + moneySymbol;
}

//---------------------------------------------------------------------------
// Get a string representing a sum of money
function get2Decimals(money)
{
	var dollars = (money > 0) ? Math.floor(money) : Math.ceil(money);
	var cents = Math.floor((money - dollars) * 100);
	
	return dollars + decimalSymbol + padRightZero(cents, 2);	
}

//---------------------------------------------------------------------------
// Write money symbol based on language and position
function writeMoneySymbol(before)
{
	if (english && before)
		document.write(moneySymbol);
	else if (!english && !before)
		document.write(moneySymbol);	
}

//---------------------------------------------------------------------------
// Select a radio button
function selectRadio(buttons, value)
{
	var i;
	var found = false;
	for (i=0; i < buttons.length; i++)
	{
		if (buttons[i].value == value)
		{
			found = true;
			buttons[i].checked = true;
		}
	}
	
	if (!found)
		selectFirstRadioOption(buttons);
}

//---------------------------------------------------------------------------
// Get a radio button's current value.  Returns undefined if not found
function getRadioValue(buttons)
{
	var i;
	var found = false;
	for (i=0; i < buttons.length; i++)
	{
		if (buttons[i].checked)
			return buttons[i].value;
	}

	if (buttons.length == undefined && buttons.checked)
			return buttons.value;		

	return undefined;
}

//---------------------------------------------------------------------------
// select the first radio button
function selectFirstRadioOption(buttons)
{
	if (buttons.length > 0)
		buttons[0].checked = true;
	else
		buttons.checked =  true;
}

//---------------------------------------------------------------------------
// Switch method to write plural strings
function writePlural(count, sing, plur, none)
{
	var num = new Number(count);
	if (num > 1)
		document.write(count + " " + plur);
	else if (num == 1)
		document.write(count + " " + sing);
	else
		document.write(none + " " + (english ? plur : sing));
}

//---------------------------------------------------------------------------
// Parse a YYYY-MM-DD string and return an array. If dateStr is empty, returns today
function parseDateString(dateString)
{
  if (dateString == "" || dateString == "null")
	{
		var d = new Date();		
		return new Array(d.getYear(), d.getMonth() + 1, d.getDate());
	}

  var myArray = new Array(3);
	var idx = 0;
	var lastIdx = 0;

	for (i=0; i < dateString.length; i++)
	{	

		switch (dateString.charAt(i))
		{
			case '-':
			case ' ':
			case ':':
				myArray[idx] = new Number(dateString.substring(lastIdx, i));
				idx++;
				lastIdx = i + 1;
				break;
		}
	}

	myArray[idx] = new Number(dateString.substring(lastIdx));
	return myArray;
}

//---------------------------------------------------------------------------
// Write a date string
function writeDateStr(dateString)
{
 	if (dateString == "null")
 	{
 		if (english)
 			document.write("Not specified");
 		else
		  document.write("Non sp&eacute;cifi&eacute;e");
  }
 	else
		writeDateByArr(parseDateString(dateString), true);
}

//---------------------------------------------------------------------------
// Write a date string
function writeShortDateStr(dateString)
{
 	if (dateString == "null")
 	{
 		if (english)
 			document.write("Not specified");
 		else
		  document.write("Non sp&eacute;cifi&eacute;e");
  }
 	else
		writeShortDateByArr(parseDateString(dateString), true);
}

//---------------------------------------------------------------------------
// Write a date string
function writeDateStrNoTime(dateString)
{
 	if (dateString == "null")
 	{
 		if (english)
 			document.write("Not specified");
 		else
		  document.write("Non sp&eacute;cifi&eacute;e");
  }
 	else
		writeDateByArr(parseDateString(dateString), false);
}

//---------------------------------------------------------------------------
// Writes a date in current language format
function writeDate(year, month, day)
{
	writeDateByArr( new Array(year, month, day), true);
}

//---------------------------------------------------------------------------
// Writes a date in current language format
function writeDateByArr(arr, writeTime)
{
	var year = arr[0];
	var month = arr[1];
	var day = arr[2];
	
  var d = new Date(year, month - 1, day);

  var dow = d.getDay();
  var date = "";
  var sDay = new String(day);

  if (english)
  {
	  date = arrDays[dow]  + ", " + arrMonths[month] + " " + sDay;

    if (sDay.substr(sDay.length - 1) == "1" && day != 11)
		  date = date + "st";
	  else if (sDay.substr(sDay.length - 1) == "2" && day != 12)
		  date = date + "nd";
    else if (sDay.substr(sDay.length - 1) == "3" && day != 13)
		  date = date + "rd";
	  else
		  date = date + "th";
	  
	  date = date  + ", "  + year;

  }
  else
  {
	  date = arrDays[dow] + " " + day;
	  if (day == 1)
	  	date = date + "er";
	  	
	  date = date + " " + arrMonths[month] + " " + year;	  
  }
  
  if (arr.length > 3 && writeTime)
  {
  	if (english)
  	{
  		if (arr[3] > 12)
	  		date = date + " at " + padLeftZero(arr[3] - 12, 2) + "h" + padLeftZero(arr[4], 2) + " PM";
	  	else
		  	date = date + " at " + padLeftZero(arr[3], 2) + "h" + padLeftZero(arr[4], 2) + " AM";
 		}
  	else
  		date = date + " &agrave; " + padLeftZero(arr[3], 2) + ":" + padLeftZero(arr[4], 2);

  }

  document.write(date);
}

//---------------------------------------------------------------------------
// Writes a date in current language format
function writeShortDateByArr(arr, writeTime)
{
	var year = arr[0];
	var month = arr[1];
	var day = arr[2];

  var date = padLeftZero(year, 4) + "-" + padLeftZero(month, 2) + "-" + padLeftZero(day, 2);
  
  if (arr.length > 3 && writeTime)
  {
  	if (english)
  	{
  		if (arr[3] > 12)
	  		date = date + ", " + padLeftZero(arr[3] - 12, 2) + "h" + padLeftZero(arr[4], 2) + " PM";
	  	else
		  	date = date + ", " + padLeftZero(arr[3], 2) + "h" + padLeftZero(arr[4], 2) + " AM";
 		}
  	else
  		date = date + ", " + padLeftZero(arr[3], 2) + ":" + padLeftZero(arr[4], 2);

  }

  document.write(date);
}

//----------------------------------
// Pad left with zeroes
function padLeftZero(str, max)
{
	str = new String(str);

	while (str.length < max)
		str = "0" + str;
		
	return str;
}

//-----------------------------------
// Pad right with zeroes
function padRightZero(str, max)
{
	str = new String(str);

	while (str.length < max)
		str = str + "0";
		
	return str;
}

//---------------------------------------------------------------------------
function selectOptionById(selectId, optionValue)
{
	document.getElementById(selectId).value = optionValue;
}

//---------------------------------------------------------------------------
// Select an option from an select object based on their value
function selectOption(opts, opVal)
{
	if (opts.length > 0)
	{

		for (i=0; i < opts.length; i++)
		{
			if (opts(i).value == opVal)
			{
				opts(i).selected = true;
				break;
			}
		}
	}
}

//---------------------------------------------------------------------------
// Write a page link (used by write pages)
function writePageLink(hRefLink, hRefClass, idx, txt)
{
	document.write("<a href=\"" + hRefLink + idx + "\"");

	if (typeof hRefClass != "undefined")
		document.write(" class=\"" + hRefClass + "\">" + txt + "</a>&nbsp");
	else
		document.write(">" + txt + "</a>&nbsp");
}

//---------------------------------------------------------------------------
// Write a 'pages' control
function writePages(curPage, maxPages, hRefLink, hRefClass)
{
	if (maxPages == 1)
		return;

	var iStart = Math.max(1, curPage - 5);
	var iEnd = Math.min(maxPages, curPage + 5);

	if (curPage > 1)
		writePageLink(hRefLink, hRefClass, (curPage - 1), "&laquo;");

	if (iStart > 1)
		document.write("...&nbsp;");

	for (i=iStart; i <=iEnd; i++)
	{
		if (i == curPage)
			writePageLink(hRefLink, hRefClass, i, "[" + i + "]");
		else
			writePageLink(hRefLink, hRefClass, i, i);
	}

	if (iEnd < maxPages)
		document.write("...&nbsp;");

	if (curPage < maxPages)
		writePageLink(hRefLink, hRefClass, (curPage + 1), "&raquo;");
}

//---------------------------------------------------------------------------
// Write a date control
function writeDateCtrl(dateString, ctrlName, formName)
{
	myArray = parseDateString(dateString);
	writeDateWebCtrl(new Number(myArray[0]), new Number(myArray[1]), new Number(myArray[2]), ctrlName, formName);
}

//---------------------------------------------------------------------------
// Write a date control
function writeDateTimeCtrl(dateString, ctrlName, formName)
{

	myArray = parseDateString(dateString);

	if (myArray.length == 3)
	{
		var d = new Date();		
		myArray.push(d.getHours());
		myArray.push(d.getMinutes());
	}

	writeDateTimeWebCtrl(myArray[0], myArray[1], myArray[2], myArray[3], myArray[4], ctrlName, formName);
}

//---------------------------------------------------------------------------
// Write a date control
function writeBaseDateWebCtrl(year, month, day, ctrlName, formName, methodName)
{
	var d = new Date();
	writeNumericSelect(year, Math.min(d.getYear() - 10, year - 5), Math.max(d.getYear() + 10, year + 5), ctrlName + "_year", methodName + "('" + ctrlName + "', '" + formName + "');");
	writeNumericSelect(month, (month == 0 ? 0 : 1), 12, ctrlName + "_month", methodName + "('" + ctrlName + "', '" + formName + "');");
	writeNumericSelect(day, (day == 0 ? 0 : 1), 31, ctrlName + "_day", methodName + "('" + ctrlName + "', '" + formName + "');");
}

//---------------------------------------------------------------------------
// Write a date control
function writeDateWebCtrl(year, month, day, ctrlName, formName)
{
	writeBaseDateWebCtrl(year, month, day, ctrlName, formName, "dateElementChanged");
	document.write('<input type="hidden" name="' + ctrlName + '" value="' + year + '-' + month + '-' + day + '">');
}

//---------------------------------------------------------------------------
// Write a date control
function writeDateTimeWebCtrl(year, month, day, hour, minute, ctrlName, formName)
{
	writeBaseDateWebCtrl(year, month, day, ctrlName, formName, "dateTimeElementChanged");

	if (english)
		document.write('&nbsp;at&nbsp;');
	else
		document.write('&nbsp;&agrave;&nbsp;');
		
	writeNumericSelect(hour, 0, 23, ctrlName + "_hour", "dateTimeElementChanged('" + ctrlName + "', '" + formName + "');");
	
	if (english)
		document.write('&nbsp;h&nbsp;');
	else
		document.write('&nbsp;:&nbsp;');
	
	writeNumericSelect(minute, 0, 59, ctrlName + "_minute", "dateTimeElementChanged('" + ctrlName + "', '" + formName + "');");
	document.write('<input type="hidden" name="' + ctrlName + '" value="' + year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':00.0">');
}

//---------------------------------------------------------------------------
// Called when an element of a date control has changed
function dateElementChanged(ctrlName, formName)
{
	var oVal = eval('document.forms[formName].' + ctrlName);
	oVal.value = 
		eval('document.forms[formName].' + ctrlName + '_year.value') + '-' +
		eval('document.forms[formName].' + ctrlName + '_month.value') + '-' +
		eval('document.forms[formName].' + ctrlName + '_day.value');
}

//---------------------------------------------------------------------------
// Called when an element of a date control has changed
function dateTimeElementChanged(ctrlName, formName)
{
	var oVal = eval('document.forms[formName].' + ctrlName);
	oVal.value = 
		eval('document.forms[formName].' + ctrlName + '_year.value') + '-' +
		eval('document.forms[formName].' + ctrlName + '_month.value') + '-' +
		eval('document.forms[formName].' + ctrlName + '_day.value') + ' ' + 
		eval('document.forms[formName].' + ctrlName + '_hour.value') + ':' + 
		eval('document.forms[formName].' + ctrlName + '_minute.value') + ":00.0";
}

//---------------------------------------------------------------------------
// Write a select that work with numbers only
function writeNumericSelect(select, start, end, ctrlName, onChange)
{
	document.write('<select name="' + ctrlName + '"' + (typeof onChange == "undefined" ? "" : 'onChange="' + onChange + '" ') + '>');
	for (i = start; i <= end; i++)
	{
		document.write('<option value="' + i + '"' + (i == select ? ' SELECTED >' : '>') + i)	;
	}
	document.write('</select>');
}

//---------------------------------------------------------------------------
// Focus on the first visible form item (if any)
function focusFirstItem()
{
	for (i=0; i < document.forms.length; i++)
	{
		myForm = document.forms[i];
			
		for (j=0; j < myForm.elements.length; j++)
		{
			el = myForm.elements[j];					

			if ((el.type != "hidden") && (el.style.display != "none") && !el.readOnly)
			{
				el.focus();
				return;
			}
		}
	}
}


//-----------------------------------------------
function initSelect(elementId, callMe)
{
	var theSelect = document.getElementById(elementId);	
	theSelect.changed 	= false;
	theSelect.onfocus 	= selectFocussed;
	theSelect.onchange 	= selectChanged;
	theSelect.onkeydown = selectKeyed;
	theSelect.onclick 	= selectClicked;
	theSelect.onblur  	= selectChanged;	
	theSelect.callMe    = callMe;
}

//-----------------------------------------------
function selectChanged(theElement)
{
	var theSelect;	
	if (theElement && theElement.value)
	{
		theSelect = theElement;
	}
	else
	{
		theSelect = this;
	}
	
	if (!theSelect.changed)
	{
		return false;
	}

	eval(theSelect.callMe);
	
	return true;
}

//-----------------------------------------------
function selectClicked()
{
	this.changed = true;
}

//-----------------------------------------------
function selectFocussed()
{
	this.initValue = this.value;
	
	return true;
}

//-----------------------------------------------
function selectKeyed(e)
{
	var theEvent;
	var keyCodeTab = "9";
	var keyCodeEnter = "13";
	var keyCodeEsc = "27";
	
	if (e)
	{
		theEvent = e;
	}
	else
	{
		theEvent = event;
	}

	if ((theEvent.keyCode == keyCodeEnter || theEvent.keyCode == keyCodeTab) && this.value != this.initValue)
	{
		this.changed = true;
		selectChanged(this);
	}
	else if (theEvent.keyCode == keyCodeEsc)
	{
		this.value = this.initValue;
	}
	else
	{
		this.changed = false;
	}
	
	return true;
}

var myMultiOp = null;

function writeMultiOperations(aText, aLink)
{
	if (aText.length == 0)
		return;
		
	if (aText.length != aLink.length)
	{
		alert('writeMultiOperations array length mismatch');
		return;
	}
	
	document.write('<table cellspacing="0" cellpadding="0" class="multiop"><tr><td>');
	
	document.write('<div class="default">');
	
	//expand		
	document.write('<a href="' + aLink[0] + '" onmouseover="popOperation(this, 250);" onmouseout="popOutOperation(this);" '+
	' style="background-image:url(images/submenu_bullet.gif);background-position:right center;background-repeat:no-repeat;padding-right:14px;"' +
	'>' + aText[0] + '</a>');

	// popup		
	document.write('<div class="popup" onmouseover="popOperation(this);" onmouseout="popOutOperation(this);">');		
	for (i=0; i < aText.length; i++)
		document.write('<a style="display:block;" href="' + aLink[i] + '" onmouseover="popOperation(this);">' + aText[i] + '</a>');			
	document.write('</div>');		
		
	document.write('</div>');	
	
	document.write('</td></tr></table>');
}

function popOutOperation(el)
{
	if (el.className != "popup")
		el = domFindFirstChildElement(el.parentNode.parentNode, "div", "popup", true);
		
	if (el.showTimeOut)
	{
		clearTimeout(el.showTimeOut);
		el.showTimeOut = null;
	}	
			
	el.clearTimeOut = setTimeout( function() 
	{ 			
		el.style.display = 'none'; 			
	}, 5 );
}

function offsetToParent(el)
{
	var x = -4;
	var y = -4;
	var p = el.parentNode;
	
	while (p)
	{
		var a = p.nodeName.toUpperCase();
		if (a != 'BODY' && a != 'HTML' && p.id != 'body') 
		{
			x = x + p.offsetLeft;
			y = y + p.offsetTop;
		}
		
		p = p.offsetParent;		
	}
	
//	alert(x + ' ' + y);
	
	el.style.position = 'absolute';
	el.style.top = y + 'px';
	el.style.left = x + 'px';
}

function popOperation(el, delay)
{
	var popup = domFindFirstChildElement(el.parentNode.parentNode, "div", "popup", true);
	if (popup)
	{
		if (myMultiOp != popup)
		{
			if (myMultiOp)
			{
				myMultiOp.style.display = 'none';
				
				if (myMultiOp.showTimeOut)
				{
					clearTimeout(myMultiOp.showTimeOut);
					myMultiOp.showTimeOut = null;
				}
			}
				
			myMultiOp = popup;
		}	
			
		if (popup.clearTimeOut)
		{
			clearTimeout(popup.clearTimeOut);
			popup.clearTimeOut = null;
		}
			
	//	var tmp = el.position;
	//		el.position = 'absolute';
		//	var msg = el.offsetLeft + ' ' + el.offsetTop;
	//		el.position = tmp;
			//alert(msg);
			

			
		popup.showTimeOut = setTimeout(function()
			{
				offsetToParent(popup);
				popup.style.display = "block";
				
			}, 
			delay);
	}	
}


//-------------------------------------
// Find first child element by tag name
//-------------------------------------
function domFindFirstChildElement(el, tagName, className, recurse)
{
	if (!el.childNodes)
		return null;
		
	tagName = tagName.toUpperCase();
		
	for (var i=0; i < el.childNodes.length; i++)		
	{
		var node = el.childNodes.item(i);
		if (typeof node.tagName != "undefined")
		{
			if (node.tagName  == tagName && (!className || node.className == className))
				return node;
		}	
		
		if (recurse)
		{
			var r = domFindFirstChildElement(node, tagName, className, recurse);
			if (r)
				return r;
		}	
	}
	
	return null;
}

//---------------------------------
function convertTooltips(el)
{
	if (el == null)
		el = ietruebody();	// tooltip helper method
	
	for (var i=0; i < el.childNodes.length; i++)		
	{
		var node = el.childNodes.item(i);
		if (typeof node.tagName != "undefined")
		{
			// Set tooltip
			if (node.title && !node.title.tagName)
			{
				domSetTooltip(node, node.title);
				node.title = "";
			}
			
			// Fix "a" tags?
			if (node.tagName.toLowerCase()  == "a")
			{
				if (node.href == "#" && !node.onclick)
					node.onclick = function() { return false;}				
			}
			
			// Recurse
			if (node.childNodes)
				convertTooltips(node);
		}
	}			
}

//-------------------------------------
function domSetTooltip(ctl, tipText)
{
	if (tipText)
	{
		domSetMember(ctl, 'tipText', tipText);		
		
		if (typeof tipText == 'function')
		{
			ctl.onmouseover = function() { 
				ddrivetip(this.tipText(this));
			};
		}
		else
		{
			
			ctl.onmouseover = function() { 
				ddrivetip(this.tipText);
			};
		}
		
		ctl.onmouseout = hideddrivetip;
	}
	else
	{
		ctl.onmouseover = null;
		ctl.onmouseout = null;
	}
}

//-------------------------------------
// Recurse through DOM, set owner to all children
//-------------------------------------
function domSetMember(el, member, value)
{
	if (typeof el.tagName != "undefined" && !el[member])
		el[member] = value;

	if (!el.childNodes)
		return;
		
	for (var i=0; i < el.childNodes.length; i++)		
		domSetMember(el.childNodes.item(i), member, value);
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function albionURLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	
	return encoded;
};

function albionURLDecode(encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   
   if (!encoded)
		encoded = "";
   	
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};

//==========================================
function startsWith(haystack, needle)
{
	return haystack.indexOf(needle) == 0;
}


//-------------------------------------
// Safe toString
//-------------------------------------
function nsToString(o)
{
	if (o == null)
		return null;
		
	if (o == undefined)
		return undefied;
		
	if (o.toString)
	return o.toString();
		
	return o.xml;
}

//=====================================================
// Controls

//-------------------------------------
// Remove all options from select control
//-------------------------------------
function ctlSelectClearOptions(ctl)
{
	while (ctl.options.length > 0)
		ctl.remove(0);
}

//-------------------------------------
// Add an option to a select control
//-------------------------------------
function ctlSelectAddOption(ctl, optionText, optionValue, optionSelected)
{
	var option = document.createElement('option');
	
	option.text = optionText;
	option.value = optionValue;	
			
	if (optionValue)
		option.selected = true;
			
	try
	{
		ctl.add(option, null); // standards compliant
	}
	catch(ex)
	{
		ctl.add(option); // IE only
	}	
}

//-------------------------------------
// Add an option to a select control
// 	Read information from an XML 'option' element
//
// Returns 'true' if the item was marked as selected
//-------------------------------------
function ctlSelectAddOptionXML(ctl, xmlElement, preventSelect)
{
	var text = "";
	var selected = false;
	
	if (xmlElement.firstChild)
		text = xmlElement.firstChild.nodeValue;	
			
	if (!preventSelect && xmlElement.getAttribute('selected'))
		selected = true;
			
	ctlSelectAddOption(ctl, text, xmlElement.getAttribute('value'), selected);
	
	return selected;
}

//-------------------------------------
// Select an option by value
//-------------------------------------
function ctlSelectSetValue(ctl, value)
{
	for (i=0; i < ctl.options.length; i++)
	{
		if (ctl.options[i].value == value)
		{
			ctl.options[i].selected = true;
			break;
		}
	}
}

//-------------------------------------
// Hide or show a div control with animation.
//	If ctl is not div, we'll look for a div within
//	and do the animation on it
//-------------------------------------
function ctlSetDivVisible(a, show, animspeed)
{	
	if (!a)
		return;
	
	if (ctlIsVisible(a) == show)
		return;
	
	if (a.nodeName != 'DIV' && a.nodeName != 'div')
	{
		// looking for first div...
		var divs = a.getElementsByTagName('div');
		if (divs.length > 0)
		{
			ctlSetVisible(divs[0], !show, false);
			ctlSetVisible(a, true, false);
			
			var myctl = a;
			var myshow = show;			
			var callback = function() { ctlSetVisible(myctl, myshow, false); };
			jqSetVisible(divs[0], show, undefined, callback);
			
		}
		else
		{
			ctlSetVisible(a, show, false);
		}
	}
	else
	{
		jqSetVisible(a, show);
	}
}

//-------------------------------------
function ctlIsVisible(ctl)
{
	var visible = $(ctl).is(':visible');
	
	// alert(ctl.innerHTML + ' ' + visible);
	
	return visible;
}

//-------------------------------------
// Hide or show a control using jQuery
//-------------------------------------
function jqSetVisible(ctl, show, animspeed, callback)
{		
//	if (ctlIsVisible(ctl) == show)
//		return $(ctl);
	
	if (!animspeed)
		animspeed = "fast";
	
	if (show)
	{
		return $(ctl).show(animspeed, callback);
	}
	else
	{
		return $(ctl).hide(animspeed, callback);
	}
}

//-------------------------------------
// Hide or show a control
//-------------------------------------
function ctlSetVisible(ctl, show, animated, animspeed, callback)
{
	if (animated)
	{
		return jqSetVisible(ctl, show, animspeed);
	}
	
	if (show)
	{
		//ctl.style.display = '';
		return $(ctl).show();
	}	
	else
	{
		//ctl.style.display = 'none';
		return $(ctl).hide();
	}	
}

//-------------------------------------
// Find an element in an array
//-------------------------------------
function arrayContains(haystack, needle)
{
	if (!haystack)
		return false;
	
	for (var i = 0; i < haystack.length; i++)
	{
		if (haystack[i] == needle)
			return true;
	}
	
	return false;
}

function disableCtl(ctl, disabled)
{
	ctl.disabled = disabled;
	var classes = new String(ctl.className).split(" ");
	var found = false;
	
	for (var i=0; i < classes.length; i++)
	{
		if (classes[i] == 'ctlDisabled')
		{
			if (!disabled)
				classes[i] = '';
			found = true;
			break;
		}
	}
	
	if (disabled && !found)
		classes.push('ctlDisabled');
	
	ctl.className = classes.join(' '); 
}

// adds the 'button' class to all input of type 'button'
$(document).ready(function(){
	   $("input[type=button]").addClass('button');
	   $("input[type=submit]").addClass('button'); 
});

function size_format (filesize) {
	if (filesize >= 1073741824) {
	     filesize = number_format(filesize / 1073741824, 2, '.', '') + ' Gb';
	} else { 
		if (filesize >= 1048576) {
     		filesize = number_format(filesize / 1048576, 2, '.', '') + ' Mb';
   	} else { 
			if (filesize >= 1024) {
    		filesize = number_format(filesize / 1024, 0) + ' Kb';
  		} else {
    		filesize = number_format(filesize, 0) + ' bytes';
			};
 		};
	};
  return filesize;
};

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}