/**
 * General store related scripts
 */

function re_check_quantity(qty_obj,hdn_obj) {
	var qty = qty_obj.value;	//document.config.order_qty.value;
	if ( !Number(qty) || isNaN(Number(qty)) || isNaN(parseInt(qty)) || parseInt(qty) <= 0 ) {
		alert("Invalid entry for quantity! Must be numeric and of a value greater than 0.");
		qty_obj.value = hdn_obj.value;
		qty_obj.focus();
		return false;
	}
	// OK, so update the hidden value with the new value
	hdn_obj.value = parseInt(qty);
	return true;
}

function reCheckQuantity(obj,hdn_id) {
	var hdn_obj = document.getElementById(hdn_id);
	if ( !hdn_obj ) {
		alert('Unable to determine quantity for '+obj.id+'! This page may not work properly.');
		return false;
	}
	return re_check_quantity(obj,hdn_obj);
}

function reRemoveLineItem(id,part_name) {
	var _args = reRemoveLineItem.arguments;
	var imgBtns = (_args.length>2&&(_args[2]===true||_args[2]=='TRUE')?true:false);
	if ( !confirm("Are you sure you want to remove "+part_name+" from the shopping cart?") ) { return; }
	document.getElementById(id).value = 'on';	// sets the check box for this line item to 'remove'
	reExecuteCartAction(document.getElementById('rebtn_update'),'update',null,imgBtns);
}

function reExecuteCartAction(obj,action,qty_objs) {
	var _args = reExecuteCartAction.arguments;
	var imgBtns = (_args.length>3&&(_args[3]===true||_args[3]=='TRUE')?true:false);
	try {
		if ( qty_objs != null && qty_objs && qty_objs.length > 0 ) {
			// loop through quantities to make sure they are valid
			for(var i=0; i<qty_objs.length; i++) {
				var qty_obj = document.getElementById(qty_objs[i].id);
				var hdn_obj = document.getElementById(qty_objs[i].hdn_id);
				if ( !qty_obj ) {
					alert('Unable to determine quantity for quantity field [corrupt or missing]! This page may not work properly.');
					return;
				} else if ( !hdn_obj ) {
					alert('Unable to determine quantity for '+qty_obj.id+'! This page may not work properly.');
					return;
				}
				if ( !re_check_quantity(qty_obj,hdn_obj) ) { return; }
			}
		}
	} catch(e) {}
	// now execute the cart action
	action = ''+action;
	if ( imgBtns === true ) {
		if ( action.length > 0 && action != '' ) {
			// update image action
			document.getElementById('action_x').value = action;
		} else {
			// something got messed up, so try to get action from image name
			document.getElementById('action_x').value = obj.name;
		}
	}
	// submit the form
	obj.form.submit();
}
			
function reTogglePackageView(obj,div_id) {
	var dom = document.getElementById(div_id);
	if ( !dom ) { return; }
	var prodname = obj.getAttribute('reitem');
	if ( dom.style.display == 'block' ) {
		dom.style.display = 'none';
		obj.title = 'Click to expand package details for '+prodname;
		obj.src = _GLOBALS.IMG_DIR+'/plus.gif';
	} else {
		dom.style.display = 'block';
		obj.title = 'Click to collapse package details for '+prodname;
		obj.src = _GLOBALS.IMG_DIR+'/minus.gif';
	}
}

