window.addEvent('domready', function() {
	addEls();
});
window.addEvent('load', function() {
	adjustHeights();
	if (window.XMLHttpRequest) { // IE >=7, mozilla, safari, opera 9
		window.addEvents({ 'resize': adjustHeights });
	}
});

function adjustHeights(){
	$('ml').setStyle('height', '0px');
	$('mr').setStyle('height', '0px');
	var bdiv = $('bodydiv').getSize().y;
	$('ml').setStyle('height', bdiv+'px');
	$('mr').setStyle('height', bdiv+'px');
}
function addEls(){
	$('bodydiv').addClass('bg');
	var tl = new Element('div', {'id' : 'tl'}).inject($('topdiv'), 'top');
	var tr = new Element('div', {'id' : 'tr'}).inject($('topdiv'), 'top');
	var ml = new Element('div', {'id' : 'ml'}).inject($('mdiv'), 'top');
	var mr = new Element('div', {'id' : 'mr'}).inject($('mdiv'), 'top');
	$('bdiv').empty();
	var bl = new Element('div', {'id' : 'bl'}).inject($('bdiv'), 'top');
	var br = new Element('div', {'id' : 'br'}).inject($('bdiv'), 'top');
	var bm = Element('div', {'id' : 'bm'}).inject($('bdiv'));
	var thread = Element('div', {'id' : 'thread'}).inject($('container'));
}

function addToCart(id){
	var spt = id.split('_');
	var myID = spt[0];
	var min = spt[1].toFloat();
	var myVal = $(id).value;
	if ( (!is_numeric(myVal)) || (is_numeric(myVal) && myVal < min) ){
		alert('The minimum order quantity for this item is '+min);
	} else {
		location.href=base+'mycart?add='+myID+'_'+myVal;
	}
}

function is_numeric( mixed_var ) {
    return !isNaN( mixed_var );
}

function setToZero(id){
	$(id).value = 0;
	checkQty($(id));
}

function checkQty(elm){
	var spt = elm.id.split('_');
	var myID = spt[0];
	var min = spt[1].toFloat();
	var curVal = elm.value.toFloat();
	if ( (curVal < min) &&  (curVal > 0) ){
		alert('The minimum purchase quantity for this product is '+min);
		elm.value=min;
	}
	else if ( (curVal == 0) || !is_numeric(curVal) ) {
		var agree = confirm('Are you sure you want to delete this item from your cart?');
		if (!agree){
			elm.value=spt[2];
			return false;
		} else {
			$('cartForm').submit();
		}
	}
	$('checkout').disabled = true;
}