document.observe("dom:loaded", init);

/*Params*/
var initFunctions = new Array();

initFunctions.push(setUpExternalLinks);
initFunctions.push(setUpPrint);

// lightbox config - see lightbox.js for details
window.LightboxOptions = {
    fileLoadingImage:        '/gen/img/lightbox/loading.gif',     
    fileBottomNavCloseImage: '/gen/img/lightbox/closelabel.gif',
    overlayOpacity: 0.8,
    animate: true,
    resizeSpeed: 7,
    borderSize: 10,
	labelImage: "Image",
	labelOf: "of"
};

function init() {
	for(var i=0;i<initFunctions.length;i++)
		initFunctions[i].call(this);
		
	//Close window button for popups
	if($("close-window"))
		Event.observe("close-window","click",closeWindow);
}


/*Useful common functions*/
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    strParamName = strParamName.toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
function trimString (str) {
  return str.replace(/^\s+|\s+$/g,"");
}

function isDefined(v) {
	return (typeof(window[v]) == "undefined")?  false: true;
}

function IsNumeric(sText,decimalOK,spacesOK,hyphensOK,plusesOK) {
   var ValidChars = "0123456789";
   if(decimalOK) ValidChars = ValidChars + ".";
   if(spacesOK) ValidChars = ValidChars + " ";
   if(hyphensOK) ValidChars = ValidChars + "-";
   if(plusesOK) ValidChars = ValidChars + "+";
   var IsNumber=true;
   var Char;
   if(sText.length == 0)
   	  return false;
   for (var i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
      }
   return IsNumber;
}

/*Form manipulation*/
function toggleCheck(chkBox) {
	chkBoxObj = $(chkBox);
	if(chkBoxObj.checked)
		chkBoxObj.checked = false;	
	else
		chkBoxObj.checked = true;	
}

function activateRadio(radio) {
	radioObj = $(radio);
	radioObj.checked = true;	
}

function roundValue( price ) {
	return Math.round( price * 100 ) / 100;
}

function formatCurrency(price) {
	var result = "";

	// Write price to temp variable
	var tmpPrice = price;
	// Get absolute value of price to format
	price = Math.abs( price );
	
	// Get the decimals from the price and add a '0' in front if needed
	var decimals = ( Math.round( ( price - Math.floor( price ) ) * 100 ) ).toString();
	if (decimals >= 100) {
		decimals = "00";
		price = price + 1;
	}
	if( decimals.length == 1 ) decimals = "0" + decimals;

	// Get the thousands from the price
	var thousands = Math.floor( price / 1000 );
	
	// If we have any add them to the formatted string with a coma
	if( thousands > 0 ) {
		price -= thousands * 1000;
		result = thousands.toString() + ",";
		units = Math.floor( price );
		if( units < 10 ) result = result + "00" + units.toString();
		else if( units < 100 ) result = result + "0" + units.toString();
		else result = result + units.toString();
	} else result = Math.floor( price ).toString();
	
	// Add the rest of the price to the string
	result = result + "." + decimals;
	
	// Add a minus sign if price is negative
	if( tmpPrice < 0 ) result = "-" + result;
	
	return result;
}

function setUpExternalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && /\bexternal\b/.test(anchor.getAttribute("rel"))) {
     anchor.target = "_blank";
     if(anchor.getAttribute("title")){
     	if(anchor.title.indexOf("(opens in a new window)") < 0) anchor.title = anchor.title + ' (opens in a new window)';
     }
   }
 }
}

function setUpPrint() {
	var arPrints=$$("a.print-page");
	for(var i=0;i<arPrints.length;i++)
		Event.observe(arPrints[i],"click",function(){window.print();return false;});
}

function createUUID() {
	var s = [], itoh = '0123456789ABCDEF';
	for (var i = 0; i <36; i++)
		s[i] = Math.floor(Math.random()*0x10);
	s[14] = 4;
	s[19] = (s[19] & 0x3) | 0x8;
	for (var i = 0; i <36; i++)
		s[i] = itoh[s[i]];
	s[8] = s[13] = s[18] = s[23] = '-';
	return s.join('');
}

function getXmlNodeValue(xmlNode){
	if(typeof(xmlNode.textContent) == 'string')
		return xmlNode.textContent;
	else
		return xmlNode.firstChild.nodeValue;
}

function listFind(list,itemToFind) {
	var aList = list.split(',');
	for(var i = 0; i < aList.length; ++i) {
		if(aList[i] == itemToFind)
		return i;
	}
	return -1;
}

try
{
	//prevent errors from missing window.console methods
	if (!window.console||typeof(window.console)!='object')
	{
	    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
	    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	
	    window.console = {};
	    for (var i = 0; i < names.length; ++i)
	        window.console[names[i]] = function() {}
	}
	//disable debugging
	if(typeof CONSOLELOGGING != "undefined" && !CONSOLELOGGING)
		window.console.log = function(s){};
}
catch(e){}

