//
// Javascript function library for use in Adm/ CMS system
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2004-12-9 - Lars de Vries
// Modified: 2006-3-2 - Lars de Vries
//

/////////////////////////////////////////////////////////////////
//
// form field decoder
// Copyright (c) 2004 - Online-Art Corp.
// 09-12-2004 - Lars de Vries
//

function urlDecode(encoded)
{
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
      
   while (i < encoded.length)
   {
       var ch = encoded.charAt(i);
	   if (ch == "+")
	   {
	       plaintext += " ";
		   i++;

	   } else if (ch == "%")
	   {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 )
			{
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;

			} else
			{
				i++;

			}
		} else
		{
		   plaintext += ch;
		   i++;

		}
	}

   return plaintext;
};


function urlDecodeAll(formObj)
{

	var i,n;


	if(formObj!=null)
	{
		n = formObj.elements.length;

		for(i=0;i<n;i++)
		{
			formObj.elements[i].value = urlDecode(formObj.elements[i].value);
		}
	}

}

/////////////////////////////////////////////////////////////////
//
// FormNumber(id,obj,form,value,style,settings)
// Restricts Number input
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2005-8-13 - Lars de Vries
// Modified: 2006-2-20 - Lars de Vries
//

function formNumberDrawField()
{
	var html, root, width;

	root = getSetting("root",this.settings);
	
	html = '<input name="' + this.id + '" type="text" style="' + this.style + '" class="FormText" '+
			'onchange="' + this.obj + '.update();" '+
			'value="' + this.value +'" >';
	//onkeyup="' + this.obj + '.kDown();" 
	document.write(html);

}

function formNumberUpdate()
{
	var min,max;
	
	min = getSetting("min",this.settings);	
	max = getSetting("max",this.settings);
	fixed = getSetting("fixed",this.settings);
			
	min = parseFloat(min);
	max = parseFloat(max);
	
	obj = getElementByName(this.id);
	
	value = obj.value;
	
	value = value.replace(/[^0-9\.,-]+/,'');
	value = value.replace(/,/,'.');

	if(value == '') 
	{
		value = 0;
	}
		
	if(min != '' && value < min)
	{
		value = min;
	}

	if(max != '' && value > max)
	{
		value = max;		
	}
	
	value = parseFloat(value);
	
	if(fixed != '')
	{
		if(value.toFixed)
		{
			value = value.toFixed(fixed);
		}
	}
	
	obj.value = value;
}

function formNumberKDown()
{
	obj = getElementByName(this.id);
	
	value = obj.value;
	
		
	obj.value = value;		
}

function FormNumber(id,obj,form,value,style,settings)
{
	//
	// vars
	//

	this.id = id;
	this.obj = obj;
	this.form = form;
	this.value = value;
	this.settings = setDefaultSettings('skin=,root=,language=en,float=true,max=,min=,fixed=',settings);
	this.style = style;
	this.fieldObj;
	
	//
	// functions
	//

	this.update = formNumberUpdate;
	this.kDown = formNumberKDown;
	this.drawField = formNumberDrawField;

	this.drawField();
	this.update();
}

/////////////////////////////////////////////////////////////////
//
// FormImage(id,obj,form,value,style,settings)
// Image upload field
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2004-4-4 - Lars de Vries
// Modified: 2006-3-2 - Lars de Vries
//

function formImageShow()
{
	this.uploaderObj.style.visibility = 'visible';
}

function formImageHide()
{
	this.uploaderObj.style.visibility = 'hidden';
}

function formImageToggle()
{
	if(this.uploaderObj.style.visibility != 'visible')
		this.show();
	else
		this.hide();
}

function formImageDrawField()
{
	var html;
	var root;
	
	root = getSetting("root",this.settings);
	skin = getSetting("skin",this.settings);
	uploadscript = getSetting("uploadscript",this.settings);
	
	html  = '<table cellpadding="0" cellspacing="0"><tr><td>';
	html += '<input type="hidden" name="' + this.id + '">';
	html += '<div id="' + this.id + '__display__"></div></td><td valign="top">';
	//html += '<img alt="' + this.uploadIcon + '" class="Icon" src="' + root + skin + 'images/icons/medium/upload.jpg" onclick="' + this.obj + '.toggle();"><br>';
	html += '<img alt="' + this.uploadIcon + '" class="Icon" src="' + root + skin + 'images/icons/medium/upload.jpg" onclick="' + this.obj + '.toggle();"><br>';
	
	html += '<div id="' + this.id + '__image__" class="FormImage">' + 
		'<table class="FormImageDialog" cellpadding="0" cellspacing="0">' +
		'<tr><td valign="top" class="FormImageDialogHeader" unselectable="on">' + this.uploaderTitle + '</td></tr>' +
		'<tr><td>' +
		'<iframe class="FormImageDialogContent" frameborder="0" scrolling="no" src="' + root + uploadscript + '?parentObject=' + this.obj + '&root=' + root + '"></iframe>' +
		'</td></tr></table></div>';
	
	html += '<img alt="' + this.removeIcon + '" class="Icon" src="' + root + skin + 'images/icons/medium/remove.jpg" onclick="' + this.obj + '.setCurrentImage();"><br>';
	html += '</td></tr></table>';

	document.write(html);
	
	this.uploaderObj = document.getElementById(this.id + '__image__');
	this.fieldObj = document.getElementById(this.id + '__display__');

}

function formImageSetCurrentImage(currentImage,originalName)
{
	root = getSetting("root",this.settings);
	
	if(currentImage == null)
		currentImage = "";
	else
		currentImage = root + currentImage;

	if(originalName == null)
		originalName = "";

	this.originalName = originalName;
	this.currentImage = currentImage;
	
	this.update();
}

function formImageUpdate()
{
	if(!empty(this.currentImage))
		this.fieldObj.innerHTML = '<img class="FormText" style="' + this.style + '" src="' + this.currentImage + '">';
	else
		this.fieldObj.innerHTML = '';

	originalName = this.originalName;
	if(!empty(originalName))
		originalName = "," + originalName;
		
	obj = getElementByName(this.id);
	obj.value = this.currentImage + originalName;
}

function FormImage(id,obj,form,value,style,settings)
{

	//
	// vars
	//
	
	this.id 		= id;
	this.obj 		= obj;
	this.form 		= form;
	this.settings 		= setDefaultSettings('root=,skin=,uploadscript=libraries/oa.form.image.php,language=en',settings);
	
	////
	// set language
	
	var lang = getSetting("language",this.settings);
	
	if(lang=="nl")
	{	
		this.uploaderTitle = 'Nieuwe afbeelding uploaden';
		this.uploadIcon = 'uploaden';
		this.removeIcon = 'verwijderen';
	} else
	{
		this.uploaderTitle = 'Upload new image';
		this.uploadIcon = 'upload';
		this.removeIcon = 'remove';
	}

	this.currentImage = value;
	this.originalName = "";
	

	if(value.indexOf(',')>-1)
	{
		value = explode(",",value);
		this.currentImage = value[0];
		this.originalName = value[1];
	}

	this.style 		= style;

	this.fieldObj;
	this.uploaderObj;

	//
	// functions
	//

	this.show 		= formImageShow;
	this.hide 		= formImageHide;
	this.toggle 		= formImageToggle;
	this.update		= formImageUpdate;
	this.setCurrentImage	= formImageSetCurrentImage;
	this.drawField		= formImageDrawField;

	this.drawField();
	this.update();
}

/////////////////////////////////////////////////////////////////
//
// FormImageBrowser(id,obj,form,value,style,settings)
// Form image browse field
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2004-8-31 - Lars de Vries
// Modified: 2006-1-8 - Lars de Vries
//

function formImageBrowserShow()
{
	var folderId;
	var root;
	
	folderId = getSetting("folderId",this.settings);
	root = getSetting("root",this.settings);
	
	this.browserObj = window.open(root + this.browserscript + '&folderId=' + folderId, 'OABackOfficeDialog', 'left=50,top=50,width=' + screen.width * 0.7 + ', height=' + screen.height * 0.7 + ', menubar=no,resizable=yes,location=no,status=yes,scrollbars=yes');
	
}

function formImageBrowserDrawField()
{
	var html;
	var root;
	
	root = getSetting("root",this.settings);
	skin = getSetting("skin",this.settings);
	
	html  = '<table cellpadding="0" cellspacing="0"><tr><td>';
	html += '<input type="hidden" name="' + this.id + '">';
	html += '<div id="' + this.id + '__display__"></div></td><td valign="top">';
	html += '<img alt="' + this.browseIcon + '" class="Icon" src="' + root + skin + 'images/icons/medium/image.jpg" onclick="' + this.obj + '.show();"><br>';
	html += '<img alt="' + this.removeIcon + '" class="Icon" src="' + root + skin + 'images/icons/medium/remove.jpg" onclick="' + this.obj + '.setCurrentImage();"><br>';
	html += '</td></tr></table>';

	document.write(html);
	
	eval('this.fieldObj = ' + this.id + '__display__');
}

function formImageBrowserSetCurrentImage(currentImage)
{
	if(currentImage == null)
		currentImage = "";

	this.currentImage = currentImage;

	this.update();
}

function formImageBrowserUpdate()
{
	if(!empty(this.currentImage))
		this.fieldObj.innerHTML = '<img class="FormText" style="' + this.style + '" src="libraries/oa.image.php?defaultTable=true&thumbImage=false&id=' + this.currentImage + '">';
	else
		this.fieldObj.innerHTML = '';

	eval(this.form + '.' + this.id + '.value = "' + this.currentImage + '"');

}


function FormImageBrowser(id,obj,form,value,style,settings)
{

	//
	// vars
	//

	this.id 		= id;
	this.obj 		= obj;
	this.form 		= form;
	//this.settings 		= setDefaultSettings('root=,skin=,folderId=,browserscript=libraries/oa.form.imagebrowser.php,language=en',settings);
	this.settings 		= setDefaultSettings('root=,skin=,folderId=,language=en',settings);
	this.browserscript = 'parse.php?action=actions.browse&type=images&typeNames=images&fck=false&method=' + this.obj + '.setCurrentImage({PARAMS})';
	
	
	////
	// set language
	
	var lang = getSetting("language",this.settings);
	
	if(lang=="nl")
	{	
		this.browseIcon = 'bladeren';
		this.removeIcon = 'verwijderen';
	} else
	{
		this.browseIcon = 'browse';
		this.removeIcon = 'remove';
	}
	
	this.currentImage 	= value;
	this.style 		= style;

	this.fieldObj;
	this.browserObj;

	//
	// functions
	//

	this.show = formImageBrowserShow;
	this.update = formImageBrowserUpdate;
	this.setCurrentImage = formImageBrowserSetCurrentImage;
	this.drawField = formImageBrowserDrawField;

	this.drawField();
	this.update();
}


/////////////////////////////////////////////////////////////////
//
// FormCombo(id,obj,form,value,settings,script,style)
// Form combo box field
// Copyright (c) 2005 - Online-Art Corp.
// 14-01-2005 - Lars de Vries
//

function formComboShow()
{
	obj = document.getElementById(this.obj + '__combo__');
	obj.style.visibility = 'visible';
}

function formComboHide()
{
	obj = document.getElementById(this.obj + '__combo__');
	obj.style.visibility = 'hidden';
}

function formComboToggle()
{
	obj = document.getElementById(this.obj + '__combo__');
	if(obj.style.visibility == 'hidden' || empty(obj.style.visibility))
		this.show();
	else
		this.hide();
}

function formComboKDown(event)
{
	event = (event) ? event : (window.event);
	
	switch(event.keyCode)
	{
	case 40:
		this.show();
		
		break
	
	default:
		this.search();
		
		break	
	}
	
}

function formComboDrawField()
{
	var html, root, width;

	root = getSetting("root",this.settings);
	skin = getSetting("skin",this.settings);
	
	html  = '<table cellpadding="0" cellspacing="0"><tr><td><input type="hidden" name="' + this.id + '">'+
				'<input style="' + this.style + '" type="text" '+
				'onchange="' + this.obj + '.setCustomValue();" onkeyup="' + this.obj + '.kDown(event);"'+
				'class="FormText" style="' + this.style + '" name="' + this.id + '__display__"></td>'+
				'<td><img class="Icon" src="' + root + skin + 'images/icons/medium/arrowdown.jpg" onclick="' + this.obj + '.toggle();"></td></table>';

	document.write(html);
	
}

function formComboDrawCombo()
{
	var html,root,obj;
	
	root = getSetting("root",this.settings);
	
	obj = getElementByName(this.id + '__display__');
	
	html  = '<div class="FormCombo" style="width:' + obj.style.width + '" id="' + this.obj + '__combo__"></div>';
	
	document.write(html);

}


function formComboDrawComboContent(limited)
{
	var html,i,j,n,height,obj,stripstring;
	
	obj = getElementByName(this.id + '__display__');			
	height = getSetting("height",this.settings);
	stripstring = getSetting("stripstring",this.settings);

	n = this.options.length;
	j = 0;

	html  = '<table class="FormComboContent">';
	
	for(i=0; i<n; i++)
	{
		option = this.options[i][1];
		if(stripstring!=-1 && !empty(stripstring))
		{
			option = option.replace(eval('/' + stripstring + '/g'),'');	
		}	
		
		if((option.substring(0,obj.value.length).toLowerCase() == obj.value.toLowerCase() && limited != null && limited == true) || limited == null)
		{		
			if(this.options[i][0] == this.value)
				styleClass='FormComboContentItemSelected';
			else
				styleClass='FormComboContentItem';
				
			if(this.options[i][0] == -1)
			{
				html += '<tr><td class="' + styleClass + '">' + this.options[i][1] + '</td></tr>';
			} else
			{
				html += '<tr><td class="' + styleClass + '" onclick="' + this.obj + '.setValueIndex(' + i + ');">' + this.options[i][1] + '</td></tr>';
			}
			j++;
		}
	}
	
	html += '</table>';
	
	obj = document.getElementById(this.obj + '__combo__');
	obj.innerHTML = html;
	
	if(j>10)
	{
		obj.style.height = height;
		
	} else if(!j)
	{
		this.hide();
	} else
	{
		obj.style.height = '';
	}
	
}

function formComboSetOptions(options,aliases)
{

	if(empty(aliases))
		aliases = options;

	options = explode("¦",options);

	aliases = explode("¦",aliases);

	var n = options.length;

	this.options = new Array(n);

	for(var i=0; i<n; i++)
	{
		this.options[i] = new Array(options[i],aliases[i]);
	}

	this.update();
}

function formComboSetValue(value)
{
	this.value = value;
	this.update();
}

function formComboSetValueIndex(index)
{
	var obj;
	
	obj = getElementByName(this.id + '__display__');
	
	this.value = this.options[index][0];
	obj.value = this.options[index][1];

	this.hide();
	this.update();
}

function formComboSetCustomValue()
{
	var obj;
	
	obj = getElementByName(this.id + '__display__');
	
	this.value = this.getOption(obj.value);
	
	this.hide();
	this.update();

}

function formComboSearch()
{
	var obj;
	
	obj = getElementByName(this.id + '__display__');
	
	if(obj.value.length)
	{
		this.show();	
	} else
	{
		this.hide();
	}
	
	this.drawComboContent(true);
}

function formComboUpdate()
{
	var obj;
	this.drawComboContent();

	obj = getElementByName(this.id + '__display__');
	obj.value = this.getAlias(this.value);
	
	obj = getElementByName(this.id);
	if(obj.value != this.value && !empty(this.script))
	{
		obj.value = this.value;
		eval(this.script);

	} else
	{
		obj.value = this.value;
	}
}

function formComboGetAlias(option)
{
	var i,n;

	if(this.options!=undefined)
	{
		n = this.options.length;
		for(i=0; i<n; i++)
		{
			if(option == this.options[i][0])
			{
				option = this.options[i][1];
				i=n;
			}
		}
	}
	
	stripstring = getSetting("stripstring",this.settings);	
	if(stripstring!=-1 && !empty(stripstring))
	{
		option = option.replace(eval('/' + stripstring + '/g'),'');	
	}
		
	return option;
}

function formComboGetOption(alias,notstrict)
{
	var i,n,option,found;

	found = false;	
	n = this.options.length;	
	stripstring = getSetting("stripstring",this.settings);
	custom = getSetting("custom",this.settings);

	option = alias;
	for(i=0; i<n && !found; i++)
	{
		_option = this.options[i][1];

		if(stripstring!=-1 && !empty(stripstring))
		{
			_option = _option.replace(eval('/' + stripstring + '/g'),'');	
		}
		
		if(option == _option)
		{
			option = this.options[i][0];
			found = true;
		}

		if(notstrict != null && notstrict && _option.substring(0,option.length).toLowerCase() == option.toLowerCase())
		{			
			option = this.options[i][0];
			found = true;
		}
	}
	
	if(!found && !notstrict && !custom)
	{
		option = this.getOption(alias,true);
	
	} else if(!found && notstrict && !custom)
	{
		option = '';	
	}
	
	this.hide();

	return option;
}

function FormCombo(id,obj,form,value,settings,script,style)
{
	//
	// vars
	//

	this.id = id;
	this.obj = obj;
	this.form = form;
	this.value = value;
	this.style = style;
	this.script	= script;
	this.settings = settings;
	
	this.options;

	this.fieldObj;
	this.comboObj;
	this.valueObj;
	
	this.settings = setDefaultSettings('root=,skin=,height=180px,custom=no,stripstring=&nbsp;',settings);
	
	//
	// functions
	//

	this.show = formComboShow;
	this.hide = formComboHide;
	this.toggle	= formComboToggle;
	this.kDown = formComboKDown;
	this.search = formComboSearch;
	this.update	= formComboUpdate;
	this.drawField = formComboDrawField;
	this.drawCombo = formComboDrawCombo;
	this.drawComboContent = formComboDrawComboContent;
	this.setOptions = formComboSetOptions;
	this.setValueIndex = formComboSetValueIndex;
	this.setCustomValue	= formComboSetCustomValue;
	this.getAlias = formComboGetAlias;
	this.setValue = formComboSetValue;
	this.getOption = formComboGetOption;

	this.drawField();

	obj = getElementByName(this.id);
	obj.value = this.value;

	this.drawCombo();
	
}

/////////////////////////////////////////////////////////////////
//
// function FormList(id,obj,form,value,script,style)// form list box field
// Copyright (c) 2005 - Online-Art Corp.
// 27-01-2005 - Lars de Vries
// last modified 07-11-2005 - Lars de Vries
//

function formListDrawField()
{

	var html;

	html  = '<input type="hidden" name="' + this.id + '">';
	html += '<div id="' + this.id + '__display__" class="FormList" style="' + this.style + '"></div>';

	document.write(html);
	
	this.fieldObj = document.getElementById(this.id + '__display__');

}

function formListDrawFieldContent()
{
	var html,i,n,value;

	n = this.options.length;
	value = explode("¦",this.value);

	html  = '<table width="100%">';

	for(i=0; i<n; i++)
	{
		if(array_search(this.options[i][0],value)!=-1)
			styleClass='FormListItemSelected';
		else
			styleClass='FormListItem';
			
		if(this.options[i][0] != -1)
			html += '<tr><td class="' + styleClass + '" unselectable="on" onclick="' + this.obj + '.setOptionIndex(' + i + ');">' + this.options[i][1] + '</td></tr>';
		else
			html += '<tr><td class="' + styleClass + '" unselectable="on">' + this.options[i][1] + '</td></tr>';
	}

	html += '</table>';

	this.fieldObj.innerHTML = html;

}

function formListSetOptions(options,aliases)
{
	if(empty(aliases))
		aliases = options;

	options = explode("¦",options);

	aliases = explode("¦",aliases);

	var n = options.length;

	this.options = new Array(n);

	for(var i=0; i<n; i++)
	{
		this.options[i] = new Array(options[i],aliases[i]);
	}

	this.update();
}

function formListSetOptionIndex(index)
{
	var i,n,value;

	value = this.options[index][0];

	this.value = explode("¦",this.value);

	n = this.value.length;
	i = array_search(value,this.value);
	if(i!=-1)
		this.value = array_remove(i,this.value);
	else
		this.value[n] = value;

	this.value = implode("¦",this.value);

	this.update();
}

function formListSetCustomValue()
{
	var i,n,value;

	this.value = this.getOption(this.fieldObj.value);
	this.update();

}

function formListUpdate()
{
	this.drawFieldContent();

	this.fieldObj.value = this.getAlias(this.value);
	
	obj = getElementByName(this.id);
	obj.value = this.value;

}

function formListGetAlias(option)
{
	var i,n,option;
	n = this.options.length;
	for(i=0; i<n; i++)
	{
		if(option == this.options[i][0])
		{
			option = this.options[i][1];
			i=n;
		}
	}
	return option;
}

function formListGetOption(alias)
{
	var i,n,option;
	n = this.option.length;
	option = alias;
	for(i=0; i<n; i++)
	{
		if(option == this.options[i][1])
		{
			option = this.options[i][0];
			i=n;
		}
	}

	return option;
}


function FormList(id,obj,form,value,script,style)
{
	//
	// vars
	//

	this.id 		= id;
	this.obj 		= obj;
	this.form 		= form;
	this.value 		= value;
	this.style 		= style;

	this.options;

	this.fieldObj;
	this.valueObj;

	//
	// functions
	//

	this.update		= formListUpdate;
	this.drawField 		= formListDrawField;
	this.drawFieldContent 	= formListDrawFieldContent;
	this.setOptions		= formListSetOptions;
	this.setOptionIndex	= formListSetOptionIndex;
	this.setCustomValue	= formListSetCustomValue;
	this.getAlias		= formListGetAlias;

	this.getOption		= formListGetOption;

	this.drawField();
}

/////////////////////////////////////////////////////////////////
//
// FormDate(id,obj,form,value,settings,style)
// form date picker field
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2004-9-11 - Lars de Vries
// Modified: 2006-2-21 - Lars de Vries
//

function formDateShowDatePicker(evt)
{
	obj = document.getElementById(this.obj + '__datePicker__');
	obj.style.visibility = 'visible';
}

function formDateHideDatePicker()
{
	obj = document.getElementById(this.obj + '__datePicker__');
	obj.style.visibility = 'hidden';
}

function formDateToggleDatePicker()
{
	obj = document.getElementById(this.obj + '__datePicker__');

	if(obj.style.visibility != 'visible')
		this.showDatePicker();
	else
		this.hideDatePicker();

}

function formDateShowTimePicker()
{
	obj = document.getElementById(this.obj + '__timePicker__');
	obj.style.visibility = 'visible';
}

function formDateHideTimePicker()
{
	obj = document.getElementById(this.obj + '__timePicker__');
	obj.style.visibility = 'hidden';
}

function formDateToggleTimePicker()
{
	obj = document.getElementById(this.obj + '__timePicker__');

	if(obj.style.visibility != 'visible')
		this.showTimePicker();
	else
		this.hideTimePicker();

}


function formDateDrawField()
{
	var html,root,date,time,width,timeWidth,readOnly;

	root 		= getSetting("root",this.settings);
	skin		= getSetting("skin",this.settings);
	readOnly 	= (getSetting("custom",this.settings)?'':'readonly');	
	date 		= getSetting("date",this.settings)
	time 		= getSetting("time",this.settings)
	width 		= getSetting("width",this.settings)
	timeWidth 	= getSetting("timeWidth",this.settings)
		
	html  = '<input type="hidden" name="' + this.id + '"><table cellpadding="0" cellspacing="0"><tr>';
	
	if(date)
	{
		html += '<td><input class="FormDate" style="width: ' + width + 'px;" type="text" name="' + this.id + '__date__" onchange="' + this.obj + '.setCustomDate();" ' + readOnly + '>' +
			'<br/><div id="' + this.obj + '__datePicker__" class="FormDatePicker"></div></td>' +
			'<td><img class="Icon" onclick="' + this.obj + '.toggleDatePicker(event);" src="' + root + skin + 'images/icons/medium/arrowdown.jpg">' +
			'</td>';
	}
	
	if(time)
	{
		html += '<td><input class="FormDateTime" style="width: ' + timeWidth + 'px;" type="text" name="' + this.id + '__time__" onchange="' + this.obj + '.setCustomTime();" ' + readOnly + '>' +
			'<br/><div id="' + this.obj + '__timePicker__" class="FormDateTimePicker"></div></td>' +
			'<td><img class="Icon" onclick="' + this.obj + '.toggleTimePicker(event);" src="' + root + skin + 'images/icons/medium/arrowdown.jpg">' + 
			'</td>';
	}	
		
	html += '</tr></table>';

	document.write(html);
}

function formDateDrawDatePicker(viewDate)
{
	var picker,root,html,date,todayDate,tmpDate;
	
	root = getSetting("root",this.settings);
	html = "";

	// get picker div
	picker = document.getElementById(this.obj + '__datePicker__');
	
	// get date
	todayDate = new Date();
	if(viewDate == null)
	{
		if(this.date == null)
			viewDate = new Date(todayDate);
		else
			viewDate = new Date(this.date);
	}
	
	// previous month			
	pViewDate = new Date(viewDate);
	pViewDate.setMonth(pViewDate.getMonth()-1);

	// next month	
	nViewDate = new Date(viewDate);
	nViewDate.setMonth(nViewDate.getMonth()+1);
	
	////	
	// draw month and navigation buttons
	
	html += '<table class="FormDatePickerMonth" style="width: 100%;"><tr>' +
		'<td style="width: 10px;" onclick="' + this.obj + '.drawDatePicker(pViewDate);"><img class="Icon" src="' + root + skin + 'images/icons/small/arrowleft.gif"></td>' + 
		'<td style="text-align: center;">' + this.months[viewDate.getMonth()] + ' ' + (viewDate.getFullYear()) + '</td>' + 
		'<td style="width: 10px;" onclick="' + this.obj + '.drawDatePicker(nViewDate);"><img class="Icon" src="' + root + skin + 'images/icons/small/arrowright.gif"></td>' + 
		'</tr></table>';
	
	////
	// draw weeks
	
	html += '<table class="FormDatePickerDays" cellpadding="0" cellspacing="0"><tr>' +
		'<td class="">&nbsp;</td>';
	for(var i=0; i<this.weekDays.length; i++)
	{
		html += '<td class="FormDatePickerWeekDay">' + this.weekDays[i].substr(0,1) + '</td>';
	}	
	html += '<td></td></tr>';
	
	////
	// draw days
	
	// get first day
	tmpDate = new Date(viewDate);
	tmpDate.setDate(1);
	
	day = tmpDate.getDay()-1; // shift days (monday 1st day of week)
	day = (day==-1?6:day);
	
	if(day > 0)
	{
		tmpDate.setMonth(tmpDate.getMonth()-1);	
		tmpDate.setDate(this.getDays(tmpDate)-(day-1));
	}
	
	day = tmpDate.getDate();
	
	for(j=0; j<6; j++)
	{
		week = this.getWeek(tmpDate);
		
		html += '<tr><td class="FormDatePickerWeek">' + week + '</td>';
		
		for(i=0; i<7; i++)
		{
			if(tmpDate.getMonth() == viewDate.getMonth())
			{
				if(this.date != null && tmpDate.getDate() == this.date.getDate() && tmpDate.getMonth() == this.date.getMonth() && tmpDate.getFullYear() == this.date.getFullYear())
				{
					html += '<td class="FormDatePickerDaySelected" ';
					
				} else if(tmpDate.getDate() == todayDate.getDate() && tmpDate.getMonth() == todayDate.getMonth() && tmpDate.getFullYear() == todayDate.getFullYear())
				{
					html += '<td class="FormDatePickerDayToday" ';
				} else
				{
					html += '<td class="FormDatePickerDay" ';
				}
				
			} else
			{
				html += '<td class="FormDatePickerDayGray" ';
			}
					
			html += 'onclick="' + this.obj + '.setPickerDate(' + tmpDate.getFullYear() + ',' + tmpDate.getMonth() + ',' + tmpDate.getDate() + ');">' + 
				day + 
				'</td>';
			
			day++;	
			tmpDate.setDate(day);
			day = tmpDate.getDate();
		}
	
		html += '</tr>';	
	}
	
	html += '</table>';
	
	////
	// draw today button
	
	html += '<div style="text-align: center;"><input class="FormButton" type="button" ' + 
		'onclick="' + this.obj + '.setPickerDate(' + todayDate.getFullYear() + ',' + todayDate.getMonth() + ',' + todayDate.getDate() + ');" ' +  
		'value="' + this.todayButton + '"/></div>';
		
	////
	// output html
			
	picker.innerHTML = html;
	
}

function formDateDrawTimePicker()
{
	// get picker div
	picker = document.getElementById(this.obj + '__timePicker__');
	
	viewDate = new Date(0,0)
	
	html = '<table>';
	
	while(viewDate.getDate()==1)
	{
		hours = viewDate.getHours();
		minutes = viewDate.getMinutes();
		
		hours = (hours.toString().length==1?'0' + hours:hours);
		minutes = (minutes.toString().length==1?'0' + minutes:minutes);
				
		html += '<tr><td class="Icon" onclick="' + this.obj + '.setPickerTime(' + viewDate.getHours() + ',' + viewDate.getMinutes() + ');">' + hours + ':' + minutes + '</td></tr>';
		viewDate.setMinutes(viewDate.getMinutes()+30);
	}	
	
	html += '</table>';
	
	picker.innerHTML = html;

}

function formDateGetDays(date)
{
	var tmpDate,month,day;
	
	tmpDate = new Date(date);
	month = tmpDate.getMonth();
	day = 28;
	
	while(month == tmpDate.getMonth())
	{
		day++;
		tmpDate.setDate(day);
	}
	
	return day-1;
}

function formDateGetWeek(date)
{
	var year,month,week,day;
	year = date.getFullYear();
	month = date.getMonth() + 1;
	day = date.getDate();
	
	var a = Math.floor((14-(month))/12);
	var y = year+4800-a;
	var m = (month)+(12*a)-3;
	var jd = day + Math.floor(((153*m)+2)/5) + 
                 (365*y) + Math.floor(y/4) - Math.floor(y/100) + 
                 Math.floor(y/400) - 32045;      // (gregorian calendar)
	
	//var jd = (day+1)+Math.Round(((153*m)+2)/5)+(365+y) + 
	//                 Math.round(y/4)-32083;    // (julian calendar)

	var d4 = (jd+31741-(jd%7))%146097%36524%1461;
	var L = Math.floor(d4/1460);
	var d1 = ((d4-L)%365)+L;
	week = Math.floor(d1/7) + 1;
	return week;
    
}

function formDateSetPickerDate(year,month,day)
{
	var date = new Date(year,month,day);
	this.date = date;
	
	this.hideDatePicker();
	this.update();
}

function formDateSetPickerTime(hours,minutes)
{
	
	var time = new Date(0,0,0,hours,minutes);
	this.time = time;

	this.hideTimePicker();
	this.update();
}

function formDateGetCustomDate(year,month,day)
{
	fieldDate = new Date(year,month,day);
	fieldDay = fieldDate.getDay()-1;
	fieldDay = (fieldDay==-1?6:fieldDay);
	fieldDay = this.weekDays[fieldDay];
	
	obj = getElementByName(this.id + '__date__');
	obj.value = fieldDay + ' ' + day + '-' + (month + 1) + '-' + year;
}

function formDateGetCustomTime(hours,minutes)
{
	hours = (hours.toString().length==1?'0' + hours:hours);
	minutes = (minutes.toString().length==1?'0' + minutes:minutes);
	
	obj = getElementByName(this.id + '__time__');
	obj.value = hours + ':' + minutes;
}

function formDateSetCustomDate()
{
	obj = getElementByName(this.id + '__date__');
	fieldDate =obj.value;

	var i = fieldDate.indexOf(' ');
	if(i>0)
		fieldDate = fieldDate.substr(i);
	fieldDate = fieldDate.replace(' ','');
	
	if(!empty(fieldDate))
	{
		fieldDate = explode("-",fieldDate);	
		this.date = new Date(fieldDate[2],fieldDate[1]-1,fieldDate[0]);
		if(this.date=='NaN')
			this.date = null;			
		
	} else
	{
		this.date = null;
	}
	
	this.update();
		
}

function formDateSetCustomTime()
{
	fieldTime = document.all.item(this.id + '__time__').value;
	fieldTime = fieldTime.replace(' ','');
	if(!empty(fieldTime))
	{
		fieldTime = explode(":",fieldTime);	
		this.time = new Date(0,0,0,fieldTime[0],fieldTime[1]);
	} else
	{
		this.time = null;
	}
	
	this.update();
}

function formDateGetValue(value)
{
	this.value = value;
	obj = getElementByName(this.id);
	obj.value = value;
}

function formDateSetValue()
{
	if(!empty(this.value) && this.value != "0000-00-00" && this.value != "0000-00-00 00:00")
	{
		if(getSetting("time",this.settings))
		{	
			value = explode(" ",this.value);
			time = explode(":",value[1]);
			date = explode("-",value[0]);
			this.time = new Date(0,0,0,time[0],time[1],time[2]);

		} else
		{
			date = explode("-",this.value);
		}

		this.date = new Date(date[0],date[1]-1,date[2]);
	}
		
}

function formDateUpdate()
{
	var value = '';
	
	// fill date field
	if(this.date!=null)
	{
		this.getCustomDate(this.date.getFullYear(),this.date.getMonth(),this.date.getDate());		
		this.drawDatePicker();
		value = this.date.getFullYear() + '-' + (this.date.getMonth()+1) + '-' + this.date.getDate();
	}
	
	// fill time field
	if(this.time!=null)
	{
		this.getCustomTime(this.time.getHours(),this.time.getMinutes());
		value += ' ' + this.time.getHours() + ':' + this.time.getMinutes() + ':00';
	}
	
	// update form value
	if(!empty(value))
	{
		this.getValue(value);			
	}
	
	if(this.finalized && this.script != '')
	{
		//alert(this.value);
		eval(this.script);		
	}

}

function FormDate(id,obj,form,value,settings,script,style)
{

	//
	// vars
	//

	value = urlDecode(value);
	
	this.id = id;
	this.obj = obj;
	this.form = form;
	this.date;
	this.time;
	this.value = value;
	this.script = script;
	this.style = style;
	this.finalized = false;

	this.settings = setDefaultSettings('root=,skin=,date=yes,time=no,width=96,timeWidth=58,language=nl,custom=yes',settings);
	
	////
	// set language
	
	var lang = getSetting("language",this.settings);
	
	if(lang=="nl")
	{
		this.weekDays	= ['ma','di','wo','do','vr','za','zo'];
		this.months	= ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'];
		this.todayButton= 'vandaag';
		
		
	} else
	{
		
		this.weekDays	= ['mo','th','we','tu','fr','sa','su'];
		this.months	= ['January','February','March','April','May','June','July','August','September','October','November','December'];
		this.todayButton= 'today';
	}

	//
	// functions
	//

	this.showDatePicker	= formDateShowDatePicker;
	this.hideDatePicker	= formDateHideDatePicker;
	this.toggleDatePicker	= formDateToggleDatePicker;
	this.showTimePicker	= formDateShowTimePicker;
	this.hideTimePicker	= formDateHideTimePicker;
	this.toggleTimePicker	= formDateToggleTimePicker;
	this.update		= formDateUpdate;
	this.drawField		= formDateDrawField;
	this.drawDatePicker	= formDateDrawDatePicker;
	this.drawTimePicker	= formDateDrawTimePicker;
	this.getDays		= formDateGetDays;
	this.getWeek		= formDateGetWeek;
	this.setPickerDate	= formDateSetPickerDate;
	this.setPickerTime	= formDateSetPickerTime;
	this.setCustomDate 	= formDateSetCustomDate;
	this.setCustomTime 	= formDateSetCustomTime;
	this.getCustomDate 	= formDateGetCustomDate;
	this.getCustomTime 	= formDateGetCustomTime;
	this.setValue		= formDateSetValue;
	this.getValue		= formDateGetValue;

	//
	// init
	//

	this.setValue();
	this.drawField();
	
	this.drawDatePicker();
	if(getSetting("time",this.settings))
		this.drawTimePicker();
	this.hideDatePicker();
	
	this.update();
	
	this.finalized = true;
}
