
//==================================================================================
//==================================================================================
// 
// UTILS-MODUL
// AUTOR: DF
// ERSTELLT: 24.01.2005
//
//==================================================================================
//==================================================================================

// ============================================================
// Globale Deklarationen
// ============================================================

// den undefinierten Wert deklarieren
var undefined;


// ============================================================
// Klasse ShopUtils
// ============================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

// ---------------------------------------
// ShopUtils()
// ---------------------------------------
//
// Beispiel:
// ---------
// new ShopUtils()
//
function ShopUtils() {
}

// ------------------------------------------------------------
// Zugriffsfunktionen
// ------------------------------------------------------------

// ------------------------------------------------------------
// Öffentliche Instanzmethoden
// ------------------------------------------------------------

// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// ------------------------------------------------------------
// Öffentliche Klasseneigenschaften
// ------------------------------------------------------------

// ------------------------------------------------------------
// Private Klasseneigenschaften
// ------------------------------------------------------------

// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// ShopUtils.createCashFormated()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode gibt String mit 2 Stellen hinterm Komma zurück
// Formatierter Geldbetrag
//
// Beispiel:
// ---------
// ShopUtils.createCashFormated(n);

ShopUtils.createCashFormated = function(n){
  var cash_float;
  if (!n || n == '0') {
    cash_float = '0,00';
  } else {
    cash_float = parseFloat(new String(n).replace(/\,/,'.'));
    cash_float = parseFloat(cash_float * 100);
    cash_float = parseFloat(Math.round(cash_float) / 100);
    cash_float = new String(cash_float);
    if (!cash_float.match(/\./)){
      cash_float = cash_float + '.00';
    }
    if (cash_float.split(/\./)[1].length == 1){
      cash_float = cash_float + '0';
    }
    cash_float = cash_float.replace(/\./,',');
  }
  return cash_float;
}

// -------------------------------------------
// ShopUtils.createCashNumber()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode gibt Float mit max 4 Stellen hinterm Komma zurück
// Zum Berechnene von Geldbeträgen
//
// Beispiel:
// ---------
// ShopUtils.createCashNumber(n);

ShopUtils.createCashNumber = function(n){
  cash_float = parseFloat(new String(n).replace(/\,/,'.'));
  cash_float = parseFloat(cash_float * 10000);
  cash_float = parseFloat(Math.round(cash_float) / 10000);
  return cash_float;
}

// -------------------------------------------
// ShopUtils.checkNumber()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode prüft Argument auf type Number
//
// Beispiel:
// ---------
// ShopUtils.checkNumber(n);

ShopUtils.checkNumber = function(n){
  if (!arguments.length){
    return false;
  }
  if (n == undefined){
    return false;
  }
  n = n.toString();
  if (n == '0'){
    return n;
  }
  if (!n.length){
    return false;
  }
  n = n.replace(/,/g,'.');
  if (isNaN(n)){
    return false;
  }
  if (!isFinite(n)) {
    return false;
  }
  n = parseFloat(n);
  return n;
}

// -------------------------------------------
// ShopUtils.divAbrunden()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode führt eine division durch und rundet Ergebnis ab.
//
// Beispiel:
// ---------
// ShopUtils.divAbrunden(dividend, divisor);

ShopUtils.divAbrunden = function(dividend, divisor){
  return Math.floor (dividend / divisor);
}

// -------------------------------------------
// ShopUtils.checkWertInArrayVorhanden(str, arr)
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode prüft ob ein Wert in einem Array vorhanden ist.
// Argument = wert und jeweiliges Array
//
// Beispiel:
// ---------
// ShopUtils.checkWertinArrayVorhanden(str, arr);

ShopUtils.checkWertInArrayVorhanden = function(){
  var str = arguments[0];
  var arr = new Array();
  arr = arguments[1];
  for (var i in arr){
    if (arr[i] == str){
      return true;
    } 
  }
  return false;
}


// -------------------------------------------
// ShopUtils.getAssArrayFromArgArr(assArr, argArr)
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode extrahiert aus einem ArgumentArray die AssoziativenArray und überschreibt das mitgelieferte AssoziativeArray,
// welches zurückgegeben wird.
//
// Beispiel:
// ---------
// ShopUtils.getArgumentsAssArray(assArr, argArr);

ShopUtils.getArgumentsAssArray = function(){
  var assArr = {};
  var argArr = [];
  assArr = arguments[0];
  argArr = arguments[1];
  if (argArr.length){
    var tempAssArr = [];
    for (var i=0; i<argArr.length; i++){
      tempAssArr[i] = {};
      tempAssArr[i] = argArr[i];
      for (var k in tempAssArr[i]){
        assArr[k] = tempAssArr[i][k];
      }
    }
  }  
  return assArr;
}

// -------------------------------------------
// ShopUtils.gibBundesland(n)
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode gibt ein dt. Bundesland zurück
//
// Beispiel:
// ---------
// ShopUtils.gibBundesland(n);

ShopUtils.gibBundesland = function(n){
  var bundesland = [];
  bundesland[0] = 'Baden-Württemberg';
  bundesland[1] = 'Bayern';
  bundesland[2] = 'Berlin';
  bundesland[3] = 'Brandenburg';
  bundesland[4] = 'Bremen';
  bundesland[5] = 'Hamburg';
  bundesland[6] = 'Hessen';
  bundesland[7] = 'Mecklenburg-Vorpommern';
  bundesland[8] = 'Niedersachsen';
  bundesland[9] = 'Nordrhein-Westfalen';
  bundesland[10] = 'Rheinland-Pfalz';
  bundesland[11] = 'Saarland';
  bundesland[12] = 'Sachsen';
  bundesland[13] = 'Sachsen-Anhalt';
  bundesland[14] = 'Schleswig-Holstein';
  bundesland[15] = 'Thüringen';
  
  if(bundesland[n]){
    return bundesland[n];
  }
  return undefined;
}

// -------------------------------------------
// ShopUtils.gibBundeslaender()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode gibt eine Liste der dt. Bundesländer zurück
//
// Beispiel:
// ---------
// ShopUtils.gibBundeslaender();

ShopUtils.gibBundeslaender = function(){
  var bundesland = [];
  bundesland[0] = 'Baden-Württemberg';
  bundesland[1] = 'Bayern';
  bundesland[2] = 'Berlin';
  bundesland[3] = 'Brandenburg';
  bundesland[4] = 'Bremen';
  bundesland[5] = 'Hamburg';
  bundesland[6] = 'Hessen';
  bundesland[7] = 'Mecklenburg-Vorpommern';
  bundesland[8] = 'Niedersachsen';
  bundesland[9] = 'Nordrhein-Westfalen';
  bundesland[10] = 'Rheinland-Pfalz';
  bundesland[11] = 'Saarland';
  bundesland[12] = 'Sachsen';
  bundesland[13] = 'Sachsen-Anhalt';
  bundesland[14] = 'Schleswig-Holstein';
  bundesland[15] = 'Thüringen';
  return bundesland;
}

// --------------
// ShopUtils.stripSpace
// --------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   var f = ShopUtils.stripSpace(number or string);
//
        
ShopUtils.stripSpace = function(attr){
  var result = '';
  if (attr){
    attr = new String(attr);
    result = attr.replace(/^\s+/,'');
    result = result.replace(/\s+$/,'');
  }
  return result;
}

// --------------
// ShopUtils.deleteItemFromArray
// --------------
//
// Beschreibung:
// -------------
// Funktion löscht Array-Item und gibt Array zurück
//
// Beispiel:
// ---------
//   ShopUtils.deleteItemFromArray(array, item);
//
        
ShopUtils.deleteItemFromArray = function(arr, item){
  var tempArr = [];
  tempArr = arr;
  if (tempArr.length){
    if (tempArr[item] != undefined){
      var tempArr1 = tempArr.slice(0,item);
      var tempArr2 = tempArr.slice(parseFloat(item+1),tempArr.length);
      tempArr = tempArr1.concat(tempArr2);
    }
  }
  return tempArr;
}

// --------------
// ShopUtils.Item_SetPropertyMap_getIndexFromArray
// --------------
//
// Beschreibung:
// -------------
// Hilfsfunktion für Item.setPropertyMap
//
// Beispiel:
// ---------
//   ShopUtils.Item_SetPropertyMap_getIndexFromArray(array, name);
//
        
ShopUtils.Item_SetPropertyMap_getIndexFromArray = function(arr, name){
  var tempArr = [];
  tempArr = arr;
  if (tempArr.length){
    for (var i=0; i<tempArr.length; i++){
      if (tempArr[i].name() == name){
        return i.toString();
      }
    }
  }
  return undefined;
}

// --------------
// Util.column2float
// --------------
//
// Beschreibung:
// -------------
// Wandelt eine Kolonnenzahl (euro/usa) in Float um.
// Wird keine Kolonnenzahl erkannt, wird der Ursprungswert zurueckgegeben.
// Bei wissenschaftlicher Notation (1,000.00e4) werden Kolonnen nicht erkannt.
// 1,00      -    1
// 1.00      -    1
// 1,000     - 1000 (Sonderfall bei genau 3 Stellen hinter dem Komma)
// 1.000     - 1000 (Sonderfall bei genau 3 Stellen hinter dem Punkt)
// 1,0000    -    1
// 1.0000    -    1
// 1.000,0+  - 1000
// 1,000.0+  - 1000
//
// Beispiel:
// ---------
// var f = Util.column2float(number);
//
 

ShopUtils.column2float = function(number){
  var result = '';
  if (number){
    result = ShopUtils.stripSpace(number);
    
    // Sonderfall euro: 1.000 soll 1000 sein
    if (result.match(/^[+-]?\d{1,3}\.\d\d\d$/)) {
      result =  result.replace(/\./g,'');
    }
    // Sonderfall usa: 1,000 soll 1000 sein
    else if (result.match(/^[+-]?\d{1,3},\d\d\d$/)){
      result =  result.replace(/\,/g,'');
    }
    // normale euro-Kommazahl (1,0)
    else if (result.match(/^[+-]?\d*,\d+$/)){
      result =  result.replace(/\,/g,'.');
    }
    // euro-Kolonne (1.000,00 oder 1.000.000)
    else if (result.match(/^[+-]?\d{1,3}(\.\d\d\d)+(,\d+)?$/)){
      result =  result.replace(/\./g,'');
      result =  result.replace(/\,/g,'.');
    }
    // usa-Kolonne (1,000.00 oder 1,000,000)
    else if (result.match(/^[+-]?\d{1,3}(,\d\d\d)+(\.\d+)?$/)){
      result =  result.replace(/\,/g,'');
    }
    
  }
  return result;
}

// --------------
// ShopUtils.float2column
// --------------
//
// Beschreibung:
// -------------
// Wandelt eine Decimal in eine Kolonnenzahl (euro) um.
// Erwartet Argument number als Float mit Punktnotation
// BSP: 1000.00 (EUR)
//
// Beispiel:
// ---------
// var f = ShopUtils.float2column(number);
//
 

ShopUtils.float2column = function(number){
  var result = '';
  if (number){
    number = ShopUtils.stripSpace(number);
    number = parseFloat(number.replace(/\,/,'.'));
    var numberStr = new String(number);
    var strEnd = numberStr.length;
    var numberAfterPoint = '';
    if (numberStr.match(/\./)){
      strEnd = numberStr.indexOf(numberStr.match(/\./));
      numberAfterPoint = numberStr.substring(numberStr.indexOf(numberStr.match(/\./))+1,numberStr.length);
    }
    var numberBeforePoint = numberStr.substring(0,strEnd);
    var tempNumber = '';
    var count = 0;
    for (var i=numberBeforePoint.length-1; i>=0; i--){
      if (count%3 == 0 && count>0){
        tempNumber = numberBeforePoint.charAt(i) +'.'+ tempNumber;
      } else {
        tempNumber = numberBeforePoint.charAt(i) + tempNumber;
      }  
      count++;
    } 
    if (numberAfterPoint.length){
      result = tempNumber + ',' + numberAfterPoint;
    } else {
      result = tempNumber;
    }  
  }
  return result;
}

// --------------
// ShopUtils.checkFloat
// --------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   var f = ShopUtils.checkFloat(number);
//
        
ShopUtils.checkFloat = function(number){
  number = ShopUtils.stripSpace(number);
  number = ShopUtils.column2float(number);
  // akzeptiert wird: ±d und ±.d und ±d.d und  ±d.dE+/-d
  if (number.match(/^[+-]?(?=\d|\.\d)(\d*)(?:\.(\d*))?([Ee]([+-]?\d+))?$/)){
    return parseFloat(number);
  }
  return undefined;
}

// --------------
// ShopUtils.checkInteger
// --------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   var f = ShopUtils.checkInteger(number);
//
        
ShopUtils.checkInteger = function(number){
  number = ShopUtils.stripSpace(number);
  if (number.match(/^[+-]?\d\d*$/)){
    return number;
  }
  return undefined;
}

// --------------
// ShopUtils.checkDecimal
// --------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   var f = ShopUtils.checkDecimal(number);
//
        
ShopUtils.checkDecimal = function(number){
  number = ShopUtils.stripSpace(number);
  number = ShopUtils.column2float(number);
  // akzeptiert wird: [+/-]d und [+/-].d und [+/-]d.d
  if (number.match(/^[+-]?(?=\d|\.\d)(\d*)(?:\.(\d*))?$/)){
    return number;
  }
  return undefined;
}

// -------------------------------------------
// ShopUtils.formatCashFloat()
// -------------------------------------------
//
// Beschreibung:
// -------------
// Methode gibt String mit 2 Stellen hinterm Komma zurück
// Formatierter Geldbetrag
//
// Beispiel:
// ---------
// ShopUtils.formatCashFloat(n);

ShopUtils.formatCashFloat = function(n){
  cash_float = parseFloat(new String(n).replace(/\,/,'.'));
  cash_float = parseFloat(cash_float * 100);
  cash_float = parseFloat(Math.round(cash_float) / 100);
  cash_float = new String(cash_float);
  if (!cash_float.match(/\./)){
    cash_float = cash_float + '.00';
  }
  if (cash_float.split(/\./)[1].length == 1){
    cash_float = cash_float + '0';
  }
  cash_float = cash_float.replace(/\./,',');
  return cash_float;
}

// ------------------------------------------------------------
// Private Klassenmethoden
// ------------------------------------------------------------

// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

ShopUtils.prototype.toString = function() {
  // zunaechst an Methode der Basisklasse weiterleiten
  return Object.prototype.toString.apply(this);
}