////////////////////////////////////////////////
// 	Project:	<-- common -->
//	File:		form.js
//	ver:		2.0
//	last update:	3/10/2005
//	create by:	Philip LI
//	description:	This file contains some using javascript function
//			these functions are useful in document forms
//
////////////////////////////////////////////////

//-----------------------------------------------------------------------
// description:	check if ANY of the input checkbox is ticked
// input :	a checkbox element, or an array of checkbox
// output:	if ANY of the input is checked, return true;
//			else return false;
// error :	if (any) input is not a type 'checkbox' or an array of 'checkbox',
//			return false;
//-----------------------------------------------------------------------
function checkselect(checkbox){
	//=======================================
	// input checking
	if (typeof checkbox == 'undefined')
	return false;
	//=======================================

	if (checkbox.length){	// it is an array
		var result = false;
		for (var i=0; i<checkbox.length; i++){
			if (checkbox[i].type != 'checkbox'){
				return false;
			}
			if (checkbox[i].checked){
				result = true;
			}
		}
		return result;
	}else{			// it is not an array
		if (checkbox.type != 'checkbox')
		return false;
		if (checkbox.checked){
			return true;
		}else return false;
	}
}

//-----------------------------------------------------------------------
// description:	change the status of the checkbox(s)
// input :	a checkbox element, or an array of checkbox
// output:	return true on no error
// error :	if (any) input is not a type 'checkbox' or an array of 'checkbox',
//			return false;
//-----------------------------------------------------------------------
function selectall(checkbox, status){
	//=======================================
	// input checking
	if (!(typeof status == 'boolean'))
	return false;

	if (typeof checkbox == 'undefined')
	return false;
	//=======================================

	if (checkbox.length){	// it is an array

		for (var i=0; i<checkbox.length; i++){
			if (!(checkbox && typeof checkbox == 'object'))
			return false;
			if (checkbox[i].type != 'checkbox')
			return false;

			// if the box is disabled, leave it unchange
			if (!checkbox[i].disabled)
			checkbox[i].checked = status;
		}
		return true;

	}else{		// it is not an array
		if (checkbox.type != 'checkbox')
		return false;

		// if the box is disabled, leave it unchange
		if (!checkbox.disabled)
		checkbox.checked = status;
		return true;
	}

}


//-----------------------------------------------------------------------
// description:	add option dynamic from a select element
// input :	the select element, the option text , the option value
// output:	return true on no error
// error :	if the first input is not a 'select' element,
//			or second input is not string,
//			or third input is not number
//			return false;
//-----------------------------------------------------------------------

function addOption(selectObject,optionText,optionValue) {
	//=======================================
	// input checking
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple')){
		return false;
	}
	if (!(typeof optionText == 'string')){
		return false;
	}
	if (!(typeof optionValue == 'string')){
		return false;
	}
	//=======================================

	var optionObject = new Option(optionText,optionValue);
	var optionRank = selectObject.options.length;
	selectObject.options[optionRank]=optionObject;
	return true;
}


//-----------------------------------------------------------------------
// description:	delete option dynamic from a select element
// input :	the select element, the option rank
// output:	return true on no error
// error :	if the first input is not a 'select' element,
//			or second input is not string,
//			return false;
//-----------------------------------------------------------------------
function deleteOption(selectObject,optionRank) {
	//=======================================
	// input checking
	if (!(selectObject && typeof selectObject == 'object'))
	return false;
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple'))
	return false;
	if (!(typeof optionRank == 'number' && isFinite(optionRank)))
	return false;
	//=======================================

	if (selectObject.options.length!=0)
	selectObject.options[optionRank]=null;
	return true;
}



//-----------------------------------------------------------------------
// description:	check if any of the radio button is selected
// input :	the radio object, or an array of radio
// output:	if ANY of the input is checked, return true;
//			else return false;
// error :	if (any) input is not a type 'radio' or an array of 'radio',
//			return false;
//-----------------------------------------------------------------------
function checkSelectRadio(radio){
	//=======================================
	// input checking
	if (typeof radio == 'undefined')
	return false;
	//=======================================

	if (radio.length){	// it is an array
		for (var i=0; i<radio.length; i++){
			if (!(radio && typeof radio == 'object'))
			return false;
			if (radio[i].type != 'radio')
			return false;

			// if this radio is checked, return true
			if (radio[i].checked)
			return true;
		} // end for
		return false;

	}else {		// it is not an array
		if (radio.type != 'radio')
		return false;

		// if the radio is checked, return true
		if (radio.checked)
		return true;
		else return false;
	}
}




//-----------------------------------------------------------------------
// description:	select all optiona dynamic from a multiple select element
// input :	the select element, boolean status
// output:	return true on no error
// error :	if the first input is not a 'select' element,
//			or status is not boolean,
//			return false;
//-----------------------------------------------------------------------
function selectAllOption(selectObject, status) {
	//=======================================
	// input checking
	if (!(typeof status == 'boolean'))
	return false;
	if (!(selectObject && typeof selectObject == 'object'))
	return false;
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple'))
	return false;
	//=======================================
	for (var i=0; i<selectObject.options.length; i++){
		selectObject.options[i].selected=status;
	}
	return true;
}



//-----------------------------------------------------------------------
// description:	return the number of input checkbox is ticked
// input :	a checkbox element, or an array of checkbox
// output:	return a number;
// error :	if (any) input is not a type 'checkbox' or an array of 'checkbox',
//			return false;
//-----------------------------------------------------------------------
function countselect(checkbox){
	//=======================================
	// input checking
	if (typeof checkbox == 'undefined')
	return false;
	//=======================================

	if (checkbox.length){	// it is an array
		var result = 0;
		for (var i=0; i<checkbox.length; i++){
			if (checkbox[i].type != 'checkbox'){
				return false;
			}
			if (checkbox[i].checked){
				++result;
			}
		}
		return result;
	}else{			// it is not an array
		if (checkbox.type != 'checkbox')
		return false;
		if (checkbox.checked){
			return true;
		}else return false;
	}
}



// Script dealing with select with size>1 and multiple select
// Author:	Sky Lau
// Date:	19/10/2004
//
function deleteSelection(selectObject){
	//=======================================
	// input checking
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple')){
		return false;
	}
	//=======================================
	if(selectObject.hasChildNodes()){
		var ptr = selectObject.firstChild;
		var oldptr;
		while(ptr!=null){
			if(ptr.selected){
				oldptr = ptr
				ptr = ptr.nextSibling;
				selectObject.removeChild(oldptr);
			}else{
				ptr = ptr.nextSibling;
			}
		}
	}
}

function insertSelection(selectObject,optionText,optionValue){
	//=======================================
	// input checking
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple')){
		return false;
	}
	if (!(typeof optionText == 'string')){
		return false;
	}
	if (!(typeof optionValue == 'string')){
		return false;
	}
	//=======================================

	for (var i=0; i<selectObject.options.length; i++){
		if(selectObject.options[i].value==optionValue)
		return false;
	}

	var optionObject = new Option(optionText,optionValue);
	var optionRank = selectObject.options.length;
	selectObject.options[optionRank]=optionObject;
	return true;
}


function removeSelection(selectObject,optionValue){
	//=======================================
	// input checking
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple')){
		return false;
	}
	if (!(typeof optionValue == 'string')){
		return false;
	}
	//=======================================
	if(selectObject.hasChildNodes()) {
		var ptr = selectObject.firstChild;
		while(ptr!=null){
			if (optionValue == ptr.value) {
				oldptr=ptr;
				if(ptr.nextSibling)
				ptr=ptr.nextSibling;
				else
				ptr=null;
				selectObject.removeChild(oldptr);
			}
			else {
				if(ptr.nextSibling)
				ptr=ptr.nextSibling;
				else
				ptr=null;
			}
		}
	}
	return true;
}


function deleteAll(selectObject){
	//=======================================
	// input checking
	if ((selectObject.type != 'select-one') && (selectObject.type != 'select-multiple')){
		return false;
	}
	//=======================================
	if(selectObject.hasChildNodes()){
		var ptr = selectObject.firstChild;
		var oldptr;
		while(ptr!=null){
			oldptr = ptr
			ptr = ptr.nextSibling;
			selectObject.removeChild(oldptr);
		}
	}
}

function initSelectBox(selectObject, theArray, theLabel, selectedObject){
	var i;
	var obj;
	while(selectObject.hasChildNodes()) {
		selectObject.removeChild(selectObject.firstChild);
	}
	for(i=0;i<theArray.length;i++){
		obj = document.createElement("option");
		obj.appendChild(document.createTextNode(theLabel[i]));
		obj.setAttribute("value",theArray[i]);
		if(selectedObject == theArray[i]){
			obj.selected = true;
		}
		selectObject.appendChild(obj);
	}
}

function checkLeapYear(year){
	if((year % 400) == 0) return true;
	if((year % 100) == 0) return false;
	if((year % 4) == 0)   return true;
	return false;
}

function getMonthDay(year, month){
	if(checkLeapYear(year) && (month == 2)) return 29;
	var monthday = 31;
	if(month == 4 || month == 6 || month == 9 || month == 11) monthday = 30;
	if(month == 2) monthday = 28;
	return monthday;
}

function getSimpleArray(minElement, maxElement){
	var offset = maxElement - minElement + 1;
	var result = new Array(offset);
	var i;
	for(i=0;i<offset;i++){
		result[i] = minElement + i;
	}
	return result;
}

function resetMonthDaySelectBox(selectObject, year, month){
	var nextArray = getSimpleArray(1, getMonthDay(year, month));
	//alert(getMonthDay(year, month));
	var oldDay = 0;
	for (var i=0; i<selectObject.options.length; i++){
		if(selectObject.options[i].selected){
			oldDay = selectObject.options[i].value;
		}
	}
	initSelectBox(selectObject, nextArray, nextArray, oldDay);
}
function loadFlash(gto,topic) {
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='600' height='100'>");
	document.write("<param name='movie' value='images/topic_preport.swf?goto="+gto+"&topic="+topic+"'>");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='wmode' value='transparent'>");
	document.write("<embed src='images/topic_preport.swf?goto="+gto+"&topic="+topic+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='600' height='100' wmode='transparent'></embed>");
	document.write("</object>");
}