/* Utility functions */
function addEvent(obj, evType, fn, useCapture) { // By Scott Andrew
 if (obj.addEventListener) { obj.addEventListener(evType, fn, useCapture); return true; }
 else if (obj.attachEvent) { var r = obj.attachEvent('on' + evType, fn); return r; }
 else { obj['on' + evType] = fn; }
 return void(0);
}
// call it like this
//addEvent(window, 'load', scrollInit, false);
function element(id) {
	var e = false;
	if (document.getElementById) { e = document.getElementById(id); }
	else if (document.all) { e = document.all(id); }
	return e;
}
function displayValue() {
  var returnvalue = 'table';
  var offset = navigator.userAgent.indexOf('MSIE');
  if (offset != -1) { returnvalue = 'block';}
  return returnvalue;
}
function checkAfleveradres() {
	var radioparent = element('afleveradres');
	var subform = element('afleverform');
	if(radioparent){
		var radios = radioparent.getElementsByTagName('input');
		if(radios){
			for(var i=0; i<radios.length; i++){
				var radio = radios[i];
				radio.onclick = function(){
					if(this.checked != false && this.value == 'anders'){
						subform.style.display = 'block';
					} else {
						subform.style.display = 'none';
					}
				}
			}
		}
	}
	return void(0);
}
function checkCondition(formid) {
	var form = element(formid);
	var check = element('akkoord');
	if(check){
		if(check.checked == false) {
			alert('U bent niet akkoord met de algemene leveringsvoorwaarden');
			//check.className = 'error';
			check.parentNode.style.backgroundColor = '#ff3333';
			return false;
		} else {
			//check.className = '';
			check.parentNode.style.backgroundColor = 'transparent';
			if(form) form.submit();
		}
	}
	return false;
}
// login
function toPassword(objOldInput) {
	var objNewElement = document.createElement('input');
	objNewElement.setAttribute('type', 'password');
	objNewElement.setAttribute('name', objOldInput.name);
	objOldInput.parentNode.replaceChild(objNewElement, objOldInput);
	toPassword.el = objNewElement;
	setTimeout('toPassword.el.focus()',100);
	return true;
}
// menu.js
function hasULchildren(linode) {
	var uls = linode.getElementsByTagName('ul')[0];
	if(uls) return true;
	return false;
}
function efMenu(menuid, menutype) {
	var menudiv = element(menuid);
	var listitems = menudiv.getElementsByTagName('li');
	if(listitems){
		for(var i=0; i<listitems.length; i++){
			var listitem = listitems[i];
			if(hasULchildren(listitem)) {
				listitem.onmouseover = function (e) {
					if (this.firstChild.nodeName.toUpperCase() == 'A') {
						if(this.firstChild.className.indexOf('active') != -1) { this.firstChild.className = 'motheractivemouse'; }
						else { this.firstChild.className = 'mothermouse'; }
					}
					for (var i=0; i < this.childNodes.length; i++) {
						var child = this.childNodes[i];
						if (child.nodeName.toUpperCase() == 'UL') {child.style.visibility = 'visible'; }
					}
					return false;
				};
				// onmouseout function
				listitem.onmouseout = function (e) {
					for (var i=0; i < this.childNodes.length; i++) {
						var child = this.childNodes[i];
						if (child.nodeName.toUpperCase() == 'UL') {	child.style.visibility = 'hidden';}
					}
					if (this.firstChild.nodeName.toUpperCase() == 'A') {
						if(this.firstChild.className.indexOf('active') != -1) { this.firstChild.className = 'motheractive'; }
						else { this.firstChild.className = 'mother'; }
					}
					return false;
				};
			}
		}
	}
	return void(0);
}

function changePage(id){
	var val='';
	var selectbox = element(id);
	if (selectbox) {
		val = selectbox.options[selectbox.selectedIndex].value;
	}
	if (val != '') {
		document.location = val;
	}
}

function foldCatList(){
	var catList = element('categories');
	if (catList) {
		var catBoxen = catList.getElementsByTagName('h2');
		if (catBoxen) {
			for (var i = 0; i < catBoxen.length; i++) {
				var catBox = catBoxen[i];
				catBox.onclick = function() {
					link = this.getElementsByTagName('a')[0];
					if (link) {
						if (link.className == 'active') {
							link.className = 'inactive';
						}
						else {
							link.className = 'active';
						}
					}

					list = this.parentNode.getElementsByTagName('ul')[0];
					if (list) {
						if (list.style.display == 'none') {
							list.style.display = 'block';
						}
						else {
							list.style.display = 'none';
						}
					}

					return false;
				}
			}
		}
	}
	return false;
}
addEvent(window, 'load', foldCatList, false);

function postOrder(action) {
	var arr = new Array();
	arr["discount"] = document.getElementById('discount').value;
	arr["date1x"] = document.getElementById('date1x').value;
	arr["betalingscondities"] = document.getElementById('betalingscondities').value;
	arr["leveringstijd"] = document.getElementById('leveringstijd').value;
	arr["leveringstijd"] = document.getElementById('leveringstijd').value;

	if (document.getElementById('welbtw').checked) {
		arr["btw"] = document.getElementById('btw').value;
	} else{
		arr["btw"] = 0;
	}
	
	if (document.getElementById('niet_franco').checked) {
		arr["verzendkosten"] = document.getElementById('verzendkosten').value;
	} else{
		arr["verzendkosten"] = 0;
	}
	
	var message = document.getElementById('message').value;
	arr["message"] = message.replace(/(\r\n)|(\n)|(\r)/g,"<br>");

	document.getElementById("arr_form1").value = serialize(arr);

	document.getElementById("korting").submit();
}

function serialize( mixed_value ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Arpad Ray (mailto:arpad@php.net)
	// +   improved by: Dino
	// +   bugfixed by: Andrej Pavlovic
	// +   bugfixed by: Garagoth
	// +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
	// +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
	// %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
	// %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
	// *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
	// *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
	// *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
	// *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'

	var _getType = function(inp) {
		var type = typeof inp, match;
		var key;
		if (type == 'object' && !inp) {
			return 'null';
		}
		if (type == "object") {
			if (!inp.constructor) {
				return 'object';
			}
			var cons = inp.constructor.toString();
			match = cons.match(/(\w+)\(/);
			if (match) {
				cons = match[1].toLowerCase();
			}
			var types = ["boolean", "number", "string", "array"];
			for (key in types) {
				if (cons == types[key]) {
					type = types[key];
					break;
				}
			}
		}
		return type;
	};
	var type = _getType(mixed_value);
	var val, ktype = '';

	switch (type) {
		case "function":
			val = "";
			break;
		case "boolean":
			val = "b:" + (mixed_value ? "1" : "0");
			break;
		case "number":
			val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
			break;
		case "string":
			val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
			break;
		case "array":
		case "object":
			val = "a";
			/*
			if (type == "object") {
				var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
				if (objname == undefined) {
					return;
				}
				objname[1] = serialize(objname[1]);
				val = "O" + objname[1].substring(1, objname[1].length - 1);
			}
			*/
			var count = 0;
			var vals = "";
			var okey;
			var key;
			for (key in mixed_value) {
				ktype = _getType(mixed_value[key]);
				if (ktype == "function") {
					continue;
				}

				okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
				vals += serialize(okey) +
						serialize(mixed_value[key]);
				count++;
			}
			val += ":" + count + ":{" + vals + "}";
			break;
		case "undefined": // Fall-through
		default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
			val = "N";
			break;
	}
	if (type != "object" && type != "array") {
		val += ";";
	}
	return val;
}

function bookmarksite(title,url){
	if (window.sidebar) {// firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all) {// ie
		window.external.AddFavorite(url, title);
	}
}
