﻿// Common Functions Library ----------------------------------------------------


/*--------------------------------------// Functions headers //----------------------------------------*\

<function-headers>

// Data validation ///////////////////////////////

function checkAlphabetic()

function checkNumeric()

function hasChars(str, charStr)

function isEmail(str)

function isUsername(str)

function isPassword(str)

// String manipulation and convertion ///////////////////////////////

function leadZero(n, units)
	.Description
		Add "zero" prefix to a number
	.Parameters
		n:			The number to be prefixed.
		units:			Fixed length of the returning string.
	.Returns
		A string comprised of value of "n", prefixed by a several number of "zero"s.

function UCC(s)
	.Description
		Converts unicode string into &#CODE; format string.
	.Parameters
		s:			The string to be converted.
	.Returns
		Converted string.

function csv2Array(str, separator)
	.Description
		Constructs an array from a comma-separated string.
	.Parameters
		str:			The "Value1,Value2,ValueN" string.
		separator:		The separator. Default is ",".
	.Returns
		An Array object.

function items2Array(sItems)
	.Description
		Constructs an array from a "Name1=Value1;Name2=Value2;" string.
	.Parameters
		sItems:			The "Name1=Value1;Name2=Value2;" string.
	.Returns
		A 1-dimension array object.

function searchArray(arr, val)

function getName(aArgs, sValue)

// UI rendering functions ///////////////////////////////

function selectItem(oSelect, itemValue)
	.Description
		Programmatically select an item that matches a provided value.
	.Parameters
		oSelect:		SELECT object to be selected
	.Returns
		Return true if item found, otherwise, return false.

function displayDate(dDate)
	.Description
		Display .
	.Parameters
		dDate:			Default date value (a valid date string or an instance of Date object).
					If omitted, client's computer time is used.
	.Returns
		Nothing

function loadSelectData(oSelect, DataItems, sDefault)
	.Description
		Loads data into a SELECT element with pre-selected item specified in sDefault variable.
	.Parameters
		oSelect:		SELECT element
		DataItems:		Data source, can be Name=Value; pairs string or an Array
		sDefault:		Default value
	.Returns
		Nothing

function displayPageNavigator(pageCount, offsetName, link, iconString)
	.Description
		Display a client-side page navigator.
	.Parameters
		pageCount:		Number of pages
		offsetName:		QueryString parameter name that is sent to the server-side script
		link:			By default, this function use the "current URL" (that you see on the address bar).
					Alternatively, you can use this parameter to specify another URL, other than the current.
		iconString:		The string contains "icon"s for the Go-First, Go-Back, Go-Next, Go-Last buttons and the Separator, separated by commas(',').
					By default, this string is "|&lt;&lt;,&lt;&lt;,&gt;&gt;,&gt;&gt;|,&nbsp;"
	.Returns
		Nothing

function getSelect(sName, args, sSelect, sAttr)
	.Description
		Renders HTML code of a SELECT element.
	.Parameters
		sName:			Name of the SELECT element.
		args:			Either a "Name1=Value1;Name2=Value2;" string or a 1-demension array.
		sSelect:		Value of the pre-selected item.
		sAttr:			Additional attributes of the SELECT element.
	.Returns
		Rendered HTML code.

function writeSelect(sName, aArgs, sSelect, sAttr)
	.Description
		Same as getSelect function, additionally, writes the rendered code directly to document.
	.Parameters
		Same as of getSelect function
	.Returns
		Nothing

function getSingleChoices(sName, args, selectedValue, sType, sAttr)
	.Description
		Renders HTML code of a single choice control.
	.Parameters
		sName:			Name of the data field.
		args:			Either a "Name1=Value1;Name2=Value2;" string or a 1-demension array.
		selectedValue:		Value of the pre-selected item.
		sType:			"select" | "radio".
		sAttr:			Additional attributes of the SELECT element.
	.Returns
		Rendered HTML code.

function getMultiChoices(sName, args, selectedValues, sType, sAttr)
	.Description
		
	.Parameters
		sType:			= {"select" | "checkbox" | "checkbox-array"} . if sTpye = "checkbox-array", return value will be an array
	.Returns
		Nothing

function writeSingleChoices(sName, args, selectedValue, sType, sAttr)
function writeMultiChoices(sName, args, selectedValues, sType, sAttr)
function writeDate(name, dateMDY, specs)
	.Description
		Display a combination of 3 SELECT elements, presenting Day, Month, Year selector.
	.Parameters
		name:			Name of the form data field. This is used to send date value to the server-side script as a date string(mm/dd/yyyy hh:nn).
		dateMDY:		Default date value (a valid date string or an instance of Date object).
		specs:			A string that tells how to display the date selector.
					Syntax: "EmptyItem=<text>:<value>,Month={vn|sort-vn|en|sort-en|number},Year={from-to},Order={dmy|mdy|ymd}"
	.Returns
		Nothing

function writeDayMonthYear(name, d, m, y)

// Utility functions ///////////////////////////////

function toQueryString(fm)
	.Description
		Constructs a QueryString string from a specified form.
	.Parameters
		fm:			The form from which input fields are gathered.
	.Returns
		A QueryString comprised of data fields treated as QueryString parameters.

function toURL(fm)
	.Description
		Constructs a URL from a specified form.
	.Parameters
		fm:			The form from which input fields are gathered.
	.Returns
		A URL comprised of the Action attribute and data fields treated as QueryString parameters.

function popupImage(src, note, title, css, border)
	.Description
		Popups a new window displaying the image then automatically resize window to fit the image dimension.
	.Parameters
		src:			Image source
		note:			Image description
		title:			Image title(appears on the title bar of the popup window)
		css:			Custom CSS styles of the popup window's document.
		border:			Padding border between the image edge and window edge.
	.Returns
		Nothing

function getTop(o)

function getLeft(o)


</function-headers>


	All code written by VanHNN <vanhnn@hcm.fpt.vn>

\*------------------------------------------------------------------------------*/





// Functions implementation //---------------------------------------------------


function toQueryString(fm) {
	if (typeof(fm) != 'object')
		return (false);

	var form_length = fm.elements.length;
	var myform = fm;
	var mytype = '';
	var temp = '';

	for (var i = 0; i < form_length; i++) {
		mytype = myform.elements[i].type
		mytype = mytype.toLowerCase();
		if (mytype == 'text' || mytype == 'hidden' || mytype == 'select-one' ||
		    mytype == 'checkbox' || mytype == 'radio' || mytype == 'select-multiple') {
			var t = myform.elements[i].name;
			var q = '';
			if (t == null || t == '')
				t = myform.elements[i].id
			if (mytype == 'text' || mytype == 'hidden')
				q = t + "=" + escape(myform.elements[i].value);
			else if (mytype == 'checkbox' || mytype == 'radio') {
				if (myform.elements[i].checked)
					q = t + "=" + escape(myform.elements[i].value);
			} else if (mytype == 'select-one' || mytype == 'select-multiple')
				q = t + "=" + escape(myform.elements[i][myform.elements[i].selectedIndex].value);

			temp = temp + ((q!='') ? (q + ((i<form_length-1) ? '&' : '')) : '');
		}
	}
	temp = temp.substring(temp, temp.length - 1)

	return temp;
}

function toURL(fm) {
	var sAxn = fm.action;
	var spr = '', lastChar = sAxn.charAt(sAxn.length-1);
	if (sAxn.indexOf('?') >= 0) {
		if (lastChar == '?' || lastChar == '&')
			spr = '';
		else
			spr = '&';
	} else
		spr = '?';
	return sAxn + spr + toQueryString(fm);
}


function loadSelectData(oSelect, DataItems, sDefault) {
	var i, a = typeof(DataItems)=='string' ? items2Array(DataItems) : DataItems;
	var sOptions = '';
	while (oSelect.options.length>0) oSelect.options.remove(0);
	
	for (i=0; i<a.length; i+=2) {
		var oOption = document.createElement('OPTION');
		oOption.text = a[i];
		oOption.value = a[i+1];
		oSelect.options.add(oOption);
	}
	for (i=0; i<oSelect.options.length; i++) 
		if (oSelect.options[i].value == sDefault) {
			oSelect.options[i].selected = true;
		}
}

function leadZero(n, units) {
	var sN = new String(n);
	var i, sRet = sN;
	for (i=sN.length; i<units; i++) {
		sRet = '0' + sRet;
	}
	return sRet;
}

function displayDate(dDate) {
	var today;
	if (dDate)
		today = new Date(dDate);
	else
		today = new Date(); 
	var dow = new Array('Chủ Nhật','Thứ Hai','Thứ Ba','Thứ Tư','Thứ Năm','Thứ Sáu','Thứ Bảy');
	var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
	var year = today.getYear();
	year = (year <= 1888) ? (1900 + year) : year;
	document.write(dow[today.getDay()] + ' ngày ' + today.getDate() + '/' + months[today.getMonth()] + '/' + year + ', ' + leadZero(today.getHours(), 2) + 'h' + leadZero(today.getMinutes(), 2) + ' GMT+7');
}

function displayPageNavigator(pageCount, offsetName, link, iconString) {
	var previousOffset, nextOffset, offsetBegin, offsetEnd, nPage, currentOffset, currentLink;
	var htmlGoFirst = '|&lt;&lt;', htmlGoBack = '&lt;&lt;', htmlGoNext = '&gt;&gt;', htmlGoLast = '&gt;&gt;|', htmlSeparator = '&nbsp;';

	// extracts [current offset] from querystring
	var moffs = location.search.match(new RegExp(offsetName + '=[0-9]*','ig'));
	if (moffs != null)
		currentOffset = moffs[0].split('=')[1];
	else
		currentOffset = 1;
	currentOffset = currentOffset=='' ? 1 : parseInt(currentOffset);

	pageCount = pageCount == '' ? 1 : parseInt(pageCount);
	if (currentOffset - 5 > 1) {
		if (currentOffset + 5 <= pageCount)
			offsetBegin = currentOffset - 5;
		else {
			if (pageCount - 10 >= 1)
				offsetBegin = pageCount - 10;
			else
				offsetBegin = 1;
		}
	} else
		offsetBegin = 1;
	
	if (offsetBegin + 10 < pageCount)
		offsetEnd = offsetBegin + 10;
	else
		offsetEnd = pageCount;
	
	if (currentOffset - 1 > 1)
		previousOffset = currentOffset - 1;
	else
		previousOffset = 1;
	
	if (currentOffset + 1 < pageCount)
		nextOffset = currentOffset + 1;
	else
		nextOffset = pageCount;
	
	currentLink = link ? link : location.href;
	currentLink = currentLink.replace(new RegExp(offsetName + '=[0-9]*[\&]*','ig'), '');
	separatorMark = currentLink.match(/(\&|\?)/) == null ? '?' : (currentLink.match(/(\&|\?)$/) != null ? '' : '&') ;

	if (iconString) {
		var dcp = iconString.split(',');
		if (dcp.length == 5) {
			htmlGoFirst = dcp[0];
			htmlGoBack = dcp[1];
			htmlGoNext = dcp[2];
			htmlGoLast = dcp[3];
			htmlSeparator = dcp[4];
		}
	}

	var html = '';
	
	html += '<span class=SearchNavigation>';
	html += currentOffset > 1 ? '<a href="' + currentLink + separatorMark + offsetName +'=1">' + htmlGoFirst + '</a>' : htmlGoFirst;
	html += htmlSeparator;
	html += previousOffset < currentOffset ? '<a href="' + currentLink + separatorMark + offsetName +'=' + previousOffset + '">' + htmlGoBack + '</a>' : htmlGoBack;
	html += htmlSeparator;

	for (nPage=offsetBegin; nPage<=offsetEnd; nPage++) {
		if (nPage-currentOffset == 0)
			html += '<span class=SearchNavigationOffset>' + nPage + '</span>';
		else
			html += '<a href="' + currentLink + separatorMark + offsetName +'=' + nPage + '">' + nPage + '</a>';
		html += htmlSeparator;
	}
	html += currentOffset < nextOffset ? '<a href="' + currentLink + separatorMark + offsetName +'=' + nextOffset + '">' + htmlGoNext + '</a>' : htmlGoNext;
	html += htmlSeparator;
	html += currentOffset < pageCount ? '<a href="' + currentLink + separatorMark + offsetName +'=' + pageCount + '">' + htmlGoLast + '</a>' : htmlGoLast;
	html += '</span>';
	document.write(html);
}

var ppimgNW;
function popupImage(src, note, title, css, border) {
	if (border==null) border = 0;
	if (note==null) note = '';
	if (ppimgNW != null) ppimgNW.close();

	var baseLoc = getBaseLocation();

	ppimgNW = window.open('','POPUPIMAGE','width=1,height=1,resizable=1');
	var doc = ppimgNW.document;
	doc.write('<html>');
	doc.write('<head>');

	if (title!=null) doc.write('<title>'+ title +'</title>');
	doc.write('<style> body {'+css+'} #ppImgText{'+ css +'} #ppImg{cursor:hand}</style></head>');
	doc.write('<body leftmargin="0" topmargin="' + border + '" onload="doResize();">');
	doc.write('<div align="center">');
	doc.write('<img src="' + baseLoc + src + '" id="ppImg" onclick="self.close();" title="Close">');
	doc.write('</div>');
	doc.write('<div style="height:1; width:' + border + '; font-size:4pt;">');
	doc.write('</div>');
	doc.write('<div id="ppImgText" align="center">');
	doc.write(note);
	doc.write('</div>');
	doc.write('</body>');
	doc.write('</html>');

	doc.write('<' + 'script>');
	doc.write('function doResize() {');
	doc.write('	var imgW = ppImg.width, imgH = ppImg.height;');		 
	doc.write('	window.resizeTo(imgW + 8 +' + border*2 +', imgH + ppImgText.offsetHeight + 76 + '+ border*2 +');');
	doc.write('	setTimeout(\'doResize()\', 1000);');
	doc.write('}');
	doc.write('doResize(); ');
	doc.write('</' + 'script>');
}

function getBaseLocation() {
    var oBaseColl = document.all.tags('BASE');
    return ( (oBaseColl && oBaseColl.length) ? oBaseColl[0].href : '');
}

// Restrict input alphabetic letters only ////////////////
// USAGE: onKeyPress="checkAlphabetic();"
function checkAlphabetic() {
	if ((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 65+32 && event.keyCode <= 90+32))
		return; 
	else
		event.returnValue = null;
}

// Restrict input numeric letters only /////////////////
// USAGE: onKeyPress="checkNumeric();"
function checkNumeric() {
	if ( (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 46 )	// 0-9 and "."
		return; 
	else
		event.returnValue = null;
}

function hasChars(str, charStr) {
	var i;
	for (i=0; i<charStr.length; i++) {
		if (str.indexOf(charStr.charAt(i)) >= 0)
			return true;
	}
	return false;
}

function isEmail(str) {
	return (str.match(/([^@ ]+)@([^\. ]+)\.([^\. ]+)(.*)$/) != null);
}

function isUsername(str) {
	return (str.length >= 4 && str.match(/[^0-9a-z_\.]/) == null);
}

function isPassword(str) {
	return (str.length >= 4 && str.match(/[^0-9a-z_\.]/) == null);
}

function UCC(s) {	// convert Unicode string into &#CODE; format string
	var i, sOut = '';
	for (i=0; i<s.length; i++) {
		var charCode = s.charCodeAt(i);
		var charLtr = s.charAt(i);
		if (charCode > 128)
			sOut += '&#' + charCode + ';';
		else
			sOut += charLtr;
	}
	return sOut;
}

function csv2Array(str, separator) {
	if (!separator) separator = ',';
	return str.split(separator);
}

function items2Array(sItems) {
	var index = 0, aRet = new Array();
	var i, a, sRowSeparator, sColumnSeparator;
	if (sItems.indexOf('[=]') >= 0 && sItems.indexOf('[;]') >= 0) {
		sRowSeparator = '[;]';
		sColumnSeparator = '[=]';
	} else {
		sRowSeparator = ';';
		sColumnSeparator = '=';
	}
	a = sItems.split(sRowSeparator);
	
	for (i=0; i<a.length; i++) 
		if (a[i] != '') {
			var b = a[i].split(sColumnSeparator);
			aRet[index++] = b[0];
			aRet[index++] = b[1];
		}
	return aRet;
}

function searchArray(arr, val) {
	if (arr)
		{
			var i;
			for (i=0; i<arr.length; i++)
				if (arr[i] == val)
					return i;
		}
	return -1;
}

function getName(aArgs, sValue) {
	for (i=0; i<aArgs.length-1; i+=2) {
		if (sValue == aArgs[i+1])
			return aArgs[i];
	}
	return null;
}

function getSelect(sName, args, sSelect, sAttr) {
	var strHTML = '';
	var aArgs;
	if (typeof(args) == 'string')
		aArgs = items2Array(args);
	else
		aArgs = args;
	strHTML += '<select name="' + sName + '" ' + (sAttr==null ? '' : sAttr) + '>';
	for (i=0; i<aArgs.length; i+=2) {
		if (sSelect == aArgs[i+1])
			strHTML += '<option' + (aArgs[i+1]!=null ? ' value="' + aArgs[i+1] + '"' : '') + ' selected>' + aArgs[i] + '</option>\r\n';
		else
			strHTML += '<option' + (aArgs[i+1]!=null ? ' value="' + aArgs[i+1] + '"' : '') + '>' + aArgs[i] + '</option>\r\n';
	}
	strHTML += '</select>';
	return strHTML;
}

function writeSelect(sName, aArgs, sSelect, sAttr) {
	document.write(getSelect(sName, aArgs, sSelect, sAttr));
}

function getSingleChoices(sName, args, selectedValue, sType, sAttr) {
	var html = '';
	var aArgs;
	if (typeof(args) == 'string')
		aArgs = items2Array(args);
	else
		aArgs = args;
	
	if (sType == 'radio') {
		for (i=0; i<aArgs.length; i+=2)
			html += '<input ' + (i==0 ? 'checked' : '') + ' type=radio name="'+ sName +'" ' + (aArgs[i+1]!=null ? ' value="' + aArgs[i+1] + '"' : '') + ' ' + (aArgs[i+1] == selectedValue ? 'checked' : '') + '>' + aArgs[i] + '&nbsp;&nbsp;\r\n';
	} else if (sType == 'select') {
		html = '<select name="' + sName + '" ' + (sAttr==null ? '' : sAttr) + '>';
		for (i=0; i<aArgs.length; i+=2)
			html += '<option value="' + aArgs[i+1] + '" ' + (aArgs[i+1] == selectedValue ? 'selected' : '') + '>' + aArgs[i] + '</option>\r\n';
		html += '</select>';
	}

	return html;
}

function getMultiChoices(sName, args, selectedValues, sType, sAttr) {
	var html = '';
	var aArgs, aSelectedValues;
	if (typeof(args) == 'string')
		aArgs = items2Array(args);
	else
		aArgs = args;
	if (typeof(selectedValues) == 'string')
		aSelectedValues = csv2Array(selectedValues, ';');
	else
		aSelectedValues = selectedValues;
	if (sType == 'checkbox') {
		for (i=0; i<aArgs.length; i+=2)
			html += '<input type=checkbox name="'+ sName +'" ' + (aArgs[i+1]!=null ? ' value="' + aArgs[i+1] + '"' : '') + ' ' + (searchArray(aSelectedValues, aArgs[i+1]) >= 0 ? 'checked' : '') + '>' + aArgs[i] + '&nbsp;&nbsp;\r\n';
	} else 	if (sType == 'checkbox-array') {
		var ca = new Array();
		for (i=0; i<aArgs.length; i+=2)
			ca[ca.length] = '<input type=checkbox name="'+ sName +'" ' + (aArgs[i+1]!=null ? ' value="' + aArgs[i+1] + '"' : '') + ' ' + (searchArray(aSelectedValues, aArgs[i+1]) >= 0 ? 'checked' : '') + '>' + aArgs[i] + '';
		return ca;
	} else if (sType == 'select') {
		html = '<select name="' + sName + '" multiple ' + sAttr + '>';
		for (i=0; i<aArgs.length; i+=2)
			html += '<option value="' + aArgs[i+1] + '" ' + (searchArray(aSelectedValues, aArgs[i+1]) >= 0 ? 'selected' : '') + '>' + aArgs[i] + '</option>\r\n';
		html += '</select>';
	}
	return html;
}

function writeSingleChoices(sName, args, selectedValue, sType, sAttr) {
	document.write(getSingleChoices(sName, args, selectedValue, sType, sAttr));
}

function writeMultiChoices(sName, args, selectedValues, sType, sAttr) {
	document.write(getMultiChoices(sName, args, selectedValues, sType, sAttr));
}

/*
Parameters:

specs = "EmptyItem=<text>:<value>,Month={vn|sort-vn|en|sort-en|number},Year={from-to},Order={dmy|mdy|ymd}"
*/

function writeDate(name, dateMDY, specs) {
	var defaultDate;
	var today = new Date();

	var d = today.getDate();
	var m = today.getMonth() + 1;
	var y = today.getYear();

	if (isNaN(new Date(dateMDY))) {
		defaultDate = new Date((today.getMonth()+1) + '/' + today.getDate() + '/' + today.getYear() + ' 00:00');
		if (typeof(dateMDY) == 'string') {
			var a = dateMDY.split('/');
			if (a.length >= 3) {
				m = a[0]; d = a[1]; y = a[2];
			}
		}
	} else {
		defaultDate = new Date(dateMDY);
		var d = defaultDate.getDate();
		var m = defaultDate.getMonth() + 1;
		var y = defaultDate.getYear();
	}

	var hr = defaultDate.getHours();
	var mn = defaultDate.getMinutes();

	var hasEmptyItem	= false;
	var emptyItemText	= '..';
	var emptyItemValue	= '';
	var month		= 'en';
	var yearFrom		= today.getYear() - 10;
	var yearTo		= today.getYear() + 10;
	var dayOrder		= 'dmy';
	var sDisabled		= '';

	if (specs) {
		var aSpecs = specs.split(',');
		for (i=0; i<aSpecs.length; i++) {
			var aPair = aSpecs[i].split('=');
			var sKey = aPair[0].toLowerCase(), sValue = aPair[1];
			switch(sKey) {
				case 'emptyitem':		var a = sValue.split(':'); emptyItemText=a[0]; emptyItemValue=a[1]; hasEmptyItem = true; break;
				case 'month':			month = sValue; break;
				case 'year':			var a = sValue.split('-'); yearFrom = a[0]; yearTo = a[1]; break;
				case 'order':			dayOrder = sValue; break;
				case 'disabled':		sDisabled = (sValue=='1') ? 'disabled' : ''; break;
			}
		}
	}

	var aDays = new Array('1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9','10','10','11','11','12','12','13','13','14','14','15','15','16','16','17','17','18','18','19','19','20','20','21','21','22','22','23','23','24','24','25','25','26','26','27','27','28','28','29','29','30','30','31','31');
	var aMonths;
	switch(month) {
		case 'vn':
			aMonths = new Array('Thang Gieng','1','Thang Hai','2','Thang Ba','3','Thang Tu','4','Thang Nam','5','Thang Sau','6','Thang Bay','7','Thang Tam','8','Thang Chin','9','Thang Muoi','10','Thang Muoi Mot','11','Thang Muoi Hai','12'); break;
		case 'sort-vn':
			aMonths = new Array('Thang 1','1','Thang 2','2','Thang 3','3','Thang 4','4','Thang 5','5','Thang 6','6','Thang 7','7','Thang 8','8','Thang 9','9','Thang 10','10','Thang 11','11','Thang 12','12'); break;
		case 'en':
			aMonths = new Array('January','1','February','2','March','3','April','4','May','5','June','6','July','7','August','8','September','9','October','10','November','11','December','12'); break;
		case 'sort-en':
			aMonths = new Array('Jan.','1','Feb.','2','Mar.','3','Apr.','4','May','5','Jun.','6','Jul.','7','Aug.','8','Sep.','9','Oct.','10','Nov.','11','Dec.','12'); break;
		case 'number':
			aMonths = new Array('1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9','10','10','11','11','12','12'); break;
	}

	var aYears = new Array();
	var i, j=0;
	for (i=yearFrom; i<=yearTo; i++) { aYears[j++] = i; aYears[j++] = i; }

	if (hasEmptyItem) {
		var emptyItem = new Array(emptyItemText, emptyItemValue);
		aDays = aDays.concat(emptyItem);
		aMonths = aMonths.concat(emptyItem);
		aYears = aYears.concat(emptyItem);
	}

	document.write('<input type="hidden" name="'+ name +'" value="'+m+'/'+d+'/'+y+'">');
	document.write('<input type="hidden" name="'+ name +'_vn" value="'+d+'/'+m+'/'+y+'">');

	function _dispD(){
		writeSelect(name + '_fpt_wdmy_d', aDays, d, 
			'onclick="document.all(\''+ name +'\').value = document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;"' +
			'onchange="document.all(\''+ name +'_vn\').value = document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;" ' + sDisabled);
	}
	function _dispM() {
		writeSelect(name + '_fpt_wdmy_m', aMonths, m, 
			'onclick="document.all(\''+ name +'\').value = document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;"' +
			'onchange="document.all(\''+ name +'_vn\').value = document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;" ' + sDisabled);
	}
	function _dispY() {
		writeSelect(name + '_fpt_wdmy_y', aYears, y, 
			'onclick="document.all(\''+ name +'\').value = document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;"' +
			'onchange="document.all(\''+ name +'_vn\').value = document.all(\'' + name + '_fpt_wdmy_d\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_m\').value + \'/\' + document.all(\'' + name + '_fpt_wdmy_y\').value;" ' + sDisabled);
	}
	function _dispBlank() { document.write('&nbsp'); }

	switch(dayOrder) {
		case 'dmy': _dispD(); _dispBlank(); _dispM(); _dispBlank(); _dispY(); break;
		case 'mdy': _dispM(); _dispBlank(); _dispD(); _dispBlank(); _dispY(); break;
		case 'ymd': _dispY(); _dispBlank(); _dispM(); _dispBlank(); _dispD(); break;
	}

}

function writeDayMonthYear(name, d, m, y) {
	writeDate(name, m+'/'+d+'/'+y);
}

function selectItem(oSelect, itemValue) {
	var i;
	for (i=0; i<oSelect.options.length; i++) {
		if (oSelect.options[i].value == itemValue) {
			oSelect.options[i].selected = true;
			return true;
		}
	}
	return false;
}

// User Interface functions -------------------------------------------

function getTop (o) {
	var nTop=0;
	while (o.tagName != "BODY") {
		nTop += o.offsetTop;
		o = o.offsetParent;
	}
	return nTop;
}

function getLeft (o) {
	var nLeft=0;
	while (o.tagName != "BODY") {
		nLeft += o.offsetLeft;
		o = o.offsetParent;
	}
	return nLeft;
}

// End -------------------------------------------------------------------------
/*
Templates:

function ()
	.Description
		
	.Parameters
		
	.Returns
*/