/* **************************************************************************
*                                                                           *
*   This application is developed and owned by Thomas Idea Co.,Ltd.         *
*   Locus Telecommunication Inc.,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.                             *
*                                                                           *
****************************************************************************/
var fieldDate;
var specialCharList = "!@#$%^&*()+={}[]\\/\"><'?:;฿~,.|";
function genCalendar(field) {
	fieldDate = field
	win = window.open("../js/clndr.htm", "Calendar", "width=180, height=180, left=350, top=300")
	if ( win.opener == null )
		win.opener = self
	win.focus()
}

function genCalendar2(field) {
	fieldDate = field
	win = window.open("../js/clndr2.htm", "Calendar", "width=180, height=180, left=350, top=300")
	if ( win.opener == null )
		win.opener = self
	win.focus()
}

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 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 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 getFileType(fileSource) {
	return fileSource.substring(fileSource.lastIndexOf('.') + 1,fileSource.length);
}

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

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 showMessage(txt, obj) {
	alert(txt);
	obj.focus();
	return false;
}


function openWin(pages, winName, detail) {
	w = open (pages, winName, detail);
	if (w.opener == null)
		w.opener = self;
	w.focus();
}

function openerReload(formStr) {
	openerForm = eval("opener.document." + formStr);
	if (!isOpenerClose() && openerForm != null)
		openerForm.submit();
}

function isOpenerClose() {
	if (opener.closed)
		return true;
	else
		return false;
}

function isElement(formStr, elementStr) {
	if (!isOpenerClose()) {
		openerForm = eval("opener.document." + formStr);
		openerObject = eval("opener.document." + formStr + "." + elementStr);
		if ((openerForm == null) && (openerObject == null))
			return false;
		else
			return true;
	} else
		return false;
}

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

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

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

function isEmail(str) {
	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(frm, start, num, skip) {
	var result, isChecked;
	end = num * skip;
	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;
}

var imageFile = "Please select a correct file (gif, jpg, png, jpeg).";
function isImage(str) {
	ext = getFileType(str);
	if (ext != "gif" && ext != "jpg" && ext != "png" && ext != "jpeg") {
		return false
	}
	else
		return true;
}


var allowFileType = "gif, jpg, png, jpeg";
var allowFile = "Please select a correct file (" + allowFileType + ").";
function isAllowFile(str) {
	fileType = getFileType(str).toLowerCase();
	if (allowFileType.indexOf(fileType) >= 0)
		return true;
	else
		return false;
}

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

function isCorrectDateOrg(fromDateObj, toDateObj, strValue) {
	result = true;
	strValue1 = strValue.substring(0, 1).toUpperCase() + strValue.substring(1, strValue.length);
	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))
			return showMessage(strValue1 + " (Start) should be later than " + strValue + " (End).", toDateObj);
	}
	return result;
}

function isCorrectDate(fromDateObj, toDateObj, lblDate1, lblDate2) {
	result = true;
	strValue1 = lblDate1.substring(0, 1).toUpperCase() + lblDate1.substring(1, lblDate1.length);
	strValue2 = lblDate2.substring(0, 1).toUpperCase() + lblDate2.substring(1, lblDate2.length);
	if (isSpace(fromDateObj.value) && !isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue1 + " .", fromDateObj);
	else if (!isSpace(fromDateObj.value) && isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue2 + " .", toDateObj);
	else if (!isSpace(fromDateObj.value) && !isSpace(toDateObj.value)) {
		if (!isCorrectDuration(fromDateObj.value, toDateObj.value))
			return showMessage(strValue1 + " should be later than " + strValue2 + " .", toDateObj);
	}
	return result;
}

function isExpiredDate(fromDateObj, toDateObj, lblDate) {
	result = true;
	strValue = lblDate.substring(0, 1).toUpperCase() + lblDate.substring(1, lblDate.length);
	if (isSpace(fromDateObj.value) && !isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue + " .", fromDateObj);
	else if (!isSpace(fromDateObj.value) && isSpace(toDateObj.value))
		result = showMessage("Please input " + strValue + " .", toDateObj);
	else if (!isSpace(fromDateObj.value) && !isSpace(toDateObj.value)) {
		if (!isCorrectDuration(fromDateObj.value, toDateObj.value))
			return showMessage(strValue + " should be equal or more than current date.", toDateObj);
	}
	return result;
}

function hidestatus() {
	window.status = " ";
	return true;
}
if (document.layers)
	document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);

document.onmouseover = hidestatus;
document.onmouseout = hidestatus;

function selectAllCheckbox(frm) {
	for(i = 0; i < frm.length; i++) {
		if (frm.elements[i].name.toUpperCase() != "CHECKALL" && !frm.elements[i].disabled) {
				frm.elements[i].checked = frm.checkAll.checked;
		}
	}
}

function cancelCheckAll(frm, me) {
	if (me.checked == false)
		frm.checkAll.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) {
				secondDrop.options[indx] = new Option(arr[2][i], arr[1][i], false, false)
				indx++;
			}
		}
	}
	secondDrop.selectedIndex = 0;
}

function refreshSelectObj(rgnValue, secondDrop, arr, langFlg) {
	secondDrop.length = 0;
	var allStr = "";
	var indx = secondDrop.length;
	var count = arr[0].length;
	if (langFlg == "TH")
		allStr = "ทั้งหมด";
	else if (langFlg == "EN")
		allStr = "All";
	secondDrop.options[indx] = new Option(allStr, rgnValue, false, false)
	indx++;
	if (!isEmpty(rgnValue)) {
		for (var i=0; i<=count - 1; i++) {
			if (arr[0][i] == rgnValue) {
				secondDrop.options[indx] = new Option(arr[2][i], arr[1][i], false, false)
				indx++;
			}
		}
	}
	secondDrop.selectedIndex = 0;
}
