var isMozilla, x, y,lx=0,ly=0;
var objDiv = null;
var over = false;
var historystage=1;
var startpage;
var running=0;
var savecommand="";
var saveform="";
var map;


function init_all(cnt)
{
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(historyChange);   
		if (dhtmlHistory.isFirstLoad()) {
			startpage=cnt;
		} 
					
		isMozilla = (document.all) ? 0 : 1;

    if (isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }

    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
}

function historyChange(newLocation, historyData) {
  var historyMsg = historyData;
	if (historyData==null) { historyMsg="index.php?ajax=true"+startpage+"&mode=ajax"}
  do_menue(historyMsg);
  var ajax_opml=new Ajax.Request( historyMsg,
  {
   method: "get",
   onSuccess: 
   function show(request)	{	
   	if (request.responseText=="login"){	window.location.href = "index.php?cnt=logout";} else {
   		$("Content").innerHTML=request.responseText;
  	}
   	},
   onFailure: 
   function error(request){document.location.href="index.php?cnt=logout";} 
  }
 	);           
}

function pause(zeit){
         //Zeit hier in ms angeben
         var starttime=(new Date).getTime();
         while (starttime+zeit>(new Date).getTime()){};
}

function opencnt(Layer,cnt,remember,update)
{

 if (running==1) {setTimeout("opencnt('"+Layer+"','"+cnt+"','"+remember+"','"+update+"')",100); 
} else {
  running=1;
 	
 if (remember!="false") {
 	dhtmlHistory.add(historystage.toString(),"index.php?ajax=true"+cnt+"&mode=ajax" );
 	historystage++;
}

 do_menue(cnt);
 quote_div_position=0;
 var ajax_opml=new Ajax.Request(
  "index.php?ajax=true"+cnt+"&mode=ajax",
  {
   method: "get",
   onSuccess: function show(request) {
   	if (request.responseText=="login"){window.location.href = "index.php?cnt=logout";} else {
   		if (update!='false') {$(Layer).innerHTML=request.responseText;}
   		
   		}
   	},
   onFailure: function error(request){document.location.href="index.php?cnt=logout";}
  }
 );}
}

function opencnt_post(Layer,cnt,pcnt,remember,update)
{
	
 	if (running==1) {

		setTimeout("opencnt_post('"+Layer+"','"+cnt+"','"+pcnt+"','"+remember+"','"+update+"')",100);
 	}else { 
  running=1;
 	/*if (remember!="false") {
 	dhtmlHistory.add(historystage.toString(),  "index.php?ajax=true"+cnt+"&mode=ajax" );
	historystage++;}*/

 do_menue(cnt);
var ajax_opml=new Ajax.Request(
  "index.php?ajax=true"+cnt+"&mode=ajax",
  {
   method: 'post',
   postBody: pcnt, 
   onSuccess: function show(request) {
   	if (request.responseText=="login"){window.location.href = "index.php?cnt=logout";} else {
	   	if (update!='false') {$(Layer).innerHTML=request.responseText;
   		
	   		}
	   	running=0; 
	  }  	
   	},
   onFailure: function error(request){document.location.href="index.php?cnt=logout";}
  }
 );
}
}
function setdatepicker()
{
 	if (running==1) {setTimeout("setdatepicker()",100);
 	}else { 
 		datePickerController.create();
 	}
}
function do_menue(cnt)
{
		if (document.getElementById('Menue1')) {document.getElementById('Menue1').style.height='25px';}
		if (document.getElementById('Menue2')) {document.getElementById('Menue2').style.height='25px';}
		if (document.getElementById('Menue3')) {document.getElementById('Menue3').style.height='25px';}
		if (document.getElementById('Menue4')) {document.getElementById('Menue4').style.height='25px';}
		if (document.getElementById('Menue5')) {document.getElementById('Menue5').style.height='25px';}
		if (document.getElementById('Menue6')) {document.getElementById('Menue6').style.height='25px';}
		if (document.getElementById('Menue7')) {document.getElementById('Menue7').style.height='25px';}
		if (document.getElementById('Menue8')) {document.getElementById('Menue8').style.height='25px';}

	var reg = cnt.search(/overview/);
	if (reg != -1) {
		if (document.getElementById('Menue1')) {document.getElementById('Menue1').style.height='40px';}
	}
	var reg = cnt.search(/stocklist/);
	if (reg != -1) {
		if (document.getElementById('Menue2')) {document.getElementById('Menue2').style.height='40px';}
	}	
	var reg = cnt.search(/pressen/);
	if (reg != -1) {
		if (document.getElementById('Menue3')) {document.getElementById('Menue3').style.height='40px';}
	}	

	var reg = cnt.search(/location/);
	if (reg != -1) {		
		if (document.getElementById('Menue5')) {document.getElementById('Menue5').style.height='40px';}	
	}
	var reg = cnt.search(/user/);
	if (reg != -1) {		
		if (document.getElementById('Menue8')) {document.getElementById('Menue8').style.height='40px';}	
	}
	var reg = cnt.search(/driveorder/);
	if (reg != -1) {
		if (document.getElementById('Menue4')) {document.getElementById('Menue4').style.height='40px';}
	}
	var reg = cnt.search(/client/);
	if (reg != -1) {
		if (document.getElementById('Menue7')) {document.getElementById('Menue7').style.height='40px';}
	}
	var reg = cnt.search(/admin/);
	if (reg != -1) {
		if (document.getElementById('Menue6')) {document.getElementById('Menue6').style.height='40px';}
	}
}
function change_input_value(id,cnt,pcnt,update) {
	if (running==1) {setTimeout("change_input_value('"+id+"','"+cnt+"','"+pcnt+"','"+update+"')",100);
 	}else { 
  running=1;
 	$("Loading").innerHTML='<img src=images/loading.gif />';
 	$("Loading").style.visibility='visible';
 	
	var ajax_opml=new Ajax.Request(
  "index.php?ajax=true"+cnt+"&mode=ajax",
  {
   method: 'post',
   postBody: pcnt, 
   onSuccess: function show(request) {
   	if (request.responseText=="login"){window.location.href = "index.php?cnt=logout";} else {
	   	if (update!='false')  {$(id).value=request.responseText;}
	   	$("Loading").innerHTML='';
	   	$("Loading").style.visibility='hidden';
	   }
   	running=0;},
   onFailure: function error(request){document.location.href="index.php?cnt=logout";}
  }
 );
}
}
function change_inner_value(id,cnt,pcnt,update) {
	if (running==1) {setTimeout("change_inner_value('"+id+"','"+cnt+"','"+pcnt+"','"+update+"')",100);
 	}else { 
  running=1;
 	$("Loading").innerHTML='<img src=images/loading.gif />';
 	$("Loading").style.visibility='visible';
 	
	var ajax_opml=new Ajax.Request(
  "index.php?ajax=true"+cnt+"&mode=ajax",
  {
   method: 'post',
   postBody: pcnt, 
   onSuccess: function show(request) {
   	if (request.responseText=="login"){window.location.href = "index.php?cnt=logout";} else {
	   	if (update!='false')  {$(id).innerHTML=request.responseText;}
	   	$("Loading").innerHTML='';
	   	$("Loading").style.visibility='hidden';
	  }
   	running=0;},
   onFailure: function error(request){document.location.href="index.php?cnt=logout";}
  }
 );
}
}
function update_pic() {
	if (running==1) {setTimeout("update_pic()",100);
 	}else { 
  	document.getElementById('picture').src='index.php?cnt=get_pic&remember=no&file=prod/pic_id'+document.getElementById('Article').value+'.jpg?{RANDOM}';
	}
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


	
function mod(divisee,base) {
	return Math.round(divisee - (Math.floor(divisee/base)*base));
}



	function set_save(val,valform){
		savecommand=val;
		saveform=valform;
	}
	function unset_save(){
		savecommand=""
		saveform="";
	}
	function MouseDown(e) 
{
	E=e;
    if (over)
    {
        if (isMozilla) {
            objDiv = document.getElementById("Layer5");
            x=e.pageX;
            y=e.pageY;
            return false;
        }
        else {
            objDiv = document.getElementById("Layer5");
            objDiv = objDiv.style;
            x=event.clientX;
            y=event.clientY;
        }
    }
}

function MouseMove(e) 
{
    if (objDiv) {
        if (isMozilla) {
            lx=lx+(e.pageX-x);
            ly=ly+(e.pageY-y);
            document.getElementById("Layer5").style.top = (ly) + 'px';
            document.getElementById("Layer5").style.left = (lx) + 'px';
 						x=e.pageX;
            y=e.pageY;
            return false;
        }
        else 
        {
        	 	lx=lx+(event.clientX-x + document.body.scrollLeft);
            ly=ly+(event.clientY-y + document.body.scrollTop);
            x=event.clientX ;
            y=event.clientY ;
            objDiv.pixelLeft = lx; 
            objDiv.pixelTop = ly; 
            return false;
        }
    }
}

function MouseUp() 
{
    objDiv = null;
}

function update_prod(get_url,vari) 
{
 var ajax_opml=new Ajax.Request(
  get_url+"&type="+document.getElementById("seltype"+vari).value,
  {
   method: "get",
   onSuccess: function(request){
   		var xml=request.responseXML;
      $("typeajax"+vari).innerHTML=request.responseText;
    },
   onFailure: function(request){
  		var xml=request.responseXML;
  		 $("typeajax"+vari).innerHTML="Error: Could not load requested stream.";
  	}
  }
 );
}

function isGeckoMac() {
  var rv = false;
  // Return value assumes failure
  var ua = navigator.userAgent;
  if (ua.indexOf("Gecko/") != -1 &&
          ua.indexOf("Mac ") != -1) {
    rv = parseFloat(ua.substring(ua.lastIndexOf('/')));
    /*
      for some older browser there is no version info
      in the end, assume true since we've gotten this far
    */
    rv = (isNaN(rv)) ? true : rv;
  }
  return rv;
};

function show_overlay(w,h,cnt,obj)
{
	if (isGeckoMac())  document.getElementById("Overlay_layer").style.overflow = "auto";
	document.getElementById("Overlay_layer").style.visibility="visible";
	
	document.getElementById("Layer5").style.visibility="visible";
	document.getElementById("O_CNT").style.visibility="visible";
	document.getElementById("Layer5").style.width=w;
	document.getElementById("Layer5").style.height=h;
	document.getElementById("O_CNT").style.height="90%";
	document.getElementById("Layer5").style.left="0px";
	document.getElementById("Layer5").style.top="0px";
	lx=0;ly=0;
	opencnt_post('O_CNT',cnt,obj,'false','true');		
}
function update_overlay(cnt) 
{
	var ajax_opml=new Ajax.Request(
  "index.php?ajax=true&remember=no"+cnt+"&mode=ajax",
  {
   method: "get",
   onSuccess: show_overlay_cnt   
  }
 );

}

function show_overlay_cnt(request)
{
	var xml=request.responseXML;
	$("O_CNT").innerHTML=request.responseText;
}

function hide_overlay()
{
	$("O_CNT").innerHTML="";
	if (isGeckoMac()) document.getElementById("Overlay_layer").style.overflow = "hidden";
	document.getElementById("Overlay_layer").style.visibility = "hidden";
	document.getElementById("Layer5").style.visibility="hidden";
	document.getElementById("O_CNT").style.visibility="hidden";
}

function show_variante(v) {
	show_overlay("700px","400px","index.php?cnt=project&action=showvariante&pic="+v);
}
function iprint(ptarget)
{
ptarget.focus();
ptarget.print();
} 
function OpenWindow (cnt) {
  hnd = window.open(cnt, "ReCoS", "width=800,height=600,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes");
  hnd.focus();
}

function resize_inner() {
	//if (document.getElementById("Content"))
	//	document.getElementById("Content").style.height=(document.getElementById("Page").offsetHeight-50)+"px";
	}
	
function checkEnter(e){ 
var characterCode;

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	return true
}
else{
	return false;
}
}
