/*****************************
* Function checkToDisplayControl
* Check box (radio) checked or unchecked
* then show or hide control
*
* @param 	strCheckControlId ... id of control to check
* 			strTargetControlId ... id of control to hide or show
* 			strClearControlId ... id of control to delete value
* 			booShowCondition ... hide(or show) when checked or unchecked
*****************************/
function checkToDisplayControl(strCheckControlId, strTargetControlId, strClearControlId, booShowCondition)
{
	var objCheckControl = document.getElementById(strCheckControlId);
	var objTargetControl = document.getElementById(strTargetControlId);
	if (booShowCondition == null) {
		booShowCondition = true;
	}
	if (objCheckControl != null && objTargetControl != null) {
		if (objCheckControl.checked == booShowCondition) {
			objTargetControl.style.display = '';
		} else {
			objTargetControl.style.display = 'none';
			if (strClearControlId != null) {
				if (document.getElementById(strClearControlId).type == "text") {
					document.getElementById(strClearControlId).value = '';
				} else {
					uncheckCheckboxRecursive($(strClearControlId));
				}
			}
		}
	}
}

function uncheckCheckboxRecursive(objParent) {
	if (objParent.type == "checkbox") {
		objParent.checked = false;
	} else {
		if (objParent.tagName == "DIV" || objParent.tagName == "SPAN") {
			objParent.hide();
		}
		var aryChild = objParent.childElements();
		for(var intIndex = 0; intIndex < aryChild.length; intIndex++) {
			uncheckCheckboxRecursive(aryChild[intIndex]);
		}
	}
}

/*****************************
* Function append_event
* イベントハンドラ登録用関数
*
* @param  ele  ... element
*         type ... event name
*         func ... function name
*****************************/
function append_event( ele, type, func )
{
	if (ele.addEventListener) {
		ele.addEventListener(type, func, false);
	} else {
		ele.attachEvent('on' + type, func);
	}
}

/*****************************
* Function set cookie
*
* @param  c_name  ... cookie name
*         value ... value
*         expiredays ... expire time in days
*         path ... path
*****************************/
function setCookie(c_name, value, expiredays, path)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name+ "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString()) + ((path == null) ? "" : ";path=" + path);
}

/*****************************
* Function get cookie
*
* @param  c_name  ... cookie name
* @return value of cookie
*****************************/
function getCookie(c_name)
{
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

/*****************************
* 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();
}

/*****************************
* Display tooltip image
*
* @param 	objImage ... Image element
* 			intWidth ... width to show
* 			intHeight ... height to show
* 			intType ... type image to show (1 : 3x4 - 2 : 4x3 - Default: 1)
*****************************/
function displayImageToolTip(objImage, intWidth, intHeight)
{
	var objImagePreview = new myImagePreview();
	objImagePreview.init(objImage, intWidth, intHeight);
}

