/*
 Find the absolute Left position of an object
*/
function positionLeft(obj)
{
 var curleft = 0;
 if (obj.offsetParent) {
  while (obj.offsetParent) {
   curleft += obj.offsetLeft;
   curleft -= obj.scrollLeft;
	 obj = obj.offsetParent;
	}
 }
 else if (obj.x) 
  curleft += obj.x;
 return curleft;
}
/*
 Find the absolute Top position of an object
*/
function positionTop(obj)
{
 var curtop = 0;
 if (obj.offsetParent) {
  while (obj.offsetParent) {
	 curtop += obj.offsetTop;
	 curtop -= obj.scrollTop;
	 obj = obj.offsetParent;
	}
 }
 else if (obj.y)
  curtop += obj.y;
 return curtop;
}

function trim( str)
{
 return str.replace(/^\s*|\s*$/g,"");
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")},
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++)
	{
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function getServerHtml( url, targetObject, furtherAction)
{
 var req = createXMLHTTPObject();
 if (!req) return;

 removenotifies();
 req.open("GET",url,true);
//	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
 req.onreadystatechange = function () {
	 if (req.readyState != 4) return;
	 if (req.status != 200 && req.status != 304) {
   if ( req.status == 499) {
    // Customized TEXT error
    alert("ERROR: " + req.responseText);
   }
   else {
    if ( req.status == 498) {
     // Customized JAVASCRIPT error
     eval(req.responseText);
    }
    else {
     if ( req.status == 299) {
      // Customized TEXT message
      alert(req.responseText);
     }
     else {
		   alert('HTTP error : '+req.status+' requesting URL ['+url+']');
     }
    }
   }
		return;
	 }
  if ( targetObject) {
   targetObject.innerHTML = req.responseText;
  }
//  alert(furtherAction);
  if (furtherAction) {
   furtherAction();
  }
 }
 req.send(null);
}


function _openlogin( handling, username, password)
{
 // Create a hidden IFRAME at top level to process the (re)login/out
 iframe = top.document.createElement('IFRAME');
 iframe.id = "iframe_login";
 iframe.style.position = "absolute";
 iframe.style.top = "100px";
 iframe.style.left = "100px";
 iframe.style.visibility = "hidden";
 iframe.src = "login.php?handling="+handling+"&username="+username+"&password="+password;
 top.document.body.appendChild( iframe);
}
function openlogin( handling, username, password)
{
 var req = createXMLHTTPObject();
	if (!req) {
  _openlogin( handling, username, password);
  return;
 }
 if ( handling == "login") {
  getServerHtml( "login.php?handling="+handling+"&username="+username+"&password="+password, top.document.getElementById("loginmenutext"), linkThrough);
 }
 else {
  if ( handling == "logout") {
   getServerHtml( "login.php?handling="+handling+"&username="+username+"&password="+password, top.document.getElementById("loginmenutext"), function() {
    top.location = "./";
   });
  }
  else {
   getServerHtml( "login.php?handling="+handling+"&username="+username+"&password="+password, top.document.getElementById("loginmenutext"), function() {
   });
  }
 }
}
function linkThrough()
{
 lnk = top.document.getElementById("userTargetURL");
// if ( lnk) lnk.click();
 if ( lnk) { a = window.open( lnk.href); a.focus(); }
 success = top.document.getElementById("loginsuccess");
 if ( success) {
  m = top.document.getElementById("menuregister");
  if ( m) {
   m.innerText = "account";
  }
  
  s = top.document.getElementById("showmycart");
  if ( s) {
   showcart();
  }
  o = top.document.getElementById("ordermycart");
  if ( o) {
   ordercart();
  }
  a = top.document.getElementById("accountdetails");
  if ( a) {
   register();
  }
 }
 failed = top.document.getElementById("loginfailed");
 if ( failed) {
  logout();
  alert("Login error, foutive gebruikersnaam of wachtwoord.");
 }
}

function login( frm)
{
 if ( frm.username.value == "") {
  alert( "Geen gebruikersnaam ingevuld.");
  return;
 }
 if ( frm.password.value == "") {
  alert( "Geen wachtwoord ingevuld.");
  return;
 }
 openlogin( "login",frm.username.value,frm.password.value);
}

function relogin()
{
 openlogin( "relogin","","");
}

function logout()
{
 openlogin( "logout","","");
 productList(-1);

 m = top.document.getElementById("menuregister");
 if ( m) {
  m.innerText = "registreer";
 }
 showCartContents();
}
function openerlogout()
{
 if ( top.opener && !top.opener.closed) {
  top.opener.logout();
  top.opener.focus();
 }
 else {
  top.logout();
 }
 self.opener=self;
 self.close();
}

function reloadpage()
{
// window.location.href = window.location.href;
 window.location.reload( false );
}
function selectStyle( style)
{
 getServerHtml( "setstyle.php?random="+Math.random()+"&newstyle="+style, null, reloadpage);
// getServerHtml( "setstyle.php?random="+Math.random()+"&newstyle="+style, top.document.getElementById("infopanel"), reloadpage);
}

function notify( obj)
{
 if ( !obj.getAttribute('notification')) return;

 div = document.createElement( 'DIV');
 div.style.position = "absolute";
 t = eval( positionTop( obj) + obj.offsetHeight + 15);
 div.style.top = t+"px";
 l = eval( positionLeft( obj) + 5);
 div.style.left = l+"px";
 div.style.padding = "3px";
 div.style.border = "1px solid navy";
 div.style.backgroundColor = "rgb(255,255,200)";
 div.style.lineHeight = "12px";
 div.style.fontSize = "10px";
 div.style.fontFamily = "sans-serif";
 div.style.zIndex = 9999;
 div.style.textAlign = "justify";
 div.opacity = 0.9;
 div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=90)";
 div.innerHTML = obj.getAttribute('notification');
 div.setAttribute('notificationbox',true);
 document.body.appendChild( div);

 if ( div.offsetWidth > 200) {
  div.style.width = "200px";
 }
 obj.noteObject = div;
 obj.onmouseout = function() {
  if ( this.noteObject) {
   try {
    document.body.removeChild( this.noteObject);
   }
   catch (e) {
   }
   this.noteObject = null;
  }
 }
}
function removenotifies()
{
 divs = document.getElementsByTagName('DIV');
 for (var i=0;i<divs.length;i++) {
  if ( divs[i].getAttribute('notificationbox')) {
   document.body.removeChild( divs[i]);
  }
 }
}

function _productList( categoryID)
{
 // Create a hidden IFRAME at top level to generate a product list
 iframe = top.document.createElement('IFRAME');
 iframe.id = "iframe_productlist";
 iframe.style.position = "absolute";
 iframe.style.top = "100px";
 iframe.style.left = "100px";
 iframe.style.visibility = "hidden";
 iframe.src = "productlist.php?categoryID="+categoryID;
 top.document.body.appendChild( iframe);
}

function setCategoryTitle( title)
{
 c = top.document.getElementById("categorytitle");
 if ( c) {
  c.innerHTML = title;
 }
}

function productList( categoryID)
{
 if ( categoryID == null) {
  categoryID = -99;
 }
 top.selectedCategoryID = categoryID;
 var req = createXMLHTTPObject();
	if (!req) {
  _productList( categoryID);
  return;
 }
 getServerHtml( "productlist.php?random="+Math.random()+"&categoryID="+categoryID, top.document.getElementById("infopanel"));
 if ( categoryID <= 0) {
    setCategoryTitle( "");
 }
}
function doSearch()
{
 s = document.getElementById("searchphrase");
 if ( s && ( trim(s.value) != "")) {
  phrase = trim(s.value);
  window.location = "?page=search&searchphrase="+escape(phrase);
 }
 else {
  alert( "Geen zoek criteria opgegeven");
 }
}
function productSearch()
{
 s = document.getElementById("searchphrase");
 if ( s && ( trim(s.value) != "")) {
  phrase = trim(s.value);
  getServerHtml( "productlist.php?random="+Math.random()+"&categoryID=-99&searchphrase="+phrase, top.document.getElementById("infopanel"));
 }
 else {
  alert( "Geen zoek criteria opgegeven");
 }
}

function openCategory( categoryID, catName)
{
 productList( categoryID);
 setCategoryTitle( catName);
}
function categoryList()
{
 getServerHtml( "categorylist.php", top.document.getElementById("categorymenutext"));
 if ( opener && opener.categoryList) {
  opener.categoryList();
 }
}

function register( handling)
{
 if ( handling != null) {
  getServerHtml( "register.php?random="+Math.random()+"&handling="+handling, top.document.getElementById("infopanel"));
 }
 else {
  getServerHtml( "register.php?random="+Math.random(), top.document.getElementById("infopanel"));
 }
 setCategoryTitle( "");
}
function getfaq()
{
 getServerHtml( "faq.php?random="+Math.random(), top.document.getElementById("infopanel"));
 setCategoryTitle( "");
}

function showcart()
{
 getServerHtml( "cartshow.php?random="+Math.random(), top.document.getElementById("infopanel"), showCartContents);
}
function ordercart()
{
 getServerHtml( "cartorder.php?random="+Math.random(), top.document.getElementById("infopanel"), showCartContents);
}
function paycart( dontget)
{
 da = document.getElementById('deliveryAddress');
 if ( da && (da.value == "")) {
  da.value = document.getElementById('invoiceAddress').value;
  da.onchange();
 }
 da = document.getElementById('deliveryPostcode');
 if ( da && (da.value == "")) {
  da.value = document.getElementById('invoicePostcode').value;
  da.onchange();
 }
 da = document.getElementById('deliveryCity');
 if ( da && (da.value == "")) {
  da.value = document.getElementById('invoiceCity').value;
  da.onchange();
 }
 if ( !dontget) {
  getServerHtml( "cartpay.php?random="+Math.random(), top.document.getElementById("infopanel"), showCartContents);
 }
}
function processcart( paymentMethod)
{
 if ( paymentMethod == 4) { // Paypal / Credit card
  /*
  i = top.document.getElementById("infopanel");
  m = top.document.getElementById("infocell");
  i.style.height = m.offsetHeight + "px";
  getServerHtml( "paypal.php?random="+Math.random(), i);
  */
//  window.open("paypal.php?random="+Math.random());
//  return; // Disabled to allow creating the invoice directly.
 }
 if ( paymentMethod == 5) { // iDEAL
  alert( "Deze betaal methode is op het ogenblik nog niet geactiveerd.\nKies een alternatieve betaal methode.")
  return;
 }
 getServerHtml( "cartinvoice.php?random="+Math.random(), top.document.getElementById("infopanel"),showCartContents);
}

function copyaddress()
{
 iAddress = document.getElementById('invoiceAddress').value.toUpperCase();
 iAddress = iAddress.replace(/\s/g,"");
 if (( iAddress.indexOf('POBOX') == 0) || ( iAddress.indexOf('POSTBUS') == 0) || ( iAddress.indexOf('PB') == 0)) {
  alert( "Afleveren op een Postbus is niet mogelijk");
  return;
 }

 document.getElementById('deliveryAddress').value = document.getElementById('invoiceAddress').value;
 document.getElementById('deliveryAddress').onchange();
 document.getElementById('deliveryPostcode').value = document.getElementById('invoicePostcode').value;
 document.getElementById('deliveryPostcode').onchange();
 document.getElementById('deliveryCity').value = document.getElementById('invoiceCity').value;
 document.getElementById('deliveryCity').onchange();
}

function updateaddress( userID, name, value)
{
 if ( userID > 0) {
  // update user invoice address
  getServerHtml( "usersave.php?details[ID]="+userID+"&details["+name+"]="+value,top.document.getElementById("debugpanel"));
 }
 else {
  if ( userID == 0) {
    // update userless invoice address
    getServerHtml( "carthandler.php?handling=invoiceaddress&details["+name+"]="+value,top.document.getElementById("debugpanel"));
  }
  else {
    // update delivery address
    getServerHtml( "carthandler.php?handling=deliveryaddress&details["+name+"]="+value,top.document.getElementById("debugpanel"));
  }
 }
}

function contact( handling)
{
 if ( handling == null) {
  getServerHtml( "contact.php?random="+Math.random()+"&handling=open", top.document.getElementById("infopanel"));
 }
 else {
  getServerHtml( "contact.php?random="+Math.random()+"&handling="+handling, top.document.getElementById("infopanel"));
 }
}

function setContactVar( name, value)
{
 if (( name == null) || ( value == null)) return;

 // update contact info
 getServerHtml( "contact.php?random="+Math.random()+"&handling=save&details["+name+"]="+escape(value));
}

function startSlide()
{
 imagelist = document.getElementById("ssimages");
 if ( imagelist) {
  imagelist.sliding = true;
  imagelist.slideTimer = window.setTimeout("showSlide()", 100);
 } else {
  // Get the image list
  getServerHtml( "productslideshow.php?random="+Math.random(), top.document.getElementById("slideshow"), startSlide);
 }
}

function showSlide()
{
 imagelist = document.getElementById("ssimages");
 f = document.getElementById("documentfooter");
 ss = document.getElementById("slideshow");
 if ( f && ss) {
  maxh = eval( positionTop(f) - positionTop(ss) - 5);
  ss.style.height = maxh+"px";
 }
 if ( ss && imagelist && imagelist.sliding) {
  h = imagelist.offsetHeight;
  h2 = document.getElementById("repeatedssimages").offsetHeight;
  h = h - h2;
  t = eval(imagelist.style.top.replace("px",""));
  if ( t>=0) {
   t = -h;
  } else {
   t = t + 1;
  }
  imagelist.style.top = eval(t)+"px";
  imagelist.slideTimer = window.setTimeout("showSlide()", 50);
 }

}
function stopSlide()
{
 imagelist = document.getElementById("ssimages");
 if ( imagelist) {
  imagelist.sliding = false;
  window.clearTimeout( imagelist.slideTimer);
 }
}

