function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null)
    	node = document;
	if (tag == null)
    	tag = '*';
	
	var els = node.getElementsByTagName(tag);
  	var elsLen = els.length;
  	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  	for (i = 0, j = 0; i < elsLen; i++) {
    	if (pattern.test(els[i].className) ) {
      		classElements[j] = els[i];
      		j++;
    	}
  	}
  	return classElements;
}

function menu_border(id, img) {
	document.getElementById(id).src = img;
}

function cart_item_image_border(id, img) {
	document.getElementById(id).style.background = 'url(\''+img+'\')';
}

function categories_hover(id, img) {
	document.getElementById(id).src = img;
}

function check_quantities() {
	// loop through quantities for any invalid fields
	var elements = getElementsByClass('cart_item_qty');
	for (i = 0; i < elements.length; i++) {
		if (elements[i].value.search("[^0-9]") >= 0 || elements[i].value == "") {
			alert("You did not enter a valid quantity.");
			elements[i].focus();
			return false;
		}	
	}
	return true;
}

function do_cart_checkout() {
	document.getElementById('cart_action').value = "checkout";
	if (check_quantities()) {
		document.forms['form_shopping_cart'].submit();
	}						
}

function do_cart_update() {
	document.getElementById('cart_action').value = "update";
	if (check_quantities()) {
		document.forms['form_shopping_cart'].submit();
	}
}

function do_goto(url) {
	document.location.href = url;
}

function do_delivery_continue() {
	document.getElementById('checkout_action').value = "continue";
	document.forms['form_checkout_delivery'].submit();
	return true;
}

function do_payment_continue() {
	document.getElementById('checkout_action').value = "continue";
	document.forms['form_checkout_payment'].submit();
	return true;
}

function show_product_popup(url) {
	tb_show("", url);
}

function show_send_to_friend(url) {
	tb_show("", url);	
}

function add_to_cart() {
	parent.document.forms['form_product'].products_id.value = document.forms['form_product'].products_id.value;
	parent.document.forms['form_product'].add_qty.selectedIndex = document.forms['form_product'].add_qty.selectedIndex; 
	if (document.forms['form_product'].product_attribute_2) {
		// thickbox product has attribute we should add to the parent product attribute
		if (parent.document.forms['form_product'].product_attribute_2) {
			var p_attrib = parent.document.forms['form_product'].product_attribute_2;
			var attrib = document.forms['form_product'].product_attribute_2;
			var option = document.createElement('option');
			option.text = attrib.options[attrib.selectedIndex].text;
			option.value = attrib.options[attrib.selectedIndex].value;
		
			p_attrib.options.add(option);
			p_attrib.selectedIndex = p_attrib.options.length - 1;		
		} else {
			// parent product doesn't have an attribute so we have to create one
			var attrib = document.forms['form_product'].product_attribute_2;
			var new_attrib = document.createElement('select');
			var option = document.createElement('option');
			option.text = attrib.options[attrib.selectedIndex].text;
			option.value = attrib.options[attrib.selectedIndex].value;
			new_attrib.options.add(option);
			new_attrib.name = "product_attribute_2";
			parent.document.forms['form_product'].appendChild(new_attrib);
			var p_attrib = parent.document.forms['form_product'].product_attribute_2;
			p_attrib.selectedIndex = p_attrib.options.length - 1;
			p_attrib.style.display = "none";

		}
	} else {
		// thickbox product doesn't have any attributes, disable any parent product attributes
		if (parent.document.forms['form_product'].product_attribute_2) {
			var p_attrib = parent.document.forms['form_product'].product_attribute_2;
			p_attrib.disabled = true;
		}
		
	}
	parent.document.forms['form_product'].submit();
	return false;
}