/* DatePicker */
YAHOO.namespace("example");
	var DatePickerObj = function(opts) {
		this.textfield = null;
		this.datepicker = null; // YAHOO.ext.DatePicker object
		this.calID = YAHOO.util.Dom.generateId(0, 'dpobj-dp-');
		this.showDatePicker = null;
		this.config = {
			textfield: null, // id/HTML Element
			showPickerOnFocus: false, // show the picker when the textfield is focused
			outputDateFmt: 'm/d/Y',
			showPicker: null,
			twoDigitYearCutoff: 1980 // null to ignore, otherwise dates before this have 100 years added to them (1904 becomes 2004).
		};
		// initialize our config
		if( typeof opts != 'undefined' ) {
			for( var c in this.config) {
				if( typeof opts[c] != "undefined" ) {
					this.config[c] = opts[c];
				}
			}
		}
	}
	DatePickerObj.prototype.init = function() {
		if( this.config.showPicker ) {
			this.showDatePicker = getEl(this.config.showPicker);
			// add our click handler to show the link
			this.showDatePicker.on('click', this.show, this, true);
		}
		if( this.config.textfield ) {
			this.textfield = getEl(this.config.textfield);
			if( this.config.showPickerOnFocus ) {
				this.textfield.set({autocomplete:'off'}); // work around firefox errors
				this.textfield.on('focus', this.show, this, true);
			}
		}
		this.datepicker = new YAHOO.ext.DatePicker(this.calID);
	}
	DatePickerObj.prototype.show = function() {
		var selectedDate = new Date();
		var _this = this; // scope our callback appropriately
		var r = { left:0, bottom:0 };
		if( this.textfield && this.config.showPickerOnFocus ) {
			r = this.textfield.getRegion();
		} else if( this.showDatePicker ) {
			r = this.showDatePicker.getRegion();
		}
		if( this.textfield && this.textfield.dom.value != '' ) {
			//selectedDate = Date.parse(this.textfield.dom.value);
			selectedDate = Date.parseDate(this.textfield.dom.value,this.config.outputDateFmt);
			if( isNaN(selectedDate) ) {
				selectedDate = new Date();
			} else {
				selectedDate = new Date(selectedDate);
				if( this.config.twoDigitYearCutoff && selectedDate.getFullYear() <= this.config.twoDigitYearCutoff ) {
					selectedDate.setFullYear(selectedDate.getFullYear()+100);
				}
			}
		}
		this.datepicker.show(r.left, r.bottom, selectedDate, function(selDate) { _this.dateSelectionHandler(selDate); } );
	}
	// override this to deal with other types of elements like select boxes
	DatePickerObj.prototype.dateSelectionHandler = function(selDate) {
		if( this.textfield ) {
			this.textfield.dom.value = selDate.dateFormat(this.config.outputDateFmt);
			this.textfield.dom.focus();
		}
	}
dg=new Array();
tb=new Array();
function dgcol(name, align, visible, maxchars, type, sortable) {
  this.name = name
  this.align = align
  this.visible = visible
  this.maxchars = maxchars
  this.type = type
  this.sortable = sortable
}
function dgfoot(value, align) {
  this.value = value
  this.align = align
}
function sortableDG(id,row){
	YAHOO.example.DDApp = function() {
	    return {
	        init: function() {
	
	            var i = 0;
	            for (j=1;j <= row;++j) {
	                new YAHOO.example.DDList(id + "r" + j);
	                //new YAHOO.example.DDList("li" + (j + 20));
	                //new YAHOO.example.DDList("li" + (j + 40), "right");
	            }
	
	            //new YAHOO.example.DDListBoundary("hidden1");
	            //new YAHOO.example.DDListBoundary("hidden6");
	            //new YAHOO.example.DDListBoundary("hidden2");
	            //new YAHOO.example.DDListBoundary("hidden3", "right");
	
	            // dd11 = new YAHOO.example.DDList("li11");
	            // dd11.addToGroup("right");
	            // dd12 = new YAHOO.example.DDList("li12");
	            // dd12.addToGroup("right");
	            YAHOO.util.DDM.mode = 0;
	        }
	    };
	} ();
	//YAHOO.util.Event.addListener(window, "load", YAHOO.example.DDApp.init);
	YAHOO.example.DDApp.init();
	// YAHOO.util.DDM.useCache = false;>
}
function scrollHandlerDG(id,top,left){
	self.document.getElementById("ZZZHead"+id).scrollLeft=left
	dg[id]["scroll"]=new Array(top,left)
}
function fdgc(id,w,sortColumn){
	cdg=dg[id]["cols"];
	grid="";
	wi=""
	i = 0;
	brt=0
	for(a in cdg) {
		if(!(a=="indexOf"||a=="filter")) {
			cc=cdg[a];
			if(cc.visible==1){
				if(w==1){
				row=self.document.getElementById(id+"-X1"+"-Y"+a)
				brtk=row.offsetWidth
				if (a==1){
				br=row.offsetWidth-5
				}else{
				br=row.offsetWidth-6
					}
				}
			talign = (cc.align!="") ? " align=\""+cc.align+"\"" : "";
			if(a==sortColumn){sortC=a*-1}else{sortC=a}
			tsorting = ""
			if(sortColumn==a || sortColumn==-a){
				if(sortColumn<0){tsorting="<img src='/images/ol.gif'>"}else{tsorting="<img src='/images/oh.gif'>"}
			}
			tclass = "datagrid_title"
				if(tsorting!=""){tclass="datagrid_title_over"}else{}
			tonclick = (cc.sortable==1) ? " onclick=\"top.JsEvent('SortColumn','"+WcsTab+"','"+id+"',"+sortC+");\"" : "";
			tmsorting = (cc.sortable==1) ? " onmouseover=\"className='datagrid_title_over'\" onmouseout=\"className='"+tclass+"'\"" : "";
			if(w==1) wi="width='"+br+"px'"
			if(cc.visible==1) grid+="<td nowrap"+tmsorting+" class='"+tclass+"'"+wi+" id='zzz"+id+"Title"+(i+1)+"'"+talign+tonclick+">"+cc.name+tsorting+"</td>";
			i+=1;
			if(w==1) brt+=brtk
			}
		}
	}
	grid+="<td width='52px'>&nbsp;</td></tr></table>"
	if(w==1){
		grid="<table border='0' cellspacing='0' cellpadding='0' width='"+(brt+53)+"'><tr>"+grid;
	}else{
		grid="<table border='0' cellspacing='0' cellpadding='0'><tr>"+grid;
	}
	self.document.getElementById("ZZZHead"+id).innerHTML = grid;
}
function fdgf(id,w){
	cdg=dg[id]["cols"];
	cdgf=dg[id]["foot"];
	grid="";
	wi=""
	i = 0;
	brt=0
	for(a in cdg) {
		if(!(a=="indexOf"||a=="filter")) {
			cc=cdg[a];
			ccf=cdgf[a];
			if(cc.visible==1){
				if(w==1){
				row=self.document.getElementById(id+"-X1"+"-Y"+a)
				brtk=row.offsetWidth
				if (a==1){
				br=row.offsetWidth-5
				}else{
				br=row.offsetWidth-6
					}
				}
			talign = (ccf.align!="") ? " align=\""+ccf.align+"\"" : "";
			if(w==1) wi="width='"+br+"px'"
			if(cc.visible==1) grid+="<td nowrap class='datagrid_foot'"+wi+" id='zzz"+id+"Foot"+(i+1)+"'"+talign+">"+ccf.value+"</td>";
			i+=1;
			if(w==1) brt+=brtk
			}
		}
	}
	grid+="<td width='52px'>&nbsp;</td></tr></table>"
	if(w==1){
		grid="<table border='0' cellspacing='0' cellpadding='0' width='"+(brt+53)+"'><tr>"+grid;
	}else{
		grid="<table border='0' cellspacing='0' cellpadding='0'><tr>"+grid;
	}
 	self.document.getElementById("ZZZFoot"+id).innerHTML = grid;
	self.document.getElementById("ZZZFootTr"+id).style.display = "block";
}
function addHTML(id,html) {
  if (document.all)
    document.getElementById(id).insertAdjacentHTML('beforeEnd', html);
  else if (document.createRange) {
    var range = document.createRange();
    range.setStartAfter(document.body.lastChild);
    var docFrag = range.createContextualFragment(html);
    document.getElementById(id).appendChild(docFrag);
  }
}
var wcsSearchWin=0;
function dLtextWindow(tab,id){
	var width=300;
	var height=200;
	var left=(YAHOO.util.Dom.getViewportWidth()/2)-(width/2);
	var top=(YAHOO.util.Dom.getViewportHeight()/2)-(height/2);
	zzzlangWin.cfg.setProperty("width",(width)+"px");
	zzzlangWin.cfg.setProperty("height",(height)+"px");
	zzzlangWin.cfg.setProperty("x",left);
	zzzlangWin.cfg.setProperty("y",top);
	document.getElementById("zzzlangWinIF").src="wcsLtext.csp?id="+id+"&tab="+tab
	document.getElementById("zzzlangWinIF").style.height=(height-20)+"px";
	document.getElementById("zzzlangWinIF").style.width=(width)+"px";
	zzzlangWin.show();
}
function dSearchWindow(id,searchid,count,width,sort,tab){
 	//win=eval("wcsSW"+id+tab)
	/*if(eval("wcsSW"+id+searchid+tab)){ 
	SWF=1
		if(!eval("wcsSW"+id+searchid+tab).closed){
			eval("wcsSW"+id+searchid+tab).close();
		}
	}*/
	var width=70+(width*8);
	if(width<220) width=220
	var height=566;
	if(count<36){ height=60+(14.2*count) }
	var left=(YAHOO.util.Dom.getViewportWidth()/2)-(width/2);
	var top=(YAHOO.util.Dom.getViewportHeight()/2)-(height/2);
	//delete zzzsearchWin
	//zzzsearchWin=new YAHOO.widget.Panel("zzzsearchWin");
	//, { visible:true, fixedcenter:true, constraintoviewport:true, close:true, zIndex:300, modal:true }
	zzzsearchWin.cfg.setProperty("width",(width)+"px");
	zzzsearchWin.cfg.setProperty("height",(height)+"px");
	zzzsearchWin.cfg.setProperty("x",left);
	zzzsearchWin.cfg.setProperty("y",top);
	//zzzsearchWin.cfg.setProperty("context",[id,"tl","bl"]);
	//zzzsearchWin.cfg.queueProperty("fixedcenter",true);
	//zzzsearchWin.cfg.queueProperty("modal",true);
	document.getElementById("zzzsearchWinIF").src="wcsSearch.csp?id="+id+"&searchid="+searchid+"&count="+count+"&sort="+sort+"&tab="+zzztab
	document.getElementById("zzzsearchWinIF").style.height=(height-20)+"px";
	document.getElementById("zzzsearchWinIF").style.width=(width)+"px";

	//zzzsearchWin.render();
	zzzsearchWin.show();

	//var zzzsearchWin=new YAHOO.widget.Panel("zzzsearchWin");
	//, { visible:true, fixedcenter:true, constraintoviewport:true, close:true, zIndex:300, modal:true }
	//document.getElementById("zzzsearchWinIF").src="wcsSearch.csp?id="+id+"&searchid="+searchid+"&count="+count+"&sort="+sort+"&tab="+zzztab
	//zzzsearchWin.cfg.setProperty("width",width+"px");
	//zzzsearchWin.cfg.setProperty("height",height+"px");
	//zzzsearchWin.cfg.setProperty("context",[id,"tl","tl"]);
	//zzzsearchWin.cfg.setProperty("constraintoviewport",true);
	//zzzsearchWin.cfg.setProperty("close",true);
	//zzzsearchWin.cfg.setProperty("modal",true);
	//zzzsearchWin.render();
	//eval("wcsSW"+id+searchid+tab+"=window.open('wcsSearch.csp?id="+id+"&searchid="+searchid+"&count="+count+"&sort="+sort+"&tab="+zzztab+"', 'wcsSW"+id+searchid+tab+"','width="+width+",height="+height+",screenX="+left+",screenY="+top+",left="+left+",top="+top+",toolbar=0,menubar=0,directories=0,location=O,status=1,copyhistory=0');");
}
function dSearchDGWindow(id,searchid,count,width,sort,tab,a,b){
 	//win=eval("wcsSW"+id+tab)
	/*if(eval("wcsSW"+id+searchid+tab)){ 
	SWF=1
		if(!eval("wcsSW"+id+searchid+tab).closed){
			eval("wcsSW"+id+searchid+tab).close();
		}
	}*/
	var width=70+(width*8);
	if(width<220) width=220
	var height=566;
	if(count<36){ height=60+(14.2*count) }
	var left=(YAHOO.util.Dom.getViewportWidth()/2)-(width/2);
	var top=(YAHOO.util.Dom.getViewportHeight()/2)-(height/2);
	//eval("wcsSW"+id+searchid+tab+"=window.open('wcsSearch.csp?id="+id+"&searchid="+searchid+"&count="+count+"&sort="+sort+"&tab="+zzztab+"&a="+a+"&b="+b+"', 'wcsSW"+id+searchid+tab+"','width="+width+",height="+height+",screenX="+left+",screenY="+top+",left="+left+",top="+top+",toolbar=0,menubar=0,directories=0,location=O,status=1,copyhistory=0');");
	zzzsearchWin.cfg.setProperty("width",(width)+"px");
	zzzsearchWin.cfg.setProperty("height",(height)+"px");
	zzzsearchWin.cfg.setProperty("x",left);
	zzzsearchWin.cfg.setProperty("y",top);
	//zzzsearchWin.cfg.setProperty("context",[id,"tl","bl"]);
	//zzzsearchWin.cfg.queueProperty("fixedcenter",true);
	//zzzsearchWin.cfg.queueProperty("modal",true);
	document.getElementById("zzzsearchWinIF").src="wcsSearch.csp?id="+id+"&searchid="+searchid+"&count="+count+"&sort="+sort+"&tab="+zzztab+"&a="+a+"&b="+b
	//document.getElementById("zzzsearchWinIF").contentWindow.focus();
	document.getElementById("zzzsearchWinIF").style.height=(height-20)+"px";
	document.getElementById("zzzsearchWinIF").style.width=(width)+"px";

	//zzzsearchWin.render();
	zzzsearchWin.show();
	//alert(document.getElementById("zzzsearchWin").style.height)

}
var wcsPopupWin=0;
function dPopupWindow(tab,program,width,height,x,y,variable){
	if(wcsPopupWin){ 
		if(!wcsPopupWin.closed){
			wcsPopupWin.close();
		}
	}
	if(x==""){
	var left=(screen.width/2)-(width/2);
	}
	if(y==""){
	var top=(screen.height/2)-(height/2);
	}
	wcsPopupWin = window.open('program.csp?tab='+tab+'&program='+program+'&variable='+variable, 'wcsPopupWin' ,'width='+width+',height='+height+',screenX='+left+',screenY='+top+',left='+left+',top='+top+',toolbar=0,scrollbars=1,menubar=0,directories=0,location=O,status=1,copyhistory=0')
	wcsPopupWin.focus();
}
function sSelected(SelectName, Value){
SelectObject = document.getElementById(SelectName)
var ok=0
for(index = 0; index < SelectObject.length; index++)
{
if(SelectObject[index].value == Value){
SelectObject.selectedIndex = index;
var ok=1
}
}
if (ok==0){
SelectObject.selectedIndex = 0;
}
}
function sCol(dg,tit,row,col){
row=self.document.getElementById(row)
if(self.document.getElementById(tit).offsetWidth>(row.offsetWidth)){
pad=5
if(col==1)
pad=6
row.width=self.document.getElementById(tit).offsetWidth-pad
}
pad=6
if(col==1)
pad=5
if(row.offsetWidth>self.document.getElementById(tit).width)
self.document.getElementById(tit).width=row.offsetWidth-pad
}
Array.prototype.indexOf = function(item) {
for (var i = 0; i < this.length; i++) {
if (this[i] == item) {
return i;
}
}
return -1;
};
Array.prototype.filter = function(test) {
var matches = [];
for (var i = 0; i < this.length; i++) {
if (test(this[i])) {
matches[matches.length] = this[i];
}
}
return matches;
};

var monthNames = "Januari Februari Maart April Mei Juni Juli Augustus September Oktober November December".split(" ");
var weekdayNames = "Zondag Maandag Dinsdag Woensdag Donderdag Vrijdag Zaterdag".split(" ");
function parseMonth(month) {
var matches = monthNames.filter(function(item) { 
return new RegExp("^" + month, "i").test(item);
});
if (matches.length == 0) {
throw new Error("Invalid month string");
}
if (matches.length > 1) {
throw new Error("Ambiguous month");
}
return monthNames.indexOf(matches[0]);
}
/* Same as parseMonth but for days of the week */
function parseWeekday(weekday) {
var matches = weekdayNames.filter(function(item) {
return new RegExp("^" + weekday, "i").test(item);
});
if (matches.length == 0) {
throw new Error("Invalid day string");
}
if (matches.length > 1) {
throw new Error("Ambiguous weekday");
}
return weekdayNames.indexOf(matches[0]);
}

/* Array of objects, each has 're', a regular expression and 'handler', a 
function for creating a date from something that matches the regular 
expression. Handlers may throw errors if string is unparseable. 
*/
var dateParsePatterns = [
// Today
{   re: /^[0]$/,
handler: function() { 
return new Date();
} 
},
// - en + waarde
{   re: /(^[+-]\d{1,5})/,
handler: function(bits) {
//alert("-+ datum")
var d = new Date(); 
d.setDate(d.getDate() + parseInt(bits[1])); 
return d;
}
},
// 4th
{   re: /^(\d{1,2})$/i, 
handler: function(bits) {
//alert("dag")
var d = new Date();
d.setDate(parseInt(bits[1], 10));
return d;
}
},
// 4th Jan
{   re: /^(\d{1,2})(\d{1,2})$/, 
handler: function(bits) {
//alert("dag maand"+parseInt(bits[1], 10)+" "+(parseInt(bits[2], 10) - 1))
var d = new Date();
d.setDate(parseInt(bits[1], 10));
d.setMonth(parseInt(bits[2], 10) - 1);
return d;
}
},
// 4th Jan 2003
{   re: /^(\d{1,2})(?:st|nd|rd|th)? (\w+),? (\d{4})$/i,
handler: function(bits) {
var d = new Date();
d.setDate(parseInt(bits[1], 10));
d.setMonth(parseMonth(bits[2]));
d.setYear(bits[3]);
return d;
}
},
// Jan 4th
{   re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?$/i, 
handler: function(bits) {
var d = new Date();
d.setDate(parseInt(bits[2], 10));
d.setMonth(parseMonth(bits[1]));
return d;
}
},
// Jan 4th 2003
{   re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?,? (\d{4})$/i,
handler: function(bits) {
var d = new Date();
d.setDate(parseInt(bits[2], 10));
d.setMonth(parseMonth(bits[1]));
d.setYear(bits[3]);
return d;
}
},
// next Tuesday - this is suspect due to weird meaning of "next"
{   re: /^next (\w+)$/i,
handler: function(bits) {
var d = new Date();
var day = d.getDay();
var newDay = parseWeekday(bits[1]);
var addDays = newDay - day;
if (newDay <= day) {
addDays += 7;
}
d.setDate(d.getDate() + addDays);
return d;
}
},
// last Tuesday
{   re: /^last (\w+)$/i,
handler: function(bits) {
throw new Error("Not yet implemented");
}
},
// mm/dd/yyyy (American style)
{   re: /(\d{1,2})\/(\d{1,2})\/(\d{4})/,
handler: function(bits) {
var d = new Date();
d.setYear(bits[3]);
d.setDate(parseInt(bits[2], 10));
d.setMonth(parseInt(bits[1], 10) - 1); // Because months indexed from 0
return d;
}
},
// 01012005
{   re: /(\d{1,2})(\d{2})(\d{4})/,
handler: function(bits) {
var d = new Date();
//alert("ddmmjjjj")
d.setYear(bits[3]);
d.setDate(parseInt(bits[1], 10));
d.setMonth(parseInt(bits[2], 10) - 1); // Because months indexed from 0
return d;
}
},
// yyyy-mm-dd (ISO style)
{   re: /(\d{4})-(\d{1,2})-(\d{1,2})/,
handler: function(bits) {
var d = new Date();
d.setYear(parseInt(bits[1]));
d.setDate(parseInt(bits[3], 10));
d.setMonth(parseInt(bits[2], 10) - 1);
return d;
}
},
];

function parseDateString(s) {
for (var i = 0; i < dateParsePatterns.length; i++) {
var re = dateParsePatterns[i].re;
var handler = dateParsePatterns[i].handler;
var bits = re.exec(s);
if (bits) {
return handler(bits);
}
}
throw new Error("Invalid date string");
}

function isDate(tab,input) {
//var messagespan = input.id + 'Msg';
try {
var d = parseDateString(input.value);
dag=d.getDate().toString()
if(dag.length==1) {dag="0"+dag}
maand=(d.getMonth() + 1).toString()
if(maand.length==1) {maand="0"+maand}
input.value =  dag + maand  + d.getFullYear().toString();

//input.className='input_date';
}
catch (e) {
if(input.value!="")
input.focus();
//input.className='input_date_error';
input.value=""
//var message = e.message;
// Fix for IE6 bug
//if (message.indexOf('is null or not an object') > -1) {
//message = 'Invalid date string';
//}
}
top.CustomEvent("blur",tab,input.id,input.value);
}