/*******************************
 * Class String extended with some helpfull functions
 *******************************/
/*
 * Trim String function
 */
String.prototype.trim = function() {
  return(this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1'));
}

/*
 * Tests if the string is empty
 */
String.prototype.isEmpty = function() {
  return (this.trim().length == 0);
}

/*
 * Tests if the string is a real number.
 */
String.prototype.isFloat = function() {
  if(this.isEmpty()) {
   return false;
  }
  return !isNaN(this);
}

/*
 * Tests if the string is an integer number.
 * If the parameter s is defined the string could be and a negative integer.
 */
String.prototype.isInt = function(s)  {
  var digits="1234567890";
  var sign="+";
  if (s != undefined || s != null) sign="-+";
  if (this.isEmpty()) {
     return false;
  }
  //is already a non-empty string
  if(digits.indexOf(this.charAt(0)) == -1 && sign.indexOf(this.charAt(0)) == -1) {
      return false;
  }

  if (this.length == 1 && sign.indexOf(this.charAt(0)) != -1)  {
     return false;
  }

  for (var i=1; i < this.length; i++)  {
       if (digits.indexOf(this.charAt(i)) == -1) {
          return false;
       }
  }
  return true;
}

/*
 * Tests if the string represents a date in 'dd.mm.yyyy' format.
 */
 String.prototype.isDate = function()  {
    var datePat = /^(\d{1,2})(\.)(\d{1,2})\2(\d{4})$/;
    var matchArray = this.match(datePat); // is the format 'dd.mm.yyyy' ok?
    if (matchArray == null) {
       return false;
    }
    var day = matchArray[1]; // parse date into variables
    var month = matchArray[3] - 1;
    var year = matchArray[4];
    var dteDate=new Date(year,month,day);
    return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

/*
 * Tests if the string could be an email address .
 */
String.prototype.isEmail = function() {
  return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this));
}

/*
 * Tests if the string is a number
 */
String.prototype.isNumber = function() {
  if(this.isEmpty()) {
   return false;
  }
  if (isNaN(parseFloat(this))) {
      return false;
  }
  return true;
}

String.prototype.encodeRom = function() {
  return (this.replace(/\u0103/g, '&a').
               replace(/\u0102/g, '&A').
               replace(/\u00E2/g, '&q').
               replace(/\u00C2/g, '&Q').
               replace(/\u00EE/g, '&i').
               replace(/\u00CE/g, '&I').
               replace(/\u015F/g, '&s').
               replace(/\u015E/g, '&S').
               replace(/\u0163/g, '&t').
               replace(/\u0162/g, '&T').
               replace(/\u20AC/g, '&E'));
}

String.prototype.decodeRom = function() {
  return (this.replace(/&a/g, '\u0103').
               replace(/&A/g, '\u0102').
               replace(/&q/g, '\u00E2').
               replace(/&Q/g, '\u00C2').
               replace(/&i/g, '\u00EE').
               replace(/&I/g, '\u00CE').
               replace(/&s/g, '\u015F').
               replace(/&S/g, '\u015E').
               replace(/&t/g, '\u0163').
               replace(/&T/g, '\u0162').
               replace(/&E/g, '\u20AC'));
}

/*
 * Check if the text field is empty
 * @return true if is empty, false in rest
 */
function isTextFieldEmpty(textField) {
    if(textField.value.isEmpty()) {
       return true;
    }
    return false;
}

/*
 * Set focus on this object
 */
function  setFocus(textField) {
  textField.focus();
  textField.select( );
}

 function validateString(obj, mesg) {
   if (obj.value.isEmpty()) {
     alert('Introduceti ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

 function validateDate(obj, mesg) {
   if (!obj.value.trim().isDate()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

 function validateInt(obj, mesg) {
    if ( obj.tagName == 'INPUT' && obj.disabled )
        return false;
         
   if (!obj.value.trim().isInt()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

 function validateFloat(obj, mesg) {
   if (!obj.value.trim().isFloat()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

function validateEmail(obj, mesg) {
   if (!obj.value.trim().isEmail()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }
/*
function validateFormSample(_form) {
   if (validateDate(_form.d, "data nasterii")) return;
   if (validateInt(_form.i, "nr inreg")) return;
  // _form.submit();
 }
*/

/**********************
 * Functions for addSucursaleConsum.jsp page
 *******************************/

/*
 * Tests if a input string value is valid. This string value must be non-empty,
 * a integer (or real). To check if is in a interval values.
 */
function isFloat(f) {
  return !isNaN(parseFloat(f.value));
}

function isInt(n) {
  return !isNaN(parseFloat(n.value));
}
/*
 * Invoke when the user submit the salveaza instalatie button from browser.
 */

function sendMesaj() {
  var _form = document.addMesaj;
  if (isTextFieldEmpty(_form.mesajc)) {
     alert("Introduceti un mesaj va rog!");
     _form.reset();
     setFocus(_form.mesajc);
     window.event.returnValue=false;
     return;
  }
   _form.submit();
}

function updateViewDate(){
  var _form = document.updateView;
  if (isTextFieldEmpty(_form.dataRef)) {
    alert("Introduceti data de referinta va rog!");
    _form.dataRef.value="";
    setFocus(_form.dataRef);
    window.event.returnValue=false;
    return;
  }
  _form.submit();
}

function updateViewMesaje() {
  var _form = document.updateView;
   _form.submit();
}
/*
 * Set a new document location
 */
function redirectTo(newURL) {
  document.location = newURL;
}
/*
 * Invoke when the user submit the login button from browser.
 */
function submitLogin() {
  var _form = document.login_form;
  //Test if the text field username or password are empty
  if (isTextFieldEmpty(_form.username)) {
     alert("Insert user name!");
     _form.reset();
     setFocus(_form.username);
     window.event.returnValue=false;
  }
}
//by annie
function isChecked(objRadio) {
    return (objRadio[0].checked || objRadio[1].checked);
}
function showMsg(mesg) {
    alert('' + mesg + '!');
    window.event.returnValue=false;
}

/*pt. a controla lungimea maxima a unui textarea*/
function validateLength(field, maxLength) {
   if (field.value.length > maxLength) {
     field.value = field.value.substring(0, maxLength);
     /*setFocus(field);*/
     alert ('Lungimea maxima acceptata este de ' + maxLength + ' caractere!');
     /*window.event.returnValue=false;*/
     return true;
   }
   return false;
 }

/*este apelata doar pt. date valide: zz.ll.aaaa*/
function getsDate(dateString){
    var s = new String(dateString);
    var s_array = s.split('.');
    s_array[2]=parseInt(s_array[2], 10);
    s_array[1]=parseInt(s_array[1], 10) - 1;
    s_array[0]=parseInt(s_array[0], 10);
    var date = new Date(s_array[2], s_array[1], s_array[0]);
    return date;
}

 /*este apelata doar pt. date valide: zz.ll.aaaa*/
function validateDifference(dateStart, dateStop, msg) {
    if (!msg) {
        msg = 'Data start este mai mare decat data stop !';
    }
    if (getsDate(dateStop) - getsDate(dateStart) < 0) {
        alert(msg);
        window.event.returnValue=false;
        return true;
    }
    return false;
}

function validateCurrentDate(dataIntrodusaObj, dataCurenta) {
    if (getsDate(dataIntrodusaObj.value) - getsDate(dataCurenta) > 0) {
        alert('Data introdusa este mai mare decat data curenta (' + dataCurenta + ')!');
        setFocus(dataIntrodusaObj);
        window.event.returnValue = false;
        return true;
    }
    return false;
  }

 function validateCifra(obj, mesg) {
   if (!obj.value.trim().isInt()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   if (obj.value > 10) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

 String.prototype.isClock = function()  {
    var clockPat = /^(\d{1,2})(\:)(\d{1,2})$/;
    var matchArray = this.match(clockPat); // is the format 'hh:mm' ok?
    if (matchArray == null) {
       return false;
    }
    var hh = matchArray[1]; // parse date into variables
    var mm = matchArray[3];
    if ((hh < 0) || (hh > 23))
        return false;
    if ((mm < 0) || (mm > 59))
        return false;
    return true;
 }

 function validateClock(obj, mesg) {
   if (!obj.value.trim().isClock()) {
     alert('Introduceti corect ' + mesg + '!');
     setFocus(obj);
     window.event.returnValue=false;
     return true;
   }
   return false;
 }

 function disableAllInputs() {
      var inputs = document.getElementsByTagName('INPUT');
      if(inputs) {
         for(var i = 0; i < inputs.length; i++) {
           if(inputs[i].nodeName=="text")
                inputs[i].readonly ="readonly";
           else
             inputs[i].disabled = true;
         }
      }
      inputs = document.getElementsByTagName('SELECT');
      if(inputs) {
         for(var i = 0; i < inputs.length; i++) {
             inputs[i].disabled = true;
         }
      }
      inputs = document.getElementsByTagName('TEXTAREA');
      if(inputs) {
         for(var i = 0; i < inputs.length; i++) {
             inputs[i].disabled = true;
         }
      }
 }

 function enableElementbyId(array) {
   if(array)
      for(var i = 0; i < array.length; i++) {
        var elem = document.getElementById(array[i]);
        if(elem)  elem.disabled = false;
      }
 }

function getPageOffsetLeft(elem){
  return getElemOffset(elem, "offsetLeft");
}

function getPageOffsetTop(elem){
  return getElemOffset(elem, "offsetTop");
}

function getElemOffset(elem, attr){
  var offset = 0;
  while(elem) {
    offset += elem[attr];
    elem = elem.offsetParent
  }
  return offset;
}

function getValue(obj) {
    if (obj)
      switch(obj.type) {
        case "text":
        case "hidden":
            return obj.value;
        case "select-one":
            if (obj.selectedIndex >= 0)
                return obj.options[obj.selectedIndex].value;
            return '';
      }
    return "";
}

function getText(obj) {
    if (obj)
      switch(obj.type) {
        case "text":
            return obj.value;
        case "select-one":
            if (obj.selectedIndex >= 0)
                return obj.options[obj.selectedIndex].text;
            return '';
      }
    return "";
}

function getSelectedValue(oCombo){
    if( oCombo){
        return oCombo.options[ oCombo.selectedIndex].value;
    }
}

function setElem(obj, combo) {
    if (obj && combo && combo.selectedIndex >= 0) {
        if (combo.options[combo.selectedIndex].value != '-1')
            obj.value = combo.options[combo.selectedIndex].text;
        else
            obj.value = '';
    }
}

function setElem2(obj, combo) {
    if (obj && combo && combo.selectedIndex >= 0)
            obj.value = combo.options[combo.selectedIndex].text;
    else
            obj.value = '';
}

function clearCombo(combo) {
      if(combo)
          while(combo.options.length > 0)
            combo.options[combo.options.length - 1] = null;
}

function addElemCombo(value, text, combo) {
      if(combo) {
          combo.options[combo.options.length] = new Option(text, value);
      }
}

function moveBetweenCombo(combo1, combo2) {
    if(combo1 && combo1.selectedIndex >= 0) {
        addElemCombo(getValue(combo1), getText(combo1), combo2);
        delElemCombo(combo1);
    }    
}

function delElemCombo(combo) {
      if(combo && combo.selectedIndex >= 0) {
          combo.options[combo.selectedIndex] = null;
      }
}
function addElemComboDef(value, text, combo) {
      if(combo)
          combo.options[combo.options.length] = new Option(text, value, true, true);
}

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

function isEqualInputCombo(obj, combo) {
    if (obj && !obj.value.isEmpty() && combo && combo.selectedIndex >= 0) {
        return (obj.value == combo.options[combo.selectedIndex].text);
    }
    return false;
}

function hideElem(objId) {
    var object = document.getElementById(objId);
    if(object) {
        object.style.visibility = 'hidden';
    }
}

function showElem(objId) {
    var object = document.getElementById(objId);
    if(object) {
        object.style.visibility = 'visible';
    }
}

function changeElem(objId) {
    var object = document.getElementById(objId);
    if(object) {
        object.style.visibility = (object.style.visibility == 'visible') ? 'hidden':'visible';
    }
}
function hideElems(aObjsId) {
    if (aObjsId && aObjsId.length) {
        for (i = 0; i < aObjsId.length; i++) {
            hideElem(aObjsId[i]);
        }
    }
}

function showElems(aObjsId) {
    if (aObjsId && aObjsId.length) {
        for (i = 0; i < aObjsId.length; i++) {
            showElem(aObjsId[i]);
        }
    }
}
function replaceHTML(idName, content) {
    var target = document.getElementById(idName);
    if (target) {
        target.innerHTML = content;
//        changeElem(target);
    }
}

function set_onmouseover() {
   var _forms = document.forms;
   if (_forms) {
       for (i = 0; i < _forms.length; i++) {
           for (j = 0; j < _forms[i].elements.length; j++) {
               if (_forms[i].elements[j] &&
                   (_forms[i].elements[j].type == 'button' || _forms[i].elements[j].type == 'submit')) {
                  var _btn = _forms[i].elements[j];
                  _btn.onmouseover =  function (e) {
                      this.style.color='#FFFFFF';
                      this.style.backgroundColor='#8ca5cb';
                  }
                  _btn.onmouseout = function (e) {
                      this.style.color='#406176';
                      this.style.backgroundColor='#c1d3e0';
                  }
               }
           }
       }
   }
}

function selectOption(oCombo, value){
    for( var i = 0; i < oCombo.length; i++){
        if( oCombo[i].value == value){
            oCombo[i].selected = true;
            break;
        }
    }
}

function resetComboLeaveFirstOption(oCombo){
    if( !oCombo || !oCombo.options || !oCombo.options.length) return;

    for (var i = 1; oCombo.options.length > 1;) {
        oCombo.remove(i);
    }
}



//function resetComboLeaveFirstOption(oCombo){
//    if(oCombo === null || oCombo === undefined) return;
//    if(oCombo.options === null || oCombo.options === undefined || typeof oCombo.options === "undefined") return;
//    for (var i = 1; oCombo.options.length > 1;) {
//        oCombo.options[i] = null;
//    }
//}

function resetComboLeaveFirstOptionAndDisable( oCombo, disabled){
    resetComboLeaveFirstOption( oCombo);
    oCombo.disabled = disabled;
}

function getResponseMesg(respPage) {
    if( respPage){
        var startIndex = respPage.indexOf("<respmesg>");
        if(startIndex >= 0){
            startIndex += 10;
        }else{
            startIndex = 0;
        }


        var endIndex = respPage.indexOf("</respmesg>");
        if( endIndex == -1){
            endIndex = respPage.length;
        }

        return respPage.substring(startIndex, endIndex);
    }
    return "";
}

function buildParams(printVersion, selectName, tipNom, codParent, nrOfParent, selectedValue){
    var codSapCE = '';
    try{ codSapCE = document.zaforma.sectie_idh.value;}catch(e){}
    return "printVersion="+printVersion + "&selectName="+ selectName + "&tipNom=" + tipNom + "&codParent="+codParent+"&nrOfParent="+nrOfParent+( selectedValue ? "&selectedValue="+selectedValue : "" ) + "&codSapCE="+codSapCE;
}

function getAppName(){
    if( !_gAppName){
        alert( 'Variabila _gAppName nu a fost initializata !');
        return null;
    }
    return _gAppName;
}


function getNomenclatorFromServer(params, responseFunction){
    var myAjax = new Ajax.Request( getAppName() + '?action=getNomenclatorFromServer',
        {
            method: 'post',
            parameters: params,
            onComplete: responseFunction
        });
}

function getCategoriesFromServer(params, responseFunction){
    var myAjax = new Ajax.Request( getAppName() + '?action=getCategoriesFromServer',
        {
            method: 'post',
            parameters: params,
            onComplete: responseFunction
        });
}

function getCategoriesDispecerFromServer(params, responseFunction){
    var myAjax = new Ajax.Request( getAppName() + '?action=getCategoriesDispecerFromServer',
        {
            method: 'post',
            parameters: params,
            onComplete: responseFunction
        });
}

//domul arata asa: <tr><td><input type="checkbox"></td></tr>
function hightLightRow(oCheck, selectedRowStyle, normalRowStyle){
    var oRow = oCheck.parentNode.parentNode;
    //function hightLightRow(oCheck, id, selectedRowStyle, normalRowStyle){
    //var oRow = document.getElementById(id);
    oRow.className = ( oCheck.checked ? selectedRowStyle : normalRowStyle);
}


function logout(){
    if( confirm('Doriti sa va delogati? ')){
        window.opener = 'something';
        window.close();
    }
    return false;
}

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
    var i, j;
    var prompt;
    // empty existing items
    for (i = selectCtrl.options.length; i >= 0; i--) {
        selectCtrl.options[i] = null;
    }
    prompt = (itemArray != null) ? goodPrompt : badPrompt;
    if (prompt == null) {
        j = 0;
    } else {
        selectCtrl.options[0] = new Option(prompt);
        j = 1;
    }
    if (itemArray != null) {
        // add new items
        for (i = 0; i < itemArray.length; i++) {
            selectCtrl.options[j] = new Option(itemArray[i][0]);
            if (itemArray[i][1] != null) {
                selectCtrl.options[j].value = itemArray[i][1];
            }
            j++;
        }
        // select first item (prompt) for sub list
        selectCtrl.options[0].selected = true;
    }
}


function limitInputLegth(oInput, length, alertText){
    if( oInput.value.length > length) {
        alert( alertText);
        oInput.select();
        oInput.focus();
    }
}

var dateReclamatieWindow = null
function showDateReclamatie(reclamatieId){
    if( dateReclamatieWindow != null) dateReclamatieWindow.close();
    dateReclamatieWindow = window.open( getAppName() + '?action=callcenter_showDateReclamatie&reclamatieId='+reclamatieId,'dateReclamatie', 'top=1,left=50,width=850 height=680, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes, scrollbars=yes');
}

var printWindow = null;
function printIncident(incidentId){
    if( printWindow != null) printWindow.close();
    printWindow = window.open( getAppName() + '?action=printIncident&id=' + incidentId);
}

function printEchipament(echipamentId){
    if( printWindow != null) printWindow.close();
    printWindow = window.open( getAppName() + '?action=printEchipament&id=' + echipamentId);
}


/*
var elementeDefecte = new Array(
        new Array(      //PA
                new Array("Selectati", -1),
                new Array("Intreruptoare JT", 1),
                new Array("Tabloul de JT", 2),
                new Array("Tablou JT de iluminat public", 3),
                new Array("Circuite de JT", 4),
                new Array("Arderi neselctive ale sigurantelor de MT", 5)
                ),

        new Array(        //PT
                new Array("Selectati", -1),
                new Array("Intreruptoare JT", 1),
                new Array("Tabloul de JT", 2),
                new Array("Tablou JT de iluminat public", 3),
                new Array("Circuite de JT", 4),
                new Array("Arderi neselctive ale sigurantelor de MT", 5)
                ),

        new Array(        //LEA JT
                new Array("Selectati", -1),
                new Array("Izolatoare", 1),
                new Array("Armaturi", 2),
                new Array("Cleme", 3),
                new Array("Stalp", 4),
                new Array("Conductoare neizolate", 5),
                new Array("Conductoare torsadate", 6),
                new Array("Cutii de sectionare (CS)", 7),
                new Array("Cutii de trecere LES/LES (CT)", 8),
                new Array("Cutii de distributie (CD)", 9),
                new Array("Cutii aprindere il. public", 10)
                ),

        new Array(       // LES JT
                new Array("Selectati", -1),
                new Array("Cabluri si accesorii", 1),
                new Array("Cutii de sectionare (CS)", 2),
                new Array("Cutii de trecere LES/LES (CT)", 3),
                new Array("Cutii de distributie (CD)", 4),
                new Array("Cutii de aprindere - Iluminat public stradal", 5),
                new Array("Firide principale de bransament", 6),
                new Array("Firide secundare de bransament (FSB)", 7),
                new Array("Coloana", 8),
                new Array("Blocuri de masura si protectie (BMP)", 9),
                new Array("Blocuri de masura si protectie trifazate (BMT)", 10)
                ),

        new Array(         //Bransamente aeriene
                new Array("Selectati", -1),
                new Array("Conductoare de bransament clasice neizolate", 1),
                new Array("Conductoare de bransament clasice izolcate", 2),
                new Array("Conductoare de bransament torsadate", 3),
                new Array("Cablu coaxial de bransament", 4),
                new Array("Blocuri de masura si protectie (BMP)", 5),
                new Array("Blocuri de masura si protectie trifazate (BMPT)", 6),
                new Array("Firide principale de bransament (FPB)", 7),
                new Array("Firide secundare de bransament (FSB)", 8),
                new Array("Coloane", 9),
                new Array("Transformatoare de curent (reductoare)", 10),
                new Array("Defectiuni aferente contoarelor", 12),
                new Array("Consola", 13),
                new Array("Cleme", 14)
                ),

        new Array(     //Bransamente subterane
                new Array("Selectati", -1),
                new Array("Cabluri si accesorii", 1),
                new Array("Blocuri de masura si protectie (BMP)", 2),
                new Array("Blocuri de masura si protectie trifazate (BMPT)", 3),
                new Array("Firide principale de bransament (FPB)", 4),
                new Array("Firide secundare de bransament (FSB)", 5),
                new Array("Coloana", 6),
                new Array("Transformatoare de curent (reductoare)", 7),
                new Array("Defectiuni aferente contoarelor", 8)
                ),

        new Array(        //LEA JT Il. Public stradal
                new Array("Selectati", -1),
                new Array("Stalpi", 1),
                new Array("Izolatoare", 2),
                new Array("Armaturi", 3),
                new Array("Cleme", 4),
                new Array("Conductoare clasice neizolate", 5),
                new Array("Conductoare torsadate", 6),
                new Array("Cutii de sectionare (CS)", 7),
                new Array("Cutii de trecere LES/LES (CT)", 8),
                new Array("Cutii de distributie (CD)", 9),
                new Array("Cutii aplindere il. public", 10),
                new Array("Punct de aprindere", 11),
                new Array("Transformatoare de curent (reductoare)", 12),
                new Array("Defectiuni aferente contoarelor", 13)
                ),

        new Array(      //LES JT Il. Public stradal
                new Array("Selectati", -1),
                new Array("Cabluri si accesorii", 1),
                new Array("Cutii trecere LES/LES (CT)", 2),
                new Array("Cutii de distributie (CD)", 3),
                new Array("Puncte de aprindere", 4),
                new Array("Transformatoare de curent (reductoare)", 5),
                new Array("Defectiuni aferente contoarelor", 6)
                )

        );
*/

var componente = new Array(
    new Array(        // PA
        new Array(
                new Array("Selectati", -1)
                ),
        new Array(
                new Array("Selectati", -1),
                new Array("Bare faze", 1),
                new Array("Bare nul", 2),
                new Array("Suport sigurante", 3),
                new Array("Sigurante fuzibile", 4)
                ),
        new Array(    
                new Array("Selectati", -1),
                new Array("Bare faze",1),
                new Array("Bare nul" , 2),
                new Array("Suport sigurante", 3),
                new Array("Sigurante fuzibile", 4),
                new Array("Contactor", 5),
                new Array("Transformatoare de curent", 6),
                new Array("Defectiuni aferente contoarelor", 7)
        ),
        new Array(
            new Array("Selectati", -1)
            ),
        new Array(
            new Array("Selectati", -1)
            )
    ),
    new Array(      //PT
            new Array(
                    new Array("Selectati", -1)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze",1),
                    new Array("bare nul" , 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4),
                    new Array("Contactor", 5),
                    new Array("Transformatoare de curent", 6),
                    new Array("Defectiuni aferente contoarelor", 7)
                ),
            new Array(
                new Array("Selectati", -1)
                ),
            new Array(
                new Array("Selectati", -1)
            )
    ),
    new Array(      //LEA JT
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                )
    ),
    new Array(      //LES JT
            new Array(
                    new Array("Selectati", -1),
                    new Array("Cablu", 1),
                    new Array("Manson", 2)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
           new Array(new Array("Selectati", -1)),
           new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3)
                ),
           new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3),
                    new Array("Transformatoare de curent (reductoare)", 4)
                )
    ),

    new Array(//Bransamente aeriene
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3)
                ),
           new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3),
                    new Array("Transformatoare de curent (reductoare)", 4)
            ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1))
        ),
    new Array(//bransamente subterane
             new Array(
                    new Array("Selectati", -1),
                    new Array("Cablu", 1),
                    new Array("Manson", 2)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3)
                ),
           new Array(
                    new Array("Selectati", -1),
                    new Array("Intrerupator diferential (disjunctor)", 1),
                    new Array("Element prot. la supratensiune", 2),
                    new Array("Borne", 3),
                    new Array("Transformatoare de curent (reductoare)", 4)
            ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1)),
            new Array(new Array("Selectati", -1))
            ),
        new Array(//LEA JT Il. Public stradal
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1)),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4),
                    new Array("Transformatoare de curent (reductoare)", 5),
                    new Array("Defectiuni aferente contoarelor", 6)
                ),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1))

        )  ,
        new Array(//LES JT Il. Public stradal
                new Array(
                    new Array("Selectati", -1),
                    new Array("Cablu", 1),
                    new Array("Manson", 2)
                ),
                 new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                 new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4)
                ),
                new Array(
                    new Array("Selectati", -1),
                    new Array("Bare faze", 1),
                    new Array("Bare nul", 2),
                    new Array("Suport sigurante", 3),
                    new Array("Sigurante fuzibile", 4),
                    new Array("Transformatoare de curent (reductoare)", 5),
                    new Array("Defectiuni aferente contoarelor", 6)
                ),
                new Array(new Array("Selectati", -1)),
                new Array(new Array("Selectati", -1))
        )
);


function getDate(inputId){
    var strDate = $( inputId).value;
    if( strDate.length != 16) return null;
    return Date.parseDate(strDate, "%d.%m.%Y %H:%M");
}

function getDiferentaDate( data1, data2){
    if( data1 != null && data2 != null ) {
        return (data1 - data2)/60000;
    }
    return 0;
}


var ScriptsUtils = {
    getScripts : function ( text){
        var scripts = [];

        if( text){
            var beginScript = "<script type=\"text/javascript\">";
            var endScript = "/script>";

            var currentIndex = 0;
            while ( currentIndex != -1){
//                    alert( currentIndex);
                var beginIndex = (currentIndex = text.indexOf( beginScript, currentIndex));
                if( beginIndex != -1) beginIndex += beginScript.length

                var endIndex   = text.indexOf( endScript, currentIndex) - 1;

//                    alert( beginIndex + " " + endIndex);

                if( beginIndex != -1 && endIndex != -1){
                    var script = text.substring( beginIndex, endIndex);
//                        alert( script);
                    scripts.push( script);
                }

                if( currentIndex > 0){
                    currentIndex = endIndex  + endScript.length + 1;
                }
            }
        }
        return scripts;
    },

    evalScripts : function ( text, alertOnError){

        var scripts = this.getScripts( text);

        for( var i = 0; i < scripts.length; i++){
            if( alertOnError){
                try{
                    eval( scripts[ i]);
                }catch(e){
                    alert( "[ScriptsUtils#evalScripts] Eroare la evaluare javascript: " + e.message);
                }
            }else{
                eval( scripts[ i]);
            }
        }
    }
}

function insertParam(key, value)
{
    key = escape(key); value = escape(value);

    var kvp = document.location.search.substr(1).split('&');

    var i=kvp.length; var x; while(i--)
{
    x = kvp[i].split('=');

    if (x[0]==key)
    {
        x[1] = value;
        kvp[i] = x.join('=');
        break;
    }
}
    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&');
}

/**
 * Add a URL parameter (or changing it if it already exists)
 * @param {search} string  this is typically document.location.search
 * @param {key}    string  the key to set
 * @param {val}    string  value
 */
var addUrlParam = function(search, key, val){
    var newParam = key + '=' + val,
        params = '?' + newParam;

    // If the "search" string exists, then build params from it
    if (search) {
        // Try to replace an existance instance
        params = search.replace(new RegExp('[\?&]' + key + '[^&]*'), '$1' + newParam);

        // If nothing was replaced, then add the new param to the end
        if (params === search) {
            params += '&' + newParam;
        }
    }

    return params;
};
