// JavaScript Document

function areYouSure(theAction) {
	return confirm('Are you sure you want to '+theAction+' this item?');
	}



function FormatNumberBy3(num, decpoint, sep) {
	if (arguments.length == 2) { sep = ","; }
	if (arguments.length == 1) { sep = ","; decpoint = "."; }
	num = num.toString();
	a = num.split(decpoint);
	x = a[0];
	y = a[1];
	z = "";

	if (typeof(x) != "undefined") {
		for (i=x.length-1;i>=0;i--) z += x.charAt(i);
		z = z.replace(/(\d{3})/g, "$1" + sep);
		if (z.slice(-sep.length) == sep) z = z.slice(0, -sep.length);
		x = "";
		for (i=z.length-1;i>=0;i--) x += z.charAt(i);
		if (typeof(y) != "undefined" && y.length > 0) x += decpoint + y;
		}

	return x;
	}



function addToCart(formId,responseContId) {
	var targetFile		= 'add_to_cart.php';
	var failureMessage	= 'FAILED!';
	var successMessage	= '<span class="addedToCartMsg">Added to my cart! &nbsp; &middot; &nbsp; <a href="./view_cart.php">Cart / Checkout &raquo;</a></span>';

	var responseObj	= document.getElementById(responseContId);

	var success = function(o) {
		var theResponse = o.responseText;
		var responseArray = theResponse.split(' | ');
		responseObj.innerHTML = successMessage;

		if (responseArray[0] == 'OK') {
			document.getElementById('mCplural').innerHTML = responseArray[3];
			document.getElementById('mCitemsTotal').innerHTML = responseArray[1];
			document.getElementById('mCsubTotal').innerHTML = responseArray[2];
			}
		};

	var failure = function(o) {
		responseObj.innerHTML = failureMessage+o.responseText;
		};

	var callback = {
		success : success,
		failure : failure
		};

	var targetUrl = targetFile+'?rtype=ajax';
	YAHOO.util.Connect.setForm(formId);
	var connectionObject = YAHOO.util.Connect.asyncRequest('GET', targetUrl, callback);

	return false;
	}


function toggleField(theObj,newVal,theAction,fType) {
	if (theAction == 'clear') {
		theObj.value = (theObj.value == theObj.defaultValue) ? newVal : theObj.value;
		if (fType == 'password') theObj.type = 'password';
		}

	else if (theAction == 'reset') {
		theObj.value = (theObj.value == '') ? theObj.defaultValue : theObj.value;
		if (fType == 'password') theObj.type = (theObj.value == theObj.defaultValue) ? 'text' : 'password';
		}
	}


function imgPop(theImg) {
	window.open(theImg,'prodImg','width=600,height=600,tollbar=no,location=no');
	}


function calcAll(formID,itemInd) {
	var theForm				= document.getElementById(formID+itemInd);
	var basePriceField		= document.getElementById('base_price'+itemInd);
	var subTotalField		= document.getElementById('subtotal'+itemInd);
	var optionsTotalField	= document.getElementById('options_total'+itemInd);
	var totalSpan			= document.getElementById('theSubTotal'+itemInd);
	var cartAmountField		= document.getElementById('cart_amount'+itemInd);
	var theQty				= cartAmountField.options[cartAmountField.selectedIndex].value*1;
	var optionsTotal		= 0;
	var theSubTotal			= 0;

	for (var i=0; i<theForm.length; i++) {
		thisElem = theForm.elements[i];
		if ((thisElem.type == 'checkbox') || (thisElem.type == 'radio')) { if (thisElem.checked) optionsTotal += thisElem.title * 1; }
		else if ((thisElem.type == 'select-one') && (thisElem.name != 'cart_amount')) { optionsTotal += thisElem.options[thisElem.selectedIndex].title * 1; }
		}

	optionsTotal	= optionsTotal * theQty;
	itemTotal		= ((basePriceField.value*1) * theQty);
	theSubTotal		+= optionsTotal+itemTotal;
	theSubTotal		= theSubTotal.toFixed(2);

	subTotalField.value		= theSubTotal;
	optionsTotalField.value	= optionsTotal.toFixed(2);
	totalSpan.innerHTML		= FormatNumberBy3(theSubTotal);
	}