//used in template files as well (Same name as in php specific files)!
MAX_TEXT_LENGTH = 3700;


// General function to open information windows
// strURL = URL, numWidth = windowWidth, numHeight = windowHeight
function openInfoWindow(strURL, numWidth, numHeight){
   var strWindowName = "infoWindow";
   var strWinProps = "width=" + numWidth + ",height=" + numHeight + ",titlebar=no,status=yes,toolbar=no,menubar=no,scrollbars=no,resizable=yes";
   openWindow(strURL, strWindowName, strWinProps); 
}

// open SMS Info-Window with specified size
function openSMSInfoWindow(){
   openInfoWindow("smsInfo.html", 500, 700);
}

function openDefaultWindow(strURL, numWidth, numHeight){
   var numOffset = 100;
   var objScreen = getScreenDimensions();
   (!numWidth) ? numWidth = "780" : 0;
   (!numHeight) ? numHeight = "600" : 0;   
   (numWidth > objScreen.numWidth) ? numWidth = (objScreen.numWidth - numOffset) : 0;
   (numHeight > objScreen.numHeight) ? numHeight = (objScreen.numHeight - numOffset) : 0;
   //alert("Breite: " + numWidth + " | Hoehe: " + numHeight);
   var strWindowName = "defaultWindow";
   var strWinProps = "width=" + numWidth + ",height=" + numHeight + ",titlebar=no,status=yes,toolbar=no,menubar=no,scrollbars=yes,resizable=yes";
   var objWin = openWindow(strURL, strWindowName, strWinProps);       
}

function openWindow(strURL, strWindowName, strWinProps){
   var objWin = window.open(strURL, strWindowName, strWinProps); 
   objWin.focus();
}

// Gets the size of the whole screen
function getScreenDimensions(){
   var obj = {numWidth:screen.availWidth, numHeight:screen.availHeight};
   return obj;
}

// Gets the size of a window
function getWindowDimensions(){
   if (window.outerWidth){
      var numWidth = window.outerWidth;
      var numHeight = window.outerHeight;
   } else if (document.body){
      var numWidth = document.body.offsetWidth;
      var numHeight = document.body.offsetHeight;
   } else { // NS 3.0 etc.
      var numWidth = Math.round(screen.availWidth * 0.75);
      var numHeight = Math.round(screen.availHeight * 0.75);
   }
   var obj = {numWidth:numWidth, numHeight:numHeight};
   return obj;
}


// Focus of defined field
function focusField(strFieldName, strFormName){
   if(!strFormName){
      var objForm = document.forms[0];
      strFormName="[0]";
   } else 
      var objForm = document.forms[strFormName];
   objField = objForm[strFieldName];
   // Message if the element to select does not exist
   if (typeof(objField) == 'undefined'){
      // remove comment below for debugging
      // alert("Feld '" + strFieldName + "' in Formular '" + strFormName + "' des Focus nicht vorhanden!")
   } else {
      objField.focus();
      // Don't select field if it is a selectbox
      (objField.type != "select-one") ? objField.select() : 0;
   }
}

// returns formnumber and elementnumber within the form as an object (numForm and numElement)
function getElementPropsObj(objElement){
   var numForm = 0, numElement = 0;
   while(document.forms[numForm]){
      while(document.forms[numForm].elements[numElement]){
         if(document.forms[numForm].elements[numElement] == objElement) {
            return {numForm:numForm, numElement:numElement};
            break;
         }
         numElement++;
      }
      numForm++;
   }
}

// Gets the value of a textfield
function getValue(objElement){
   var strValue = getDom(objElement).value;
   return strValue;
}

function loadPage(strUrl){
   location.href = strUrl;
}

/*********************************************************/
/******************* String-Functions ********************/

function trim(s) {
   while (s.substring(0, 1) == " ") {
      s = s.substring(1, s.length);
   }
   while (s.substring(s.length-1, s.length) == ' ') {
      s = s.substring(0, s.length-1);
   }
   return s;
}

function getURLValue(varname){
   var url = window.location.href;
   
   // split the url by the ?
   var qparts = url.split("?");
   if (qparts.length == 1) return "";
   
   // Then find the querystring, everything after the ?
   var query = qparts[1];
   
   // Split the query string into variables (separates by &s)
   var vars = query.split("&");
   
   // Initialize the value with "" as default
   var value = "";
   
   // Iterate through vars, checking each one for varname
   for (i=0;i<vars.length;i++){
      // Split the variable by =, which splits name and value
      var parts = vars[i].split("=");
      
      // Check if the correct variable
      if (parts[0] == varname) {
         // Load value into variable
         value = parts[1];
         
         // End the loop
         break;
      }
   }
   
   // Convert escape code
   value = unescape(value);
   
   // Convert "+"s to " "s
   value.replace(/\+/g," ");
   
   // Return the value
   return value;
}

/******************************************************/
/******************* Key-Tools ************************/
/* (Used in pages with select-fields for submitting 
   the page when ENTER was pressed in select-fields) */

// Checks for ENTER-key
function checkKey(e){
   var ev = (window.Event) ? e : window.event;
   var pressedKey = (window.Event) ? ev.which : ev.keyCode;
   var target = (window.Event) ? ev.target : ev.srcElement;
   //alert("Key '" + pressedKey + "' was pressed at field '" + target.name + "'");
   //alert(target.name);
   // Call "DoSearch"-Function in page if ENTER was pressed
   if (pressedKey == 13) {
      // Check if function is defined on side
      var booAction = doKeyAction(target.name);
      // If there is a function defined return false 
      // (used for onkeypress in textfields to proof if ENTER was pressed)
      return (booAction) ? false : true;
   }
}

// Defines keypress-handler for defined element
// ( Called in init()-Function in page )
function setField(elementToObserve){
   (window.Event) ? document.captureEvents(Event.KEYPRESS) : 0;     // Netscape, Mozilla, Opera Handling
   eval(elementToObserve).onkeypress = checkKey;                // IE Handling
}


/******************************************************/
/******************* DHTML-Tools **********************/
/* (Used e.g. in Startpage with bestOf-Entries)       */

function toggleLoginWindow(){
   var objRef = getReference("loginWindow");
   (objRef.display == "none" || objRef.display == "") ? showElement("loginWindow") : hideElement("loginWindow");   
}

// Switch display-property. Hide if visible - show if not.
function toggleDisplay(strElement){   
   var objRef = getReference(strElement);
   var arrHelpToggleElements;
   var numCounter = 0;
   var objRef2 = null;

   if (objRef.display == "none" || objRef.display == ""){
      showElement(strElement);
      if( strElement == "helpLayer" ) {
         // Get array of elements that should be toggled when helpLayer is shown and set visibility to hidden
         if (typeof(getHelpToggleElements) == 'function'){
            arrHelpToggleElements = getHelpToggleElements();
            var infoObj = getDom( strElement );
            var infoLayer = getDom( "infoLayer" );
   
            infoLayerTop = getRealTop( infoLayer );

            while (numCounter < arrHelpToggleElements.length) {
               var listObj = getDom( arrHelpToggleElements[numCounter] );

               if( listObj.type == "hidden" ) {                
                  numCounter++; 
                  continue; 
               }

               listObjectTop = getRealTop( listObj );

               if( listObj != null ) {
                  if( listObjectTop < (infoLayerTop + infoLayer.offsetHeight) &&
                      listObj.offsetLeft < (infoLayer.offsetLeft + infoLayer.offsetWidth )) 
                  {
                     getReference( arrHelpToggleElements[numCounter]).visibility = "hidden";
                  }
               }
               numCounter++;
            }
         }
       
         scrollUp();
      }
   } else {
      hideElement(strElement);
      if(strElement == "helpLayer"){
         // Get array of elements that should be toggled when helpLayer is shown and set visibility to visible
         if (typeof(getHelpToggleElements) == 'function'){
            arrHelpToggleElements = getHelpToggleElements();
            while (numCounter < arrHelpToggleElements.length) {
               getReference(arrHelpToggleElements[numCounter++]).visibility = "visible";
            }
         }
      }
   }
}


// scroll page up to make info window visible on the screen
function scrollUp() {
   window.scrollTo(0,0);
}


function to_string( arr ) {
   var txt="";
   var i;
   for( i=0; i<arr.length; i++ ) {
      txt += arr[i] +',';
   }
   return txt;
}


function in_array( arr, value ) {
   var i;
   for( i=0; i<arr.length; i++ ) {
      if( arr[i] == value )
         return true;
   }
   return false;
}

function isTableTag( obj ) {
   return (obj.tagName == 'TD' || obj.tagName == 'TR' );
}

//calculates the top position of the object
function getRealTop( obj ) {
   var y = obj.offsetTop;
   var parent = obj.parentElement;
   var values = new Array();
   values.push( y );
   
   while( parent  ) {
      var nextParent = parent.parentElement;
      var top = parent.offsetTop;
      if( top > 0 && !in_array( values, top ) && !isTableTag( nextParent) ) 
      {
         values.push( parent.offsetTop );
         y += top;
      }
      parent = nextParent;
   }
   
   return y;
}


// Show Element (display)
function showElement(strElementName){
   var objRef = getReference(strElementName);
   objRef.display = "block";
   objRef.visibility = "visible";
}

// Hide Element (display)
function hideElement(strElementName){
   var objRef = getReference(strElementName);
   objRef.display = "none";
}

// Hide Element (display)
function blindOutElement(strElementName){
   var objRef = getReference(strElementName);
   objRef.visibility = "hidden";
}

// Set and get Color
function setColor(strElementName, strColor){
   var objRef = getReference(strElementName);   
   objRef.color = strColor;
}
function getColor(strElementName){
   var objRef = getReference(strElementName);   
   return objRef.color;
}

// Set and get BackgroundColor
function setBackgroundColor(strElementName, strColor){
   var objRef = getReference(strElementName);
   objRef.backgroundColor  = strColor;
}
function getBackgroundColor(strElementName){
   var objRef = getReference(strElementName);
   return objRef.backgroundColor;
}

// Disable and enable elements
var strCurrTextfieldColor, strCurrentTextfieldBG;
function disableElement(strElementID){
   // store current element-properties
   strCurrTextfieldColor = getColor(strElementID);
   strCurrentTextfieldBG = getBackgroundColor(strElementID);
   // set disabled-properties of element
   var objElement = getDom(strElementID);
   if(typeof objElement != "undefined"){
      setColor(strElementID, "#666666");
      switch(objElement.type){
         case "text":            
            setBackgroundColor(strElementID, "#cccccc");
            objElement.disabled = true;
            break;
      }      
   }
}
function enableElement(strElementID){
   // use default-properties for element (e.g. textfield) if nothing available
   if(!strCurrTextfieldColor) var strCurrTextfieldColor = "#000000";
   if(!strCurrentTextfieldBG) var strCurrentTextfieldBG = "#ffffff";
   // set enabled-properties of element
   var objElement = getDom(strElementID);
   if(typeof objElement != "undefined"){
      setColor(strElementID, strCurrTextfieldColor);
      switch(objElement.type){
         case "text":            
            setBackgroundColor(strElementID, strCurrentTextfieldBG);
            objElement.disabled = false;
            break;
      }
   }
}



// Get Object-Reference, dependent on browser
function getReference(strElementName){
   var objRef;
   if(document.all) objRef = document.all[strElementName].style;
   else if(document.layers) objRef = document[strElementName];
   else if(document.getElementById) objRef = document.getElementById(strElementName).style;
   return objRef;
}

function getDom(strElementName){
   var objRef;
   if(document.all) objRef = document.all[strElementName];
   else if(document.layers) objRef = document[strElementName];
   else if(document.getElementById) objRef = document.getElementById(strElementName);
   return objRef;
}

/******************************************************/
/* Form field Limiter script- By Dynamic Drive
/* For full source code and more DHTML scripts, visit http://www.dynamicdrive.com */

var ns6 = document.getElementById && !document.all;

function restrictinput(maxlength,e,placeholder){
   if (window.event && event.srcElement.value.length >= maxlength)
      return false;
   else if (e.target && e.target == eval(placeholder) && e.target.value.length >= maxlength){
      var pressedkey = /[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
      if (pressedkey.test(String.fromCharCode(e.which)))
      e.stopPropagation();
   }
}

function countlimit(maxlength, e, placeholder){
   var theform = eval(placeholder)
   var lengthleft = maxlength-theform.value.length
   var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder)
   if (window.event || e.target && e.target == eval(placeholder)){
      if (lengthleft < 0)
         theform.value = theform.value.substring(0, maxlength);
      placeholderobj.innerHTML = lengthleft;
   }
}

function displaylimit(thename, theid, thelimit){
   var theform = theid != "" ? document.getElementById(theid) : thename;
   var limit_text = '<span id="' + theform.toString() + '">' + thelimit + '</span>';
   if (document.all||ns6)
      document.write(limit_text);
   if (document.all){
      eval(theform).onkeypress = function(){ return restrictinput(thelimit, event, theform)}
      eval(theform).onkeyup = function(){ countlimit(thelimit, event, theform)}
   }
   else if (ns6){
      document.body.addEventListener('keypress', function(event) { restrictinput(thelimit, event, theform) }, true); 
      document.body.addEventListener('keyup', function(event) { countlimit(thelimit, event, theform) }, true); 
   }
}

function initCounterChars(thename, theid, maxlength){
   var theform = theid != "" ? document.getElementById(theid) : thename;
   var numLength = maxlength - (eval(theform).value.length);
   getDom(theform).innerHTML = numLength;
}

