/**
 * Datei behinhaltet BO Funktionalitäten.
 * 
 * @author marlettas
 * @version $Id: backoffice.js 3115 2010-11-05 12:25:05Z marlettas $ 
 */
var logReader = null;
var newBooking = 0;
var errorsCollection = new Array();  
            
/**
 * Funktion liefert eine GUI Komponente zurück.
 * 
 * @param String component, welche Komponente soll geldane werden.
 * @param int id, die ID einer Abhängigkeit in der DB.
 * @param String eventHandler, javascript funktions aufruf für einen/mehrere Event(s).
 * @param int width die Breite der GUI Komponente.
 * @param String lang_code, der Sprachcode, in welcher Sprache die Komponente geladen werden soll.
 * @param String selected, welcher Eintrag Selektiert sein soll, bei Textfelder der anzuzeigende Text. 
 */
function getAJAXComponent(component, id, width, eventHandler, lang_code, selected, editorId) {
    var invokeService = ajaxPath + 'ajax_gui_components.php?';
    
    if(component == null || component == "") {
    	alert("AJAX Error.");
    }else {
        invokeService += 'component='+ component;
        
        invokeService += '&id='+id;
        invokeService += '&width='+width;
        invokeService += '&eventHandler='+eventHandler;
        invokeService += '&lang_code=' + lang_code;
        invokeService += '&selected=' + selected;
        invokeService += '&editorId=' + editorId;
        
        ajax_loadContent(component, invokeService);
    }
}

/**
 * Funktion ändern den Aktiv Zustand eines Users.
 * @param int userId die Benutzerkennung.
 * @param int der Aktiv-Zustand des Benutzers.
 */
function changeUserState(userId, userMod) {
	var invokeService = ajaxPath  + 'ajax_gui_direct.php?';
	invokeService += 'function=enable_user';
	invokeService += '&id=' + userId;
	invokeService += '&mod_user=' + userMod;
	
	ajax_loadContent('user_state_'+userId, invokeService); 
}

/**
 * Funktion gibt den UserMapping Editor zurück, nur wenn eingeloggter benutzer
 * überhaupt editieren darf.
 * @param String divId Name des DIVs.
 * @param String lang_code Sprachcode für das Gui-Element.
 * @param int userId die Benutzer des zu editierenden Benutzers.
 * @param int campId die Id des gemappten Campingplatzes.
 * @param int editorId die ID des edittierenden Benutzers.
 */
function getUserMappingGui(divId, lang_code, userId, editorId, width) {
    var invokeService = ajaxPath + 'ajax_gui_components.php?';
    
    if(divId == null || divId== "") {
        alert("AJAX Error.");
    }else {
        invokeService += 'component='+ divId;   
        invokeService += '&lang_code=' + lang_code;
        invokeService += '&userId=' + userId;
        invokeService += '&editorId=' + editorId;
        invokeService += '&width=' + width;
        
        ajax_loadContent(divId, invokeService);
    }
}

/**
 * Funktion läs front end sparch liste. 
 */
function ajaxLoadLanguageList(content, language, userId, list, sort, direction) {
	var invokeService = ajaxPath + "ajax_gui_components.php?";
	if(language == null || language == "")
	   return;
	   
	invokeService += "editorId=" + userId;
    
    invokeService += "&component=" + list;
    invokeService += "&content=" + content;
    invokeService += "&language=" + language;
    invokeService += "&sort=" + sort;
    invokeService += "&direction=" + direction;
    
	ajax_loadContent(list, invokeService);
}

function editLangData(id, langCode, modUser, event, new_value) {
    var invokeService = ajaxPath + "ajax_gui_direct.php?";
    invokeService += "id=" + id;
    invokeService += "&langCode=" + langCode;    
    invokeService += "&mod_user=" + modUser;
    invokeService += "&component=" + 'edit_data_lang'; 
    invokeService += "&event=" + event;
    invokeService += "&new_value=" + new_value;
    
    ajax_loadContent('edit_data_lang', invokeService);	
}

// AJAX ENDE

/**
 * Funktion schaltet zwischen Zeilen Highlight und Zilen normal um.
 * @param Object obj Zeile die gefärbt werden soll.
 * @param String className der CSS Klassenname auf den gefärbt werden soll.
 */
function highlightTableRow(obj, className) {
	obj.className = className;
}

/**
 * Prüft ein Feld auf Gültigkeit. 
 * @return boolean TRUE wenn gültig, anderfalls FALSE.
 * @param String value Zu prüfender Wert.
 * @param String checkFor Auf was das feld geprüft werden soll.
 */
function validate(element, checkFor) {
    var result = false;
    var value = element.value;
    
    if('email' == checkFor) {
    	   if(value.length <= 0)
    	       result = true;
    	       
    	   splitted = value.split("@");
    	   
    	   if(splitted.length >= 2 && splitted[splitted.length-1] != null) {
    	   	  if(splitted[splitted.length-1].search(/.[.][a-zA-Z]{2,4}/) != -1)
    	   	       result = true;
    	   }
    }else if('url' == checkFor) {
        if(value.length > 0) {
            if(value.search(/[a-z0-9.-]{10,255}/) != -1)
                result = true;
                
            if(result == true) {
            	if(value.indexOf("http://") > 1)
                    result = false;
                else if(value.indexOf("http://") == -1)
                    element.value = "http://" + value;
            }
        }else
           result = true;
    
    }else if('distance' == checkFor) {
          
          if(value != null && value != '') {
              var distance = getFloatFromValue(value);
              
              if(distance != 'NaN' || distance != Number.NaN) {
              	var newValue = formatNumber(distance, ',', '', 1);
                
              	if(newValue != 'NaN') {
              	     element.value = newValue;
              	     result = true;
              	}
              }
          }else
            result = true;
    }else if('integer' == checkFor) {
          if(value == "" || (parseInt(value) >= 0 && parseInt(value) != 'NaN' && parseInt(value) != Number.NaN))
                result = true;
    }else if('currency' == checkFor) {
        
        if(value != null && value != '') {
              var distance = getFloatFromValue(value);
          
              if(distance != 'NaN' || distance != Number.NaN) {
                var newValue = formatNumber(distance, ',', '', 2);

                if(newValue != 'NaN') {
                     element.value = newValue;
                     result = true;
                }
              }
          }else
            result = true;
    }else if('phone' == checkFor) { //nach DIN 5008
            /*if(value.length == 0) // leer werte erlaubt
                result = true;
            else if(value.length <= 15) {
    	       if(value.search(/[+]{0,1}[0-9]{1,}[-\]{0,1}[0-9]{1,}/) != -1) {
    	   	       result = true;  
    	       }
            }*/
    	result = true;
    }else if('textonly' == checkFor) {
    	if(value.search(/[A-Za-zäÄöÖüÜß]{0,}/) != -1)
    	   result = true;
    } else {
    	result = true;
    }
    
    if(result == true)
        removeError(element.id);
    else
        addError(element.id);
    
    return result;
}

/**
 * Funktion checkt den Beschreibungstext.
 * @param Object object Das TextArea Element.
 * @param String divId Die Id des DIVs für die Ausgabe.
 */
function changeRestChars(object, divId, booking) {
    var len = object.value.length;
    var maxLen = newBooking;
    var div = document.getElementById(divId);
    
    //alert("Length: " + len);
    if(newBooking >= 0 && divId == "beschreibung_kurz_rest") 
    	maxLen = 180;
    else if(
      newBooking >= 0 && booking == "drittel" || booking == "halbe" || booking == "ganze" || divId.startsWith("rest_") )
        maxLen = 360;
    
/*else
        alert("Fehler in Booking steht:" + booking + " & newBooking: " + newBooking);*/
    
    if(newBooking == 1)
        maxLen = 0;
    
    var restChars = maxLen-len;
    div.innerHTML =  maxLen-len + " / " + maxLen;
    
    addError(object.id);
    if(restChars < 0) {  // Text zu lang
    	 div.style.border = "solid 1px red";
    	 var confirmation = confirm("Ihre Selbstbechreibung ist " + (restChars*(-1)) + " zu lang.\nWünschen Sie eine automatische Begrenzung?");
    	 
         if(confirmation) {
    	   object.value = object.value.substr(0, maxLen-1);
    	   changeRestChars(object, divId, booking);
    	   removeError(object.id);
         }
    }else if(restChars == 0) { // Text genau
    	div.style.border = "solid 1px green";
    	removeError(object.id);
    }else {
    	div.style.border = "solid 1px green";
    	removeError(object.id);
    }
    
	changeTextArea(object);
}

/**
 * Wenn Anfahrt oder Beschreibung ändert.
 */
function changeTextArea(object) {
	var replacedEntry = object.value.replace(/&/g, "§UND§");
	var replacedEntry = replacedEntry.replace(/=/g, "§GLEICH§");
		
	var arrEntry = object.id + "=" + replacedEntry + "&";
    appendField(object.id, replacedEntry);
}

function setBooking(booking) {
    newBooking = booking;
}

function removeError(elementsId) {
	var index = findError(elementsId);
	if(index != -1) {
	   errorsCollection.splice(index, 1);
	   removeError(elementsId);
	}
}

function addError(elementsId) {
	var index = findError(elementsId);
	if(index == -1)
	   errorsCollection.push(elementsId); 
}

function findError(elementsId) {
    for(i = 0; i <= errorsCollection.length; i++) {
    	if(errorsCollection[i] == elementsId)
    	   return i;
    }
    
    return -1;
}

