function field_focus(item){
	item.style.border = "solid 1px #999999";
	item.style.backgroundColor = "#ffffff";
}

function field_blur(item){
	item.style.border = "solid 1px #c4c4c4";
	item.style.backgroundColor = "#f9f9f9";
}

var pic_id = null;

function show_menu(item){
	pic_id = parseInt(item.parentNode.getAttribute('id').substring(1));
	x = item.offsetLeft + item.offsetParent.offsetLeft;
	y = item.offsetTop + item.offsetParent.offsetTop;
	document.getElementById("drop_menu").style.top = y + 2 + "px";
	document.getElementById("drop_menu").style.left = x - 385 + "px";
	document.getElementById("drop_menu").style.display = "block";
}

var menu_delay = null;

function hide_menu(){
	document.getElementById("drop_menu").style.display = "none";
}

function menu_over(){
	clearTimeout(menu_delay);
}

function menu_out(){
	menu_delay = setTimeout("hide_menu()", 50);
}

function item_over(item){
	item.style.color = "#333333";
	item.style.backgroundColor = "#eeeeee";
}

function item_out(item){
	item.style.color = "#666666";
	item.style.backgroundColor = "#f9f9f9";
}

function add_to_cart(item){
	hide_menu();
	var size = encodeURI(item.innerHTML.substr(0, item.innerHTML.search(/\$/)-1));
	var ajax = new HttpRequest();
	ajax.successCallback = update_tot;
	ajax.failureCallback = null;
	ajax.url = "/add";
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.post("pic_id=" + pic_id + "&size=" + size);
}

function mod_q(item){
	var item_row = item.parentNode.parentNode;
	var item_id = parseInt(item_row.getAttribute('id').substring(1));
	var q = item.value;
	if(q == 0){
		if(confirm("Remove this item from your cart?")){
			child = item.parentNode.parentNode;
			item.parentNode.parentNode.parentNode.removeChild(child);
		} else {
			return;
		}
	}
	var unit_price = parseFloat(item_row.childNodes[3].firstChild.nodeValue.substring(1));
	var item_tot = unit_price * q;
	item_row.childNodes[5].firstChild.nodeValue = "$" + item_tot + ".00";
	var ajax = new HttpRequest();
	ajax.successCallback = update_tot;
	ajax.failureCallback = null;
	ajax.url = "/mod";
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.post("item_id=" + item_id + "&quantity=" + q);
}

function mod_s(item){
	var item_row = item.parentNode.parentNode;
	var item_id = parseInt(item_row.getAttribute('id').substring(1));
	var s = item.value;

	var ajax = new HttpRequest();
	ajax.successCallback = update_tot;
	ajax.failureCallback = null;
	ajax.url = "/mod";
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.post("item_id=" + item_id + "&size=" + s);
}

function update_tot(ajax){
	var response = ajax.responseText;
	if(response == "mod_q"){
		var cart_tot = 0;
		var item_rows = document.getElementsByTagName("tbody")[0].childNodes;
		for(i=0;i<item_rows.length;i++){
			if(item_rows[i].nodeType == '1'){
				cart_tot += parseFloat(item_rows[i].childNodes[5].firstChild.nodeValue.substring(1));
			}
		}
		cart_tot = "$" + cart_tot + ".00";
		document.getElementById("order_tot").innerHTML = "Order Total: " + cart_tot;
		document.getElementById("cart_tot").innerHTML = "Cart (" + cart_tot + ")";
	} else {
		document.getElementById("cart_tot").innerHTML = "Cart (" + response + ")";
	}
}

function checkout(){
	var ajax = new HttpRequest();
	ajax.successCallback = submit_paypal;
	ajax.failureCallback = null;
	ajax.url = "/checkout";
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.post();
}

function submit_paypal(ajax){
	var form = document.getElementsByTagName("form")[0];
	form.innerHTML = ajax.responseText;
	form.submit();
}

function set_working_cat(){
	document.getElementById("cat_selection").submit();
}

function modify_cats(){
	document.getElementById("modify_cats").style.display = "block";
}

var remove = null;
function pic_del(item){
	if(confirm("Remove this picture from the gallery?")){
		li = getElementsByAttribute(document, "li", "itemid", item);
		ul = li[0].parentNode;
		ul.removeChild(li[0]);
		remove = item;
		save_gallery();
	}
}

function save_gallery(){
	var pic_list = encodeURI(junkdrawer.inspectListOrder('pic_list'));
	var ajax = new HttpRequest();
	ajax.successCallback = null;
	ajax.failureCallback = null;
	ajax.url = "save_gallery.php";
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.post("pic_list=" + pic_list + "&remove=" + encodeURI(remove));
	remove = null;
}

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;

}

