/*
##############################################################################
# UCC User Created Content Version
# 2007 (c) J3S Jan Sass	und Sven Sass GbR, info@j3s.de
# $Revision: 1.1 $ - $Author: stan $ - $Date: 2007/02/15 22:20:52 $
##############################################################################
# copying prohibited, do not use without written permission of j3s GbR
##############################################################################
*/

function lite(o) {o.style.backgroundColor = '#dAe4eC'; o.style.cursor='hand';o.style.cursor='pointer';}
function dark(o) {o.style.backgroundColor = '';}

function openlink(link, name, width, height,toolbar) {
 if (typeof (name)   =='undefined') { name='popup';}
 if (typeof (width)  =='undefined') { width=800;}

 // pass additional hoverwidth information =)
 if(typeof(current_hover_width) != 'undefined' && current_hover_width != -1) {
   var hoverwidth= current_hover_width;
   link+= "&hoverwidth="+hoverwidth;
 }
if (typeof (height) =='undefined') { height=600;}
 if (typeof (toolbar)=='undefined') { toolbar='status=yes,scrollbars=yes,resizable=yes,width='+width+',height='+height;}
 var posx= Math.round((screen.availWidth -width )/2);
 var posy= Math.round((screen.availHeight-height)/2);
 toolbar += ",left="+posx+",top="+posy;
 var popup = window.open(link,name,toolbar);
 popup.focus();
 return false;
}

function Demystify(data) {
  var chars=data.split(",");
  for (var c in chars){
    document.write(String.fromCharCode(chars[c]-37));
  }
}

/*****************************************************************************
 * getHeight
 * retrieves inner height of the body
 ****************************************************************************/

function getHeight() {
  var y;
  if (self.innerHeight) // all except Explorer
  {
  	y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  	// Explorer 6 Strict Mode
  {
  	y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
  	y = document.body.clientHeight;
  }
  return y;
}

/*****************************************************************************
 * getWidth
 * retrieves inner width of the body
 ****************************************************************************/

function getWidth() {
  var x;
  if (self.innerHeight) // all except Explorer
  {
  	y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  	// Explorer 6 Strict Mode
  {
  	x = document.documentElement.clientWidth;
  }
  else if (document.body) // other Explorers
  {
  	x = document.body.clientWidth;
  }
  return x;
}

/*****************************************************************************
 * getScrollX
 * retrieves x scroll position
 ****************************************************************************/

function getScrollX() {
  var x;
  if (self.pageXOffset) // all except Explorer
  {
  	x = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollLeft)
  	// Explorer 6 Strict
  {
  	x = document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
  	x = document.body.scrollLeft;
  }
  return x;
}

/*****************************************************************************
 * getScrollY
 * retrieves y scroll position
 ****************************************************************************/

function getScrollY() {
  var y;
  if (self.pageYOffset) // all except Explorer
  {
  	y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  	// Explorer 6 Strict
  {
  	y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
  	y = document.body.scrollTop;
  }
  return y;
}

/*****************************************************************************
 * getx
 * retrieves x position of element specified by id
 * NOTE: the object has to be rendered *before* this function is called
 *
 * Parameters
 *   id:     id of object
 *
 * returns x position
 ****************************************************************************/

function getx(imgID,node) {
 var tempEl;
  if (imgID!='') tempEl = document.getElementById(imgID);
  else           tempEl = node;

  var xPos = tempEl.offsetLeft;
  tempEl = tempEl.offsetParent;
  while (tempEl != null) {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}

/*****************************************************************************
 * gety
 * retrieves y position of element specified by id
 * NOTE: the object has to be rendered *before* this function is called
 *
 * Parameters
 *   id:     id of object
 *
 * returns y position
 ****************************************************************************/

function gety(imgID,node) {
  var tempEl;
  if (imgID!='') tempEl = document.getElementById(imgID);
  else           tempEl = node;

  var yPos = tempEl.offsetTop;
  tempEl = tempEl.offsetParent;
  while (tempEl != null) {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
  return yPos;
}


/**********************************************************************************
 * ShowObject
 * creates an overly which will fade in at source object position to show detail
 * view. content will be loaded ajax style
 *
 * NOTE: require shop.js to be include in html as well for xmlhttp function
 *
 *  oid:    DOM object id of source element
 *  objurl: URL of object in normal case
 *********************************************************************************/

var object_xml;
var object_div;
var object_cache = new Array();
var object_top;
var object_left;
var object_leftfade;

var cart_xml;

function HideObject() {
  if (document.getElementById('objectview').style.visibility!='visible') {
    GoBack();
  }
  setTimeout('ObjectFadeOut(100);',10);
}

function ShowObject(idx,oid,objurl) {
  var dobj = document.getElementById(oid);
  object_leftfade = idx % 2;

  // create xmlhttp object - if this is not possible fallback to
  // standard mechanis (open url directly)
  object_xml=SearchGetXMLHttp();
  if (! object_xml) {
    location.href=objurl;
    return false;
  }

  // create overlay object div
  object_div = document.getElementById('objectview');
  object_div.style.position   = 'absolute';

  object_top                  = gety(oid);
  object_div.style.top        = object_top+'px';

  object_left                 = getx(oid);
  object_div.style.left       = object_left+'px';

  object_div.style.zIndex     = 100;
  ObjectFadeState(0);
  object_div.style.visibility = 'visible';

  // use cached result if possible
  if (typeof(object_cache[objurl]) != 'undefined') {
    object_div.innerHTML = object_cache[objurl];
    ObjectFadeIn(0);
  } else {
    object_xml.open('GET', '/w2'+objurl, true);
    object_xml.onreadystatechange = function () {
      if (object_xml.readyState == 4) {
        search_pos=-1;
        object_div.innerHTML =object_xml.responseText;
        object_cache[objurl] =object_xml.responseText;
        ObjectFadeIn(0);
      }
    }
    object_xml.send(null);
  }
}

function ObjectFadeState(pct) {
  var height = Math.round(355/100*pct);
  var width  = Math.round(505/100*pct);
  var obj = document.getElementById('objectview');
  obj.style.width=width+'px';
  obj.style.height=height+'px';
  obj.style.filter='alpha(opacity:'+pct+')';
  obj.style.MozOpacity=pct/100;

  // scroll in left if object is on right hand side
  if (object_leftfade) {
    // note: 240px needs to be in sync with width of object wrapper div (style.css::.objectwrap)
    obj.style.left =  object_left + 240- width + 'px';
  }

  // move view port so full object is visible
  if (getScrollY()<object_top+height-getHeight()+5) {
    window.scrollTo(getScrollX(),object_top+height-getHeight()+5);
  }
}

function ObjectFadeIn(pct) {
  ObjectFadeState(pct);
  if (pct<100) {
    setTimeout('ObjectFadeIn('+(pct+10)+')',10);
  }
}

function ObjectFadeOut(pct) {
  ObjectFadeState(pct);
  if (pct>0) {
    setTimeout('ObjectFadeOut('+(pct-10)+')',10);
  }
}

function AddCart() {
  if (!CookiesEnabled()) {
    alert('Bitte aktivieren Sie Cookies, um einen Bestellvorgang durchzuführen.');
    return false;
  }

  var f = document.forms['objectcart'];
  var amount = f['amount'].value;
  if (amount == '') {
    alert('Bitte geben sie die gewünschte Menge ein.');
    f['amount'].focus();
    return false;
  }
  amount = amount.replace(',','.');
  amount=parseFloat(amount);
  if (isNaN(amount)) {
    alert('Bitte geben sie einen numerischen Wert ein.');
    f['amount'].focus();
    return false;
  }

  // retrieve xml object
  cart_xml=SearchGetXMLHttp();
  if (! cart_xml) {
    alert('Interner Fehler: XMLHTTP Objeckt nicht unterstützt.');
    return false;
  }


  var url = '/w2/cartadd/'+escape(amount)+'/'+Normalize(f.objname.value)+'.html';
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      //alert(cart_xml.responseText);
      eval(cart_xml.responseText);
      HideObject();
    }
  }
  cart_xml.send(null);
}

function UpdateCartContent() {
  var f = document.forms['cartcontent'];
  var res='';
  for (var i=0;i<256;i++) {
    if (typeof(f['amount'+i]) == 'undefined') break;

    var amount = f['amount'+i].value;
    if (amount == '') {
      alert('Bitte geben sie die gewünschte Menge ein.');
      f['amount'+i].focus();
      return false;
    }
    amount = amount.replace(',','.');
    amount=parseFloat(amount);
    if (isNaN(amount)) {
      alert('Bitte geben sie einen numerischen Wert ein.');
      f['amount'+i].focus();
      return false;
    }
    res+= f['objid'+i].value+'~'+amount+'~';
  }

  // retrieve xml object
  cart_xml=SearchGetXMLHttp();
  if (! cart_xml) {
    alert('Interner Fehler: XMLHTTP Objeckt nicht unterstützt.');
    return false;
  }

  var url = '/w2/cartupdate/'+escape(res)+'.html';
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      eval(cart_xml.responseText);
      location.replace('/cart/Warenkorb.html');
    }
  }
  cart_xml.send(null);
}

function CancelEvents(event) {
  if(!event) var e = window.event;
  else           e =event;

  e.cancelBubble=true;
  if (e.stopPropagation) e.stopPropagation();
}

function DeleteCart(event,objname) {
  CancelEvents(event);

  // retrieve xml object
  cart_xml=SearchGetXMLHttp();

  var url = '/w2/cartdel/'+Normalize(objname)+'.html';
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      location = location.href;
    }
  }
  cart_xml.send(null);
}

function UpdateCart() {
  // retrieve xml object
  cart_xml=SearchGetXMLHttp();

  var url = '/w2/cartupdate';
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      //alert(cart_xml.responseText);
      eval(cart_xml.responseText);
      document.cookie = 'cart=;path=/';
    }
  }
  cart_xml.send(null);
}

var zip_main;
function SetZIP(main,fname) {
  var f = document.forms[fname];
  var zip = f['zip'].options[f['zip'].selectedIndex].value;

  zip_main = main;

  // retrieve xml object
  cart_xml=SearchGetXMLHttp();

  var url = '/w2/cartzip/'+zip;
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      if (zip_main) {
        location = location.href;
      } else {
        eval(cart_xml.responseText);
      }
    }
  }
  cart_xml.send(null);
}

function ClearCart() {
  // retrieve xml object
  cart_xml=SearchGetXMLHttp();

  var url = '/w2/cartclear';
  cart_xml.open('GET', url, true);
  cart_xml.onreadystatechange = function () {
    if (cart_xml.readyState == 4) {
      GoBack();
    }
  }
  cart_xml.send(null);
}

function GoBack(update) {
  if(typeof(update) == 'undefined' || getCookie('mainloc') == '') {
    history.back();
  } else {
    location.href = getCookie('mainloc');
  }
}

function SetMainLocation() {
  document.cookie = 'mainloc='+location.href+';path=/;'
}

function CheckUpdate() {
  if (getCookie('cart') == "update" && location.href.indexOf('Warenkorb') == -1) {
    UpdateCart();
  }
}


