/**
* Pops a window up
*/
function showWindow(uniqueid,name,url,width,height) {
  var windowObj;
  windowObj = null;
  width=screen.width;
  height=screen.height;
  windowObj = top.open(url,uniqueid,'toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,left=0,top=0');
}

/**
* Formats a given number to a currency format.
*/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + num + '.' + cents);
}

/*
* Function for adding body onload functions
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/*
* Function for adding body onunload functions
*/
function addUnLoadEvent(func) {
  var oldunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      if (oldunload) {
        oldunload();
      }
      func();
    }
  }
}

/*
* Structure to hold prices and blocks.
*/
function PriceBlockLevel(pricecode, blockcode, pricedescription, blockdescription)
{ this.pricecode = pricecode;
  this.blockcode = blockcode;
  this.pricedescription = pricedescription;
  this.blockdescription = blockdescription;
}

/*
* Structure to hold prices and blocks.
*/
function Concession(code, maxtickets, price)
{ this.code = code;
  this.maxtickets = maxtickets;
  this.price = price;
}

/*
* Prints the prices in a selectbox (elemId). When a priceLevelCode
* is given only blocks for the given priceLevelCode are printed.
*/
function printSelectBlocks() {
  el = document.getElementById('blocks');
  // See if their is an pricecode selected
  priceEl = document.getElementById('prices');
  var priceLevelCode;
  if (null != priceEl && undefined != priceEl) {
	if (priceEl.selectedIndex != -1) {
    	priceLevelCode = priceEl.options[priceEl.selectedIndex].value;
    }
  }

  // See which blockcode option is currently selected
  var selectedBlockCode;
  // -1 indicates nothing is selected
  if (el.selectedIndex != -1) {
	  selectedBlockCode = el.options[el.selectedIndex].value;
  }

  // Remove old options before adding new ones
  el.options.length = 0;

  // Add valid options
  var j=0;
  for (var i=0;i<levels.length;i++) {

  	// Select the previsous selected option
	selected = false;
	if (null != selectedBlockCode && selectedBlockCode == levels[i].blockcode) {
		selected = true;
	}

   if (null == priceLevelCode || priceLevelCode == '-1') {
	    // Not a priceLevelCode is set, just add all options
	  	el.options[j] = new Option(levels[i].blockdescription, levels[i].blockcode, false, selected);
	  	j++;
	} else {
		// A priceLevelCode is set, only add blocks for the given pricelevel
		if (levels[i].pricecode == priceLevelCode || levels[i].blockcode == '-1') {
			el.options[j] = new Option(levels[i].blockdescription, levels[i].blockcode, false, selected);
			j++;
		}
	}

  }
}

/*
* Prints the prices in a selectbox (elemId). When a blockLevelCode
* is given only prices for the given blocklevel are printed.
*/
function printSelectPrices() {
  el = document.getElementById('prices');
  // See if their is a blocklevel set
  blockEl = document.getElementById('blocks');
  var blockLevelCode;
  if (null != blockEl) {
    blockLevelCode = blockEl.options[blockEl.selectedIndex].value;
  }

  // See which pricecode option is currently selected
  var selectedPriceCode;
  // -1 indicates nothing is selected
  if (el.selectedIndex != -1) {
	  selectedPriceCode = el.options[el.selectedIndex].value;
  }

  // Remove old options before adding new ones
  el.options.length = 0;

  // Add valid options
  var j=0;
  printedPriceLevelCodes = new Array();
  for (var i=0;i<levels.length;i++) {

    alreadyPrinted = false;
    for (var j=0;j<printedPriceLevelCodes.length;j++) {
	    // Voorkomt dat prijzen dubbel in de select verschijnen
	    if (printedPriceLevelCodes[j] == levels[i].pricecode) {
			alreadyPrinted = true;
		}
	}

    if (!alreadyPrinted) {

	    var priceDesc = levels[i].pricedescription;
	    if (levels[i].pricecode != -1) {
		    priceDesc = 'EUR ' + levels[i].pricedescription;
	    }
	    // Select the previsous selected option
		selected = false;
		if (null != selectedPriceCode && selectedPriceCode == levels[i].pricecode) {
			selected = true;
		}

	   if (null == blockLevelCode || blockLevelCode == '-1') {
		    // Not a blockLevelCode is set, just add all options
		  	el.options[j] = new Option(priceDesc, levels[i].pricecode, false, selected);
		  	printedPriceLevelCodes[printedPriceLevelCodes.length] = levels[i].pricecode;
		  	j++;
		} else {
			// A blockLevelCode is set, only add prices for the given blocklevel
			if (levels[i].blockcode == blockLevelCode || levels[i].pricecode == '-1') {
				el.options[j] = new Option(priceDesc, levels[i].pricecode, false, selected);
			  	printedPriceLevelCodes[printedPriceLevelCodes.length] = levels[i].pricecode;
				j++;
			}
		}

	}
  }
}

/*
* Sets the currently selected pricelevelcode into a formpart.
*/
function setPriceLevel() {
	// Get the formpart
	elPriceLevelFp = document.getElementById('selectedPrice');
	// Get the selectbox
	elPriceLevel = document.getElementById('prices');

	// Get the code of the selected pricelevel
	selectedPriceCode = '';
    if (null != elPriceLevel && undefined != elPriceLevel) {
		if (elPriceLevel.selectedIndex > 0) {
			selectedPriceCode = elPriceLevel.options[elPriceLevel.selectedIndex].value;
		}
	}

	// Assign the pricelevecode to the formpart
	elPriceLevelFp.value = selectedPriceCode;
}

/*
* Sets the currently selected blockcode into a formpart.
*/
function setBlockLevel() {
	// Get the formpart
	elBlockFp = document.getElementById('selectedBlock');
	// Get the selectbox
	elBlock = document.getElementById('blocks');

	// Get the code of the selected block
	selectedBlockCode = '';
    if (null != elBlock && undefined != elBlock) {
		if (elBlock.selectedIndex > 0) {
			selectedBlockCode = elBlock.options[elBlock.selectedIndex].value;
		}
	}

	// Assign the blockcode to the formpart
	elBlockFp.value = selectedBlockCode;
}

/*
* Checks if the given text is numeric
*/
function IsNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) {
      Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)  {
         IsNumber = false;
		}
	}
   return IsNumber;
}

/**
* Cleans up any values assigned to the holders of the selected price and block
*/
function cleanUpHolders() {
	elBlock = document.getElementById('blockcode');
	elPrice = document.getElementById('pricelevelcode');

	if (null != elPrice && undefined != elPrice)  {
		elPrice.value = '';
	}

	if (null != elBlock && undefined != elBlock)  {
		elBlock.value = '';
	}
}

/*
* Constructs a string with all the ordered tickets per concession.
*/
function setupTickets() {
	elTicket = document.getElementById('ticketholder');
	allElements = document.getElementsByTagName('select');

	var ticketstr = '';
	for (i=0;i<allElements.length;i++) {
		if (allElements[i].id.indexOf('_') == 0) {
			value = allElements[i].options[allElements[i].selectedIndex].value;
			if (null == value || value == '') {
			  value = '0';
			}
			ticketstr += allElements[i].id.substring(1,3) + ':' + value + ';';
		}
	}
	elTicket.value = ticketstr;
}


//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here.
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

//Clock
function updateClock() {  
  setInterval ("updateClockTimeout()", 1000);
}
  
  
function updateClockTimeout() {
  var currentTimeString = "";
  
  // currentServerTime set in header of page(for personalization).
  if (currentServerTime) {
    var currentHours   = currentServerTime.getHours();
    var currentMinutes = currentServerTime.getMinutes();
    var currentSeconds = currentServerTime.getSeconds();

    // Add a second to the time and call this function again in 1 second.
    currentServerTime.setSeconds(currentServerTime.getSeconds()+1);
    
    currentMinutes     = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds     = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    currentTimeString = currentTimeString + currentHours + ":" + currentMinutes + ":" + currentSeconds;

    document.getElementById('timedate').innerHTML = currentTimeString;
  }
}
