/*
# =====================================================================================================================
#
#    bcart_js.js		v1.0 (31 may 2006, 02 jun 2006)
#
#    ©2006, Thomas Biegeleisen		info@biegeldesigns.com
#
#    NOTICE: This file is copyrighted; you may not redistribute this file without the expressed permission
#	of the copyright holder listed above.  Modifications to this file are not recommended.  Any modifications you
#	make to this file are done at your own risk.
#
#    CHANGELOG: The changelog for this file can be found bcart_change.log
#
#    DESCRIPTION: This file contains all javascript functions necessary to run bCart on the front-end,
#
#    DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU'RE DOING!
#    Doing so could stop your shopping cart from working or release secure information
#
# =====================================================================================================================
*/



function bcart_selectSwatch(did, dcid, swapImage, qty, choiceName, detailName) {
	// get cell element
	thisCell = document.getElementById('swatch_' + dcid);

	// first, change the class of the new cell
	thisCell.className = "bcart_td_swatch_selected";

	// set hidden input
	hiddenInput = eval('document.mainForm.detail_' + did);

	// now, change the class of the old cell
	currentID = hiddenInput.value;
	if (currentID != "") {
		lastCell = document.getElementById('swatch_' + currentID);
		lastCell.className = "bcart_td_swatch";
	}

	// finally, update the hidden input field
	hiddenInput.value = dcid;
	
	// if there is a swap image, load it
	if (swapImage != 0 && swapImage != -1) {
		document.images['bcart_main_item_image'].src = "images/bcart_images/swap/" + swapImage + ".web.jpg";
	} else if (swapImage == 0) {
		document.images['bcart_main_item_image'].src = document.mainForm.original_src.value;
	}

	// handle text
	thisDiv = document.getElementById('detail_text_' + did);
	submitButton = document.mainForm.add;
	if (qty > 0 || qty == "unlimited") {
		bcart_writeText('detail_text_' + did, 'You chose: <b>' + choiceName + '</b>');
		thisDiv.className = "bcart_item_addtocart_main";
		// enable quantity field, set to default if exists
		if (submitButton) {
			submitButton.disabled = false;
		}
	} else {
		bcart_writeText('detail_text_' + did, choiceName + ' is currently unavailable.  Please choose a different ' + detailName);
		thisDiv.className = "bcart_item_addtocart_warning";
		// if it's unavailable, we have to disable the quantity field so they can't add
		if (submitButton) {
			submitButton.disabled = true;
		}
	}
}

function bcart_selectDetail(did) {
	if (!did || did == "") {
		return;
	}
	// get current detail choice id
	detailChoice = eval('document.mainForm.detail_' + did + '.options[document.mainForm.detail_' + did + '.selectedIndex].value');

	if (detailChoice == "" || !detailChoice) {
		return;
	}

	// split the detail choice
	detailChoiceSplit = detailChoice.split(":");

	// parsing array
	dcid = detailChoiceSplit[0];
	swapImage = detailChoiceSplit[1];
	qty = detailChoiceSplit[2];

	// if there is a swap, swap the image
	if (dcid != "") {
		if (swapImage != "") {
			swapSrc = "images/bcart_images/swap/" + swapImage + ".web.jpg";
		} else {
			swapSrc = "";
		}
	} else {
		swapSrc = "";
	}

	if (swapSrc != "") {
		document.images['bcart_main_item_image'].src = swapSrc;
	}

	// handle text
	thisDiv = document.getElementById('detail_text_' + did);
	submitButton = document.mainForm.add;
	if (qty > 0 || qty == "unlimited") {
		bcart_writeText('detail_text_' + did, '');
		if (submitButton) {
			submitButton.disabled = false;
		}
	} else {
		bcart_writeText('detail_text_' + did, 'Currently unavailable.');
		thisDiv.className = "bcart_item_addtocart_warning";
		// if it's unavailable, we have to disable the quantity field so they can't add
		if (submitButton) {
			submitButton.disabled = true;
		}
	}
}

function bcart_toggleTab(thisTab, tabCount) {
	for (var i=0; i<tabCount; i++) {
		iTab = document.getElementById('bcart_tab_' + i);
		if ( iTab ) {
			iBox = document.getElementById('bcart_box_' + i);
			if (i == thisTab) {
				myTab = iTab;
				iTab.className = "bcart_tab_a_on";
				iBox.style.display = "block";
			} else {
				iTab.className = "bcart_tab_a_off";
				iBox.style.display = "none";
			}
		}
	}
	myTab.blur();
}

function bcart_toggleSection(thisSection, tabs) {
	for (i in tabs) {
		thisElement = document.getElementById('div_' + tabs[i]);
		if (thisElement) {
			if (tabs[i] == thisSection) {
				thisElement.style.display = "inline";
			} else {
				thisElement.style.display = "none";
			}
		}
	}
			
}

function bcart_writeText(elementID, thisText) {
	if (document.getElementById) {
		thisElement = document.getElementById(elementID);
		thisElement.innerHTML = "";
		thisElement.innerHTML = thisText;
	} else if (document.all) {
		thisElement = document.all[elementID];
		thisElement.innerHTML = text;
	} else if (document.layers) {
		alert("Your browser is out-of-date.");
	}
}

function bcart_changeQty() {
	// set variable for qty
	thisQty = document.mainForm.quantity.value;

	// make sure this is a number, otherwise it's a zero
	if (parseInt(thisQty) == thisQty && thisQty >= 1) {
		// set quantity
		document.mainForm.quantity.value = parseInt(thisQty);
	} else {
		alert('Please enter a whole number.');
		// reset the value to one
		document.mainForm.quantity.value = 1;
	}
}

function bcart_toggleShipping() {
	// first, get country information
	thisCountry = document.mainForm.x_country.options[document.mainForm.x_country.selectedIndex].value;
	thisChecked = document.mainForm.address_same.checked;
	if (thisChecked) {
		// disabled
		thisStatus = true;
	} else {
		thisStatus = false;
	}

	document.mainForm.ship_to_first_name.disabled = thisStatus;
	document.mainForm.ship_to_last_name.disabled = thisStatus;
	document.mainForm.ship_to_company.disabled = thisStatus;
	document.mainForm.ship_to_address1.disabled = thisStatus;
	document.mainForm.ship_to_address2.disabled = thisStatus;
	document.mainForm.ship_to_city.disabled = thisStatus;
	if (eval('document.mainForm.ship_to_state_' + thisCountry) != "undefined") {
		eval('document.mainForm.ship_to_state_' + thisCountry + '.disabled = thisStatus');
	}
	document.mainForm.ship_to_zip.disabled = thisStatus;
	document.mainForm.ship_to_country.disabled = thisStatus;
}

function bcart_CVV(toggle) {
	// displays the CVV information box
	cvv = document.getElementById('cvv_box');
	if (toggle == "show") {
		cvv.style.display = "inline";
	} else {
		cvv.style.display = "none";
	}
}

function bcart_echeck(toggle) {
	// displays the echeck information box
	echeck = document.getElementById('echeck_box');
	if (toggle == "show") {
		echeck.style.display = "inline";
	} else {
		echeck.style.display = "none";
	}
}

