/* $Id: default.js,v 1.14 2008-01-11 13:59:45 mgreiner Exp $ */

function getWidthOrHeight(getValue) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE Browsers
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible 	
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (getValue == 'width') return myWidth;
  if (getValue == 'height') return myHeight;
  return false;
}

	
function getHeight() {
	return getWidthOrHeight('height');
}

function getWidth() {
	return getWidthOrHeight('width');
}


function resize(top){
   element  = document.getElementById('navbar');
   newHeight = getHeight()-top;
   if (newHeight > 0 )
   		element.style.height = newHeight;
   
   var quicksearch = document.getElementById('quicksearch');		
   var quicksearchResult = document.getElementById('quicksearchResult');		
   if (quicksearch) {
	   var newHeight = getHeight()-135;	
	   var newHeight2 = getHeight()-230;
	   if (newHeight > 0 ) {
	   	quicksearch.style.height = newHeight+'px';
	   	quicksearchResult.style.height = newHeight2+'px';
       }
   } 
   
   var iFrame = document.getElementById('__frame__');		
   if (iFrame) {   	
	   	$('__frame__').style.width = document.viewport.getWidth()-203+'px';
		$('__frame__').style.height = document.viewport.getHeight()-125+'px';;   		
   }
     
}

function setbg(element, color) {
	element.style.background = color;
}

function setbg2(element, color, fontcolor) {
	element.style.background = color;
	element.style.color = fontcolor;
}

function selectToday() {}


function switchFormLayer(ebene) {
    
    document.getElementById('ebene1').style.visibility='hidden'; 
    document.getElementById('ebene2').style.visibility='hidden'; 
    document.getElementById('ebene3').style.visibility='hidden'; 
    document.getElementById('ebene4').style.visibility='hidden';
    
    document.getElementById(ebene).style.visibility='visible';

    return false;
}

function loadURL(href) {
	if (href != '') document.location = href;
}

function showHelp(topic) {
 	window.open("main.php?show=help&topic="+topic,"remote", "height=360, width=570, scrollbars=AUTO");
	return false;
}

function previewWindow() {
	document.forms.helpedit.preview.value = "1";
 	document.forms.helpedit.submit();
}

function gotoCust(id,cp) {
    if (id) {
		document.location = "main.php?ws=vcust&sub=details&edit="+id+"&cp="+cp;
	}
}

/*
wmtt = null;
wmtt = document.getElementById('tooltip');

document.onmousemove = updateWMTT;
document.onclick     = hideWMTT;

function updateWMTT(e) {

	if (!window.opera && document.all) {
		x = window.event.x + document.body.scrollLeft;
	    y = window.event.y + document.body.scrollTop;
	} else {	
		x = e.pageX;
		y = e.pageY;
	}
	if (wmtt != null) {
		wmtt.style.left = (x + 20) + "px";
		wmtt.style.top 	= (y - 20) + "px";
	}
}

function showWMTT() {
	wmtt = document.getElementById('tooltip');
	wmtt.style.display = "block"
}

function hideWMTT() {
	wmtt = document.getElementById('tooltip');
	wmtt.style.display = "none";
}
*/


var showError = function(r)
{
        alert("Fehler: " +r.status+ "\n" +r.statusText);
}

var loadHelpText = function(item)
{
    var param = "remote=loadHelpText&id="+item;
   	var myAjax = new Ajax.Updater(
       	"tooltip",
        "main.php",
   	    {
       	    parameters:param,
           	evalScripts:false,
            onFailure:showError,
            callback:showWMTT
   	    } 
   	  );   	
}



function indexOF(formuar,feld,value) {
}


function jump_to_today(formular,fieldName) {

           var datum = new Date();
           var day   = datum.getDate();
           var month = datum.getMonth();
           var year  = datum.getFullYear();
           
           var daySelector   = fieldName+'[D]';
           var monthSelector = fieldName+'[M]';
           var yearSelector  = fieldName+'[Y]';
           
           var form = document.forms[formular];
           
           if (form[daySelector].value != '0') {
           	  if (confirm('Datum ueberschreiben?')) {
           	  	form[daySelector].selectedIndex = day;
      	        form[monthSelector].selectedIndex = month+1;
        	    form[yearSelector].value = year;
           	  }           
           } else {           
	           form[daySelector].selectedIndex = day;
    	       form[monthSelector].selectedIndex = month+1;
        	   form[yearSelector].value = year;
           }
                      
           return false;
}

function uploadFile(module,dataset) {	
	window.open("main.php?show=upload&module="+module+"&dataset="+dataset,"remote", "height=240, width=480");
}

function uploadFileSubmit(module,id) {
	if (checkUploadData()) {
		var filename = document.forms.uploadFile.uploadFilename.value;
		if (document.forms.uploadFile.overwriteFile[0].checked) {
			submitFileupload();
		} else {	
			checkFileExists(filename,module,id);	
		}
	}
}

// ###############  Ajax #################### //

function checkFileExists(filename,module,id) {	

	var pb = "command=checkFileExists&filename="+filename+"&module="+module+"&id="+id;	

	var MyAjax = new Ajax.Request (
		"main.php",
		{
			method: 'post',
			postBody: pb,
			on404: submitFileupload,
			onSuccess: showOverwriteButton
		}
	);
}

function submitFileupload() {
	document.forms.uploadFile.submit();
}

function showOverwriteButton() {
	document.getElementById('ovw').style.visibility = 'visible';
}

function checkUploadData() {
	fields    = new Array();
	is_error  = false;
	result    = true;
	fields.push('uploadFilename');
	fields.push('uploadFileComment');
	
	for (i = 0; i < fields.length; i++) {
		if (!not_empty('uploadFile',fields[i])) {
			is_error = true;
			result = false;
		}
	}
	
	return result;
}

function nI() {
	alert('Noch nicht implementiert!');
}



/**
*	toogleVisibility(index) 
*	Schaltet die Sichtbarkeit in den Menüs um
*/

function toggleVisibility(index,cookie,rootElement) {

	/* The Prototype way - without animation 
	var element = $(index);
	if (element) {
	if (element.style.visibility == 'hidden') {
			element.style.visibility = 'visible';
			element.style.display    = 'block';
			storeMenustateInCookie(cookie,index,'open');
		} else {
			element.style.visibility = 'hidden';
			element.style.display    = 'none';
			storeMenustateInCookie(cookie,index,'closed');
		}
	}
	*/ 
		
	/* The jQuery way  - with animation */	
	var element = $j('#'+index);
	
	if (element) {
		if (element.is(':hidden')) {
			element.slideDown('fast');
			storeMenustateInCookie(cookie,index,'open');
			rootElement.className = 'menutitle_open';
		} else {
			element.slideUp('fast');			
			storeMenustateInCookie(cookie,index,'closed');
			rootElement.className = 'menutitle';
		}
	}

}

/**
*	showMenu(index) 
*	Schaltet die Sichtbarkeit in den Menüs an
*/

function showMenu(index,cookie) {

	/* The jQuery way  - with animation */	
	var element = $j('#'+index);
	
	if (element) {
		if (element.is(':hidden')) {
			element.slideDown('fast');
			storeMenustateInCookie(cookie,index,'open');
		}
	}
}


/**
*	hideMenu(index) 
*	Blendet ein Menu aus!
*/

function hideMenu(index) {
	
	var element = $j('#'+index);	
	if (element) {		
		element.hide();
	}
	var element = $j('#parent_'+index);	
	if (element) {
		element.get()[0].className = 'menutitle';		
	}
}



/* Cookie setzen */
function setCookie(c_name,value,expiredays) {
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}


/* Cookie lesen */
function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=")
  		if (c_start!=-1) {
        	c_start=c_start + c_name.length+1
        	c_end=document.cookie.indexOf(";",c_start)
        	if (c_end==-1) c_end=document.cookie.length
        	return unescape(document.cookie.substring(c_start,c_end))
    	}
  	}
	return ""
}


function addEvent(obj, evType, _function_, useCapture)
{
  if (obj.addEventListener) {
    obj.addEventListener(evType, _function_, useCapture);
    return true;
  } else if (obj.attachEvent) {
    var retVal = obj.attachEvent("on"+evType, _function_);
    return retVal;
  } else {
    return false;
  }
}

/* 
* Men�status zur�ck in das Cookie schreiben 
* TODO:
* Bei aktivem Javascript und deaktivierten Cookies,
* den Status per AJAX Request in die Session auf dem
* Server speichern!
*/
function storeMenustateInCookie(cookie,id,state) {
	var currentState = getCookie(cookie);
	var cookieElements = currentState.split(';');
	var newCookieState = "";
	for (var i = 0; i < cookieElements.length; i++) {
		var element = cookieElements[i].split('=');
		if (element[0] == id) {
			newCookieState += id+'='+state+';';
		} else {
			if (cookieElements[i].length)
				newCookieState += cookieElements[i]+';';
		}
	} 
	setCookie(cookie,newCookieState,1); 
}


function utf8_decode(utftext) {
	var plaintext = ""; 
	var i=0; 
	var c=c1=c2=0;	
	// while-Schleife, weil einige Zeichen uebersprungen werden
	while(i<utftext.length) {
		c = utftext.charCodeAt(i);
        if (c<128) {
        	plaintext += String.fromCharCode(c);
            i++;
        } else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
            i+=2;
        } else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
            plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
            i+=3;
        }
    } //while 
    
    return plaintext;
}


function isutf8(utftext) {
	var plaintext = ""; 
	var i=0; 
	var c=c1=c2=0;	
	// while-Schleife, weil einige Zeichen uebersprungen werden
	while(i<utftext.length) {
		c = utftext.charCodeAt(i);
        if (c<128) {
        	plaintext += String.fromCharCode(c);
            i++;
        } else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
            i+=2;
        } else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
            plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
            i+=3;
        }
    } //while 
    
    return plaintext == utftext ? false : true;
}


/* Workaround um die Daten in der Session an das aktive Fenster anzugeleichen */
function updateSessionValues() {
	var field = $('__fk_customerId__');
	if (field && field.value) {
		var param = "remote=updateSessionValue&sv=currentCustomerId&currentCustomerId="+field.value;
		var myAjax = new Ajax.Request(       	
        "main.php",
   	    {
       	    parameters:param,
           	evalScripts:false          
   	    } 
   	  );  	
	}
}

function updateCurrentCustomerSessionValues(fk_customerId) {		
		var param = "remote=updateSessionValue&sv=currentCustomerId&currentCustomerId="+fk_customerId;
		var myAjax = new Ajax.Request(       	
        "main.php",
   	    {
       	    parameters:param,
           	evalScripts:false          
   	    } 
   	  );  	
}


function setStartPage() {
		
		var currentPage = document.location;
		var param = "remote=setStartPage&location="+escape(encodeURI(currentPage));
		var myAjax = new Ajax.Request(       	
        "main.php",
   	    {
       	    parameters:param,
           	evalScripts:false           
   	    } 
   	  );
	
}

function showInfo(transport) {
	alert(transport.responseText);
}
