// JavaScript Document
var xmlHttp = null;
var id = 0;
var div_namespace = "angebot_details_";
var xmlHttpRequest_state_uninitialized = 0;
var xmlHttpRequest_state_loading = 1;
var xmlHttpRequest_state_loaded = 2;
var xmlHttpRequest_state_interactive = 3;
var xmlHttpRequest_state_complete = 4;


String.prototype.trim = function () {
  return (this.replace(/\s+$/,"").replace(/^\s+/,""));
};

function popup(file) {
  if (window.screenX) {
    new_top = window.screenY + 50;
  	new_left = window.screenX + 50;
	} else {
    new_top = 50;
  	new_left = 150;
	}
  F1 = window.open(file,"anlass_print","width=680,height=400,left="+new_left+",top="+new_top+",location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,dependent=yes");
	// F1.moveTo(150, screen.height - 400);
  F1.focus();
  return(false);
}

function showhide(elem, type) {
  if (type == null) { type = "anlass" }
  var action = "open";
	if (elem.className == "angebot_titel_klein") { elem.className = "angebot_titel"; }
	else if (elem.className == "angebot_titel") { elem.className = "angebot_titel_klein"; }	
  if (elem.firstChild.nodeName.toLowerCase() == "span" && (elem.firstChild.className == "closed" || elem.firstChild.className == "opened")) {
    if (elem.firstChild.className=="closed") {
      action = "open";
      elem.firstChild.className = "opened";
    } else {
      action = "close";
      elem.firstChild.className = "closed";
    }
  }
  if (elem.parentNode.nodeName.toLowerCase() == "td") {
    while (elem.nodeName.toLowerCase() != "table") {	elem = elem.parentNode; }
  }
  if (elem.parentNode.className == "angebot") {
    var parent = elem.parentNode;
    for (var i=0; i<parent.childNodes.length; i++) {
      var el = parent.childNodes[i];
      if (el.nodeName.toLowerCase() == "div") {
        el.style.display = (action=="close")?"none":"block"; // (el.style.display=="block")?"none":"block";
        // @TODO: nur das erste div-Element mit loadAngebot behandeln!
        if (el.innerHTML.trim().length < 20 && el.style.display == "block") {
          var anlass_id = parseInt(el.id.substr(div_namespace.length));
          loadAngebotDetails(el, anlass_id, type + "_id");
        }
      }
    }
  }
  return(0);
}

function confirmLink(text) {
  return (confirm("Änderungen verwerfen und " + text + "?"))
}

function loadAngebotDetails(el, item_id, id_field) {
	xmlHttp = getXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="angebot_details.php";
	url=url+"?" + id_field + "="+String(item_id);
	url=url+"&hide_title=true";
	url=url+"&sid="+Math.random();
	id = item_id;
	xmlHttp.onreadystatechange=stateChanged;
 	xmlHttp.open("GET",url,true);
 	xmlHttp.send(null);
} 

function stateChanged() { 
  if (xmlHttp.readyState==4) {
    document.getElementById(div_namespace+id).innerHTML = xmlHttp.responseText;
  }
}

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;
}

function externalLinks() {
 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") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
