
// ObjCookie is an obkect to handle the cookie functionality
//
// V.1.0
// Author : Ralf Struwe

//if ( !_path ) { var _path = '/Users/Develop/Documents/html/Development/'; }
if ( !_path ) { var _path = ''; }
if ( !_cPath ) { var _cPath = 'www.ab-initio-versand.de'; }

function checkDomain() {
 var locUrl = document.URL;
 var locRef = "http://" + _cPath;
 
 if ( document.domain != _cPath ) {
    if ( locUrl.indexOf("-versand.de") > -1 ) {
       locRef += locUrl.substring(locUrl.indexOf("-versand.de")+11,locUrl.lenght);
       //alert('locRef='+locRef);
    }
    window.location.href = locRef;
 }
}
function ObjCookie() {
 //alert('ObjCookie IN');
 var locCookieName = "";
 var arrSettings = new Array();
 var dTmp   = new Array(new Date(),new Date(),new Date());
 var tmp = 0;

 tmp = dTmp[0].getTime() + (1*24*60*60*1000);
 //tmp.setFormat("DD-Mon-YYYY HH:MM:SS GMT");
 dTmp[0].setTime(tmp);
 tmp = dTmp[1].getTime() + (90*24*60*60*1000);
 //tmp.setFormat("DD-Mon-YYYY HH:MM:SS GMT");
 dTmp[1].setTime(tmp);
 tmp = dTmp[2].getTime() + (90*24*60*60*1000);
 //tmp.setFormat("DD-Mon-YYYY HH:MM:SS GMT");
 dTmp[2].setTime(tmp);

 //arrSettings['xxxx'] = new Array("Datum", "domain", "path", "sequre");
 arrSettings['wako'] = new Array(dTmp[0],_path,_cPath);
 arrSettings['cust'] = new Array(dTmp[1],_path,_cPath);
 //arrSettings['wako'] = new Array(dTmp[0],'');
 //arrSettings['cust'] = new Array(dTmp[1],'');

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // createArrayFromString
 // create an array from the string delivered
 // return : array
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 function createArrayFromString(_string) {
 //alert('ObjCookie>>createArrayFromString in');
  var temp = new Array(), str = "";

  // Daten aus dem Cookie in ein Array umwandeln
  if (_string != "") {
    str = _string.replace(/,/g, "\",\"");
    str = "\"" + str + "\""
    eval("temp = [" + str + "]");
  }

  //alert('ObjCookie>>createArrayFromString in');
  return temp;
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // doRead
 // read the cookies' data
 // return : string  the unescaped values as string
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.doRead = function doRead() {
 //alert('ObjCookie>>doRead in');
  var i = document.cookie.indexOf(locCookieName + "=");
  var c = "";

  if ( i > -1 ) {
    var ende = document.cookie.indexOf("; ", i+locCookieName.length+1);
    if ( ende == -1 ) {
     ende = document.cookie.length;
    }
    c = document.cookie.substring(i+locCookieName.length+1, ende);
  }
  //alert('ObjCookie>>doRead out  >' + locCookieName + '< empty ');
  return unescape(c);
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // doReadAsString
 // read the cookies' data
 // return : string  the values as string
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.doReadAsString = function doReadAsString() {
  return unescape(this.doRead());
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // doSave
 // write the data into the cookie
 // params : _value   the value to store
 //          _expires the expiration time
 //          _domain  the accepted domain able to read the cookie
 //          _path    the accepted domain-path able to read the cookie
 //          _secure  the ssl protocol is used
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.doSave = function doSave() {
 //alert('ObjCookie>>doSave in');

  var anzParameter = this.doSave.arguments.length;
  var parameter = this.doSave.arguments;
  
  //alert('vor check auf name >' + locCookieName + '<');
  if ( !arrSettings[locCookieName] ) {
    //alert('adding data to parameter list ' + locCookieName);
    arrSettings[locCookieName] = new Array(dTmp[dTmp.length-1],_cPath);
    //arrSettings[locCookieName] = new Array(dTmp[dTmp.length-1],'');
  }
  
  // 1. value
  var value = (anzParameter >= 1) ? parameter[0] : "";
  
  value = escape(value); // URL-Codierung
  
  
  // 2. expires
  var expires = (arrSettings[locCookieName][0]) ? arrSettings[locCookieName][0] : null;
  expires = (anzParameter >= 2) ? parameter[1] : expires;

  if (expires != null) {
    //alert('expires 1 --> ' + expires);
  }
  //alert('expires='+expires);


  // 3. domain
  var domain = (arrSettings[locCookieName][1]) ? arrSettings[locCookieName][1] : null;
  domain = (anzParameter >= 3) ? parameter[2] : domain;
  //alert('domain='+domain);
  //var domain = (anzParameter >= 3) ? parameter[2] : null;

  // 4. path
  var path = (anzParameter >= 4) ? parameter[3] : null;
  if (path != null) {
    path = escape(path); // Sonderzeichen umwandeln
  }

  // 5. secure
  var secure = (anzParameter >= 5) ? parameter[4] : null;

  // Zusammensetzen des Cookies
  var c = locCookieName + "=" + escape(value);
  if (expires != null) {
    c += "; expires=" + expires;
  }

  if (path != null) {
    c += "; path=" + path;
  }
  
  // Cookie setzen
  document.cookie = c;
 //alert('ObjCookie>>doSave out >' + locCookieName + '< ' + c);
}

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // getCookieName
 // delivery the actual cookie name
 // return : String
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.getCookieName = function getCookieName() {
  return locCookieName;
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // getVal
 // delivery data for entry _objName
 // param  : _objName
 // return : String
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.getVal = function getVal(_objName) {
  return this.getValArrayAssoziativ()[_objName];
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // getValArray
 // delivery all data in an array arr[0]
 // param :  _filter delivers only entries with _filter
 // return : Array
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.getValArray = function getValArray(_filter) {
 //alert('ObjCookie>>getValArray in ' + _filter);
  var str = this.doReadAsString();
  var temp = new Array();

  // Daten aus dem Cookie in ein Array umwandeln
  if (str != "") {
    str = str.replace(/,/g, "\",\"");
    str = "\"" + str + "\""
    eval("temp = [" + str + "]");
  }

  var arrRet = new Array();
  for ( var iLoop=0; iLoop<temp.length; iLoop+=2 ) {
    if ( ( _filter == 'ITEM' && temp[iLoop].indexOf('-') == 7 ) ||
         ( temp[iLoop].indexOf(_filter) >= 0 ) ) {
      arrRet[arrRet.length] = temp[iLoop];
      arrRet[arrRet.length] = temp[iLoop+1];
    }
  }

  //alert('ObjCookie>>getValArray out');
  return arrRet;
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // getValArrayAssoziativ
 // delivery all data in an assozitiv array arr['text']
 // return : Array
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.getValArrayAssoziativ = function getValArrayAssoziativ() {
 //alert('ObjCookie>>getValArrayAssoziativ in');
  // Daten aus dem Cookie in ein Array umwandeln
  var temp = createArrayFromString(this.doReadAsString());

  // assoziatives Array erstellen
  var c = new Array();
  for (var i=0; i<temp.length; i+=2) {
    c[temp[i]] = temp[i+1];
  }

  //alert('ObjCookie>>getValArrayAssoziativ out');
  return c;
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // loescheCookie
 // deletes the actual cookie, by setting the expires values
 // return : Array
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.loescheCookie = function loescheCookie() {
 //alert('loescheCookie >'+locCookieName+'< IN');
  //alert('vor check auf name >' + locCookieName + '<');
  if ( !arrSettings[locCookieName] ) {
    //alert('adding data to parameter list ' + locCookieName);
    arrSettings[locCookieName] = new Array(dTmp[dTmp.length-1],_cPath);
  }
  arrSettings[locCookieName][0] = new Date(1977, 0, 1, 0, 0, 1);
  this.doSave();
  //alert('loescheCookie >'+locCookieName+'< OUT');
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // removeVal
 // delivery all data in an assozitiv array arr['text']
 // return : Array
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.removeVal = function removeVal(_objName) {
  //alert('ObjCookie>>removeVal in ' + _objName);
  var temp = createArrayFromString(this.doReadAsString());
  var c = new Array();

  // assoziatives Array erstellen
  for (var i=0; i<temp.length; i+=2) {
    if ( temp[i] != _objName ) {
      c[temp[i]] = temp[i+1];
    }
  }

  this.setVals(c);
  //alert('ObjCookie>>removeVal out');
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // setCookieName
 // Set the name of the cookie the functions should act on
 // params : _name : the cookies' name
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.setCookieName = function setCookieName(_name) {
 //alert('ObjCookie>>setCookieName in '+ _name);
   locCookieName = _name;
 //alert('ObjCookie>>setCookieName out');
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // setVal
 // set the data for _objName
 // param  : _objName    the name of value to set
 //          _value      the value
 //          _cookieName the cookie
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.setVal = function setVal(_objName, _value) {
 //alert('ObjCookie>>setVal in ' + _objName + ' value=' + _value);
  var c = this.getValArrayAssoziativ();

  c[_objName] = _value;
  this.setVals(c);

  //alert('ObjCookie>>setVal out');
 }

 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // setVals
 // set the data for all entries
 // param  : c
 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 this.setVals = function setVals(_arrAss) {
 //alert('ObjCookie>>setVals in ');
  var temp = new Array();
  for (var e in _arrAss) {
    temp[temp.length]=e;
    temp[temp.length]=_arrAss[e];
  }

  this.doSave(temp.toString());
  //alert('ObjCookie>>setVals in ');
 }

 //alert('ObjCookie OUT');
 return this;
}
