/*****************************
* Function updateJpDate
* Change date in format yyyy/mm/dd to jp date
*
* @param strElementValue ... id of hidden element
* @param strElementResult ... id of element to display
* @param strPrefix ... the string added before value
* @param strSubfix ... the string added behind value
*****************************/
function updateJpDate(strElementValue, strElementResult, strPrefix, strSubfix)
{
	var objItem = document.getElementById(strElementResult);
	var objValue = document.getElementById(strElementValue);
	if(objItem && objValue) {
		if (objValue.value != "") {
			// Create JpCalendar object
			var objJpCalendar = new myJpCalendar();
			var strResult = objJpCalendar.changeJPDate(objValue.value, false);
			if (strPrefix != null) {
				strResult = strPrefix + strResult;
			}
			if (strSubfix != null) {
				strResult += strSubfix;
			}
			if (objItem.tagName == 'INPUT') {
				objItem.value = strResult;
			} else {
				objItem.innerHTML = strResult;
			}
		}
	}
}

/*****************************
* Function printJpDate
* Print date in JP format
*
* @param strDate ... date in format yyyy/mm/dd
*****************************/
function printJpDate(strDate)
{
	// Create JpCalendar object
	var objJpCalendar = new myJpCalendar();
	strDate = objJpCalendar.formatDate(strDate);
	if (objJpCalendar.checkValidDate(strDate)) {
		document.write(objJpCalendar.changeJPDate(strDate));
	}
}

/*****************************
* Function changeJpDateInputIntoNormal
* Get date value of a item in JP format
* copy into another item then change into normal format date
*
* @param strSourceName ... name of item contains jp date
* @param strTargetName ... name of item will be updated value
*****************************/
function changeJpDateInputIntoNormal(strSourceName, strTargetName)
{
	// Create JpCalendar object
	var objJpCalendar = new myJpCalendar();

	// Get elements
	var objSource = document.getElementById(strSourceName);
	var objTarget = document.getElementById(strTargetName);

	// Trim all input
	objSource.value = objSource.value.trimAll();

	// Change input from jp calendar to format yyyy/mm/dd
	var strInputValue = objJpCalendar.formatExceptFullDate(objSource.value);
	strInputValue = objJpCalendar.formatDate(strInputValue);
	if (objJpCalendar.checkValidDate(strInputValue)) {
		objTarget.value = strInputValue;
	} else {
		objTarget.value = "";
	}
}

/*****************************
* prototype trim
* Trim space 1 and 2 bytes
*****************************/
String.prototype.trim = function()
{
	return this.replace(/^[\s\u3000]+|[\s\u3000]+$/g, '');
}

/*****************************
* Function updateDisplayToShowJpDate
* Update link that contains jpdate
*
* @param intCount ... number of records
* @param strSpanId ... name of item contain date in normal format
* @param strSpanId ... name of item link
*****************************/
function updateDisplayToShowJpDate(intCount, strSpanId, strLinkId)
{
	// Create JpCalendar object
	var objJpCalendar = new myJpCalendar();

	for (var intIndex = 1; intIndex <= intCount; intIndex++ ) {
		var objSpan = document.getElementById(strSpanId + intIndex);

		if (objSpan) {
			var strDateValue = objSpan.innerHTML;
			if (objJpCalendar.checkValidDate(strDateValue)) {
				strDateValue = objJpCalendar.changeJPDate(strDateValue);
				objSpan.innerHTML = strDateValue + "&nbsp;&nbsp;";
				if (strLinkId) {
					var objLink = document.getElementById(strLinkId + intIndex);
					objLink.title = strDateValue + "  " + objLink.title;
				}
			} else {
				objSpan.innerHTML = "";
			}
		} else {
			return;
		}
	}
}

/*****************************
* Function getFloatingSize
* Calculate the size for floating window
*
*****************************/
function getFloatingSize()
{
	// for Internet Explorer
	if (document.all) {
		if ( document.documentElement ) {
			pos = document.documentElement;
		} else {
			pos = document.body;
		}
		var floatHeight = pos.clientHeight - 100;
		var floatWidth = pos.clientWidth - 300;
	// for Netscape 6.*
	} else if (document.getElementById) {
		var floatHeight = window.innerHeight - 100;
		var floatWidth = window.innerWidth - 300;
	}

	return new Array(floatHeight, floatWidth);
}

/*****************************
* Function createFloatingWindow
* Create new floating window
*
* @param intWidth ... width
* @param intHeight ... height
*****************************/
function createFloatingWindow(intWidth, intHeight)
{
	if (intWidth == null) {
		intWidth = 300;
	}
	if (intHeight == null) {
		intHeight = 200;
	}
	var objFloating = new Window({className: "dialog", url: "",
		minimizable: false, maximizable: false, closable: true, width: intWidth, height: intHeight, zIndex: 100,
		showEffect: Element.show, hideEffect: Element.hide, resizable: false, title: "", draggable: false, wiredDrag: false});
	var objStatus = document.getElementById(objFloating.getId() + '_bottom');
	if (objStatus) {
		objStatus.innerHTML = '';
	}
	return objFloating;
}

/*****************************
* Function trimElementValue
* Trim value of element
*
* @param strId ... id of element
*****************************/
function trimElementValue(strId)
{
	var objElement = document.getElementById(strId);
	objElement.value = objElement.value.trim();
}

/*****************************
* Function floatingWindowTabKeyDown
* Move within window when press tab key
*
* @param 	strFirstControl ... id of first control
* 			strLastControl ... id of last control
*****************************/
function floatingWindowTabKeyDown(strFirstControl, strLastControl)
{
	setTimeout("doWindowTabKeyDown('" + strFirstControl +  "','" + strLastControl + "')", 50, "Javascript");
}

function doWindowTabKeyDown(strFirstControl, strLastControl)
{
	// Init gloabal variable
	document.myActiveElement = null;
	var strFirstControlId = strFirstControl;

	// Declare function handler key down event
	function keypressHandler(event){
		var key;
		if (window.event) {
			//it's IE
			key = window.event.keyCode;
		} else {
			key = event.which;
		}
		// Press TAB key and the last control is focused
		if (key == 9 && document.myActiveElement) {
			if (document.myActiveElement.id == strLastControl) {
				document.getElementById(strFirstControlId).focus();
				document.myActiveElement = null;
				return false;
			}
		}
		document.myActiveElement = null;
	}
	document.onkeydown = function(event) {
		return keypressHandler(event);
	}
	var objLast = document.getElementById(strLastControl)
	objLast.onfocus = new Function ("document.myActiveElement=this;");

	document.getElementById(strFirstControlId).focus();
}

