/* Math functions */
function changeQPrice(addPrice) {
	var otherstuff = document.getElementById('additprice').value;
	var quant = document.getElementById('quant').options[(document.getElementById('quant').selectedIndex)].value;
	var unitprice = document.getElementById('unitprice').value;

	if(document.getElementById('select_cuffs') ) {
    if ( document.getElementById('select_cuffs').options[(document.getElementById('select_cuffs').selectedIndex)].index!==0) {
      monoYN = document.getElementById('monogram').options[(document.getElementById('monogram').selectedIndex)].index;
      monoprice = monoYN==0 ? 0 : 15;
      newprice = ((otherstuff*1)+(quant*unitprice)+(quant*monoprice)).toFixed(2);
    } else {
      newprice = (otherstuff*1).toFixed(2);
    }
  } else {
    newprice = ((otherstuff*1)+(quant*unitprice)).toFixed(2);
  }
	document.getElementById('sub-price').innerHTML="$ " + newprice;
}
function changeQPrice2(addPrice) {
	var otherstuff = document.getElementById('additprice').value;
	var quant = document.getElementById('quant').options[(document.getElementById('quant').selectedIndex)].value;
	var unitprice = document.getElementById('unitprice').value;
	var newprice;
	if ( otherstuff!=0.00 ) {
    newprice = ((otherstuff*1)+(quant*unitprice)).toFixed(2);
  } else {
    newprice = (quant*unitprice).toFixed(2);
  }
	document.getElementById('sub-price').innerHTML="$ " + newprice;
}
function changeQPrice3(addPrice) {
	var otherstuff = document.getElementById('additprice').value;
	monoYN = document.getElementById('monogram').options[(document.getElementById('monogram').selectedIndex)].index;
	monoprice = monoYN==0 ? 0 : 15;
	quant = document.getElementById('quant').options[(document.getElementById('quant').selectedIndex)].value;
	unitprice = document.getElementById('unitprice').value;
	if (quant>0) {
		newprice = ((otherstuff*1)+(quant*unitprice)+(quant*monoprice)).toFixed(2);
	} else {
		newprice = (otherstuff*1).toFixed(2);
	}
	document.getElementById('sub-price').innerHTML="$ " + newprice;
}
function addSurcharge() {
	occfee = '0.00';
	cardtype = document.getElementById('cardtype').options[(document.getElementById('cardtype').selectedIndex)].value;
	tot = document.getElementById('subtotal').innerHTML;
	tot = tot.substring(2);
	ship = document.getElementById('shipping').innerHTML;
	ship = ship.substring(2);
	if (isNaN(ship)) { ship = 0.00;}
	if ( cardtype == 'AmericanExpress' || cardtype == 'DinersClub') {
		surcharge = parseFloat(surch/100);
		occfee = parseFloat( (parseFloat(tot)+parseFloat(ship))*parseFloat(surcharge));
		occfee = occfee.toFixed(2);
	}
	document.getElementById("ccfee").innerHTML = '$ '+occfee;
	ordtotal = parseFloat(parseFloat(parseFloat(occfee)+parseFloat(tot)+parseFloat(ship))).toFixed(2);
	document.getElementById("total").innerHTML = '$ '+ordtotal;
}
function returnSurcharge(onamount) {
  occfee = '0.00';
	cardtype = document.getElementById('cardtype').options[(document.getElementById('cardtype').selectedIndex)].value;
	if ( cardtype == 'AmericanExpress' || cardtype == 'DinersClub') {
		surcharge = parseFloat(surch/100);
		occfee = parseFloat(parseFloat(onamount)*parseFloat(surcharge));
		occfee = occfee.toFixed(2);
	}
	return occfee;
}
function delsub(basketitemid){
	elemid = 'quant'+basketitemid;
	chkid = 'remove'+basketitemid;
	if (document.getElementById(chkid).checked==true) {
		document.getElementById(elemid).selectedIndex = 0;
	}
	return true;
}
function addaccessory(pid,pcolour,aprice,status) {
	additval = document.getElementById('additprice').value;
	pcolour = pcolour.replace(",","~");
	if (status==true) {
		document.getElementById('pid').value += ','+pid;
		document.getElementById('additcolour').value += ','+pid+':'+pcolour;
		document.getElementById('additprice').value = (parseFloat(additval)+parseFloat(aprice)).toFixed(2);
		changeQPrice();
	} else {
		var tmpstr = document.getElementById('pid').value;
		var regexp = eval ('/,'+pid+'/gi');
		tmpstr = tmpstr.replace(regexp,"");
		document.getElementById('pid').value = tmpstr;
		var tmpstr2 = document.getElementById('additcolour').value;
		var regexp2 = eval ('/,'+pid+':'+pcolour+'/gi');
		tmpstr2 = tmpstr2.replace(regexp2,"");
		document.getElementById('pid').value = tmpstr;
		document.getElementById('additcolour').value = tmpstr2;
		document.getElementById('additprice').value = (parseFloat(additval)-parseFloat(aprice)).toFixed(2);
		changeQPrice();
	}
}
/* Display functions */
function showhide(elementID,thisValue) {
	if ( thisValue == 0 ) {
		document.getElementById(elementID).style.visibility = "hidden";
		document.getElementById(elementID).style.display = "none";
		var del = 'same';
	}
	if ( thisValue == 1 ) {
		document.getElementById(elementID).style.visibility = "visible";
		document.getElementById(elementID).style.display = "";
		var del = 'diff';
	}
}
function showhide2(test,elementID,thisValue) {
	var theval = nowhite(thisValue);
	if ( theval.indexOf(test) !== -1 ) {
		document.getElementById(elementID).style.visibility = "hidden";
		document.getElementById(elementID).style.display = "none";
		document.getElementById(theval).style.visibility = "visible";
		document.getElementById(theval).style.display = "block";
	}
	if ( theval.indexOf(test) == -1 ) {
		document.getElementById(elementID).style.visibility = "visible";
		document.getElementById(elementID).style.display = "block";
		document.getElementById(theval).style.visibility = "hidden";
		document.getElementById(theval).style.display = "none";
	}
}

function doToggle(elemID) {
  var elem = document.getElementById('toggle'+elemID);
  elem.className = elem.className=='shiftleftactive' ? '':'shiftleftactive';
  var elem2 = document.getElementById('toggler'+elemID);
  elem2.innerHTML = elem2.innerHTML=='Read more' ? 'Hide':'Read more';
}

function getImg(imgPath,imgID,imgTxt,imgZoom) {
	var sel = document.getElementById('select_colour').options[(document.getElementById('select_colour').selectedIndex)].text;
	
	if ( sel!=="select") {
		/* change the displayed image and zoom popup link */
		var sellow = sel.toLowerCase();
		document.images["prodImage"].src = imgPath+imgID+sellow+".jpg";
		document.images["prodImage"].title = imgTxt+" - "+sel;
		document.images["prodImage"].alt = imgTxt+" - "+sel;
		document.getElementById('poplink').innerHTML = "<a href=\"javascript:popimg('"+imgID+sellow+"',"+imgZoom+");\" style=\"display:block;height:20px;width:50px;float:left;clear:left;padding-left: 20px;background-image: url('themes/cssimages/zoom.gif');background-position: bottom left;background-repeat:no-repeat;\"></a>";
	}
}
function fillUname() {
	document.getElementById('username').value = document.getElementById('email').value;
}
function nowhite(str) {
  return str.replace(/\s+/g,"");//.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,"$1");
}