// overload the Array class with pop and push, in case we are using an older browser
Array.prototype.pop = pop;
Array.prototype.push = push;

function push() {
// PFS Aug 2004
  var sub = this.length;
  for (var i = 0; i < push.arguments.length; ++i) {
    this[sub] = push.arguments[i];
	sub++;
  }
}

function pop() {
// PFS Aug 2004
  var lastElement = this[this.length - 1];
  this.length--;
  return lastElement;
}

function getDOMElement (sID) {

// Cross-browser version of GetElementById
// PFS Aug 2004

var oElement;

     //figure out what capabilities the browser we are dealing with has
     bUseLayers = (document.layers) ? true : false;
     bUseDocumentAll = (document.all && !document.getElementById) ? true : false;
     bUseGetElementByID = (document.getElementById) ? true : false;

     //identify the element based on browser type
     if (bUseLayers){
        oElement = document.layers[sID];
     } else if (bUseDocumentAll) {
        oElement = document.all[sID];
     } else if (bUseGetElementByID) {
        oElement = document.getElementById(sID);
     }

     return oElement;
}

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas) {
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/

    if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
			
	// PFS Aug 2004 
	// See if we need to pad the decimal
	var iStart = tmpNumStr.indexOf(".");
	var sDecimals = tmpNumStr.substring(iStart + 1);
	if (iStart == -1) // whole number
		tmpNumStr += ".00"
	else if (sDecimals.length < decimalNum) {
		while (sDecimals.length < decimalNum) {
			tmpNumStr += "0";
			sDecimals = tmpNumStr.substring(iStart + 1);
		}
	}
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

var maxLength = 160 


function count(str) { 
	var count = 0; 
	for (i=0;i<str.length;i++) { 
		val = str.charCodeAt(i); 
		if (val >31) count++; 
	} 
	return count; 
} 

function chargeableCount(str) { 
	var chargeableCount = 0;
	for (i=0;i<str.length;i++) { 
		val = str.charCodeAt(i); 
		if (val >32) chargeableCount++; 
	} 
	return chargeableCount; 
} 

function find(str,num) { 
	var count = 0; 
	var i=0; 
	while (count < num) { 
		val = str.charCodeAt(i); 
		if (val >31) count++; 
		i++; 
	} 
	return i; 
} 


function initialiseLetterChooser (sTheme, sColour) {
// PFS Aug 2004
// Set the chooser images up in the appropriate theme and colour

var sThisLetterLCase;
var sThisLetterUCase;
var sElementPrefix = "chooser_"
var sImagePrefix = "images/chooser/";

	for (var iUcase=ASCII_A, iLcase=ASCII_a; iUcase <= ASCII_Z; iUcase++, iLcase++) {
		sThisLetterLCase = String.fromCharCode(iLcase);
		sThisLetterUCase = String.fromCharCode(iUcase);
		oChooserImage = getDOMElement(sElementPrefix + sThisLetterUCase + sThisLetterUCase);	// Get the UCase letter, identified with double letter suffix. e.g. _AA = A
		oChooserImage.src = sImagePrefix + sTheme + "_" + sColour + "_" + sThisLetterLCase + sThisLetterLCase + ".gif";	// Set the upper case chooser image in format prefix_theme_colour_letterletter.gif
		oChooserImage = getDOMElement(sElementPrefix + sThisLetterUCase);	// Get the LCase letter, identified with single letter suffix. e.g. _A = A
		oChooserImage.src = sImagePrefix + sTheme + "_" + sColour + "_" + sThisLetterLCase + ".gif";		// Set the lower case chooser image in format prefix_theme_colour_letter.gif
	}
	
	// Deal with the special case of the space...
	oChooserImage = getDOMElement(sElementPrefix + "space");
	oChooserImage.src = sImagePrefix + sTheme + "_" + sColour + "_space.gif";
	
	displayRunningTotal();
}

function changeValues(text, textlgth) { 
	cutoff = find(text,maxLength) 
	document.main.PersonalMessage.value=text.substring(0,cutoff); 
	text=document.main.PersonalMessage.value; 
	textlgth = text.length; 
} 

function doChange() { 
	//var nbr, chr;
	//if (window.Event) nbr = evt.which;
	//else nbr = event.keyCode;
					
	text = document.main.PersonalMessage.value; 
	textlgth = count(text); 

	if (textlgth > maxLength)  
		changeValues(text, textlgth); 
		
	displayRunningTotal();

  return true; 
} 

function displayRunningTotal() {
// PFS Aug 2004

	// Show the running total for the item
	var oItemTotal = getDOMElement("itemTotal");	
	oItemTotal.innerHTML = FormatNumber(getGrandTotal(),2,true,false,false);
}