/* --- Globaalit muuttujat --- */

var selectedSeats = new Array();

/* --- Funktioiden prototyypit --- */

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!String.prototype.trim)
{
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
  };
}

/* --- Apufunktioita --- */

function insertOption(element, value, text)
{
  var y=document.createElement('option');
  y.text=text;
  y.value=value;
  try
  {
    element.add(y,null); // standards compliant
  }
  catch(ex)
  {
    element.add(y); // IE only
  }
}

function getXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

/* --- IdeaTicketin funktiot --- */

function ajaxProductionChanged() {
 
  if(xmlHttp.readyState==4 && xmlHttp.status == 200) {
   
    var showlist = document.getElementById("show");

    // Pilkotaan saatu näytösluettelo osiin
    var items = xmlHttp.responseText.split('#');

    // ... ja lisätään ne yksittäin listalle
    for(i = 0; i < items.length; i++) { 
      if(items[i].length > 0) {
        var showitem = items[i].split(';');
        insertOption(showlist, showitem[0].trim(), showitem[1]);
      }
    }

    showlist.disabled = false;
  }
}

function ajaxShowList(production, future) {

  if(production.length == 0) { 
    return;
  }

  // Tyhjennetään vanhat näytökset
  var showlist = document.getElementById("show");
  showlist.disabled = true;
  showlist.options.length = 0;

  // Muodostetaan käytettävä URL-osoite
  var url = "/servlet/ideaticket/ShowList";
  url = url + "?production=" + production;
  
  // Lisäparametri, jos halutaan vain tulevat näytökset
  if(future) {
    url = url + "&future=1";
  }
  
  // Lisätään satunnaisdataa, jotta tiedot ei tule cachesta
  url = url + "&dummy=" + Math.random();

  xmlHttp = getXmlHttpObject();
  if(xmlHttp == null) {
    alert("Selaimesi ei tue AJAX-toiminnallisuutta!");
    return;
  }

  // Käynnistetään haku
  xmlHttp.onreadystatechange = ajaxProductionChanged;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function selectSeat(clickedCell) {

  var pos = -1;
  if(clickedCell.style.backgroundColor != "blue")   {
    //merkitään varaus
    clickedCell.style.backgroundColor = "blue";
    clickedCell.style.color = "white";
    selectedSeats.push(clickedCell.innerHTML);

  } else {
    //perutaan varaus
    clickedCell.style.backgroundColor = "";
    clickedCell.style.color = "";
    if((pos = selectedSeats.indexOf(clickedCell.innerHTML)) != -1) {
      //poistetaan varaus listasta
      selectedSeats.splice(pos, 1);
    }
  }
}

function submitSeats() {

  // Kopioidaan valitut paikat lomakekenttään
  document.getElementById("seats").value = selectedSeats.join(';');

  return true;
}

function doInputFocus() {

  var elements = document.getElementsByTagName("input");
  if(elements.length > 0) {
    elements[0].focus();
  }
}

function openReport(production, show, url) {

  var options = '';
  var url_parts = url.split('?');  
  if(url_parts.length > 1) {
    url = url_parts[0];
    options = '&' + url_parts[1];
  }
  
  var win = window.open(url + '?production=' + production + '&show=' + show + options ,'reportwin','width=735,menubar=1,toolbar=1,resizable=1,scrollbars=1');
  win.focus();
}


