function writeEmil(prefix, suffix, clazz) {
  document.write('<');
  document.write('a');
  document.write(' ');
  if (clazz != null && clazz != '') {
    document.write('class="' + clazz + '"');
    document.write(' ');
  }
  document.write('h');
  document.write('r');
  document.write('e');
  document.write('f');
  document.write('=');
  document.write('"');
  document.write('m');
  document.write('a');
  document.write('i');
  document.write('l');
  document.write('t');
  document.write('o');
  document.write(':');
  // email
  document.write(prefix);
  document.write('@');
  document.write(suffix);
  document.write('"');
  document.write('>');
  // text
  document.write(prefix);
  document.write('@');
  document.write(suffix);
  // end tag
  document.write('<');
  document.write('/');
  document.write('a');
  document.write('>');
}

function postForm() {
    document.forms[0].submit();
}
function resetForm() {
    document.forms[0].reset();
}

function selectItemPosWindow(customerId) {
        var breite = 420; //Breite des neuen Fensters
        var hoehe = 300;  //Höhe des neuen Fensters

        var seite = (screen.availWidth - breite) / 2;
        var oben = (screen.availHeight - hoehe) / 2;

        params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');

        F4 = window.open('../servlet/ReportServlet?action=show&name=item_additempos&param_cid=' + customerId ,'name',params);
        F4.focus();
}

function addItemPos(customerid, itemid) {
   window.opener.location.href = '../servlet/CustomerServlet?action=additempos&itemid=' + itemid + '&customerid=' + customerid;
   window.close();
}

/**
 * Open a window with a list of ItemPositions.
 * The current customer id has to be in the field with the id 'customerId'.
 * @return
 */
function itemPosSelect() {
    var breite = 420; //Breite des neuen Fensters
    var hoehe = 300;  //Höhe des neuen Fensters

    var seite = (screen.availWidth - breite) / 2;
    var oben = (screen.availHeight - hoehe) / 2;

    params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');

    F4 = window.open('../servlet/ReportServlet?action=show&name=customerItemPositions&param_customerId=' + document.getElementById('customerId').value + '&param_field1=itemposid&param_field2=itemposname' ,'name',params);
}

function searchCustomerByName() {
        var breite = 420; //Breite des neuen Fensters
        var hoehe = 300;  //Höhe des neuen Fensters

        var seite = (screen.availWidth - breite) / 2;
        var oben = (screen.availHeight - hoehe) / 2;

        params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');
        desc = document.forms[0].customerName.value;

        F4 = window.open('../servlet/ReportServlet?action=show&name=customerSearchByName&param_0=' + desc ,'name',params);
}
function setCustomer( id, description ) {
    window.opener.document.forms[0].customerId.value = id;
    window.opener.document.forms[0].customerName.value = description;
    window.opener.document.forms[0].customerName.focus();
    window.close();
}

function searchArticleGroup(linkParams) {
    var breite = 500; //Breite des neuen Fensters
    var hoehe  = 400;  //Höhe des neuen Fensters

    var seite = (screen.availWidth - breite) / 2;
    var oben = (screen.availHeight - hoehe) / 2;


    //params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes');
    params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');
    //desc = document.forms[0].customerName.value;

    if (linkParams == null) {
          linkParams = '';
    }

    F4 = window.open('../servlet/ArticleServlet?action=aglist&pagekey=agauswahl&expandAll=1' + linkParams ,'name',params);
}

function openInWindow(link) {
    var breite = 500; //Breite des neuen Fensters
    var hoehe  = 400;  //Höhe des neuen Fensters

    var seite = (screen.availWidth - breite) / 2;
    var oben = (screen.availHeight - hoehe) / 2;
    params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');    

    F4 = window.open(link,'name',params);
}


function searchReport( reportname, searchParam, searchText, fieldName1, fieldName2 ) {
    var breite = 500; //Breite des neuen Fensters
    var hoehe  = 400;  //Höhe des neuen Fensters

    var seite = (screen.availWidth - breite) / 2;
    var oben = (screen.availHeight - hoehe) / 2;

    params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben + ',scrollbars=yes' + ',status=yes' + ',resizable=yes');
    //desc = document.forms[0].customerName.value;

    F4 = window.open('../servlet/ReportServlet?action=show&name=' + reportname + '&param_' + searchParam + '=' + searchText + '&param_field1=' + fieldName1 + '&param_field2=' + fieldName2 ,'name',params);
}

function setValues( fieldName1, fieldValue1, fieldName2, fieldValue2 ) {
    window.opener.document.getElementById( fieldName1 ).value = fieldValue1;
    if ( fieldName2!=null && fieldName2 != 'null' ) {
        window.opener.document.getElementById( fieldName2 ).value = fieldValue2;
        window.opener.document.getElementById( fieldName2 ).focus();
    } else {
        window.opener.document.getElementById( fieldName1 ).focus();
    }
    window.close();
}

// swap the state of a boolean field (true/false)
function swapBoolean( elementId ) {
    val = document.getElementById( elementId ).value;
    if ( val=='true' ) {
        document.getElementById( elementId ).value = 'false';
        //alert( "Boolean: " + document.getElementById( elementId ).value );
    } else if ( val=='false' ) {
        document.getElementById( elementId ).value = 'true';
        //alert( "Boolean: " + document.getElementById( elementId ).value );
    } else {
        alert( "Kein boolean feld!" );
    }
}

// replace the special characters to use the valua as link attribute
function filterAttributeValue( val ) {
    neu = "";
    i = 0;
    while ( i < val.length ) {
        ch = val.charAt(i);
        if ( ch==" " )
            neu = neu + "%20";
        else if ( ch=="=" )
            neu = neu + "%3D";
        else if ( ch=="?" )
            neu = neu + "%3F";
        else if ( ch=="/" )
            neu = neu + "%2F";
        else if ( ch=="%" )
            neu = neu + "%25";
        else if ( ch=="!" )
            neu = neu + "%21";
        else if ( ch=="#" )
            neu = neu + "%23";
        else
            neu = neu + ch;
        i = i + 1;
    }
    return neu;
}

function setCurrentDateTime( field ) {
    var d = new Date();
    var day = d.getDate();
    if ( day < 10 ) {
        day = '0' + day;
    }
    var month = d.getMonth() + 1;
    if ( month < 10 ) {
        month = '0' + month;
    }
    var hour = d.getHours();
    if ( hour < 10 ) {
        hour = '0' + hour;
    }
    var min = d.getMinutes();
    if ( min < 10 ) {
        min = '0' + min;
    }
    var s = day + '.' + month + '.' + d.getFullYear() + ' ' + hour + ':' + min;
    document.getElementById( field ).value = s;
}

function findPos(obj) {
  var curtop = 0;
  var curleft = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return [curleft,curtop];
}

function toggleShow(itemId) {
  if (document.getElementById(itemId).style.display == '') {
    document.getElementById(itemId).style.display = 'none';
  } else {
    document.getElementById(itemId).style.display = '';
  }
}

function centeredWindow(link, breite, hoehe) {	//Zentriertes Fenster VLK Audio-Link VLK Rechter Frame

     var seite = (screen.availWidth - breite) / 2;
     var oben = (screen.availHeight - hoehe) / 2;

     params = ('width='+ breite +',height='+ hoehe +',left='+ seite +',top='+ oben+',scrollbars=yes');

     F4 = window.open( link,'name',params);
}

function setValue(elementId, value) {
  var el = document.getElementById(elementId);
  if (el == null) {
    alert("Element " + elementId + " wurde nicht gefunden.");
  } else {
    el.value = value;
  }
}
function setSource(elementId, value) {
  var el = document.getElementById(elementId);
  if (el == null) {
    //alert("Element " + elementId + " wurde nicht gefunden.");
  } else {
    el.src = value;
  }
}

function getMouseXY(e) {
  posx=0;posy=0;
  var ev =(!e) ? window.event : e; //IE:Moz
  if (ev.pageX){ //Moz    
    posx=ev.pageX - window.pageXOffset;
    posy=ev.pageY - window.pageYOffset;
  } else if(ev.clientX){ //IE
    posx=ev.clientX - document.body.scrollLeft;
    posy=ev.clientY - document.body.scrollTop;
  } 
  return [posx, posy];
}

function moveObjectY(elementId, dy, event) {
    var el = document.getElementById(elementId);
    if (el != null) {
      y = event.clientY + document.body.scrollTop;
      el.style.top = (y + dy) + 'px';
    }
}

function moveObjectXY(elementId, dx, dy, event) {
    var el = document.getElementById(elementId);
    if (el != null) {
      var y = event.clientY + document.body.scrollTop;
      el.style.top = (y + dy) + 'px';

      var x = event.clientX + document.body.scrollLeft;
      el.style.left = (x + dx) + 'px';
    }
}

function centerElementById(elementId) {
	var elem = document.getElementById(elementId);
	
	var viewport = getViewportDimensions();
    var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
    var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);
    
    // add scroll pos
    var pc = document.documentElement;
    if (pc) {    	
    	top = top + pc.scrollTop;    
    } 
    

    elem.style.left = left + 'px';
    elem.style.top = top + 'px';            
}

function toHtml(str) {
	str = str.replace('<','&lt;');
	str = str.replace('>','&gt;');
	str = str.replace('"','&quot;');
	return str;
}

/* use this method in onkeydown method of an input field */
function checkMaxCharLength(e, field, maxLen, remaining) {	
	if (e.keyCode == 8 ||
		e.keyCode == 35 ||
		e.keyCode == 36 ||	
		e.keyCode == 37 ||
		e.keyCode == 39 ||	 
		e.keyCode == 46 ) {          
		return true;
	}
	
    if (field.value.length < maxLen) {
	    return true;
    } else {  
	    return false;
    }        	  	 
}	

/* use this method in onkeyup method of an input field */
function calcRemainingChars(field, maxLen, remaining) {
	var len = field.value.length;
	var rem = maxLen - len;
	if (rem < 0) {
		// cut!
		field.value = field.value.substring(0, maxLen);
		rem = 0;
	}	

	document.getElementById(remaining).innerHTML = 'Verfügbare Zeichen: ' + rem;
}

_holdHelp = 'no';

function holdHelp() {
	_holdHelp = 'yes';    	  
} 

function showHelp(text, event) {
	var hel = document.getElementById('help')
	if (hel == null) {
    	hel = createHelp();
    }
	
    var el = document.getElementById('helpText');    
    el.innerHTML = text;

    hel.style.display = '';

    moveObjectXY('help', 5, 5, event);
    _holdHelp = 'no';
}

function hideHelp() {    
    if (_holdHelp == 'no') {
    	var el = document.getElementById('help');
        el.style.display = 'none';    
    }
} 

function createHelp() {
	var el = document.createElement("div");
	el.id = 'help';
	el.className = 'help';
	el.style.position = 'fixed';
	el.style.display = 'none';
	el.style.width = '250px';
	el.style.zIndex = '200';
		
	var html = 
    '<table class="help" border="0" cellspacing="0" cellpadding="0" style="width: 250px;">' +
      '<tr class="top" style="cursor: move;" onmousedown="dragstart(\'help\')">' +
          '<th class="left">Hilfe</th>' +
          '<th class="right"><b><a class="closeWindow" href="#" onclick="javascript:document.getElementById(\'help\').style.display = \'none\'; _holdHelp = false;">X</a></b></th>' +
      '</tr>' +
      '<tr class="bottom"><td class="bottom" id="helpText" colspan="2"> </td></tr>' +      
    '</table>'
    el.innerHTML = html;
      
	document.body.appendChild(el);
	return el;
}

function allowLength(e, field, maxLen, remaining) {	
	if (e.keyCode == 8 || 
	    e.keyCode == 33 ||
	    e.keyCode == 34 ||
		e.keyCode == 35 ||
		e.keyCode == 36 ||	
		e.keyCode == 37 ||
		e.keyCode == 38 ||
		e.keyCode == 39 ||	 
		e.keyCode == 40 ||
		e.keyCode == 46 
		) 
	{          
		return true;
	}
	
    if (field.value.length < maxLen) {
	    return true;
    } else {  
	    return false;
    }        	  	 
}	
function calcRemaining(field, maxLen, remaining) {
	var len = field.value.length;
	var rem = maxLen - len;
	if (rem < 0) {
		// cut!
		field.value = field.value.substring(0, maxLen);
		rem = 0;
	}	

	document.getElementById(remaining).innerHTML = 'noch ' + rem + ' Zeichen';
}	

function getViewportDimensions() {
    var h = 0, w = 0;
    
    if(self.innerHeight) {
       h = window.innerHeight;
       w = window.innerWidth;
    } else {
        if(document.documentElement && document.documentElement.clientHeight) {
            h = document.documentElement.clientHeight;
            w = document.documentElement.clientWidth;
        } else if(document.body) {
            h = document.body.clientHeight;
            w = document.body.clientWidth;            
        }
    }

    return {
        height: parseInt(h, 10),
        width: parseInt(w, 10)
    };
}

