var oc = 0;
var ocIE = 0;


function suchfenster()
{
	suchoption = "";
	suchtext = "";
	
	if (document.suchform.suche[0].checked==true) { suchoption = "artikelnummer"; }
	if (document.suchform.suche[1].checked==true) { suchoption = "bestellnummer"; }
	if (document.suchform.suche[2].checked==true) { suchoption = "volltext"; }
	
	suchtext = document.suchform.suchtext.value;
	if(suchtext == "" || suchoption == "")
	{
		alert("<?php echo addslashes($lang_suchfehler); ?>");
	}
	else
	{
		if(window.name == "search")
		{
			location.href = "suche.php?suchtext="+suchtext+"&suche="+suchoption;
		}
		else
		{
			searchwindow = window.open("suche.php?suchtext="+suchtext+"&suche="+suchoption,"search","width=820,height=600,scrollbars=yes,resizable=yes'");
		}
	}
}


function bigpic(datei,text,breite,hoehe)
{	

  if(parseInt(breite) >= 800)
  {
    breite = parseInt(breite) + 17;
  }
	if(text != "")
	{
	 hoehe = parseInt(hoehe) + 50;
  }
	vorschau = window.open("bildansicht.php?bild="+datei+"&text="+text+"", "winbild", "width="+breite+",height="+hoehe+",scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,directories=no");
  vorschau.focus();	
}

function open_stkl()
{
	fenster = window.open('stueckliste.php','stkl','width=800,height=500,scrollbars=yes,menubar=no,statusbar=no');
	fenster.focus();
}

function open_hilfe()
{
	fenster = window.open('hilfe.php','stkl','width=460,height=250,scrollbars=no,menubar=no,statusbar=no');
	fenster.focus();
}


var printWindow;
function open_printWindow()
{
  if(printWindow) { printWindow.close(); printWindow = null; }
  var produktID = parent.document.getElementById('produkt_alt').value;
  var popupLink = "artikel_print.php?produkt="+produktID;
  printWindow = window.open(popupLink,'print','width=750,height=500,resizable=yes');
}

function toggleLayer(ID)
{
	if(document.getElementById(ID).style.display == "none" || document.getElementById(ID).style.display == "")
	{
    document.getElementById(ID).style.display = 'block';
  }
  else
  {
    document.getElementById(ID).style.display = 'none';
  }
}


function ajaxObj()
{
	// var ajaxObj = this;
	
	if (/\b(function|object)\b/i.test(typeof XMLHttpRequest) && XMLHttpRequest)
	{
		// try..catch wird nicht universell unterstützt, ist aber hier
		// möglicherweise notwendig
		try
		{
			this.request = new XMLHttpRequest();
		}
		catch (e) {}
	}
	else if(/\b(function|object)\b/i.test(typeof ActiveXObject) && ActiveXObject)
	{
		// s.o.; try..catch ist hier notwendig
		try
		{
			this.request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {}
	}
}
	
	ajaxObj.prototype = {
    constructor: ajaxObj,
    request: null,
	
	
	////////// PROFIL FUNCTIONS //////////
	
	startGlobalSearch: function(string,searchArticleNr,searchOrderNr,searchText,thisName)
	{
		if(string.length > 3)
		{
		  showLayer('suggestions');		
			loadWait(thisName);
			this.constructor();
			var req = this.request;
			var me = this;
		  req.open('POST', 'inc/inc_suggest_globalsearch.php', true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.onreadystatechange = function()
			{				
				me.handleResponseSearch(req,thisName);											
			}			
			req.send('queryString='+string+'&searchArticleNr='+searchArticleNr+'&searchOrderNr='+searchOrderNr+'&searchText='+searchText);
		}
		else
		{
			hideLayer('suggestions');		
		}
	},
	
	
	openAddLayer: function(artikel,mode,anzahl)
	{
	
	  if(mode == "sl") { var thisName = "addSL"; }
	  if(mode == "sk") { var thisName = "addSK"; }
	  
    var toggle = false; 
    
    if(mode == "sl" && document.getElementById('addSL').innerHTML != "" && !anzahl) { document.getElementById('addSL').innerHTML = ""; toggle = true; }
	  if(mode == "sk" && document.getElementById('addSK').innerHTML != "" && !anzahl) { document.getElementById('addSK').innerHTML = ""; toggle = true; }
	  
	  if(toggle == false)
	  {	 	  
	    document.getElementById('addSL').style.opacity = 0.01;
	    document.getElementById('addSK').style.opacity = 0.01;
	    
	    document.getElementById('addSL').innerHTML = "";
	    document.getElementById('addSK').innerHTML = "";
	  
  		this.constructor();
  		var req = this.request;
  		var me = this;
  	  req.open('POST', 'add_artikel.php', true);
  		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  		req.onreadystatechange = function()
  		{				
  			me.handleResponseSearch(req,thisName);											
  		}			
  		
      if(!anzahl)
      {
        req.send('artikel='+artikel+'&mode='+mode);
        if(mode == "sl") { document.getElementById('addSL').style.opacity = 1; }
	      if(mode == "sk") { document.getElementById('addSK').style.opacity = 1; }              
      }
      else
      {
        req.send('artikel='+artikel+'&mode='+mode+'&anzahl='+anzahl);
        
        document.getElementById(thisName).style.MozOpacity = 1;
  		  document.getElementById(thisName).style.opacity = 1;
  		  document.getElementById(thisName).style.filter="alpha(opacity=100)";
        setTimeout("fadeOutItem('"+thisName+"')",1500);
      }
    }
	},
	
	
	
	
	openKombi: function(artikel)
	{ 
	  thisName = "autoSuggestionsList";
    
    showLayer('suggestions');		
		loadWait(thisName);
			
    this.constructor();
		var req = this.request;
		var me = this;
	  req.open('POST', 'kombi.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.onreadystatechange = function()
		{				
			me.handleResponseSearch(req,thisName);											
		}
    req.send('artikel='+artikel);    
  },
	
	
	handleResponseSearch: function(req,thisName)
	{	
	  if(req.readyState == 4)
		{
		  document.getElementById(thisName).innerHTML = req.responseText;			
		}
	}
}

var Timer;


function showLayer(ID)
{
	clearTimeout(Timer);
	document.getElementById(ID).style.display = 'block';	
}

function hideLayer(ID)
{
	clearTimeout(Timer);
	document.getElementById(ID).style.display = 'none';
}

function loadWait(divID)
{
  if(document.getElementById(divID))
  {
    document.getElementById(divID).innerHTML = "<img src=\"p/ajax_waiting.gif\" alt=\"wait\" title=\"wait\">";
  }
}

var globalSearch = new ajaxObj();



function fadeOutItem(divID)
{
  oc = document.getElementById(divID).style.opacity;
  if(oc > 0.0)
	{
	  
		
		oc = oc - 0.10;
		ocIE = ocIE - 10;
		
		document.getElementById(divID).style.MozOpacity = oc;
		document.getElementById(divID).style.opacity = oc;
		document.getElementById(divID).style.filter="alpha(opacity="+ocIE+")";
		
		setTimeout("fadeOutItem('"+divID+"')",30);		
	}
  else
  {
  
    document.getElementById(divID).innerHTML = "";
    var oc = 1;
    var ocIE = 100;

    // document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.MozOpacity = oc;
		document.getElementById(divID).style.opacity = oc;
		document.getElementById(divID).style.filter="alpha(opacity="+ocIE+")";
  }	
}


function fadeInItem(divID)
{	
  clearTimeout();
  oc = parseFloat(document.getElementById(divID).style.opacity);
  
  if(oc < 1)
	{
    oc = oc + 0.10;
		ocIE = ocIE + 10;
		
		document.getElementById(divID).style.MozOpacity = oc;
		document.getElementById(divID).style.opacity = oc;
		document.getElementById(divID).style.filter="alpha(opacity="+ocIE+")";
		
		setTimeout("fadeInItem('"+divID+"')",50);		
	}
  else
  {
  
    var oc = 1;
    var ocIE = 100;

    // document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.MozOpacity = oc;
		document.getElementById(divID).style.opacity = oc;
		document.getElementById(divID).style.filter="alpha(opacity="+ocIE+")";
  }	
}






function changeHeight(thisName,oldHeight,newHeight,clearing)
{
	changeSize = 40;
	changeTimeout = 10;
	
	if(parseInt(document.getElementById(thisName).style.height) == newHeight)
	{		
		if(oldHeight < newHeight)
		{
			if(parseInt(newHeight)-changeSize < 0)
			{
				document.getElementById(thisName).style.height = 0+"px";
				
				if(clearing == true)
				{
					document.getElementById('inner'+thisName).innerHTML = "";
				}
					
			}
			else
			{
				document.getElementById(thisName).style.height = parseInt(newHeight)-changeSize+"px";
				setTimeout(function() { changeHeight(thisName,oldHeight,parseInt(newHeight)-changeSize) } ,changeTimeout) ;
			}
		}		
		else
		{
		  document.getElementById(thisName).style.height = oldHeight+"px";
		}	
	}
	else
	{
		if(oldHeight < newHeight)
		{
			document.getElementById(thisName).style.height = parseInt(oldHeight)+changeSize+"px";
			setTimeout(function() { changeHeight(thisName,parseInt(oldHeight)+changeSize,newHeight)} ,changeTimeout) ;
		}		
		else
		{
			document.getElementById(thisName).style.height = newHeight+"px";			
		}
	}
}

function toggleHeight(thisName,setOldHeight,setNewHeight)
{  
	oldHeight = parseInt(document.getElementById(thisName).offsetHeight);
	newHeight = parseInt(document.getElementById('inner'+thisName).offsetHeight);
	
  if(setNewHeight) { newHeight = setNewHeight; }
	
	if(oldHeight >= newHeight)
	{
		if(setOldHeight) { oldHeight = setOldHeight; } else { oldHeight = 0; } 
	}
	else
	{
		if(oldHeight > 0 && oldHeight < newHeight)
		{
			if(setOldHeight) { oldHeight = setOldHeight; } else { oldHeight = 0; }
		}
	}
	changeHeight(thisName,oldHeight,newHeight);
}
