var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 700,
      height: 500,
      location: 0,
      scrollbars: 1
    }
    
    Object.extend(this.options, options || {});
    var i = 0;
    var opts = "";
    
    for (key in this.options) {
      if (key != 'url') {
        opts += [key, this.options[key]].join("=");
        opts += ',';
      }
      i++;
    }
    
    window.open(this.options.url, '', opts);
  }
}

function toggleEnableDisable(id) {
  if ($(id) || $(id).disabled) {
    $(id).disabled = false;
  } else {
    $(id).disabled = true;
  }  
}

function toggleShowHide(id) {
	if ($(id).style.display != "none") {
	  Effect.SlideUp(id, {duration: 0.7, queue: 'front'});
	} else {
		Effect.SlideDown(id, {duration: 0.7, queue: 'end'});
	}
}

function clone_text_input(from, to) {
  if (to.value == "") {
    to.value = from.value
  }
}

function next_entry_if_sold_out(select) {
  elem = document.getElementById(select);
  var text = elem.options[elem.selectedIndex].text
  $(select + '>option:selected').index()
  
  while (text.match(/AUSVERKAUFT/) != null) {
    try { // In FF an Exception is thrown
      if (select.selectedIndex < select.length-1) {
        select.selectedIndex += 1;
      } else {
        select.selectedIndex = 0;
      }
      
      if (select.selectedIndex == -1) break; // Safari assumes an index of -1 if it is not valid
      text = select.options[select.selectedIndex].text
    } catch (err) {}
  }
}

// http://www.elated.com/articles/javascript-and-cookies/
function get_cookie(cookie_name) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if (results)
    return (unescape(results[2]));
  else
    return null;
}

function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
  var cookie_string = name + "=" + escape(value);

  if (exp_y) {
    var expires = new Date(exp_y, exp_m, exp_d);
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if (path)
    cookie_string += "; path=" + escape(path);

  if (domain)
    cookie_string += "; domain=" + escape(domain);
  
  if (secure)
    cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

