/************************************************************************
*                                                                       *
*   This application is developed and owned by Thomas Idea Co.,Ltd.     *
*   XXXXXXX CO.,LTD. is granted  license from Thomas Idea 			    *
*   for its internal use only. Usage on any parts of the program without*
*   a permission from Thomas Idea Co.,Ltd. is illegal and prohibited.   *
*   This application is protected by the International Right Law.		*
*                       -  All rights reserved.                         *
*                                                                       *
************************************************************************/
function loading(openFlg, vPath){
	if(document.getElementById('loaddingDiv')==null){
		var loading = "";
		var hg = document.body.offsetHeight;
		if(hg<500){
			hg=500;
		}
		loading += '<div id="loaddingDiv" style="position:absolute;top:0px;left:0px;width:100%;height:auto;background-color:#FFFFFF;display:none;z-index:8">';
		loading += '<table width="100%">';
		loading += '<tr>';
		loading += '<td id="loaddingHeight" height="'+hg+'" align="center" valign="top" style="padding-top:200px"><img src="'+vPath+'/bcknd/images/share/img/ldng.gif" /></td>';
		loading += '</tr>';
		loading += '</table>';
		loading += '</div>';
		loading += '<div id="loaddingDivBg" style="position:absolute;top:0px;left:0px;width:100%;height:auto;background-color:#FFFFFF;display:none">';
		loading += '<iframe id="loaddingFrame" style="z-index:7" width="100%" height="'+hg+'" frameborder="0" scrolling="no"></iframe>';
		loading += '</div>';
		document.write(loading);
		
	}
	if(openFlg){
		var hg = document.body.offsetHeight;
		if(hg<500){
			hg=500;
		}
		document.getElementById('loaddingHeight').height = hg;
		document.getElementById('loaddingFrame').height = hg;
		document.getElementById('loaddingDiv').style.display = "inline";
		document.getElementById('loaddingDivBg').style.display = "inline";		
		//alert("Test loading...");
	}else{
		document.getElementById('loaddingDiv').style.display = "none";
	}	
}

function replaceAll(strValue, oldStr, newStr) {
	var index;
	var i = 0, start = 0, end = 0;
	tempStr = "";
	while (strValue.indexOf(oldStr, start) != -1) {
		end = strValue.indexOf(oldStr, start);
		tempStr = tempStr + strValue.substring(start, end) + newStr;
		start = end + oldStr.length;
	}
	tempStr = tempStr + strValue.substring(start, strValue.length);
	return tempStr;
} 

function openWin(pages, winName, detail) {
	w = open (pages, winName, detail);
	if (w.opener == null)
		w.opener = self;
	w.focus();
}
function getSelectValue(drpdwn) {
	str = drpdwn.options[drpdwn.selectedIndex].value;
	return str;
}

function getRadioValue(radioObj) {
	var i;
	for(i=0; i<radioObj.length; i++) {
		if(radioObj[i].checked == true)
			return radioObj[i].value;
	}
	return "";
}

function chkNumber(){
	if(event.keyCode < 48 || event.keyCode > 57) {
		if(event.keyCode != 46)
			event.keyCode = 0;
	}
}

function getFileType(fileSource) {
	return (fileSource.substring(fileSource.lastIndexOf('.') + 1,fileSource.length)).toLowerCase();
}

function getFileName(fileSource) {
	return fileSource.substring(fileSource.lastIndexOf('\\') + 1,fileSource.lastIndexOf('.'));
}

function getFullFileName(fileSource) {
	return fileSource.substring(fileSource.lastIndexOf('\\') + 1,fileSource.length);
}

function isAllowFile(flName,fltyp) {
	var allowFile = fltyp;
	fileType = getFileType(flName).toLowerCase();
	if (allowFile.indexOf(fileType) >= 0)
		return false;
	else
		return true;
}

function chkOnlyEng(str){
	var allowEng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for(i=0;i<str.length;i++){
		if (allowEng.indexOf((str.charAt(i)).toUpperCase()) < 0)
			return true;
	}
	return false;
}

function getCurrentDate() {
	tdy = new Date();
	tdyDay = tdy.getDate();
	tdyMonth = tdy.getMonth() + 1;
	tdyYear = tdy.getYear();
	if (tdyDay < 10)
		tdyDay = "0" + tdyDay;
	if (tdyMonth < 10)
		tdyMonth = "0" + tdyMonth;
	return tdyDay + "/" + tdyMonth + "/" + tdyYear;
}

function markSelectValue(drpdwn, data) {
	for (i=0; i<drpdwn.length; i++) {
		if (drpdwn.options[i].value == data)
			drpdwn.selectedIndex = i;
	}
}

function markRadioValue(radioObj, data) {
	for (i=0; i<radioObj.length; i++) {
		if (data == radioObj[i].value)
			radioObj[i].checked = true;
	}
}

function markCheckValue(radioObj, data) {
		if (data == radioObj.value)
			radioObj.checked = true;
}

/*function showMessage(txt, obj) {
	if(obj!=null&&!obj.disable){
		obj.focus();
	}
	return false;
}*/

function isEmpty(str) {
	if (str == null || str == "" )
		return true;
	else
		return false;
}

function isSpacer(str) {
	var chk = false;
	for (var index = 0; index < str.length; index++) {
		if(str.charAt(index) == " ")
			chk = true;
	}
	return chk;
}
function isSpace(str) {
	var chk = false;
	for (var index = 0; index < str.length; index++) {
		if(str.charAt(index) == " ")
			chk = true;
	}
	return chk;
}

function isNumber(numStr) {
	allowDouble = "0123456789";

	if (numStr.indexOf(".") != numStr.lastIndexOf(".")) {
		return false;
	}
	
	for (i = 0; i < numStr.length; i++) {
		if (allowDouble.indexOf(numStr.charAt(i)) < 0)
			return false;
	}
	return true;
}

function isFloat(str) {
	dotindx = 0, dotflag = false;
	field = "", tmp = "";
	for (i = 0; i < str.length; i++)
	{
		tmp = str.substring(i, i+1);
		if (tmp == ".")
		{
			if (dotflag==false)
			{
				dotflag = true;
			}
			else
			{
				return false;
			}
		}
	}
	if (dotflag)
	{
		field=replaceAll(str, ".", "");
		if (isNumber(field))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{		
		if (isNumber(str))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}	


function isEmail(str) {
	notAllowEmail = "#$%&*()=+/!{}[]?\\\"':;,<>^|";
	
	for (i = 0; i < str.length; i++) {
		if (notAllowEmail.indexOf(str.charAt(i)) >= 0)
			return false;
	}

	emailLength = str.length;
	posAt = str.indexOf('@');
	posAt1 = str.lastIndexOf('@');
	posDotFirst = str.indexOf('.');
	posDotLast = str.lastIndexOf('.');
	posSpace = str.lastIndexOf(' ');
	if (posAt <= 0 || posDotFirst <= 0 || posAt == emailLength - 1 || posDotLast - posAt < 2 || posDotLast == emailLength - 1 || posAt != posAt1 || posSpace > 0)
		return false;
	else
		return true;
}

function isSelectDropDown(drpdwn) {
	str = drpdwn.options[drpdwn.selectedIndex].value;
	if ( str == null || str == "" || str.toUpperCase() == "-NONE-")
		return false;
	else
		return true;
}
/*
function isClickCheckBox(frmnm, checkAll, checkBox) {
	var check;
	var frm = document.getElementById(frmnm);
	
	for(i = 0; i < frm.length; i++) {
		if (frm.elements[i].name != checkAll && frm.elements[i].name == checkBox && !frm.elements[i].disabled&&frm.elements[i].type.toUpperCase() == "CHECKBOX") {
			if(frm.elements[i].checked == true){
				check=true;
				break;
			}
		}
	}
	return check;
}*/

function isClickCheckBox(frm, start, num, skip) {
	var result, isChecked;
	//var end = parseInt(num) * parseInt(skip);
	var end = num;
	for (i = start; i < end; i+=skip) {
		if (frm.elements[i].checked) {
			isChecked = true;
			i = end++;
		}
		else{
			isChecked = false;
		}
	}
	return isChecked;
}

function isClickRadio(radioObj) {
	var i;
	for(i = 0; i < radioObj.length; i++) {
		if (radioObj[i].checked == true)
			return true;
	}
	return false;
}

function makeNumeric(str) {
	if (isNumber(str))
		return str;
	else
		return "";
}

/*
 * Format DD.MM.YYYY
 */
var dayPerMonth  = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var newDate = new Array("", "", "");
function isAllowFormatDate(dateValue) {
	newDate[0] = "";
	newDate[1] = "";
	newDate[2] = "";
	var dd= "", mm = "", yy = "";
	var isDate = true;
	var dateValueLength = dateValue.length;
	
	if (dateValueLength == 10) {
		firstSlash = dateValue.indexOf('.');
		secondSlash = dateValue.lastIndexOf('.');
		if (firstSlash != 2 && secondSlash != 5)
			isDate = false;
		else {
			firstDim = firstSlash;
			secondDim = secondSlash;
			
			if (firstDim == secondDim)
				isDate = false;
			else {
				dd = makeNumeric(dateValue.substring(0, firstDim));
				mm = makeNumeric(dateValue.substring(firstDim + 1, secondDim));
				yy = makeNumeric(dateValue.substring(secondDim + 1, dateValueLength));
				if ((dd.length <= 0) || (dd.length > 2) || (mm.length <= 0) || (mm.length > 2) || (yy.length != 4))
					isDate = false;
				else {
					if ((parseFloat(dd) > 31) || (parseFloat(mm) > 12) || (parseFloat(yy) > 9999) || (parseFloat(dd) <= 0) || (parseFloat(mm) <= 0))
						isDate = false;
					else {
						Year = yy;
						if (((Year % 4 == 0) && (Year % 100 != 0)) || (yy % 400 == 0))
							dayPerMonth[2] = 29;
						else
							dayPerMonth[2] = 28;
						if (parseFloat(dd) > dayPerMonth[parseFloat(mm)])
							isDate = false;
					}
				}
			}
		}
	} else if (dateValueLength == 0)
		isDate = true; 
	else
		isDate = false;
	
	if (isDate) {
		newDate[0] = dd;
		newDate[1] = mm;
		newDate[2] = yy;
	}
	return isDate;
}

function isDate(dd, mm, yy) {
  var rtn = false;
  yy = (parseInt(yy)-543)+"";
  if (mm == 2){
    if (((yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) {
      if (dd <= 29) {rtn = true;}
    } else {
      if (dd <= 28) {rtn = true;}
    }
  }
  else if  ((mm == 1) || (mm == 3) || (mm == 5) ||(mm == 7) || (mm == 8) || (mm == 10) || (mm == 12)){
    if (dd <= 31) rtn = true;
  }
  else {     
    if (dd <= 30) rtn = true;
  }
  return rtn;    
}

function getLongDateFormat(dateValue) { //DD.MM.YYYY to Mon dd, yyyy
	var longMonthName = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	dd = eval(dateValue.substring(0,dateValue.indexOf('/')));
	mm = eval(dateValue.substring(dateValue.indexOf('/') + 1,dateValue.lastIndexOf('/')));
	yy = eval(dateValue.substring(dateValue.lastIndexOf('/') + 1,dateValue.length));
	longDate = longMonthName[mm] + " " + dd + ", " + yy;
	longDate = new Date(longDate);
	return longDate
}


function isCorrectDuration(fromField, toField) {
	startDate = getLongDateFormat(fromField);
	endDate = getLongDateFormat(toField);
	if (endDate < startDate)
		return false;
	else
		return true;
}

function isCorrectDate(fromDateObj, toDateObj, strValue) {
	result = true;
	strValue1 = strValue.substring(0, 1).toUpperCase() + strValue.substring(1, strValue.length);
	if (!isAllowFormatDate(fromDateObj.value))
		result = showMessage("Please input " + strValue + " (Start) in correct format.", fromDateObj);
	else if (!isAllowFormatDate(toDateObj.value))
		result = showMessage("Please input " + strValue + " (End) in correct format.", toDateObj);
	
	if (isSpace(fromDateObj.value) && !isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue + " (Start).", fromDateObj);
	else if (!isSpace(fromDateObj.value) && isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue + " (End).", toDateObj);
	else if (!isSpace(fromDateObj.value) && !isSpace(toDateObj.value)) {
		if (!isCorrectDuration(fromDateObj.value, toDateObj.value))
			result = showMessage(strValue1 + " (Start) should be later than " + strValue + " (End).", toDateObj);
	}
	return result;
}

//overwrite method by NDP
function selectAllCheckbox(frmnm,start) {
	//alert(start);
	frm = document.getElementById(frmnm);
	for(i = 0; i < frm.length; i++) {
		if (frm.elements[i].name.toUpperCase() != "CHECKALL" && !frm.elements[i].disabled&&frm.elements[i].type.toUpperCase() == "CHECKBOX" && i >= start) {
			
				frm.elements[i].checked = frm.checkAll.checked;
		}
	}
}

/*function selectAllCheckbox(me, frmnm, checkNm) {
	frm = document.getElementById(frmnm);
	for(i = 0; i < frm.length; i++) {
		if (frm.elements[i].name != me.name && frm.elements[i].name == checkNm && !frm.elements[i].disabled&&frm.elements[i].type.toUpperCase() == "CHECKBOX") {
				frm.elements[i].checked = frm.checkAll.checked;
		}
	}
}*/


function verifyCheckbox(me, frmnm, checkAllnm){
	var check;
	frm = document.getElementById(frmnm);
	if (me.checked == false){
		document.getElementById(checkAllnm).checked = false;
	}
	else{
		for(i = 0; i < frm.length; i++){
		if(frm.elements[i].name != checkAllnm && frm.elements[i].name == me.name && !frm.elements[i].disabled&&frm.elements[i].type.toUpperCase() == "CHECKBOX")
			if(frm.elements[i].checked == true){
				check=true;
			}
			else{
				check=false;
				break;
			}
		}
		if(check){
			document.getElementById(checkAllnm).checked = true;
		}
	}
}

function setCheckAllCheckbox(frm, chkObj, chkAllObj, start, num, skip) {
	var count = 0;
	var chkCount = 0;
	//var end = num * skip;
	var end = num;

	if (chkObj.checked) {	
		for (i = start; i < end; i+=skip) {
			count++;
			if (frm.elements[i].checked && !frm.elements[i].disabled) {
				chkCount++;
			}
		}
		
		if (count == chkCount)
			chkAllObj.checked = true;
		else
			chkAllObj.checked = false;

	} else {
		chkAllObj.checked = false;
	}
}

function refreshDropDown(firstDrop, secondDrop, arr, remain) {
	var dataID = getSelectValue(firstDrop);
	secondDrop.length = remain;
	var indx = secondDrop.length;
	var count = arr[0].length;
	//if (isSelectDropDown(firstDrop)) {
		for (var i=0; i<=count - 1; i++) {
			if (arr[0][i]==dataID||dataID=="") {
				secondDrop.options[indx] = new Option(arr[2][i], arr[1][i], false, false)
				indx++;
			}
		}
	//}
	secondDrop.selectedIndex = 0;
}

function clearDropDown(dropObj, remain) {
	dropObj.length = remain;
	dropObj.selectedIndex = 0;
}

function goToPage(frmnm, page, currentPage) {
  var frm = document.getElementById(frmnm);
  frm.hidCurr.value = currentPage;
  frm.action=page;
  frm.submit();
}

function fillChar( txt, ch, position, length) {
    	var rtn = txt;
   		if (!isEmpty(txt) ) {
        	var txtlen = txt.length;
        	if (length > txtlen) {  
          	for(var i = 0; i < (length - txtlen) ; i++) {
						if(position == 0)
              				rtn = ch + rtn;
						else if(position == 1)
							rtn = rtn + ch;
            			else
            				rtn = rtn + ch;
          	}
        	}else if (length < txtlen) {
			  rtn = txt.substring(0,length);
        	}
    	}else {
     	 rtn = "";
      	for(var j = 0; j < length ; j++) {
              rtn = rtn + ch;
      	}
    }
    return rtn; 
 }

/////////////////////////////////// calendar ////////////////////////////////////
function genCalendar(field, local) {
	var cal;
	if(local.toUpperCase()=="TH"){
		cal = new calendar(eval("document."+field),543);
	}else{
		cal = new calendar(eval("document."+field),0);
	}
	cal.popup();
}
/// calendar
// if two digit year input dates after this year considered 20 century.
var NUM_CENTYEAR = 30;
// is time input control required by default
var BUL_TIMECOMPONENT = false;
// are year scrolling buttons required by default
var BUL_YEARSCROLL = true;

var calendars = [];
var RE_NUM = /^\-?\d+$/;

function calendar(obj_target, add_year) {
	// assigning methods
	this.gen_date = cal_gen_date;
	this.gen_time = cal_gen_time;
	this.gen_tsmp = cal_gen_tsmp;
	this.prs_date = cal_prs_date;
	this.prs_time = cal_prs_time;
	this.prs_tsmp = cal_prs_tsmp;
	this.popup    = cal_popup;
	this.add_year = add_year;

	// validate input parameters
	if (!obj_target)
		return cal_error("Error calling the calendar: no target control specified");
	if (obj_target.value == null)
		return cal_error("Error calling the calendar: parameter specified is not valid target control");
	this.target = obj_target;
	this.time_comp = BUL_TIMECOMPONENT;
	this.year_scroll = BUL_YEARSCROLL;
	
	// register in global collections
	this.id = calendars.length;
	calendars[this.id] = this;
}

//var obj_calwindow;
function cal_popup(str_datetime) {
	//this.dt_current = this.prs_tsmp(str_datetime ? str_datetime : this.target.value);
	this.dt_current = this.prs_tsmp(str_datetime);
	if (!this.dt_current) return;	
	var top_w = 200;
	var left_w = 200;
	if(this.obj_calwindow!=null){
		this.obj_calwindow.location='../../js/clndr/clndr.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id;
	}else{
		this.obj_calwindow = window.open(
			'../../js/clndr/clndr.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
			'Calendar', 'width=178,height='+(this.time_comp ? 220 : 200)+
			',status=no,resizable=no,top='+top_w+',left='+left_w+',dependent=yes,alwaysRaised=yes'
		);
	}
	this.obj_calwindow.opener = window;
	this.obj_calwindow.focus();
}

// timestamp generating function
function cal_gen_tsmp(dt_datetime) {
	return(this.gen_date(dt_datetime) + ' ' + this.gen_time(dt_datetime));
}

// date generating function
function cal_gen_date(dt_datetime) {
	return (
		(dt_datetime.getDate() < 10 ? '0' : '') + dt_datetime.getDate() + "/"
		+ (dt_datetime.getMonth() < 9 ? '0' : '') + (dt_datetime.getMonth() + 1) + "/"
		+ (dt_datetime.getFullYear()+this.add_year)
	);
}
// time generating function
function cal_gen_time(dt_datetime) {
	return (
		(dt_datetime.getHours() < 10 ? '0' : '') + dt_datetime.getHours() + ":"
		+ (dt_datetime.getMinutes() < 10 ? '0' : '') + (dt_datetime.getMinutes()) + ":"
		+ (dt_datetime.getSeconds() < 10 ? '0' : '') + (dt_datetime.getSeconds())
	);
}

// timestamp parsing function
function cal_prs_tsmp(str_datetime) {
	// if no parameter specified return current timestamp
	if (!str_datetime)
		return (new Date());

	// if positive integer treat as milliseconds from epoch
	if (RE_NUM.exec(str_datetime))
		return new Date(str_datetime);
		
	// else treat as date in string format
	var arr_datetime = str_datetime.split(' ');
	return this.prs_time(arr_datetime[1], this.prs_date(arr_datetime[0]));
}

// date parsing function
function cal_prs_date(str_date) {

	var arr_date = str_date.split('/');

	if (arr_date.length != 3) return cal_error ("Invalid date format: '" + str_date + "'.\nFormat accepted is dd-mm-yyyy.");
	if (!arr_date[0]) return cal_error ("Invalid date format: '" + str_date + "'.\nNo day of month value can be found.");
	if (!RE_NUM.exec(arr_date[0])) return cal_error ("Invalid day of month value: '" + arr_date[0] + "'.\nAllowed values are unsigned integers.");
	if (!arr_date[1]) return cal_error ("Invalid date format: '" + str_date + "'.\nNo month value can be found.");
	if (!RE_NUM.exec(arr_date[1])) return cal_error ("Invalid month value: '" + arr_date[1] + "'.\nAllowed values are unsigned integers.");
	if (!arr_date[2]) return cal_error ("Invalid date format: '" + str_date + "'.\nNo year value can be found.");
	if (!RE_NUM.exec(arr_date[2])) return cal_error ("Invalid year value: '" + arr_date[2] + "'.\nAllowed values are unsigned integers.");

	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[1] < 1 || arr_date[1] > 12) return cal_error ("Invalid month value: '" + arr_date[1] + "'.\nAllowed range is 01-12.");
	dt_date.setMonth(arr_date[1]-1);
	 
	if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	dt_date.setFullYear(arr_date[2]);

	var dt_numdays = new Date(arr_date[2], arr_date[1], 0);
	dt_date.setDate(arr_date[0]);
	if (dt_date.getMonth() != (arr_date[1]-1)) return cal_error ("Invalid day of month value: '" + arr_date[0] + "'.\nAllowed range is 01-"+dt_numdays.getDate()+".");

	return (dt_date)
}

// time parsing function
function cal_prs_time(str_time, dt_date) {

	if (!dt_date) return null;
	var arr_time = String(str_time ? str_time : '').split(':');

	if (!arr_time[0]) dt_date.setHours(0);
	else if (RE_NUM.exec(arr_time[0]))
		if (arr_time[0] < 24) dt_date.setHours(arr_time[0]);
		else return cal_error ("Invalid hours value: '" + arr_time[0] + "'.\nAllowed range is 00-23.");
	else return cal_error ("Invalid hours value: '" + arr_time[0] + "'.\nAllowed values are unsigned integers.");
	
	if (!arr_time[1]) dt_date.setMinutes(0);
	else if (RE_NUM.exec(arr_time[1]))
		if (arr_time[1] < 60) dt_date.setMinutes(arr_time[1]);
		else return cal_error ("Invalid minutes value: '" + arr_time[1] + "'.\nAllowed range is 00-59.");
	else return cal_error ("Invalid minutes value: '" + arr_time[1] + "'.\nAllowed values are unsigned integers.");

	if (!arr_time[2]) dt_date.setSeconds(0);
	else if (RE_NUM.exec(arr_time[2]))
		if (arr_time[2] < 60) dt_date.setSeconds(arr_time[2]);
		else return cal_error ("Invalid seconds value: '" + arr_time[2] + "'.\nAllowed range is 00-59.");
	else return cal_error ("Invalid seconds value: '" + arr_time[2] + "'.\nAllowed values are unsigned integers.");

	dt_date.setMilliseconds(0);
	return dt_date;
}

function cal_error(str_message) {
	alert (str_message);
	return null;
}
// ตรวจสอบ บัตรประชาชน
function isValidIdNr(objId) {
	var IntTotal=0;
	var IntCheckDigit;
	var IntDigitThirteen ;
	var CharDigit ;
	var N = 13;
	var i = 1;
	var IntTemp ;
	if(objId.value == "") {
		return true
	}else if(objId.length < 13 || objId.length > 13){
		return false
	}else{
		IntDigitThirteen = parseInt(objId.substr(12, 1))
	   
		for( i = 1 ; i <= 12; i++){
			IntTotal = IntTotal + (parseInt(objId.substr(i-1, 1)) * N);
			N = N - 1;
		}
		IntTemp = IntTotal % 11
		switch ( IntTemp){
			case 0 : IntCheckDigit = 1;break;
			case 1 : IntCheckDigit = 0;break;
			default : IntCheckDigit = 11 - IntTemp;break;
		}
		if (IntDigitThirteen != IntCheckDigit){
			return false
		}
		return true
	}
}

function getObjectRef(name) {
	if(document.getElementById)
		return document.getElementById(name);
	else if(document.all) 
		return document.all[name];
	else 
		return null;
}
// CSS
function bgChang(color){
	this.css.backgroundColor=color; 
	this.css.bgColor=color; 
	this.css.background=color;
}
// general functions to assist the script
//========================================
function show(name) {
	var el = getObjectRef(name);
	if(el) 
		el.style.visibility = "visible";
}
function hide(name) {
	var el = getObjectRef(name);
	if(el) 
		el.style.visibility = "hidden";
}
function getWidth(name) {
	var el = getObjectRef(name);
	return el.offsetWidth;
}
function getHeight(name) {
	var el = getObjectRef(name);
	return el.offsetHeight;
}
function moveMeTo(name,x,y) {
	var el = getObjectRef(name);
	if(el) { 
		el.style.top = parseInt(y); 
		el.style.left = parseInt(x); 
	}  
}
function moveMeBy(name,x,y) {
	var el = getObjectRef(name);
	if(el) { 
		el.style.top = parseInt(el.style.top) + parseInt(y); 
		el.style.left = parseInt(el.style.left) + parseInt(x); 
	}  
}
// Math functions
//================
var pi = Math.PI;
function square(x) { 
	return (x*x);
}
function sqrt(x) { 
	return Math.sqrt(x); 
}
function round(x) { 
	return Math.round(x); 
}
function rand(x,y) { 
	return (round(Math.random()*(y-x)) + x); 
}
function cos(x) { 
	return Math.cos(x)
 }
function sin(x) { 
	return Math.sin(x) 
}
function degToRad(x) { 
	return ( x/(360/(2*pi)) ); 
}
function radToDeg(x) { 
	return ( x*(360/(2*pi)) );
}
function atan(s,t) {
	if( s == 0.0 && t > 0.0)
		angle = 90.0;
	else if(s == 0.0 && t < 0.0) 
		angle = 270.0;
	else if (s < 0.0 ) 
		angle = 180.0 + radToDeg(Math.atan(t/s));
	else if (s > 0.0 && t < 0.0)
		angle = 360.0 + radToDeg(Math.atan(t/s));
	else {
		if(s==0.0) s=0.00001;
			angle = radToDeg(Math.atan(t/s));
	}
	if(angle < 0.0) angle += 360.0;
		return angle;
}
////////// key event ///////////
function isKeyPass(){
	if (event.keyCode < 48 || event.keyCode > 57  && event.keyCode < 65 || event.keyCode > 90  && event.keyCode < 97 || event.keyCode> 122)
	{event.returnValue = false} 
}
function isKeyEngOnly(){
	if (!(event.keyCode < 3585 || event.keyCode > 3652))
	{event.returnValue = false} 
}
function isKeyNumber(){
	if (event.keyCode < 48 || event.keyCode > 57 )
	{event.returnValue = false} 
}
///////////////////////////////
function showTitleMessage(msg, chk){
	document.getElementById('tiMssg').innerHTML = msg;
	if(chk){
		document.getElementById('tiMssg').style.color = "#000000";
	}else{
		document.getElementById('tiMssg').style.color = "#FF0000";
	}
}
function showMessage(mssg, obj){
	if(document.getElementById(obj.id+"_label")!=null)
	document.getElementById(obj.id+"_label").style.color = "#FF0000";
	alert(mssg);
	document.getElementById(obj.id).focus();
	return "<br/>"+"- <a class=\"error\" href=\"javascript:document.getElementById(\'"+obj.id+"\').focus();void(0);\">"+mssg+"</a>";
}

function resetObject(obj){
	//obj.style.border = "1px solid #666666";
	document.getElementById(obj.id+"_label").style.color = "#000000";
	return obj;
}
//////////////////////////////
function selectItem(select_list01,select_list02){
	if(document.getElementById(select_list01).length>0){
		for(var i=0; i<document.getElementById(select_list01).length ;i++){
			if(document.getElementById(select_list01).options[i].selected){
				var newElem = document.createElement("option");
				newElem.text = document.getElementById(select_list01).options[i].text;
				newElem.value = document.getElementById(select_list01).options[i].value;
				document.getElementById(select_list02).options.add(newElem);
			}
		}
		for(var i=document.getElementById(select_list01).length-1; i>=0 ;i--){
			if(document.getElementById(select_list01).options[i].selected){
				document.getElementById(select_list01).options.remove(i);
			}
		}
	}
}
function cancelItem(select_list01,select_list02){
	if(document.getElementById(select_list02).length>0){
		for(var i=0; i<document.getElementById(select_list02).length ;i++){
			if(document.getElementById(select_list02).options[i].selected){
				var newElem = document.createElement("option");
				newElem.text = document.getElementById(select_list02).options[i].text;
				newElem.value = document.getElementById(select_list02).options[i].value;
				document.getElementById(select_list01).options.add(newElem);
			}
		}
		for(var i=document.getElementById(select_list02).length-1; i>=0 ;i--){
			if(document.getElementById(select_list02).options[i].selected){
				document.getElementById(select_list02).options.remove(i);
			}
		}
	}
}

function selectAllItem(select_list01,select_list02){
	if(document.getElementById(select_list01).length>0){
		for(var i=0; i<document.getElementById(select_list01).length ;i++){
			var newElem = document.createElement("option");
			newElem.text = document.getElementById(select_list01).options[i].text;
			newElem.value = document.getElementById(select_list01).options[i].value;
			document.getElementById(select_list02).options.add(newElem);
		}
		document.getElementById(select_list01).length=0;
	}
}

function cancelAllItems(select_list01,select_list02){
	if(document.getElementById(select_list02).length>0){
		for(var i=0; i<document.getElementById(select_list02).length ;i++){
			var newElem = document.createElement("option");
			newElem.text = document.getElementById(select_list02).options[i].text;
			newElem.value = document.getElementById(select_list02).options[i].value;
			document.getElementById(select_list01).options.add(newElem);
		}
		document.getElementById(select_list02).length=0;
	}
}

function isValChkboxLst(cbl,count) {
    var checked = false;
    var table = document.getElementById(cbl);  
    for (i = 0; i<count; i++){
        if (document.getElementById(cbl+'_'+i).type.toUpperCase() == "CHECKBOX" && document.getElementById(cbl+'_'+i).checked == true){
			checked=true;
			break;
		}
	}
	return checked  
}

function isValRadioLst(rdo,count) {
    var checked = false;
    var table = document.getElementById(rdo);
    for (i = 0; i<count; i++){
        if (document.getElementById(rdo+'_'+i).type.toUpperCase() == "RADIO" && document.getElementById(rdo+'_'+i).checked == true){
			checked=true;
			break;
		}
	}
	return checked  
}