// global xmlhttprequest object
var xmlHttp = false;
/** AJAX functions **/

// constants
var REQUEST_GET   = 0;
var REQEST_POST   = 2;
var REQUEST_HEAD  = 1;
var REQUEST_XML   = 3;

/**
 * instantiates a new xmlhttprequest object
 *
 * @return xmlhttprequest object or false
 */
var msProgIDs = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];

function getXMLRequester (){
	var req = null;
	try
	{
		if (window.XMLHttpRequest)
			req = new XMLHttpRequest();
		else if (window.ActiveXObject)
		{
			while (!req && msProgIDs.length)
			{
				try { req = new ActiveXObject(msProgIDs[0]); } catch (e) { req = null; }
				if (!req)
					msProgIDs.splice(0, 1);
			}
		}
	}
	catch (e) { req = null;	}

	if (!req)
		alert("Der Inhalt konnte nicht geladen werden!" );

	return req;
};

/**
 * sends a http request to server
 *
 * @param strSource, String, datasource on server, e.g. data.php
 *
 * @param strData, String, data to send to server, optionally
 *
 * @param intType, Integer,request type, possible values: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
 *
 * @param strData, Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
 *
 * @return String, request data or data source
 */
 
function sendRequest(strSource, strData, intType, intID ){
   
    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET

    //default id
    if (isNaN(intID))
      intID = 0;
      
    // previous request not finished yet, abort it before sending a new request
    if( xmlHttp && xmlHttp.readyState ){
        xmlHttp.abort( );
        xmlHttp = false;
    }
        
    // create a new instance of xmlhttprequest object
    // if it fails, return
    if( !xmlHttp ){
        xmlHttp = getXMLRequester( );
        if( !xmlHttp )
            return;
    }
    
    // parse query string
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );

    // data to send using POST
    var dataReturn = strData ? strData : strSource;
    
    switch( intType ){
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST
            // open the connection 
            xmlHttp.open( "POST", strSource, true );
            xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            xmlHttp.setRequestHeader( 'Content-length', strData.length );
            break;
        case 3: // HEAD
            // open the connection 
            xmlHttp.open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            xmlHttp.open( "GET", strDataFile, true );
            strData = null;
    }
    // set onload data event-handler        
    xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); ;

    // send request to server
    xmlHttp.send(strData);    // param = POST data    
    return dataReturn;
}
    

/**
 * process the response data from server
 *
 * @param intID, Integer, ID of this response
 */
function processResponse(intID){
    // status 0 UNINITIALIZED open() has not been called yet.
    // status 1 LOADING send() has not been called yet.
    // status 2 LOADED send() has been called, headers and status are available.
    // status 3 INTERACTIVE Downloading, responseText holds the partial data.
    // status 4 COMPLETED Finished with all operations.
    switch( xmlHttp.readyState ){
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:    
            // check http status
            // success
            if( xmlHttp.status == 200 ){
              processData( xmlHttp, intID );
            }
            // loading not successfull, e.g. page not available
            else{
              if( window.handleAJAXError )
                handleAJAXError( xmlHttp, intID );
              else
                alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
            }
    }
}

/** End AJAX functions **/

function handleAJAXError( xmlHttp, intID ){
  try{}
  catch(e){}
}

/** real application functions **/

// process data from server
function processData( xmlHttp, intID ){  
  
// process text data
  if (intID==1) {
    TMP = xmlHttp.responseText;
    Pos=TMP.indexOf('PSVAJAXResultbox">')+18;
    PosEnde=TMP.indexOf('PSVAJAXEnde');
    TMP = TMP.substring(Pos,PosEnde);
    
    TMP = "<div align=\"right\" style=\"padding:2px 5px 2px 5px;\"><a href=\"javascript:closeBox('PSVBereich');\">x</a></div>" + TMP;
        
    BoxID = 'PSVBereich';
    BoxHauptID = 'PSVHauptBereich';
    
    if (!document.getElementById(BoxID))
    {
      var newNode = document.createElement("div");
      newNode.setAttribute("id", BoxID);
      newNode.setAttribute("class", BoxID);
      document.body.appendChild(newNode);
    }
    
    if (!document.getElementById(BoxHauptID))
    {
      var newNode = document.createElement("div");
      newNode.setAttribute("id", BoxHauptID);
      newNode.setAttribute("class", BoxHauptID);      
      newNode.setAttribute('onclick', 'closeBox("PSVBereich")');      
      document.body.appendChild(newNode);
    }

    arrPSVPageSize = PSVGetPageSize();
    var pageX = arrPSVPageSize[0];
    var pageY = arrPSVPageSize[1];
        
    var PSVHauptDiv = document.getElementById(BoxHauptID);
    PSVHauptDiv.style.position = "absolute";
    PSVHauptDiv.style.zIndex = 99;
    PSVHauptDiv.style.width= pageX +50 + "px";
    PSVHauptDiv.style.height= pageY + "px";
    PSVHauptDiv.style.background = "black";
    PSVHauptDiv.style.visibility = "visible";
    PSVHauptDiv.style.left = "0px";
    PSVHauptDiv.style.top = "0px";
    PSVHauptDiv.style.filter = "Alpha(opacity=50, finishopacity=50, style=3); progid:DXImageTransform.Microsoft.Alpha(Opacity=50);";
    PSVHauptDiv.style.MozOpacity = 0.5;
    PSVHauptDiv.style.opacity = 0.5;
    
    var PSVDiv = document.getElementById(BoxID);
    PSVDiv.style.position = "absolute";
    PSVDiv.style.zIndex = 100;
    PSVDiv.style.background = "white";
    PSVDiv.style.border = "solid black 1px";
    PSVDiv.style.width= "400px";
    //PSVDiv.style.height= "300px";
    PSVDiv.style.padding= "0px 5px 0px 5px";
    PSVDiv.style.visibility = "visible";
    PSVDiv.innerHTML = "<div style=\"position:relative;\">" + TMP + "</div>";
    centerAll("PSVBereich");
  }
    
}

function closeBox(objID){
  var PSVDiv = document.getElementById(objID);  
  PSVDiv.style.zIndex = -100; 
  PSVDiv.style.visibility = "hidden";
  var PSVHauptDiv = document.getElementById('PSVHauptBereich');
  PSVHauptDiv.style.zIndex = -100; 
  PSVHauptDiv.style.visibility = "hidden";
}

function centerAll(objID) {
    var pageX = (document.all)?document.body.offsetWidth:window.innerWidth;
    var pageY = (document.all)?document.body.offsetHeight:window.innerHeight;
    
    var objRef = document.getElementById(objID);
    var objW = objRef.offsetWidth;
    var objH = objRef.offsetHeight;
    objRef.style.left = ((pageX/2)-(objW/2))+"px";
    objRef.style.top = ((pageY/2)-(objH/2))+"px";   
}

function PSVGetPageSize()
{
  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) {	// all except Explorer
    if (document.documentElement.clientWidth) {
      windowWidth = document.documentElement.clientWidth;
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if (yScroll < windowHeight) {
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if (xScroll < windowWidth) {
    pageWidth = xScroll;
  } else {
    pageWidth = windowWidth;
  }

  return [pageWidth, pageHeight];
}

// process data from server

function GetPSVDetails(URL) {
  sendRequest( URL, '', REQEST_POST,1);
}
