var toolTipElement;

function checkAmount(box, qbag, qbox) {
	
	if (box.value=="") {
		box.value = "0";
	} else {
		if(box.value != parseInt(box.value)) {
			alert("U dient een getal op te geven");
			box.value=0;
		} else {
			cnt_box = 0;
			cnt_bag = 0;
			if(box.value > qbox) {
				cnt_box = box.value / qbox;
				nbox = box.value / cnt_box;
			}
			if(box.value > qbag) {
				cnt_bag = box.value / qbag;
				nbag = box.value / cnt_bag;
			}
			hbag = qbag / 2;
			hbox = qbox / 2;
			temp_box = cnt_box.toString();
			temp_bag = cnt_bag.toString();
			dec_box = temp_box.indexOf(".");
			dec_bag = temp_bag.indexOf(".");
			
			if(box.value > hbox && dec_box != -1) {
				if(cnt_box >= 1.5) {
					alert("Met de huidige hoeveelheid kunt u wellicht beter " + Math.round(cnt_box) + " dozen bestellen");
				} else {
					if(cnt_box == 0) {
						alert("Met de huidige hoeveelheid kunt u wellicht beter een hele doos bestellen");
					}
				}
			} else {
				if(box.value > hbag && dec_bag != -1) {
					if(cnt_bag >= 1.5) {
						alert("Met de huidige hoeveelheid kunt u wellicht beter " + Math.round(cnt_bag) + " zakken bestellen");
					} else {
						if(cnt_bag == 0) {
							alert("Met de huidige hoeveelheid kunt u wellicht beter een hele zak bestellen");
						}
					}
				}
			}
		}
	}
}


function onLoad() {
	menuRollovers();
	loadPrefilledTextFields();
	
	start_time = new Date();
//	loadToolTips();
	end_time = new Date();
	//alert((end_time - start_time) + " ms");
//	loadCheckInputs();
}

function loadToolTips() {
	toolTipElement = $('tooltip');
	toolTipElement.startHTML = toolTipElement.innerHTML;
	$$('a[tooltip], div.btn[tooltip], img[tooltip], input[tooltip]').each(function(e) {
		e.tip = $(e).readAttribute('tooltip');
		e.onmouseover = $(showTip).bind();
		e.onmouseout = hideTip;
	});
}

function showTip() {
	toolTipElement.innerHTML = this.tip;
}

function hideTip() {
	toolTipElement.innerHTML = toolTipElement.startHTML;
}

function menuRollovers() {
	var items = $A($('menu').getElementsByClassName('menu_item'));
	
	items.each(function(i) {
		i.onmouseover = function() {
			this.src = this.src.slice(0, -4) + '_a.gif';
			this.overstate = "over";
		}
		i.onmouseout = function() {
			if(this.overstate == "over") { // make sure that the state was mouseover before onmouseout action is executed
				this.src = this.src.slice(0, -6) + '.gif';
			}
			this.overstate = "out";
		}
	});
}

function copyStyles(from, to) {
	$H(from.style).each(function(pair) {
		if(typeof pair.value != 'function' && typeof pair.value != 'undefined' && pair.value != '' && pair.key.match(/^(background|border|margin|outline|padding|css|display|cursor|height|width|over|vertical|visib|clear|list|bottom|left|position|right|top|zIndex|orphans|page|scrollbar|empty|line|color|font|letter|white|text|word)/)) {
			Try.these(function() { to.style[pair.key] = pair.value; });
		}
	});
}

function loadPrefilledTextFields(element) {
	if(element) {
		element = $(element);
	} else {
		element = $(document.body);
	}
	var prefilled_color = '#d3deed';
	var filled = $A(element.getElementsBySelector('input.prefilled'));
	filled.each( function (f) {
		f.normal_color = f.getStyle('color');
		f.passwd = f.hasClassName('password');
		if(f.passwd) {
			if(document.all) {
				f.pwField = $(document.createElement('<input type="password" name="'+f.name+'">'));
			} else {
				f.pwField = $(document.createElement('input'));
				f.pwField.type = 'password';
				f.pwField.name = f.name;
			}
			copyStyles(f, f.pwField);
			f.pwField.txtField = f;
			f.pwField.setStyle({display:'none', color:f.normal_color});
			f.up().insertBefore(f.pwField, f);
			f.pwField.onblur = function() {
				if(this.value.strip() == '') {
					this.hide();
					this.txtField.show();
				}
			}
			f.setAttribute('name', '');
		}
		f.setStyle( {color:prefilled_color} );
		f.onfocus = function() {
			if(!this.passwd) {
				if(!this.start_value || this.value.strip() == this.start_value.strip()) {
					if(!this.start_value) {
						this.start_value=this.value;
					}
					this.value='';
					$(this).setStyle( {color:this.normal_color} );
				}
			} else {
				this.pwField.show();
				this.hide();
				this.pwField.focus();
			}
		}
		f.onblur = function() {
			if(!this.passwd) {
				if($(this).value.strip() == '') {
					$(this).setStyle( {color:prefilled_color} );
					this.value = this.start_value;
				}
			}
		}
	});
}

/* snippet from http://www.garyharan.com/index.php/2007/11/26/how-to-unobtrusively-scroll-a-div-with-prototype-scriptaculous/ */
/* adds Scroll animation to animation library of script.aculo.us  */
/* adds scrollTo function to Element methods of prototype library */
Element.addMethods({
  scrollTo: function(element, left, top){
    var element = $(element);
    if (arguments.length == 1){
      var pos = element.cumulativeOffset();
      window.scrollTo(pos[0], pos[1]);
    } else {
      element.scrollLeft = left;
      element.scrollTop  = top;
    }
    return element;
  }
});

Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    this.start(Object.extend({x: 0, y: 0}, arguments[1] || {}));
  },
  setup: function() {
    var scrollOffsets = (this.element == window) 
                ? document.viewport.getScrollOffsets() 
                : Element._returnOffset(this.element.scrollLeft, this.element.scrollTop) ;
    this.originalScrollLeft = scrollOffsets.left;
    this.originalScrollTop  = scrollOffsets.top;
  },
  update: function(pos) {
    this.element.scrollTo(Math.round(this.options.x * pos + this.originalScrollLeft), Math.round(this.options.y * pos + this.originalScrollTop));
  }
});
/* end snippet */