
var totalSubsectionLists = 0;
var currentSubsectionList = 0;

var currentmax;

var $ = function(id) {
  return document.getElementById(id);
}

function retf() {
  return false;
}

function checkMatrixStock() {
  var matrix = $('component-inner');
  if (matrix) {
    var arr = matrix.getElementsByTagName("A");
	for (var i = 0; i < arr.length; i++) {
	  var braSize = getBraSize(arr[i].id.replace(/bs/, ''));
	  if (braSize != null) {
	    if (!braSize.isInStock) {
		  arr[i].style.backgroundColor = '#A44994';
		  arr[i].style.color = '#fff';
		  arr[i].style.cursor = 'default';
		  arr[i].style.textDecoration = 'none';
		  arr[i].rel = '';
		  arr[i].onclick = retf;
		  arr[i].title = 'Sold out';
		}
	  }
	}
  }
}

function addToCart(cupSize, backSize, name, altName) {
  if (cupSize && backSize) {
     //$('cup').value = cupSize;
	 //$('back').value = backSize;
	 var arr = document.getElementsByName($('cup').name);
	 for (var i = 0; i < arr.length; i++) {
	   arr[i].value = cupSize; 
	 }
	 arr = document.getElementsByName($('back').name);
	 for (var i = 0; i < arr.length; i++) {
	   arr[i].value = backSize; 
	 }
	 
	 var braSize = getBraSize(name);
     if (braSize != null) {
	   $('bsName').innerHTML = altName;
       $('instock').style.display = braSize.isInStock ? 'block' : 'none';
	   $('ostock').style.display = braSize.isInStock ? 'none' : 'block';
	   currentmax = braSize.maxOrderable;
	   if (!braSize.isInStock) {
	     //sendOOSMessage($('mainName').innerHTML + " " + altName);
	   }
	 }
  }
}

function getBraSize(name) {
  if (braSizeArr) {
	for (each in braSizeArr) {
	  if (braSizeArr[each].name == name) {
	    return braSizeArr[each];
	  }
	}
  }
  return null;
}

function completeATC() {
  $('act-quant').value = $('pop-quant').value;
  $('prodform').submit();
}

function doSubmit() {
  var ib = $('ibox_content');
  if (ib) {
    var forms = ib.getElementsByTagName("FORM");
	for (var i = 0; i < forms.length; i++) {
	  var kids = forms[i].getElementsByTagName("INPUT");
	  for (var j = 0; j < kids.length; j++) {
		if (kids[j].name && kids[j].name.indexOf("Q_") != -1) {
		  var quant = parseInt(kids[j].value);
		  if (isNaN(quant)) {
		    //alert("Please enter a valid quantity.");
			showErrorMessage("Please enter a valid quantity.");
			kids[j].focus();
			return false;
		  }
		  else if (quant > currentmax) {
		    var msg = "This exceeds our stock levels - ";
			msg += (currentmax == 1) ? "you can only purchase one of this item." : ("please enter a value less than or equal to " + currentmax + ".");
			showErrorMessage(msg);
			kids[j].focus();
			kids[j].value = 1;
			kids[j].select();
			return false;
		  }
		  else {			  
	        forms[i].submit();
			break;
		  }
		}
	  }
	}
  }
  return true;
}

function showErrorMessage(msg) {
  var ib = $('ibox_w');
  if (ib && ib.style.display == '') {
	ib = $('ibox_content');
	var arr = ib.getElementsByTagName("P");
	for (var i = 0; i < arr.length; i++) {
	  arr[i].innerHTML = msg;
	  arr[i].style.margin = "0px";
	  arr[i].style.padding = "0px 0px 8px 0px";
	}
  }
  else {
    $('mainerr').innerHTML = msg;
  }
}

function sendOOSMessage(altName) {
  var oosImg = $('oosImg');
  var theSrc = "OOSMail.php?product=" + encodeURIComponent(altName) + "&rcpt=" + a_1 + "&dom=" +juub9;
  if (!oosImg) {
    oosImg = document.createElement("IMG");
	oosImg.width = "1";
	oosImg.height = "1";
	oosImg.id = "oosImg";
	oosImg.src = theSrc;
	document.getElementsByTagName("BODY")[0].appendChild(oosImg);
  }
  else {
    if (oosImg.src != theSrc) {
	  oosImg.src = theSrc;
	}
  }
}

function BraSize(name, isInStock, maxOrderable) {
  this.name = name;
  this.isInStock = isInStock;
  this.maxOrderable = maxOrderable;
}

function checkEmailAddresses() {
  var links = document.getElementsByTagName("A");
  for (var i = 0; i < links.length; i++) {
	var rel = links[i].getAttribute("rel");
    if (rel && rel.indexOf("Lem-") != -1) {
	  rel = rel.replace(/Lem-/, "") + "@lembrassa.com";
	  links[i].href = "mailto:" + rel;
	  links[i].innerHTML = rel;
	}
  }
}

function checkSubSectionLists() {
  var ssListCont = $('sslistcont');
  if (ssListCont) {
    var allLists = ssListCont.getElementsByTagName("DIV");
	for (var i = 0; i < allLists.length; i++) {
	  if (allLists[i].id && allLists[i].id.indexOf("sslist") != -1) {
	    totalSubsectionLists++;
	  }
	}
  }
}

function swapSectionList(direction) {
  var ssListCont = $('sslistcont');
  if (ssListCont) {
	var indexToShow = currentSubsectionList + direction;
    var divToShow = $('sslist' + indexToShow);
	if (divToShow) {
	   var currentShowingDiv = $('sslist' + currentSubsectionList);
	   if (currentShowingDiv) {
	     currentShowingDiv.style.display = 'none';
		 divToShow.style.display = 'block';
		 currentSubsectionList = indexToShow;
		 $('prevlink').style.display = (currentSubsectionList == 0) ? 'none' : 'inline'; 
		 $('nextlink').style.display = (currentSubsectionList == totalSubsectionLists-1) ? 'none' : 'inline'; 
	   }
	}
  }
}

function checkBraFinder() {
  var theForm = $('bra-finder');
  if (theForm) {
    theForm.SS.value = theForm.S_BackSize0_0.options[theForm.S_BackSize0_0.selectedIndex].value + "" + theForm.S_CupSize0_1.options[theForm.S_CupSize0_1.selectedIndex].value;
	theForm.S_BackSize0_0.selectedIndex = 0;
	theForm.S_CupSize0_1.selectedIndex = 0;
  }
}

function LembrassaLoad() {
  checkEmailAddresses();
  checkSubSectionLists();
}

if (window.attachEvent) { 
  window.attachEvent("onload", LembrassaLoad); 
} 
else {  
  window.addEventListener("load", LembrassaLoad, false); 
}

function ThArr(nSize) {
  this.length = nSize;
  for (var i = 0; i < nSize; i++) {
    this[i] = new Object();
  }
}

ThArr.prototype.getMaxOrderable = function(searchAnc) {
  for (var i = 0; i < this.length; i++) {
    if (this[i].sEncAnc && this[i].sEncAnc == searchAnc) {
	  return this[i].nMax;
	}
  }
  return -1;
};

function checkTH(theForm, listIndex) {
  if (!theForm) return;
  var sel = theForm.getElementsByTagName("SELECT")[0];
  var quant = parseInt($('thquant' + listIndex).value);
  if (isNaN(quant)) {
    showMiniErrorMessage("Please enter a valid quantity.", listIndex);
	$('thquant' + listIndex).focus();
	return false; 
  }
  
  if (sel) {
    var selectedVal = sel.options[sel.selectedIndex];
	if (thArr) {
	  var theMax = thArr.getMaxOrderable(selectedVal.id.replace(/th/, ''));
	  if (quant > theMax) {
	    var msg = "The quantity you have entered exceeds our stock levels.\n";
	    msg += (theMax == 1) ? "You can only purchase one of this item." : ("Please enter a value less than or equal to " + theMax);
		showMiniErrorMessage(msg, listIndex);
		$('thquant' + listIndex).focus();
		return false;
	  }
	}
  }
  
  return true;
}

function showMiniErrorMessage(msg, index) {
  var m = $('compactmsg' + index);
  if (m) {
    m.innerHTML = msg;  
  }
}

/***********************************************************************
*
* getMiniCart		-	fetches cart content summary
*
* Returns:		-	HTML string for summary display
*
* Graham Bradley 2007
* Comments & bug reports to web@gbradley.co.uk
*
* To make editing the output HTML easier, output code has been split into
* nine 'template' strings.
*
*	tableOpen	-	the opening table code
*	itemLine	-	repeated for each item in the cart
*	shippingLine	-	for displaying shipping charges (if any)
*	vatLine		-	for displaying VAT on items & shipping (if any)
*	discountLine	-	for displaying discount total (if any)
*	surchargeLine	-	for displaying surcharge total (if any)
*	totalLine	-	for displaying the cart total
*	tableClose	-	the closing table code
*	emptyLine	-	displayed when cart is empty
*
* Within these templates, you can use variables to insert values into
* the summary, similar to variables in Actinic:
*
*	VAR:QUANTITY	-	The quantity of item in cart
*	VAR:TITLE	-	Abbreviated name of item
*	VAR:LINK	-	URL of item via the cgi-bin
*	VAR:PRICE	-	Formatted price of item
*	VAR:SHIPPING	-	Total of shipping applied
*	VAR:VAT		-	Total VAT on cart items & shipping
*	VAR:DISCOUNT	-	Total of discounts applied
*	VAR:SURCHARGE	-	Total of surcharges applied
*	VAR:TOTAL	-	Grand total
*
* Variables can only be used in the templates in which they are found.	
*
************************************************************************/

function getMiniCart(){

var tableOpen="<table width='100%'>";
var itemLine="<tr><td valign='top'>VAR:QUANTITYx </td><td><a href='VAR:LINK'>VAR:TITLE...</a></td><td align='right' valign='top'>VAR:PRICE</td></tr>"
var shippingLine="<tr><td colspan='2'>Shipping</td><td align='right'>VAR:SHIPPING</td></tr>"
var vatLine="<tr><td colspan='2'>VAT</td><td align='right'>VAR:VAT</td></tr>"
var discountLine="<tr><td colspan='2'>Discounts</td><td align='right'>VAR:DISCOUNT</td></tr>"
var surchargeLine="<tr><td colspan='2'>Surcharges</td><td align='right'>VAR:SURCHARGE</td></tr>"
var totalLine="</table><table width='100%'><tr><td><b>Total</b></td><td align='right'><b>VAR:TOTAL</b></td></tr>"
var tableClose="</table>"
var emptyLine="<div style='padding:5px 0px'>Your shopping bag is empty.</div>";

var cookie=getCartItem(4);

if (!cookie){
	return emptyLine;
	}
var re=new Array();
var match=new Array();
var total=0;
var str;
re[1]=/cur=([^&]*)&!/g;
re[2]=/&!(.*)/g;
re[3]=/ss=(.*)cur=/i;
re[4]=/tx=(.*)sh=/i;
re[5]=/sh=(.*)ss=/i;
for (i=1;i<re.length;i++){
	match[i]=re[i].exec(cookie);
	match[i]=match[i][1];
	}
lines=match[2].split("&!");
store=new Array();
re[0]=/^(\d*)x/;
re[1]=/x([^>]*)>/;
re[2]=/>([^>]*)>/;
re[3]=/>([^>]*)$/;
str=tableOpen;
for (i=0;i<lines.length;i++){
	for (j=0;j<re.length-2;j++){
		detail=lines[i].match(re[j]);
		store[j]=detail[1];
		}
	str+=itemLine.replace("VAR:QUANTITY",store[0]).replace("VAR:LINK",match[3]+"?PRODREF="+store[1]+"&NOLOGIN=1").replace("VAR:TITLE",store[2]).replace("VAR:PRICE",match[1]+((store[3]*1).toFixed(2)));
	total+=(store[3]*1);
	}
if (match[5] > 0){
	str+=shippingLine.replace("VAR:SHIPPING",match[1]+(match[5]/100).toFixed(2));
	total=total+=(match[5]/100);
	}
if (match[4] > 0){
	str+=vatLine.replace("VAR:VAT",match[1]+(match[4]/100).toFixed(2));
	total=total+=(match[4]/100);
	}
gTotal=getCartItem(1);
var r=/&#(\d*);/gi;
var m=new Array();
var f=new Array();
while((m = r.exec(gTotal))!=null){
	f[f.length]=m[1];
	}
for (i=0;i<f.length;i++){
	gTotal=gTotal.replace("&#"+f[i]+";","");
	}
total=total.toFixed(2);
gTotal=(gTotal/100).toFixed(2);
if (total>gTotal){
	str=str+=discountLine.replace("VAR:DISCOUNT","-"+match[1]+(total-gTotal).toFixed(2));
	}
else if (total<gTotal){
	str=str+=surchargeLine.replace("VAR:SURCHARGE",match[1]+(gTotal-total).toFixed(2));
	}
str+=totalLine.replace("VAR:TOTAL",match[1]+(gTotal));
str+=tableClose;
return str;
}