//CROSS-BROWSER COMPATIBILITY	
var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isIE = (document.all ? true : false);
var isNS4 = (document.layers ? true : false);
//var isDyn = (isDOM || isIE4 || isNS4);
function getRef(id){
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	//if (isNS4) return document.layers[id];
}
function getSty(id){		
	 if (isDOM) return document.getElementById(id).style;
	 if (isIE4) return document.all[id].style;
	 //if (isNS4) return document.layers[id];
}
function popup(url, name, w,h,sb,res){
	window.open(url, name,'width=' + w + ',height=' + h + ',scrollbars=' + sb + ',resizable=' + res );
}
function zichtbaar(mode, i){
	document.frm.SUBMIT.value = 'visibility';
	document.frm.scrl.value = isIE?document.body.scrollTop:top.cnt.window.scrollY;
	document.frm.item.value = i;
	document.frm.visibl.value = mode;
	document.frm.submit();
}
function del(i){
	if(confirm("Weet u zeker dat u dit item wilt verwijderen?")){
		document.frm.SUBMIT.value = 'delete';
		document.frm.item.value = i;
		document.frm.submit();
	}
}
function del_grp(i){
	if(confirm("Weet u zeker dat u deze groep wilt verwijderen?")){
		document.frm.SUBMIT.value = 'delete';
		document.frm.delgrp.value = i;
		document.frm.submit();
	}
}
function res(x,y){
	window.resizeTo(x + 8,y+(isIE?53:70));
}
//MAKE LAYERS DRAGABLE
function beginDrag(id, event, w, h){
	var scrlX = (isIE?document.body.scrollLeft:window.scrollX);
	var scrlY = (isIE?document.body.scrollTop:window.scrollY);
	var x = parseInt(id.style.left) - scrlX;
	var y = parseInt(id.style.top) - scrlY;
	var deltaX = event.clientX - x;
	var deltaY = event.clientY - y;
	window.status = document.body.scrollLeft;
	//move mode
	if(deltaX  < (parseInt(id.style.width)-(isIE?15:11)) && deltaY  < (parseInt(id.style.height)-(isIE?15:11))){
		if (document.addEventListener) {
		    document.addEventListener("mousemove", moveHandler, true)
			document.addEventListener("mouseup", upHandler, true);
		    event.preventDefault();
		} else if (document.attachEvent) {
		    document.attachEvent("onmousemove", moveHandler, true);
		    document.attachEvent("onmouseup", upHandler, true);
			return false;
		} 
		event.stopPropagation();
		event.preventDefault();
	}else{
	//scale mode
		if (document.addEventListener) {
		    document.addEventListener("mousemove", scaleHandler, true)
			document.addEventListener("mouseup", stopHandler, true);
		    event.preventDefault();
		} else if (document.attachEvent) {
		    document.attachEvent("onmousemove", scaleHandler, true);
		    document.attachEvent("onmouseup", stopHandler, true);
			return false;
		} 
		event.stopPropagation();
		event.preventDefault();
	}
	function moveHandler(event){
		id.style.left = (event.clientX - deltaX + scrlX) + "px";
		id.style.top = (event.clientY - deltaY + scrlY) + "px";
		if(parseInt(id.style.left) < 0){
			id.style.left = 0+"px";
		}
		if(parseInt(id.style.left) > w - parseInt(id.style.width) - 4){
			id.style.left = w - parseInt(id.style.width) - (isIE?0:4) + "px";
		}
		if(parseInt(id.style.top) < 0){
			id.style.top = 0+"px";
		}
		if(parseInt(id.style.top) > h - parseInt(id.style.height)){
			id.style.top = h - parseInt(id.style.height) + "px";
		}
		getSty('fill-left').width = parseInt(id.style.left) + "px";
		getSty('fill-right').left = parseInt(id.style.left) + parseInt(id.style.width) + (!isIE?4:0) + "px";
		getSty('fill-right').width = w - parseInt(id.style.width) - parseInt(id.style.left);
		getSty('fill-top').height = parseInt(id.style.top) + "px";
		getSty('fill-top').width = parseInt(id.style.width) + (!isIE?4:0);
		getSty('fill-top').left = parseInt(id.style.left);
		getSty('fill-bottom').top = parseInt(id.style.top)+parseInt(id.style.height) + (!isIE?4:0) +"px";
		getSty('fill-bottom').width = parseInt(id.style.width) + (!isIE?4:0);
		getSty('fill-bottom').left = parseInt(id.style.left);
		getSty('fill-bottom').height = h - parseInt(getSty('fill-bottom').top) +"px";
		if (event.preventDefault){
			event.preventDefault();
			return true;
		}else{
			return false;
		}
	}
	function scaleHandler(event){
		if(event.clientX > event.clientY){
			id.style.width = (event.clientX + scrlX - parseInt(id.style.left)) + "px";
			id.style.height = id.style.width;
			getSty('scale').left = (event.clientX + scrlX - parseInt(id.style.left)-(isIE?15:11)) + "px";
			getSty('scale').top = (event.clientX  + scrlX - parseInt(id.style.left)-(isIE?15:11)) + "px";
			getSty('fill-left').width = parseInt(id.style.left) + "px";
			getSty('fill-right').left = parseInt(id.style.left) + parseInt(id.style.width) + (!isIE?4:0) + "px";
			getSty('fill-right').width = w - parseInt(id.style.width) - parseInt(id.style.left);
			getSty('fill-top').height = parseInt(id.style.top) + "px";
			getSty('fill-top').width = parseInt(id.style.width) + (!isIE?4:0);
			getSty('fill-top').left = parseInt(id.style.left);
			getSty('fill-bottom').top = parseInt(id.style.top)+parseInt(id.style.height) + (!isIE?4:0) +"px";
			getSty('fill-bottom').width = parseInt(id.style.width) + (!isIE?4:0);
			getSty('fill-bottom').left = parseInt(id.style.left);
			getSty('fill-bottom').height = h - parseInt(getSty('fill-bottom').top) +"px";
		}else{
			id.style.height = (event.clientY + scrlY - parseInt(id.style.top)) + "px";
			id.style.width = id.style.height;
			getSty('scale').left = (event.clientY + scrlY - parseInt(id.style.top)-(isIE?15:11)) + "px";
			getSty('scale').top = (event.clientY + scrlY - parseInt(id.style.top)-(isIE?15:11)) + "px";
			getSty('fill-left').width = parseInt(id.style.left) + "px";
			getSty('fill-right').left = parseInt(id.style.left) + parseInt(id.style.width) + (!isIE?4:0) + "px";
			getSty('fill-right').width = w - parseInt(id.style.width) - parseInt(id.style.left);
			getSty('fill-top').height = parseInt(id.style.top) + "px";
			getSty('fill-top').width = parseInt(id.style.width) + (!isIE?4:0);
			getSty('fill-top').left = parseInt(id.style.left);
			getSty('fill-bottom').top = parseInt(id.style.top)+parseInt(id.style.height) + (!isIE?4:0) +"px";
			getSty('fill-bottom').width = parseInt(id.style.width) + (!isIE?4:0);
			getSty('fill-bottom').left = parseInt(id.style.left);
			getSty('fill-bottom').height = h - parseInt(getSty('fill-bottom').top) +"px";
		}
		if(parseInt(id.style.left) < 0){
			id.style.left = 0+"px";
		}
		if(parseInt(id.style.left) > w - parseInt(id.style.width) - 4){
			id.style.left = w - parseInt(id.style.width) - (isIE?0:4) + "px";
		}
		if(parseInt(id.style.top) < 0){
			id.style.top = 0+"px";
		}
		if(parseInt(id.style.top) > h - parseInt(id.style.height) - (isIE?22:26)){
			id.style.top = h - parseInt(id.style.height) - (isIE?22:26) + "px";
		}
		if (event.preventDefault){
			event.preventDefault();
			return true;
		}else{
			return false;
		}
	}
	function upHandler(event){
		if (document.removeEventListener) {
			document.removeEventListener("mousemove", moveHandler, true);
			document.removeEventListener("mouseup", upHandler, true);
		} else if (document.detachEvent) {
			document.detachEvent("onmousemove", moveHandler);
			document.detachEvent("onmouseup", upHandler);
		}
		return true;
	}
	function stopHandler(event){
		if (document.removeEventListener) {
			document.removeEventListener("mousemove", scaleHandler, true);
			document.removeEventListener("mouseup", stopHandler, true);
		} else if (document.detachEvent) {
			document.detachEvent("onmousemove", scaleHandler);
			document.detachEvent("onmouseup", stopHandler);
		} 
		return true;
	}
	return true;
}
function buttons(mode){
	var scrlX = (isIE?document.body.scrollLeft:window.scrollX);
	var scrlY = (isIE?document.body.scrollTop:window.scrollY);
	getSty('buttons').top = document.body.clientHeight + scrlY - parseInt(getSty('buttons').height) +"px";
	getSty('buttons').left = scrlX  + "px";
	getSty('previ').top =  scrlY + "px";
	getSty('previ').left = scrlX  + "px";
}
function prev(x,y){
	getSty('preview').width = x +"px";
	getSty('preview').height = y +"px"
	perc = Math.round(93/(parseInt(getSty('crop').width)/0.93/100));
	getSty('preview').width = parseInt(getSty('preview').width) / 100 * perc +"px";
	getSty('preview').height = parseInt(getSty('preview').height) / 100 * perc +"px"
	getSty('preview').top = -parseInt(getSty('crop').top) / 100 * perc +"px";
	getSty('preview').left = -parseInt(getSty('crop').left) / 100 * perc +"px";
	getSty('previ').visibility = 'visible';
}
function crop(){
	document.frm.SUBMIT.value = 1;
	document.frm.ptop.value = parseInt(getSty('crop').top);
	document.frm.pleft.value = parseInt(getSty('crop').left);
	document.frm.pwidth.value = parseInt(getSty('crop').width);
	document.frm.pheight.value = parseInt(getSty('crop').height);
	document.frm.submit();
}
function grp_info(id, mode, event){
	if(mode){
		getRef('info').innerHTML = '<div style="color: #ffffff; background: #595a5c; padding: 3px;"><strong>Verbonden aan:</strong></div>' + eval('info'+id+' != ""?info'+id+':"nog niet verbonden";');
		getSty('info').top = event.clientY + 20+"px";
		getSty('info').left = event.clientX - parseInt(getSty('info').width) - 20 +"px";
		getSty('info').visibility = 'visible';
	}else{
		getSty('info').visibility = 'hidden';
	}
}
function rn_dates(){
	//alert('rn_dates');
	document.frm2.scrl.value = isIE?document.body.scrollTop:window.scrollY;
	el = document.frm2.elements;
	for(i=0;i<document.frm2.elements.length;i++){
		if(el[i].name.lastIndexOf("dates") != -1){
			idxs = el[i].name.substr(el[i].name.lastIndexOf("dates")+5).split('_');
			//el[i].name = 'dates'+idxs[2]+'['+idxs[0]+']['+idxs[1]+']';
			el[i].name = 'dates'+idxs[2]+'['+idxs[0]+'][]';
		}
	}
	document.frm2.submit();
}
function chk_year(){
	var yearpat = /^2[0-9]{3}/;
	var msg = '';
	if(document.frm2.newyear.value == ''){
		msg += "Vul het jaar in waarvoor u data wilt genereren.";
	}else{
		if(!yearpat.test(document.frm2.newyear.value)){
			msg += "Het jaar dat u opgeeft klopt niet.\n Vul vier cijfers in.";
		}
	}
	if(msg != ''){
		alert(msg);
	}else{
		if(confirm('Weet u zeker dat u voor '+ document.frm2.newyear.value +' de prijzen wilt genereren?\nBestaande prijzen en data zullen worden overschreven.')){
			rn_dates();
		}else{
			document.frm2.newyear.value = '';
		}
	}
}
