var gUtilHostPages = "/CC_host/pages/";

// debugging routine to convert object and arrays to strings
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


function waitCursor(bTF)
{
	var gpObj = document.getElementById("glassPane");
	if (gpObj==null) return;
	gpObj.style.display = (bTF)?'':'none';
	gpObj.style.cursor = '';
	gpObj.style.cursor = 'wait';
	if (document.all) {
		// ie - we don't need the background image
		gpObj.style.backgroundImage = "";
	} else {
		// not ie - we don't need the filter
		gpObj.style.backgroundColor = "";
	}
	if (bTF) {
		window.status = "please wait...";
	} else {
		window.status = "";
	}

	var bDisableFormElements=true;
	if (arguments.length > 1) {
		bDisableFormElements = arguments[1];
	}
	
	// loop through the form to take care of selects.
	if (document.all && bDisableFormElements) { // ie only
		for (fi=0; fi<document.forms.length; fi++) {
			for (i=0; i<document.forms[fi].elements.length; i++) {
				if (document.forms[fi].elements[i].type == 'select-one' 
				|| document.forms[fi].elements[i].type == 'select-multiple') { 
					document.forms[fi].elements[i].disabled = bTF;
					document.forms[fi].elements[i].style.cursor = bTF?'wait':'';
				}
			}
		}
	}
}

function showHttpWindow( windowURL, windowName /* features optional */) { 
	var sFeatures = 'width=400,height=300,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1';
	
	if (arguments.length > 2) {
		sFeatures = arguments[2];
	}
	
	var newWindow = window.open( windowURL, windowName, sFeatures ) ; 
	return newWindow;
} 

var oPopup;
function closeHelp()
{
	if ("undefined" != typeof window.createPopup && oPopup!=null) {
		oPopup.hide();
	} else if (null != document.getElementById('popupmessage')) {
		document.getElementById('popupmessage').style.display='none';
	}
}


function displayHelp(xp, yp, title, reason, sx, sy)
{
	if ("undefined" != typeof window.createPopup) {
		oPopup = window.createPopup();
		oPopupBody = oPopup.document.body;
		oPopupBody.innerHTML = '<div style="border: 1px outset Gray;" onclick="parent.oPopup.hide();"><table bgcolor="#ffffcc" cellpadding="2"><tr><td width="'+sx+'" style="font-size: 10pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold;">'+title+'</td><td align="right"><img src="'+gUtilHostPages+'images/box_x.GIF" alt="" width="12" height="13" border="0"></td></tr><tr><td colspan="2" width="'+sx+'" style="font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif;">'+reason+'</td></tr></table></div>';
		oPopup.show(0, 0, sx+2, 0);
		var realHeight = oPopupBody.scrollHeight;
		oPopup.hide();
		inPopUp = true;
		// the popup is based on the browser window not the scroll content.
		var popx = xp - document.body.scrollLeft;
		var popy = yp - document.body.scrollTop;
		oPopup.show(popx, popy, sx+2, realHeight, document.body);
		//window.status="("+popx+", "+popy+")"
	} else if (null != document.getElementById('popupmessage')) {
		// downlevel 
		if (document.getElementById('popupmessage').style.display=="") {
			document.getElementById('popupmessage').style.display='none';
		} else {
			document.getElementById('popupmessage').innerHTML = '<div style="border: 1px outset Gray;"><table onclick="document.getElementById(\'popupmessage\').style.display=\'none\';" bgcolor="#ffffcc" cellpadding="2"><tr><td width="'+sx+'" style="font-size: 10pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold;">'+title+'</td><td align="right"><img src="images/box_x.GIF" alt="" width="12" height="13" border="0"></td></tr><tr><td colspan="2" width="'+sx+'" style="font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif;">'+reason+'</td></tr></table></div>';
			document.getElementById('popupmessage').style.top=yp+"px";
			document.getElementById('popupmessage').style.left=xp+"px";
			document.getElementById('popupmessage').style.width=sx+"px";
			document.getElementById('popupmessage').style.display='';
			//window.status="("+xp+", "+yp+")"
		}
	} else {
		alert( reason );
	}
}

function getPositionX(wobj)
{
	xpos = 0;
	while (wobj != null) {
		xpos = xpos+wobj.offsetLeft;
		wobj = wobj.offsetParent;
	}
	return xpos;
}

function getPositionY(wobj)
{
	ypos = 0;
	while (wobj != null) {
		ypos = ypos+wobj.offsetTop;
		wobj = wobj.offsetParent;
	}
	return ypos;
}


function formatCurrency(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function formatNumber(num, signifcant_digits)
{
	numstr = new String("x"+num+".00000000000");
	returnval = new String("");
	foundDec = false;
	foundNonZero = false;
	for (i=1; i<numstr.length; i++)
	{
		c = numstr.substring(i,i+1);
		if (c == ".") {
			if (!foundDec) {
				returnval = returnval+".";
			}
			foundDec = true;
		} else if (c == "0") {
		    if (foundNonZero || foundDec) returnval = returnval+"0";
		} else {
			foundNonZero = true;
			returnval = returnval + c;
		}
	}
	return returnval.substring(0,signifcant_digits+1);
}
 // CONSTANTS
 var separator = ",";  // use comma as 000's separator
 var decpoint = ".";  // use period as decimal point
 var percent = "%";
 var currency = "$";  // use dollar sign for currency

 function formatNumber2(number, format, print) {  // use: formatNumber(number, "format")
   if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

   if (number - 0 != number) return null;  // if number is NaN return null
   var useSeparator = format.indexOf(separator) != -1;  // use separators in number
   var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
   var useCurrency = format.indexOf(currency) != -1;  // use currency format
   var isNegative = (number < 0);
   number = Math.abs (number);
   if (usePercent) number *= 100;
   format = fn2_strip(format, separator + percent + currency);  // remove key characters
   number = "" + number;  // convert number input to string

    // split input value into LHS and RHS using decpoint as divider
   var dec = number.indexOf(decpoint) != -1;
   var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
   var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

    // split format string into LHS and RHS using decpoint as divider
   dec = format.indexOf(decpoint) != -1;
   var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
   var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

    // adjust decimal places by cropping or adding zeros to LHS of number
   if (srightEnd.length < nrightEnd.length) {
     var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
     nrightEnd = nrightEnd.substring(0, srightEnd.length);
     if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

// patch provided by Patti Marcoux 1999/08/06
     while (srightEnd.length > nrightEnd.length) {
       nrightEnd = "0" + nrightEnd;
     }

     if (srightEnd.length < nrightEnd.length) {
       nrightEnd = nrightEnd.substring(1);
       nleftEnd = (nleftEnd - 0) + 1;
     }
   } else {
     for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
       if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
       else break;
     }
   }

    // adjust leading zeros
   sleftEnd = fn2_strip(sleftEnd, "#");  // remove hashes from LHS of format
   while (sleftEnd.length > nleftEnd.length) {
     nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
   }

   if (useSeparator) nleftEnd = fn2_separate(nleftEnd, separator);  // add separator
   var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
   output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
   if (isNegative) {
     // patch suggested by Tom Denn 25/4/2001
     output = (useCurrency) ? "(" + output + ")" : "-" + output;
   }
   return output;
 }

 function fn2_strip(input, chars) {  // strip all characters in 'chars' from input
   var output = "";  // initialise output string
   for (var i=0; i < input.length; i++)
     if (chars.indexOf(input.charAt(i)) == -1)
       output += input.charAt(i);
   return output;
 }

 function fn2_separate(input, separator) {  // format input using 'separator' to mark 000's
   input = "" + input;
   var output = "";  // initialise output string
   for (var i=0; i < input.length; i++) {
     if (i != 0 && (input.length - i) % 3 == 0) output += separator;
     output += input.charAt(i);
   }
   return output;
 }

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	    ((expires) ? "; expires=" + expires.toGMTString() : "") +
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	  begin = dc.indexOf(prefix);
	  if (begin != 0) return null;
	} else
	  begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	  end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
	  date.setTime(date.getTime() - skew);
}

/***

Replacement functions for JScript 5.5 functions that are unavailable in previous versions of JScript


***/

function Number_toFixed (f) {
    f = parseInt(f / 1 || 0);
    if (f < 0 || f > 20) {
        alert("The number of fractional digits is out of range");
    }
    if (isNaN(this)) {
        return "NaN";
    }
    var s = this < 0 ? "-" : "", x = Math.abs(this);
    if (x > Math.pow(10, 21)) {
        return s + x.toString();
    }
    var m = Math.round(x * Math.pow(10, f)).toString();
    if (!f) {
        return s + m;
    }
    while (m.length <= f) {
        m = "0" + m;
    }
    return s + m.substring(0, m.length - f) + "." + m.substring(m.length - f);
}

if (typeof(Number.prototype.toFixed) == "undefined") {
  Number.prototype.toFixed = Number_toFixed;
}

function Array_pop() {
  var response = this[this.length - 1];
  this.length--;
  return response;
}

if (typeof(Array.prototype.pop) == "undefined") {
  Array.prototype.pop = Array_pop;
}

function Array_push() {
  var A_p = 0
  for (A_p = 0; A_p < arguments.length; A_p++) {
	this[this.length] = arguments[A_p];
  }
  return this.length;
}

if (typeof(Array.prototype.push) == "undefined") {
  Array.prototype.push = Array_push;
}

function Array_shift() {
  var A_s = 0
  var response = this[0];
  for (A_s = 0; A_s < this.length-1; A_s++) {
   this[A_s] = this[A_s + 1];
  }
  this.length--;
  return response;
}

if (typeof(Array.prototype.shift) == "undefined") {
  Array.prototype.shift = Array_shift;
}

function Array_splice(index, delTotal) {
  var temp = new Array()
  var response = new Array()
  var A_s = 0
  for (A_s = 0; A_s < index; A_s++) {
   temp[temp.length] = this[A_s]
  }
  for (A_s = 2; A_s < arguments.length; A_s++) {
   temp[temp.length] = arguments[A_s];
  }
  for (A_s = index + delTotal; A_s < this.length; A_s++) {
   temp[temp.length] = this[A_s];
  }
  for (A_s = 0; A_s < delTotal; A_s++) {
   response[A_s] = this[index + A_s];
  }
  this.length = 0
  for (A_s = 0; A_s < temp.length; A_s++) {
   this[this.length] = temp[A_s];
  }
  return response
}

if (typeof(Array.prototype.splice) == "undefined") {
  Array.prototype.splice = Array_splice
}

function Array_unshift() {
  var A_u = 0;
  for (A_u = this.length-1; A_u >= 0; A_u--) {
     this[A_u + arguments.length] = this[A_u];
  }
  for (A_u = 0; A_u < arguments.length; A_u++) {
     this[A_u] = arguments[A_u];
  }
  return this.length
}

if (typeof(Array.prototype.unshift) == "undefined") {
  Array.prototype.unshift = Array_unshift;
}

function Object_hasOwnProperty(sPropertyName) {
	var bHasProp = true;
	
	switch (typeof (this[sPropertyName])) {
	case "undefined":
		bHasProp = false;
		break;
	case "function":
		bHasProp = false;
		break
	}
	
	//Should check that this is not a prototype property
	
	return bHasProp;
}

if (typeof (Object.prototype.hasOwnProperty) == "undefined") {
  Object.prototype.hasOwnProperty = Object_hasOwnProperty;
}
/**
	End of replacement for unavailable builtin function
**/
