/**
 * This document has been written for teclan's 'Free product add to cart' and minicart functionality.
 * teclan cannot support any modifications made to this file.
 *
 * @author Lewis MacKenzie, teclan, http://www.teclan.com/
 * @date   05/01/09
 */

//The HTTP request object
var request = null;

//attach the cart check function to the load event
if (window.attachEvent) { 
  window.attachEvent("onload", checkCart); 
} 
else {  
  window.addEventListener("load", checkCart, false); 
}

/**
 * Shortcut function.
 */
var $$ = function(id) {
  return document.getElementById(id);
};

/**
 * Adds a trim method to the String class. This will remove
 * leading and trailing white space.
 */
String.prototype.trim = function() { 
  return this.replace(/^\s+|\s+$/, '');
};

/**
 * Adds a method to the String class to check whether or not an instance 
 * is a valid email address.
 */
String.prototype.isValidEmailAddress = function() {
  return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(this);
}

/**
 * Checks if there are items in the shopping cart and shows the mini cart 
 * if necessary. If checkFree is true, it checks if the free product needs
 * to be added to or removed from cart.
 */
function checkCart() { 
  if (getCartItem(3) > 0 || window.location.href.toString().indexOf('file:') != -1) {
    var miniLink = $$('cartsummary');
	if (miniLink) {
	  miniLink.onclick = showCartSummary;
	  miniLink.style.cursor = 'pointer';
	}
	
	if (checkFreeProduct) {
	  getSessionData(1);
	}
  }
}

/**
 * Toggles the mini cart display in the shopping cart summary.
 */
function showCartSummary() {
  var mc = $$('minicart');
  var links = $$('cartlinks');
  if (mc && links) {
    if (mc.style.display == 'none') {
	  if (!mc.innerHTML || mc.innerHTML == "") {
	    $$('cartsummary').src = 'loading.gif';
		getSessionData(0);
	  }
	  else {
	    changeOpac(0, "minicart");
	    mc.style.display = 'block';
	    doOpacity("minicart", 0, 100, 800);
	  }
	}
	else {
	  mc.style.display = 'none';
	}
  }
}

function getSessionData(callType) {
  //Mozilla
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  //Microsoft
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (request) {
    request.onreadystatechange = callType == 0 ? handleMiniResponse : handleFreeProductResponse;
    try {
      request.open("POST", sessionScript, true);
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      request.send(callType == 0 ? "action=getSummary" : "action=checkFreeProduct");
    }
    catch (e) {
      alert(e);
    }
  }
  else {
    window.alert("Your browser does not support AJAX!");
  }
}

function handleMiniResponse() {
  if (request.readyState == 4) {
    if (request.status == 200) {
	  var mc = $$('minicart');
	  var links = $$('cartlinks');
	  mc.innerHTML = request.responseText;
	  mc.innerHTML += "<div align='center'>" + links.innerHTML + "</div>";
  	  changeOpac(0, "minicart");
      mc.style.display = 'block';
      doOpacity("minicart", 0, 100, 800);
	  $$('cartsummary').src = 'cartbg.gif';
    }
    else {
      window.alert("XMLHttpRequest failed!");
    }
  }
}

function handleFreeProductResponse() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      if (request.responseText.indexOf('OK') == -1 && request.responseText.indexOf('http://') != -1) {
	    window.location = request.responseText;
	  }
	}
  }
}

/**
 * Fades an element in or out.
 */
function doOpacity(id, opacStart, opacEnd, millisec) {
  var speed = Math.round(millisec / 100);
  var timer = 0;

  if (opacStart > opacEnd) {
    for(var i = opacStart; i >= opacEnd; i--) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	  timer++;
    }
  } 
  else if (opacStart < opacEnd) {
    for (var i = opacStart; i <= opacEnd; i++) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	  timer++;
    }
  }
}

/**
 * Cross-browser functionality to change the opacity of a given
 * element to the specified value.
 */
function changeOpac(opacity, id) {
  var object = $$(id).style; 
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}


function ShowPopUpteclan(sUrl, nWidth, nHeight)
  	{  
	window.open(sUrl, 'ActPopup', 'width=' + nWidth + ',height=' + nHeight + ',scrollbars, resizable');
	}