var loadingMessage = '   <table><tr><td><img src="../images/loading.gif" /></td><td> Bitte warten ...</td></tr></table>';

function createXMLHttpRequest() {
  var xmlHttp;
  // Firefox, Opera 8.0+, Safari
  try { xmlHttp=new XMLHttpRequest(); }
    // Internet Explorer
    catch (e) {
      try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
      //Older IE?
      catch (e) {
        try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) {
          alert("Your browser does not support AJAX!"); return false;
        }
      }
    }
    return xmlHttp;
}

function toHtml(str) {
  if (str != null) {
	str = str.replace('/</','&lt;');
	str = str.replace('/>/','&gt;');
	str = str.replace('/"/','&quot;');
  }
  return str;
}

function getXMLHttpResponse(url) {
  var req = createXMLHttpRequest();  
  req.open('GET', url, false);
  req.send(null);
  
  return req.responseText;  
}

function getXMLHttpResponseXML(url) {
	  var req = createXMLHttpRequest();  
	  req.open('GET', url, false);
	  req.send(null);
	  
	  return req.responseXML;  
	}

function setLoadingMessageToInnerHtml(elementId) {
	document.getElementById(elementId).innerHTML = loadingMessage;	
}

function setInnerHTML(elementId, url, setDisplayToTrue, displayElementId) {
  if (displayElementId == null) {
    displayElementId = elementId;
  }
  
  if (typeof window.jQuery != 'undefined') {
	  $.get(url, function(html) {
		  el = document.getElementById(elementId);
	      if (el == null) {
	        alert('Element not found: ' + elementId);
	      } else {  		  
	        var txt = getBodyHtml(html);
	        if (el.innerHTML) {
	          el.innerHTML = txt;
	        } else {           	
	          try {                    
		          var newdiv = document.createElement("div");
		          newdiv.innerHTML = txt;          
		          el.appendChild(newdiv);
	          } catch (ex) {
	        	  alert('el.innerHTML (' + elementId + '): ' + el.nodeName);
	          }
	        }
	      }
	      if (setDisplayToTrue) {
	        document.getElementById(displayElementId).style.display = '';
	      }
	  });	 
  } else {
	  new Ajax.Request(url, {
	    method: 'get',
	    onSuccess: function (transport) {
	      el = document.getElementById(elementId);
	      if (el == null) {
	        alert('Element not found: ' + elementId);
	      } else {  		  
	        var txt = getBodyHtml(transport.responseText);
	        if (el.innerHTML) {
	          el.innerHTML = txt;
	        } else {           	
	          try {                    
		          var newdiv = document.createElement("div");
		          newdiv.innerHTML = txt;          
		          el.appendChild(newdiv);
	          } catch (ex) {
	        	  alert('el.innerHTML (' + elementId + '): ' + el.nodeName);
	          }
	        }
	      }
	      if (setDisplayToTrue) {
	        document.getElementById(displayElementId).style.display = '';
	      }
	    } 
      });
  }
}


function loadBodyHtmlTo(elementId, url) {	
	setLoadingMessageToInnerHtml(elementId);
	
	var req = createXMLHttpRequest();  
    req.open('GET', url, true);    
    req.onreadystatechange =  function () {
    	if (this.readyState != 4)
            return;
    	if (this.status == 200) {
		    var txt = this.responseText;    
		    var html = getBodyHtml(txt);
		  
		    document.getElementById(elementId).innerHTML = html;
    	} else {
    		document.getElementById(elementId).innerHTML = 'Unbekannter Fehler!';
    	}
    };
    req.send(null);   
}

function dummy() {
	alert('dummy');
}

function vtgdbWindow(id, title, url) {
	loadBodyHtmlIntoMessageWindow(id, title, url);
	centerElementById(id);
	vtgdbSetupListTables();
}

function vtgdbImageWindow(id, title, imgSource) {
	var d = createMessageWindow(id, title);
	var content = document.getElementById(id + 'Content');				
	content.innerHTML = loadingMessage;
	d.style.display = '';
	var html = '<div width="100%" style="text-align: center; padding: 5px;"><img borer="0" alt="' + imgSource + '" src="' + imgSource + '" /></div>';
	content.innerHTML = html;
	centerElementById(id);
}

function vtgdbElementIdToWindow(id, title, elementId) {
	var d = createMessageWindow(id, title);	
	var element = document.getElementById(elementId);
	if (d.style.display != '') {
		if (element) {			
			document.getElementById(id + 'Content').innerHTML = loadingMessage;
		}
		if (window.jQuery == 'undefined') {
			d.style.display = '';
		} else {
			$(d).fadeIn();			
		}	
	}				
	if (element) {
		var parent = element.parentNode;
		var removed = parent.removeChild(element);		
	
    	document.getElementById(id + 'Content').innerHTML = removed.innerHTML;
	}
}


function loadBodyHtmlIntoMessageWindow(id, title, url) {		
	d = createMessageWindow(id, title);	
	if (d.style.display != '') {
		document.getElementById(id + 'Content').innerHTML = loadingMessage;
	    d.style.display = '';
	}    
	
	var xml = null; //getXMLHttpResponseXML(url);	
	if (xml == null) {		
		var html = getXMLHttpResponse(url);		
		html = getBodyHtml(html);						
		document.getElementById(id + 'Content').innerHTML = html;		
		vtgdbSetupListTables(document);
	} 				
}

function getBodyHtml(txt) {
	var html = txt;
	var idx = html.indexOf('<body');	
	if (idx != -1) {
	  html = html.substring(idx + 5);	
	  idx = html.indexOf('>');
	  if (idx != -1) {
		  html = html.substring(idx + 1);
	      idx = html.indexOf('</body>');
	      if (idx != -1) {
	    	  html = html.substring(0, idx);
	      }
	  }		  		  
	}
	return html;
}

function getLastStringPart(txt, start, end) {
	var html = txt;
	var idx = html.lastIndexOf(start);	
	if (idx != -1) {      	
	  html = html.substring(idx + start.length);

      idx = html.lastIndexOf(end);
      if (idx != -1) {
    	  html = html.substring(0, idx);
      }	  		  		  
	}
	return html;
}

function createMessageWindow(id, title) {
	d = document.getElementById(id);
	if (d != null) {
		return d;
	}
	
	id = toHtml(id);
	title = toHtml(title);
	
	d = document.createElement('div');
	d.id = id;
	d.className = 'window';
	d.style.position = 'absolute';
	d.style.left = '100px';
	d.style.top = '50px';
	d.style.minWidth = '400px';	
	d.style.display = '';
	d.style.zIndex = '201';
	d.style.width = 'auto';
	d.style.height = 'auto';
        		
	var html = 	  
'  <table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">' +
     '<tr style="cursor: move;" onmousedown="dragstart(\'' + id + '\')"  >' +
       '<td class="leftTop"> </td>' +
       '<th class="windowTitle" rowspan="2">' + title + '</th>' +
       '<th class="windowTitle" rowspan="2" align="right" width="40px" nowrap="nowrap">' + 
       '&#160;<a href="javascript:void(0);" onclick="openElementIdInWindow(\'' + id + 'Content\',document,true);toggleShow(\'' + id + '\');"><img border="0" title="Drucken" alt="Drucken" src="../images/printer.png" /></a>' +
         '&#160;<a href="javascript:void(0);" onclick="openElementIdInWindow(\'' + id + 'Content\',document);toggleShow(\'' + id + '\');"><img border="0" title="Im neuen Fenster öffnen" alt="Im neuen Browser-Fenster öffnen" src="../images/newWindow.png" /></a>' + 
         '&#160;<a style="font-weight: bold; color: #000000;" href="javascript:toggleShow(\'' + id + '\');"><img title="Fenster schließen" border="0" src="../images/cancel.png" /></a>' +
       '</th>' +
       '<td class="rightTop"> </td>' +
     '</tr>' +
     '<tr>' +
       '<td class="left">&#160;</td>' +
       '<td class="right">&#160;</td>' +
     '</tr>' +      
'  </table>' +
'  <div id="' + id + 'Content" class="content">' + 
   loadingMessage +
'  </div>' + 
'  <table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">' +
'    <tr>' +
'        <td class="leftBottom"> </td>' +
'        <td class="bottom"><img src="../images/dummy.gif" /></td>' +            
'        <td class="rightBottom"> </td>' +
'    </tr>' +           
'  </table>';
	
	d.innerHTML = html;
	//d.innerHTML = htmlTop + htmlBottom;
		
	document.body.appendChild(d);	
	return d;
}

function blockInput(doc) {	
	if (doc == null) {
		doc = document;
	}
			
	var d = doc.getElementById('preventInput');
	if (d != null) {
		resizeBlockInput();
		d.style.display = '';
		return;
	}
	
	var width = doc.documentElement.clientWidth + doc.documentElement.scrollLeft;
	d = doc.createElement('div');	
	d.id = 'preventInput';
	d.style.position = 'absolute';
	d.style.top = '0px';	
	d.style.backgroundColor = '#CCCCCC';
	d.style.opacity = '0.6';	
	d.style.zIndex = '200';
	d.style.filter = 'alpha(opacity=60)'; // IE
	doc.body.appendChild(d);
	resizeBlockInput(doc);
	
	if (doc.attachEvent) {
      // IE		
	  window.attachEvent('onresize', resizeBlockInput);	
	  doc.body.attachEvent('onscroll', resizeBlockInput);
	} else if (doc.body.addEventListener) {      
	  window.addEventListener('resize', function (e) {resizeBlockInput(doc);}, false);	  
	  doc.addEventListener('scroll', function (e) {resizeBlockInput(doc);}, false);
	} 
}

/**
 * set the div to the visible window
 */
function resizeBlockInput(doc) {	
	if (doc == null) {
		doc = document;
	}
	 
	var x;
	var width;
	var height;
	
	if (doc.documentElement && doc.documentElement.clientWidth) {
	    //alert('document.element')
		x = doc.documentElement.scrollLeft;
		width = doc.documentElement.clientWidth;
	    height = doc.documentElement.scrollHeight;	 
	    var ch = doc.documentElement.clientHeight;
	    if (ch > height) {	    	
	    	height = ch + doc.documentElement.scrollTop;
	    }	
	} else if (doc.body) {
		x = doc.body.scrollWidth;
		width = doc.body.clientWidth;
		height = doc.body.scrollHeight;
	}	
	
	var d = doc.getElementById('preventInput');
	if (d != null) {
		d.style.left = x + 'px';
		d.style.height = height + 'px';
		d.style.width = width + 'px';				
	}	
}

function hideBlockInput() {
	document.getElementById('preventInput').style.display = 'none';
}

function inputWindowURL(id, title, message, showButtons, okText, okAction, cancelText, showImage, doc) {	
	if (doc == null) {
		doc = document;
	}	
    blockInput(doc);	
	
	var d = doc.getElementById(id);
	if (d != null) {
		if (typeof window.jQuery != 'undefined') {
		  $(d).fadeIn('slow');	
		} else {
		  d.style.display = '';
		}
	} else {	
		id = toHtml(id);
		title = toHtml(title);
		
		d = doc.createElement('div');
		d.id = id;
		d.className = 'confirm';
		d.style.position = 'absolute';
		d.style.left = '100px';
		d.style.top = '50px';
		d.style.width = '400px';		
		d.style.zIndex = '300';		
		//d.style.width = 'auto';
		
		if (typeof window.jQuery != 'undefined') {
			d.style.display = ''; // none
		} else {
			d.style.display = '';
		}
		
		doc.body.appendChild(d);
		
		if (window.addEventListener) {		
	        window.addEventListener("scroll", function() {centerElementById(id, doc);}, false);
		} else if (window.attachEvent){		
	        window.attachEvent("onscroll", function() {centerElementById(id, doc);});
	    }
	}     		
	var html = 	  
'  <table class="confirm" border="0" cellspacing="0" cellpadding="0" width="400">' +
'    <tr class="top" style="cursor: move;" onmousedown="dragstart(\'' + id + '\')"  >' +     
'      <th class="left">' + title + '</th>' +
'      <th class="right"><a class="closeWindow" href="javascript:toggleShow(\'' + id + '\');hideBlockInput();">X</a></th>' +
'    </tr>' +
'    <tr>' +     
'      <td colspan="2">';
	 if (showImage) {
		 html += 
         '<div style="float: left; padding-right: 5px;"><img src="../images/qm50.png" alt="?"></div>';
	 }
	 html +=
         '<div id="' + id + '_text">' + message + '</div></td>' +
'	 </tr>';
     if (showButtons) { 	
     html += 	
'    <tr class="bottom">' +
'      <td class="buttons" colspan="2" align="center">';
       if (okText != '') html += '<input onclick="this.disabled=true;hideBlockInput();' + okAction + '" type="button" value="' + okText + '" />'; 
       if (cancelText != '') html += '<input onclick="toggleShow(\'' + id + '\');hideBlockInput();" type="button" value="' + cancelText + '" />'; 
       html += '</td>' +
'	 </tr>';
     } else {
     html += 
'    <tr class="bottom">' +
'      <td class="buttons" colspan="2" align="center">&#160;</td>' +
'	 </tr>';    	 
     }
     html +=
'  </table>';
	d.innerHTML = html;							
	
	//if (window.jQuery) {
	//	$(d).CenterIt();
	//} else {
		centerElementById(id, doc);
	//}
        
	return d;
}

function confirmWindow(id, title, message, action) {	
	blockInput();
		
	var d = document.getElementById(id);
	//if (d != null) {
	//	d.style.display = '';
	//	centerElementById(id);
	//	return d;
	//}
	
	id = toHtml(id);
	title = toHtml(title);
	
	if (d == null) {
		d = document.createElement('div');
	}
	d.id = id;
	d.className = 'confirm';
	d.style.position = 'absolute';
	d.style.left = '100px';
	d.style.top = '50px';
	d.style.width = '400px';	
	d.style.display = '';
	d.style.zIndex = '300';		
	//d.style.width = 'auto';
        		
	var html = 	
'  <table class="confirm" border="0" cellspacing="0" cellpadding="0" width="400">' +
'    <tr class="top" style="cursor: move;" onmousedown="dragstart(\'' + id + '\')"  >' +     
'      <th class="left">' + title + '</th>' +
'      <th class="right"><a class="closeWindow" href="javascript:toggleShow(\'' + id + '\');hideBlockInput();"><img border="0" src="../images/cancel.png" /></a></th>' +
'    </tr>' +
'    <tr>' +     
'      <td colspan="2">' +
         '<div style="float: left; padding-right: 5px;">' + 
         '<img id="' + id + '_confirmImg" src="../images/qm50.png" alt="?">' + 
         '</div> ' + message + '</td>' +
'	 </tr>' +
'    <tr class="bottom">' +
'      <td class="buttons" colspan="2" align="center">';
	if (action != null) {
      html += '<input onclick="hideBlockInput();' + action + '" type="button" value="OK" />';
	}  
	html += 
'        <input onclick="toggleShow(\'' + id + '\');hideBlockInput();" type="button" value="Abbrechen" />' + 
'      </td>' +
'	 </tr>' +
'  </table>';
	
	d.innerHTML = html;
	//d.innerHTML = htmlTop + htmlBottom;
		
	document.body.appendChild(d);	
	
	centerElementById(id);
	
	if (window.addEventListener) {		
        window.addEventListener("scroll", function() {centerElementById(id);}, false);
	} else if (window.attachEvent){		
        window.attachEvent("onscroll", function() {centerElementById(id);});
    }
	
	return d;
}

function ajaxPostForm(formId, targetElementId) {	
	var url = '';
	var f = document.getElementById(formId);
	if (f != null) {
		url = f.action;		
	} else {
		alert('id not found: ' + formId);
		return;
	}	
	if (targetElementId==null && form.parentNode) {
		targetElementId = form.parentNode.id;
	}
	
    blockInput();
    if (typeof window.jQuery != 'undefined') {    	
    	var data = $('#' + formId).serialize(true);
    	$.post(url, data, function(html) {
    		el = document.getElementById(targetElementId);
		    if (el == null) {
		        alert('Element not found: ' + targetElementId);
		    } else {            		    	
		        var txt = getBodyHtml(html);
		        
		        // get div with class="printContent" if available
	    		var temp = document.createElement('div');
	    		temp.innerHTML = txt;		    				    					   
		    	temp = $("div[class='printContent']", temp);
		    	if (temp && temp.html()) {
		    		txt = temp.html();
		    	}
		        
		        if (el.innerHTML) {
		          el.innerHTML = txt;
		        } else {
		          alert('el.innerHTML' + el);
		        }
		    }
            hideBlockInput();	
    	});
    } else {
		new Ajax.Request(url, {
			    method: 'post',
			    parameters: $(formId).serialize(true),            
			    onSuccess: function (transport) {
			      el = document.getElementById(targetElementId);
			      if (el == null) {
			        alert('Element not found: ' + targetElementId);
			      } else {            
			        var txt = getBodyHtml(transport.responseText);
			        if (el.innerHTML) {
			          el.innerHTML = txt;
			        } else {
			          alert('el.innerHTML' + el);
			        }
			      }
	              hideBlockInput();		      		      		      
			    } 
			  });
    }
}

function postMe(form, targetElementId) {	
	var formId = form.id;
	var url = form.action;
	
	var targetElement = null;
	if (targetElementId) {		
	    targetElement = document.getElementById(targetElementId);
	} else if (form.parentNode) {		
		targetElement = form.parentNode;	
	} 
    if (targetElement == null) {
		alert('target element not found: ' + targetElementId);
		return;
	}	
	
    if (typeof window.jQuery != 'undefined') {    	
    	var data = $('#' + formId).serialize();
    	$.post(url, data, 
    		   function(html) {
    		        var txt = getBodyHtml(html);        
    		    		        
    		        // get div with class="printContent" if available
		    		var temp = document.createElement('div');
		    		temp.innerHTML = txt;		    				    					   
			    	temp = $("div[class='printContent']", temp);
			    	if (temp && temp.html()) {
			    		txt = temp.html();
			    	}
		    		
			        if (targetElement.innerHTML) {
			        	targetElement.innerHTML = txt;
			        } else {
			            alert('targetElement.innerHTML' + el);
			        }	                     		      		     
    	       },'html');
    } else {    
	    new Ajax.Request(url, {
		    method: 'post',
		    parameters: $(formId).serialize(true),            
		    onSuccess: function (transport) {	                
		        var txt = getBodyHtml(transport.responseText);
		        if (targetElement.innerHTML) {
		        	targetElement.innerHTML = txt;
		        } else {
		            alert('targetElement.innerHTML' + el);
		        }	                     		      		     
		    } 
		  });
    }    
}

	