/*
	scripts voor publishing object
	
*/

/*
	de row waar je op klikt
*/
function _getRow(key){
	try {
		var row = document.getElementById("col_"+key);
		return row;
	}
	catch(e){
		window.status = e.message;
	}
	return null;
}


/*
	als er op een row geklikt wordt moet e.e.a. veranderen.
	key = col_id
	force = sluiten 
*/	
function collapse(key, force, cookieName){
	var cn = (cookieName==null)?"categories":cookieName;
	var row = _getRow(key);
	if(row != null){
		var open = (row.getAttribute("open")=="true")?"false":"true";			//--- swap wat'ie nu is
		open = (force)?"false":open;																			//--- verplicht dicht?
		row.setAttribute("open", open);																		//--- terugschrijven
		/*
			check de children, style.display = "none" als ze dicht zitten
		*/
		var coll = document.getElementsByTagName("TR");
		for (var i=0;i<coll.length;i++){
			var child = coll[i];
			if(child.getAttribute("parentId") == key){											//--- is dit wel mijn child?		
				child.style.display = (open=="true")?"":"none";								//--- update display
				var itemId = child.getAttribute("itemId");
				if(open=="false" && itemId != null) collapse(itemId, true, cn);		//--- sluit dan ook de child
			}
		}
	}
	_saveState(cn);
}

/*
	saved het cookie met welke rows open staan
*/
function _saveState(cookieName){
	var arr = new Array();
	var coll = document.getElementsByTagName("TR");
	for (var i=0;i<coll.length;i++){
		var open = coll[i].getAttribute("open");
		if(open == "true"){
			arr.push(coll[i].getAttribute("itemId"));
		}
	}
	setCookie(cookieName, arr.join(','));
}

/*
	init voor de collapses
*/
function initCollapse(cookieName){
	var cn = (cookieName==null)?"categories":cookieName;
	var open = new String(getCookie(cn));
	if(open != null){
		var coll = open.split(',');
		for(var i=0;i<coll.length;i++){
			collapse(coll[i], false, cn);
		}
	}
}


/*
	activeert 1 bepaalde tab, tabs zijn duo's
	
*/
function clickTab(tab){
	if(!tab.id.match(/^th.+$/)) return false;
	var key = tab.id.replace(/^th(.+)$/, "$1");
	
	/* alleen als we een pair hebben komen we in actie */
	var body = document.getElementById("tb"+key);
	if(!body) return false;

	setCookie("lasttab", key);
	
	//--- sluit alle tabs
	var divs = document.getElementsByTagName("DIV");
	for(var i=0;i<divs.length;i++){
		if(divs[i].id.match(/^th.+$/)){
			
			var key = divs[i].id.replace(/^th(.+)$/, "$1");
			var tb = document.getElementById("tb"+key);
			if(!tb) continue;
			divs[i].className = "tab selectable inactive";
			tb.className = "tabbody hidden";
		}
	}

	//--- en open de gekozen tabs
	tab.className = "tab active";
	body.className = "tabbody show";
	
	
}

/*
	opent de laatst geopende tab
*/
function initTabs(){
	var key = getCookie("lasttab");
	if(key != null){
		var obj = document.getElementById("th"+key);
		if(obj)	clickTab(obj);
	}
}	

/*
	als de statusId wijzigt moeten online form fields ook wijzigen!
*/
function statusChanged(obj){
	if(!obj) return false;
	if(obj.value == "published") return false;	/* online moet dan met de hand gezet worden */
		
	var online = obj.form.online;
	/* aangezien online in deze forms een yes/no is, is yes de 0e, en no de 1e */
	online[1].checked = true;
}

/*
	als de statusId wijzigt moeten online form fields ook wijzigen!
*/
function onlineChanged(obj){
	if(!obj) return false;
	try {
		if(obj.form.status.value == "published") return true;
		obj.form.online[1].checked = 1;
	}
	catch(e){}//-- form heeft geen status
}


/*
	is opgelost met een behaviour en stylesheet
*/
function menuItemHover(obj){
	obj.className = "menuItem hover";
}
function menuItemNover(obj){
	obj.className = "menuItem";
}





/*
	cookie functies
*/
function getCookie(name){
	try {
		var re = new RegExp(name+"=(.+?);");
		return unescape(re.exec(document.cookie+";")[1]);	/* ; aan het eind ;-) */
	}
	catch(e){
		return null;
	}
}

function setCookie(name, value){ 
	document.cookie = name + "=" + escape(value);
}

function deleteCookie(name){ 
	document.cookie = name + "=" + escape(""); + ";expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}






/*
	functies voor custom popup msgs
*/
var POPWARNING = 1;
var POPABOUT = 2;
var POPERROR = 3; 
var POPREFERENCE = 4;

function popup(event, title, msg, url, popupType){
	if(window._popupWindow != null) window._popupWindow.destroy();
	window._popupWindow = new popupWindow(event, title, msg, url, popupType);
	return false;
}

function popupWindow(event, title, msg, url, popupType){
	this.setEvent(event);
	this._url = url;
	this._popupType = popupType;

	
	this.prepare();

	switch(popupType){
		case POPWARNING:
			this.setSize();	// default
			this.setIcon("/shared/images/publish/warning.gif");
			this.setBgColor("#eeeeee");
			this.addButton("Ja", "location.href='" + url + "'", "#e8e8e8");
			this.addButton("Nee", "this._window.destroy()", "#e8e8e8");
			
			break;
			
		case POPABOUT:
			this.setSize();	// default
			this.setIcon("/shared/images/publish/error.gif");
			this.setBgColor("#eeeeff");
			this.addButton("Ok", "this._window.destroy()", "#e8e8ff");
			break;
			
		case POPERROR:
			this.setSize();	// default
			this.setIcon("/shared/images/publish/error.gif");
			this.setBgColor("#ffeeee");
			this.addButton("Ok", "this._window.destroy()", "#ffe8e8");
			break;
			
		case POPREFERENCE:
			this.setSize(500, 120);	
			this.setIcon("/shared/images/publish/reference.gif");
			this.setBgColor("#ffffdd");
			this.addButton("Ok", "this._window.destroy()", "#ffffee");
			break;
			
		default:
			alert(msg);
	}
	
	this.setTitle(title);
	this.setBody(msg);
	this.show();
	this.centerScreen();
	
	return this;
}

popupWindow.prototype.setSize = function(w, h){
	this._width = (w != null) ? w : 320;
	this._height =  (h != null) ? h : 200;
	this._window.style.cssText = "position: absolute; width: " + this._width + "px; " + this._height + "px; border: 1px solid #000000; padding: 5px";
	this._shadow.style.cssText = "position: absolute; width: " + this._width + "px; " + this._height + "px; border: none; background-color: #999999; filter:alpha(opacity=50); -moz-opacity:0.5; opacity: 0.5;";
}

popupWindow.prototype.prepare = function(){

	/* defaults */
	this._width = 320;
	this._height = 200;

	this._window = document.createElement("DIV");

	this._shadow = document.createElement("DIV");
	
	this._header = document.createElement("DIV");
	this._header.style.cssText = "display: block; margin: 2px;";
	this._window.appendChild(this._header);

	this._icon = document.createElement("IMG");
	this._icon.style.cssText = "border: 0px; margin: 0px; display: inline; text-align: left; vertical-align: absmiddle;"
	this._header.appendChild(this._icon);

	this._title = document.createElement("DIV");
	this._title.style.cssText = "font-size: 14px; font-weight: bold; display: inline; margin: 2px;";
	this._header.appendChild(this._title);

	this._body = document.createElement("DIV");
	this._body.style.cssText = "font-size: 10px; margin: 2px; margin-top: 5px; margin-bottom: 10px; font-weight: bold";
	this._window.appendChild(this._body);
	
	this._buttons = document.createElement("DIV");
	this._buttons.style.cssText = "text-align: right; padding: 2px;";	
	this._window.appendChild(this._buttons);
	
}

popupWindow.prototype.addButton = function(val, func, col){
	var btn = document.createElement("BUTTON");
	btn.style.cssText = "margin: 1px; border: 1px outset; background-color: " + col + "; width: 80px; height: 20px; padding: 0px; font-family: Arial; font-weight: bold; font-size: 10px";
	btn._window = this;
	btn.innerHTML = val;
	btn.onclick = new Function(func);
	this._buttons.appendChild(btn);
}
popupWindow.prototype.centerScreen = function(){
	var x = (this._windowWidth/2) - (this._width/2);
	var y = (this._windowHeight/2) - (this._height/2);
	this.moveTo(x,y);	
}
popupWindow.prototype.show = function(){
	if(this._shadow) document.body.appendChild(this._shadow);
	document.body.appendChild(this._window);
}

popupWindow.prototype.setEvent = function(event){
	this.getScrollOffset();	/* deze doen we stiekum ook */
	this.getWindowSize();
	if(event != null){
		this._event = event;
		this._eventX = event.x ? event.x : event.clientX;
		this._eventY = event.y ? event.y : event.clientY;
	}
}
popupWindow.prototype.setIcon = function(url){
	this._icon.src = url;
}
popupWindow.prototype.setTitle = function(str){
	this._title.innerHTML = str;
}
popupWindow.prototype.setBody = function(str){
	this._msg = str;
	this._body.innerHTML = str;
}
popupWindow.prototype.setBgColor = function(col){
	this._window.style.backgroundColor = col;
}
popupWindow.prototype.destroy = function(){
	if(this._shadow) document.body.removeChild(this._shadow);	
	document.body.removeChild(this._window);
	window._popupWindow = null;
}
popupWindow.prototype.getScrollOffset = function (){
	var x,y;
	if (window.pageYOffset){	//-- alles wat geen ie is
		x = window.pageXOffset;
		y = window.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop) { //-- ie strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body){	//-- alles wat ie is
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	this._bodyScrollLeft = x;
	this._bodyScrollTop = y;
}
popupWindow.prototype.getWindowSize = function(){
	var x,y;
	if (window.innerHeight){
		x = window.innerWidth;
		y = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body){
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	this._windowWidth = x;
	this._windowHeight = y;
}

popupWindow.prototype.moveTo = function(x,y){
	this.x = (x ? x : this.x) + this._bodyScrollLeft;
	this.y = (y ? y : this.y) + this._bodyScrollTop;
	this._window.style.left = this.x + "px";
	this._window.style.top = this.y + "px";

	this._shadow.style.height = this._window.offsetHeight + "px";
	this._shadow.style.width = this._window.offsetWidth + "px";
	
	this._shadow.style.left = (this.x + 3) + "px" ;
	this._shadow.style.top = (this.y + 3) + "px";
	
}


/* filters en checks */
function filterInteger(obj){								
	obj.value = obj.value.replace(/[^\d\-]/, "").replace(/^(.+)-(.*)/, "$1$2").replace(/^(-?)0+(\d+)$/, "$1$2");
}
/* 
	accepteert 0..9, - . en , 
	accepteer - alleen als'ie vooraan staat
	komma wordt punt
	een eenzame punt krijgt 0.
	en 0.123 wordt 0.12
	er mag geen - in die niet aan het begin staat
	er mogen niet meer dan . punt in
	
*/
function filterReal(obj){										
	obj.value = obj.value.replace(/[^\.\,\-\d]/, "").replace(/\,/, ".").replace(/^(-?)\./, "$10.").replace(/^(-?\d+\.\d{2})\d+$/, "$1").replace(/^(.+)-(.*)/, "$1$2").replace(/^(.+)\.(.*)\.(.*)$/, "$1.$2$3");
}
/*
	adres gegevens, postcode b.v.
*/

function filterPostcode(obj){
	obj.value = obj.value.replace(/[^\w\d\s]/, "").toUpperCase().replace(/^(\d{0,3})[A-Z\s]/, "$1").replace(/^(\d{4})$/, "$1 ").replace(/^(\d{4})([^\s])/, "$1 $2").replace(/^(\d{4}\s)[^A-Z]/, "$1").replace(/^(\d{4}\s[A-Z]{2}).*$/, "$1");
}

/*
	check functies controleren textvelden op specifieke input
	de optionele msg wordt getoond als de check fout is
	optioneel kan een boolean focus worden meegegeven om de focus weer naar dat formfield te zetten
*/
function checkInteger(obj, msg, focus){
	if (obj.value.match(/^-?\d+$/)) return true;
	if (msg) alertWindow(msg);
	if (focus) obj.focus();
	return false;	
}	

function checkReal(obj, msg, focus){
	if (obj.value.match(/^-?\d+(\.\d{1,2})?$/)) return true;
	if (msg) alertWindow(msg);
	if (focus) obj.focus();
	return false;
}

/*
	adres gegevens
*/
function checkPostcode(obj, msg, focus){
	if (obj.value.match(/^\d{4}\s\w{2}$/)) return true;
	if (msg) alertWindow(msg);
	if (focus) obj.focus();
	return false;
}

/* compatible met oude openWindow functie */
function openWindow(id){
	var reference = document.getElementById("reference" + id);
	popup(null, "Referentie", reference.innerHTML, null, POPREFERENCE);
}
