/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function invokeSOAP(url, method, parameters, async, callbackfunction,parseEntityAsId){
    var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    xml += "<action method='"+method+"'>";
    xml += parameters.toXml();
    xml+= "</action>";
  
    url = "http://"+document.location.host+"/com.hme.web.new-war/RequestProvider?action=processSOAPXML&idparsing="+(parseEntityAsId?"true":"false");
    
    var req = createXMLHttpRequest();
   
    req.open('post', url,async);
    if (async){
        req.onreadystatechange= function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    //alert(http_request.responseText);
                    if (!callbackfunction){
                        var obj = new Array();
                        if ( req.responseXML){
                            for (var i = 0; i< req.responseXML.getElementsByTagName("root")[0].childNodes.length;i++){
                                obj[obj.length] =xmlToObject(req.responseXML.getElementsByTagName("root")[0].childNodes[i]);
                            }
                        }       
                        return obj;
                    }
                    else {
                        callbackfunction(req);
                    }
                } else {
                    alert('Bei dem Request ist ein Problem aufgetreten.');
                }
            }

            
        }

    }

      
    
  //  alert(xml);
    req.send(xml);
      if (!async && !callbackfunction){
            var obj = new Array();
            if ( req.responseXML){

                for (var i = 0; i< req.responseXML.getElementsByTagName("root")[0].childNodes.length;i++){

                    obj[obj.length] =xmlToObject(req.responseXML.getElementsByTagName("root")[0].childNodes[i]);
                }
            }
            return obj;
        }
        else if (!async){
            callbackfunction();
        }
       
}
function refillData(values,divname){
    tablediv  = document.getElementById(divname);
    tablediv.innerHTML = "";
    createTable(tablediv.columnNames, tablediv.dataColumns, values, divname, tablediv.onselect, tablediv.checkbox, tablediv.editors, tablediv.widths)
}

function createTable(columnNames,dataColumns,values,divname,onselect,checkbox,editors,widths){
    div  = document.getElementById(divname);
    div.widths = widths;
    div.onselect = onselect;
    div.columnNames= columnNames;
    div.checkbox = checkbox;
    div.editors = editors;
    div.dataColumns = dataColumns;
    html = "<table width='100%' cellspacing='0' cellpadding='0' class=\"formtable\"><tr class=\"formtable_header\"> "+(checkbox?"<td class='formtable_header formtable_td' style='width:20px;'></td>":"");
    // html += "<td>Name</td><td>Beschreibung</td><td>Preis</td></tr>";
    for (var i = 0; i< columnNames.length;i++){
        html += "<td class='formtable_header formtable_td'>"+columnNames[i]+"</td>";
    }
    if (values){
        for (i =0; i< values.length;i++){
            var arr = new Array();
            for (var j = 0; j < dataColumns.length;j++){
                var col = dataColumns[j];
                if (col && col.toString() &&col.toString().indexOf("date=")>-1){
                    col = col.substring(5);
                    var val = values[i][col];
                    if (val){
                        //  var date = new Date(val);
                        arr[j]= longToDateString(val);
                    }
                    else {
                        arr[j]= "";
                    }
                }
                else {
                    arr[j]=values[i][dataColumns[j]];
                }
            }
            html += createTableRow(i,arr , divname,onselect,checkbox,editors,widths);
        }
        div.values = values;
    }
    else{
        div.values = new Array();
    }
    div.innerHTML = html;
    
}

function removeTableRow(i,divname){
    var trRow = document.getElementById(divname+"tr"+i);
    correctureColors(i,divname);
    var previous = trRow.previousSibling.previousSibling;
    var next = trRow.nextSibling;
    trRow.parentNode.removeChild(trRow);
    
    var busdiv  = document.getElementById(divname);
    var index = busdiv.index;
    var value = busdiv.values[index];
    //alert(previous.nodeName+" "+next.nodeName);
    if (previous ){
        var va = previous.nextSibling.id.substring((divname+"tr").length);
        selectTableRow(divname,va);
    }
    else if (next){
        var va2 = next.id.substring((divname+"tr").length);
        selectTableRow(divname,va2);
    }
    else {
        busdiv.index = null;
        defaultButtonVisible(divname);
    }
       
}
function selectTableRow(divname,i){
    
    var oldIndex = document.getElementById(divname).selectionindex;
    document.getElementById(divname).index=i;
    document.getElementById(divname).selectionindex=i;
    defaultButtonVisible(divname);
    if (oldIndex != i){
        var trElement = document.getElementById(divname+"tr"+i);
        var trOldElement = document.getElementById(divname+"tr"+oldIndex);
       
        if (trElement)
            trElement.className = "formtable_row_selected";
        if (trOldElement)
            trOldElement.className=getTableColor(oldIndex);
    }
    
    var editDiv = document.getElementById(divname+"_edit");
    //  alert("edit "+editDiv);
    if (editDiv){
        var values = document.getElementById(divname).values;
        var value = values[document.getElementById(divname).index];
        for (var i in value){
            var ele = document.getElementById(divname+"_edit"+i);
            if (!ele){
                ele = document.getElementById(divname+"_"+i);
            }
            //alert("edi2t "+divname+"_edit"+i+" ele = "+ele);
            if (ele){
                ele.value = value[i];
                if (ele.hasAttribute("internalType")){
                    var attribute = ele.getAttribute("internalType");
                    if (attribute == 'text'){
                        validateTextValue(ele.getAttribute("optional"), ele,divname);
                    }
                    if (attribute == 'number'){
                        validateNumberValue(ele.getAttribute("optional"), ele,divname);
                    }
                    if (attribute == 'currency'){
                        validateCurrencyValue(ele.getAttribute("optional"), ele,divname);
                    }
                }
                if (ele.type == 'checkbox'){
                  
                    ele.checked = value[i] == 'true'?true:false;
                }
            }
        }
        updateMessage(divname);
        
    }
                
}
function getTableColor(c){
    return (c%2 ?"formtable_row1":"formtable_row2");
}


function correctureColors(i,divname){
    var element = document.getElementById(divname+"tr"+i);
    var elements = element.parentNode.childNodes;
    for (var j = 0;j < elements.length;j++){
        if (j > i){
            elements[j].className = getTableColor(j);
        //elements[j].id = divname+"tr"+j;
        }
    }
}
function appendTableChanges(values,i,divname){
    var element = document.getElementById(divname+"tr"+i);
    var html = "<td></td>";
    for (var j = 0;j < values.length;j++){
        html += "<td>"+values[j]+"</td>";
    }
    element.innerHTML = html;
}
function defaultButtonVisible(divname){
    var div = document.getElementById(divname);
    var editButton = document.getElementById(divname+"_buttonedit");
    var deleteButton = document.getElementById(divname+"_buttondelete");
    
    if (div){
        var index = div.index;
        if (index != null){
            if (editButton){
                editButton.style.display='block';
            }
            if (deleteButton){
                deleteButton.style.display='block';
            }
        }
        else {
            if (editButton){
                editButton.style.display='none';
            }
            if (deleteButton){
                deleteButton.style.display='none';
            }
        }
        
        
    }
}

var onselect;
function checkbox(onselect) {
    this.onselect = onselect;
}
var column;
var fieldName;
var openCheck;
var type;

function editor(column,openCheck,fieldName,type,callback){
    this.column = column;
    this.openCheck = openCheck;
    this.fieldName = fieldName;
    this.type = type;
    this.callback = callback;
}

function aktivateEditor(divname,position){

    var div = document.getElementById(divname);
    var index = div.index;
    var value = div.values[index];
    
    var editors = div.editors;
    for (var i = 0; i< editors.length;i++){
        if (editors[i].column == position){
            if (editors[i].openCheck(divname))
            {
                var tdDivName = divname+"tr"+index+"td"+editors[i].column;
                var actionName;
                var internalType;
                if (editors[i].type == 'currency'){
                    actionName = "setTableCurrencyValue";
                }  if (editors[i].type == 'text'){
                    actionName = "setTableValue";
                }
                if (editors[i].type == 'own'){
                    editors[i].callback(column,row);
                }
                else if (actionName){
                   
   
                    var mainDiv = document.getElementById(divname);
                    var mainIndex = mainDiv.index;
                    var val = mainDiv.values[mainIndex];
                    var mainVal =  val[mainDiv.dataColumns[position-1]];
            
                    var html = '<table cellpadding="0" cellspacing="0" style="width:100%;">';
                    html+=' <tr> <td width="5"><img id="'+tdDivName+'_img" src="hmepage_images/empty.png"/></td> <td >'

                    html +="<input id='"+tdDivName+"input' type='text' valid=true internalType='currency' optional=true class=\"inputfield\"";
                    html+="  onchange=\""+actionName+"(this,'"+divname+"',"+position+","+mainIndex+");\"";
                    html+=" onfocus=\"appendStylePrefix('focused',this);\"";
                    html+= " onblur=\"removeStylePrefix('focused',this);"+actionName+"(this,'"+divname+"',"+position+","+mainIndex+");\"";
                    html+=" value='"+mainVal+"'>";
                    //     <input id="${param.inputname}" name="${param.inputname}" valid="${param.optional}" type="text" internalType="currency" optional="${param.optional}" "
                    // >
                    html += "</td></tr></table>";

                   
                    var ele = document.getElementById(tdDivName);
                    ele.editor = editors[i];
                    ele.innerHTML = html;
                   
                    focusEditor(tdDivName+"input");
                }
            }
        }
    }
}

function focusEditor(elementName){
    var element = document.getElementById(elementName);
    element.focus();
}
function focusErrorEditor(elementName,divname,oldindex){
    var div = document.getElementById(divname);
    if (div.index != oldindex){
        selectTableRow(divname, oldindex);
    }
    
    var element = document.getElementById(elementName);
    var divName = element.id.substring(0,element.id.indexOf('input'));
    var imgElement = document.getElementById(divName+"_img");
    imgElement.src = "hmepage_images/alert_round.png";
    imgElement.title ="Ungültiger Wert";
    element.focus();
}
function setTableValue(element,divname,column,oldindex){
    if (element){
        var editor = element.editor;
        var value = element.value;
         
        var divName2 = element.id.substring(0,element.id.indexOf('input'));
        var div = document.getElementById(divName2);
        var mainDiv = document.getElementById(divname);
        var index = mainDiv.index;
        var val = mainDiv.values[index];
        val[mainDiv.dataColumns[column-1]] = value;
        div.innerHTML = value;
    /*
        imgElement.src = emptyPic;
        imgElement.title ="";
        element.setAttribute("valid", true);
        if (getFormValueStatus(divname)){
            setMessageHTML(null,null , divname);
        }*/
        
    }
}
function setTableCurrencyValue(element,divname,column,oldindex){
    if (element){
        var editor = element.editor;
        var value = element.value;
        if (!IsNumeric(value)){
            focusErrorEditor(element.id,divname,oldindex);
            setTimeout(focusErrorEditor, 100, element.id,divname,oldindex);
        /*        element.setAttribute("valid", false);
        setMessageHTML("Einige benötigte Felder haben keine Daten",alertPic , divname);
         imgElement.title =!IsNumeric(value)?"Der Wert darf nur Zahlen enthalten":"Dieses Feld darf nicht leer sein";  
        imgElement.src = "images/alert_round.png";*/
        }
        else {
            var divName2 = element.id.substring(0,element.id.indexOf('input'));
            var div = document.getElementById(divName2);
            var mainDiv = document.getElementById(divname);
            var index = mainDiv.index;
            var val = mainDiv.values[index];
            val[mainDiv.dataColumns[column-1]] = value;
            div.innerHTML = value;
        /*
        imgElement.src = emptyPic;
        imgElement.title ="";
        element.setAttribute("valid", true);
        if (getFormValueStatus(divname)){
            setMessageHTML(null,null , divname);
        }*/
        }
    }
}

function createTableRow(i,values,divname,onselect,checkbox,editors,widths){
    var html = "<tr id='"+divname+"tr"+i+"' ";
    if (!editors){
        html+=" onclick=\"selectTableRow('"+divname+"',"+i+");"+(onselect?onselect:"")+"\" ";
    }
    html+=  " class=\""+getTableColor(i)+"\">";
    if (checkbox){
        var divElement = document.getElementById(divname);
        var check = false;
        if (divElement.values){
            var element = divElement.values[i];
            if (element.checked != 'null' && element.checked == true){
                check = true;
            }
        }
        html+= "<td  class='formtable_td'><input type='checkbox' id='"+divname+"tr"+i+"checkbox"+"' onchange=\"selectTableRow('"+divname+"',"+i+");"+checkbox.onselect+"\" "+(check?" checked ":"")+"/></td>";
    }
    for (var j = 0;j < values.length;j++){
        html += "<td  ";
        if (widths){
            html+= " width='"+widths[j]+"' ";
        }
        html+=" class='formtable_td' ";
        if (editors != null){
            html += " onclick=\"selectTableRow('"+divname+"',"+i+");"+(onselect?onselect:"")+";aktivateEditor('"+divname+"',"+(checkbox?(j+1):j)+");\"";
        }
        html +=">";
        var divPart= "<div id='"+divname+"tr"+i+"td"+(checkbox?(j+1):j)+"'>"+values[j]+"</div>";
        if ( editors){
            html += "<table class='inputfield_label' border='0' cellspacing='0' cellpadding='0' width='100%'><tr><td width='100%'>";
            html += divPart+"</td><td width='20'><img id='"+divname+"tr"+i+"td"+(checkbox?(j+1):j)+"_statusimg' src='hmepage_images/empty.png'/></td></tr></table>";
        }
        else {
            html += divPart;
        }
        html +="</td>";
    }
    html += "</tr>";
    return html;
}


function appendTableRow(values,i,div){
    var html = document.getElementById(div).getElementsByTagName("table")[0].innerHTML;
    html = html.substring(0,html.length-8);
    html += createTableRow(i,values,div);
    document.getElementById(div).getElementsByTagName("table")[0].innerHTML = html;
//div.getElementsByName("<tr")
}

