projectPath = "CPD/"
'projectPath = "rcpt/CPD/"

sub showLog(pTable,pKey_iden) 
	ret=showDialog("menu/log/Log-v2.asp?table_name=" & pTable & "&Key_iden=" & pKey_iden,800,550,"Log")
'	alert(pTable + ":" + pKey_iden)
end sub

sub showLog_byParent(pTable, pParent_Field_Name, pParent_iden) 
'Use Parent Key for identifier
	ret=showDialog("menu/log/Log-v2-byparent.asp?table_name=" & pTable & "&parent_field_name=" & pParent_Field_Name & "&Parent_iden=" & pParent_iden,800,550,"Log by Parent Key")
'	alert(pTable + ":" + pKey_iden)
end sub

public sqlPrint
'----- pRpt_Name = ชื่อไฟล์ report.rpt -----
'----- pParam = parameter ขั้นด้วย |--------
'----- pSql = sql update การ print --------
sub showReport(pRpt_Name,pParam)
'	sqlPrint = pSql
	call window.open("/crystal_report/viewRPT.asp?rpt=" & projectPath & "/" & pRpt_Name & "&param=" & escape(pParam),"_blank","width=750,height=550,toolbar=0,resizable=1 ")
end sub
sub showReportSession(pRpt_Name)
'	sqlPrint = pSql
	call window.open("/" & projectPath & "/menu/rptParam.asp?rpt=" & projectPath & "/" & pRpt_Name ,"_blank","width=750,height=550,toolbar=0,resizable=1 ")
end sub

sub showReportView(pRpt_Name,pParam)
	call window.open("/crystal_report/viewRPT.asp?rpt=" & projectPath & "/" & pRpt_Name & "&view=T&param=" & escape(pParam),"_blank","width=750,height=550,toolbar=0,resizable=1")
end sub
dim oPopup
sub showWait() 
set oPopup = window.createPopup()
	if trim(flagWait)="" then
		
		wStyle="BORDER-RIGHT: black 1px ridge;BORDER-TOP: black 1px ridge;BORDER-LEFT: black 1px ridge;BORDER-BOTTOM: black 1px ridge;BACKGROUND-COLOR: #FCDA68;" & _
				" PADDING-TOP: 5px;PADDING-BOTTOM: 5px;PADDING-LEFT: 5px;PADDING-RIGHT: 5px;" & _
				"   FONT-SIZE: 8pt;FONT-FAMILY: 'MS Sans Serif';"
			oPopup.document.body.innerHTML = "<div style='" & wStyle &  "' ><b>Please wait</b> ... <img src='/" & projectPath & "/images/loadingbar.gif' border=0></div>"
		set popupBody = oPopup.document.body
		call oPopup.show(0, 0, 220, 0)
		realHeight = popupBody.scrollHeight
		lefter=(document.body.scrollWidth/2)-(popupBody.scrollWidth/2)
		topper = (document.body.offsetHeight/3)-(popupBody.scrollHeight/2) 
		call oPopup.show(lefter, topper, popupBody.scrollWidth, realHeight, document.body)
	end if
end sub

sub msgShow(pMsg)
	call msgbox(" " & pMsg & "                ",64,"ข่าวสาร")
end sub

'----------------- จัดรูปแบบค่าตัวเลขจำนวนเงิน ------------------------------

function showMoney(pValue,pDigit)
	dim nValue
	nValue=pValue
	if(not isnull(pValue)  and trim(pValue)<>"") then
		if(isnumeric(pValue)) then
			if ccur(nValue) <> 0 then 
				nValue=formatnumber(pValue,pDigit,-1)
			else
				nValue = "-"
			end if
		end if
	end if
	showMoney=nValue
end function

function showDialog_tool(fName,width,height,pTitle) 
	dim fnameArr,arg,ret
	fnameArr=split(fName,"?")
	if ubound(fnameArr)>0 then
		arg=fnameArr(1)
	else
		arg=""
	end if
	ret = window.showModalDialog("/" & projectPath & "/menu/p-dialog_tool.asp?title=" & pTitle & "&src=" + fnameArr(0) & "&" & arg,"","scroll:no;dialogHeight: " & height & "px; dialogWidth: " & width & "px; center: Yes; help: No; resizable: Yes; status: No;")
	if trim(ret)="time_out" then
		window.location.reload 
	else
		showDialog_tool = ret
	end if
end function

function showDialog(fName,width,height,pTitle) 
	dim fnameArr,arg,ret
	fnameArr=split(fName,"?")
	if ubound(fnameArr)>0 then
		arg=fnameArr(1)
	else
		arg=""
	end if
	ret = window.showModalDialog("/" & projectPath & "/p-dialog.asp?title=" & pTitle & "&src=" + fnameArr(0) & "&" & arg,"","scroll:no;dialogHeight: " & height & "px; dialogWidth: " & width & "px; center: Yes; help: No; resizable: Yes; status: No;")
	if trim(ret)="time_out" then
		window.location.reload 
	else
		showDialog = ret
	end if
end function

function showDialog_option(fName,width,height,pTitle) 
	dim fnameArr,arg,ret
	fnameArr=split(fName,"?")
	if ubound(fnameArr)>0 then
		arg=fnameArr(1)
	else
		arg=""
	end if
	ret = window.showModalDialog("/" & projectPath & "/menu/p-dialog.asp?title=" & pTitle & "&src=" + fnameArr(0) & "&" & arg,"","scroll:yes;dialogHeight: " & height & "px; dialogWidth: " & width & "px; center: Yes; help: No; resizable: Yes; status: Yes;")
	if trim(ret)="time_out" then
		window.location.reload 
	else
		showDialog = ret
	end if
end function

function selectColor(pColor) 
	selectColor=window.showModalDialog("/" & projectPath & "/menu/color.htm?" & pColor ,"","scroll:no;dialogHeight: 170px; dialogWidth: 220px; center: Yes; help: No; resizable: Yes; status: No;")
end function

function checkDateMinMax(pDay,pMonth,pYear,pMin,pMax,pTitle,pFlag)
	dim pDate
	pFlag  = checkDate_require(pDay,pMonth,pYear,pTitle,pFlag)
	if pFlag=true then
		pDate = dateserial(pYear.value,pMonth.value,pDay.value)
		if not isnull(pMin) and not isnull(pMax) then 
			if pDate < pMin or pDate > pMax then
				alert(pTitle & "ต้องอยู่ในช่วง " & day(pMin) & "/" &  month(pMin) & "/" &  year(pMin)+543 & " ถึง " &  day(pMax) & "/" &  month(pMax) & "/" &  year(pMax)+543  )
				pDay.focus
				pFlag = false
			end if
		elseif isnull(pMax) then 
			if pDate < pMin  then
				alert(pTitle & "ต้องมากกว่าหรือเท่ากับ " & day(pMin) & "/" &  month(pMin) & "/" &  year(pMin)+543)
				pDay.focus
				pFlag = false
			end if
		elseif isnull(pMin) then 
			if  pDate > pMax then
				alert(pTitle & "ต้องน้อยกว่าหรือเท่ากับ " &  day(pMax) & "/" &  month(pMax) & "/" &  year(pMax)+543  )
				pDay.focus
				pFlag = false
			end if
		end if		
		
	end if
	checkDateMinMax = pFlag
end function


function checkNumber_not_require_sub(pObj,pName,pMin,pMax,pFlag)
	if(pFlag=true) then
		
		if trim(pObj.value) <> "" then
			if (not isnumeric(pObj.value))	then 
				alert("ระบุ" & pName & " ให้ถูกต้อง")
				if pObj.type="text" and pObj.disabled = false then
					pObj.focus()
				end if
				pFlag=false
			else
				if isnumeric(pMin) then
					if(ccur(pObj.value)<ccur(pMin)) then
						alert(pName & " ต้องมากกว่าหรือเท่ากับ " & pMin)
						if pObj.type="text" and pObj.disabled = false then
							pObj.focus()
						end if
						pFlag=false
					end if
				end if
				if pFlag=true then
					if isnumeric(pMax) then
						if(ccur(pObj.value)>ccur(pMax)) then
							alert(pName & "ต้องน้อยกว่าหรือเท่ากับ " & pMax)
							if pObj.type="text" and pObj.disabled = false then
								pObj.focus()
							end if
							pFlag=false
						end if
					end if
				end if
			end if
		end if
	end if
	checkNumber_not_require_sub=pFlag
end function


function checkNumber_not_require(pObj,pName,pMin,pMax,pFlag)
	dim i
	if(pFlag=true) then
			select case checkArray(pObj)			
				case 0
				case 1
					pFlag=checkNumber_not_require_sub(pObj,pName,pMin,pMax,pFlag)
				case else
					for each  i in pObj 
						pFlag=checkNumber_not_require_sub(i,pName,pMin,pMax,pFlag)
						if pFlag=false then
							exit for
						end if
					next
			end select
	end if
	checkNumber_not_require=pFlag
end function

function checkNumber(pObj,pName,pMin,pMax,pFlag)
	if(pFlag=true) then
		if (not isnumeric(pObj.value))	then 
			alert("โปรดระบุ " & pName & " ให้ถูกต้อง")
			if pObj.type="text" and pObj.disabled = false then
					pObj.focus()
			end if
			pFlag=false
		else
			if isnumeric(pMin) then
				if(cdbl(pObj.value)<cdbl(pMin)) then
					alert(pName & "ต้องมากกว่าหรือเท่ากับ " & pMin)
					if pObj.type="text" and pObj.disabled = false then
						pObj.focus()
					end if
					pFlag=false
				end if
			end if
			if pFlag=true then
				if isnumeric(pMax) then
					if(cdbl(pObj.value)>cdbl(pMax)) then
						alert(pName & "ต้องน้อยกว่าหรือเท่ากับ " & pMax)
						if pObj.type="text" and pObj.disabled = false then
							pObj.focus()
						end if
						pFlag=false
					end if
				end if
			end if
		end if
	end if
	checkNumber=pFlag
end function
function checkString(pObj,pName,pFlag)
	if(pFlag=true and trim(pObj.value)="")	then 
			alert("โปรดระบุ " & pName)
			pObj.focus()
			pFlag=false
	end if
	checkString=pFlag
end function
function checkOption(pObj,pName,pFlag)
	if(pFlag=true and pObj.options(0).selected)	then 
			alert("โปรดระบุ " & pName)
			pObj.focus()
			pFlag=false
	end if
	checkOption=pFlag
end function
function checkURL(pObj,pName,pFlag)
	if(pFlag=true and trim(pObj.value)="")	then 
			alert("โปรดระบุ " & pName)
			pObj.focus()
			pFlag=false
	end if
	if(pFlag=true and ucase(left(trim(pObj.value),7)) <> "HTTP://")	then 
			alert("รูปแบบของ " & pName & " ไม่ถูกต้อง")
			pObj.focus()
			pFlag=false
	end if
	checkURL=pFlag
end function

function checkTextArea(pObj,pName,pLength,pFlag,pRequire)
	if(pRequire=true and trim(pObj.value)="" and pFlag=true)	then 
			alert("โปรดระบุ " & pName)
			pObj.focus()
			pFlag=false
	end if
	if(pFlag=true and len(trim(pObj.value))>pLength)	then 
			alert(pName & " ต้องไม่เกิน " & pLength & " ตัวอักษร " & vbCrlf & "ขณะนี้มีจำนวน " & len(trim(pObj.value)) & " ตัวอักษร")
			pObj.focus()
			pFlag=false
	end if

	checkTextArea=pFlag
end function
function checkEmail(pObj,pName,pFlag)
	if(pFlag=true and trim(pObj.value)="")	then 
			alert("โปรดระบุ " & pName)
			pObj.focus()
			pFlag=false
	end if
	if(pFlag=true and (instr(1,trim(pObj.value),"@")<=1 or right(trim(pObj.value),1)="@") )	then 
			alert("รูปแบบของ " & pName & " ไม่ถูกต้อง")
			pObj.focus()
			pFlag=false
	end if
	checkEmail=pFlag
end function
sub f_formatnumber(pObj,pDigit)
	if isnumeric(pObj.value) then
		pObj.value=formatnumber(pObj.value,pDigit,-1)
	end if
end sub
function n_formatnumber(pValue,pDigit)
	if isnumeric(pValue) then
		n_formatnumber=formatnumber(pValue,pDigit,-1)
	else
		n_formatnumber=pValue
	end if
end function
function n_formatnumberAll(pValue)
	dim nValue
	nValue=pValue
		if left(nValue,1)="." then
			nValue = "0" & nValue 
		end if
	n_formatnumberAll=nValue
end function


function checkRadio(pObj,pName,pFlag)
	dim i
on error resume next
	if(pFlag=true) then
			pFlag=false
			select case checkArray(pObj)			
				case 0
				case 1
					if pObj.checked then
						pFlag=true
					else
						alert("โปรดระบุ " & pName )
						if pObj.disabled = false then
							pObj.focus()
						end if
					end if
				case else
					for each  i in pObj 
						if i.checked then
							pFlag=true
							exit for
						end if
					next
					if pFlag=false then
						alert("โปรดระบุ " & pName )
						if pObj(0).disabled = false then
							pObj(0).focus()
						end if
					end if
			end select
	end if
	checkRadio=pFlag
end function
function calAmount(pObjPercent,pObjAmount,pPrice)
		calAmount=false
		if not isnumeric(pPrice) then
			alert("ไม่พบข้อมูลราคา")
			exit function
		end if
		if(isnumeric(pObjPercent.value)) then
			pObjPercent.value=n_formatnumber(pObjPercent.value,2)
			if(cdbl(pObjPercent.value<0) or cdbl(pObjPercent.value>100)) then
				alert("เปอร์เซ็นส่วนลดต้องอยู่ในช่วง 0 - 100")
				if pObjPercent.type="text" and pObjPercent.disabled = false then
					pObjPercent.focus
				end if
			else
				calAmount=true
				if(cdbl(pPrice)>0) then
					pObjAmount.value=n_formatnumber(((pObjPercent.value)*pPrice)/100,2)
					pObjAmount.value=n_formatnumber(pObjAmount.value,2)
					pObjPercent.value=n_formatnumber(((pObjAmount.value)/pPrice)*100,2)			
				else
					pObjAmount.value=n_formatnumber(0,2)
					pObjPercent.value=n_formatnumber(0,2)			
				end if

			end if
		else
			pObjAmount.value = ""
'			alert("เปอร์เซ็นส่วนลดต้องเป็นตัวเลข")
'			if pObjPercent.type="text" and pObjPercent.disabled = false then
'				pObjPercent.focus
'			end if
		end if
end function

function calPercent(pObjPercent,pObjAmount,pPrice)
		calPercent=false
		if not isnumeric(pPrice) then
			alert("ไม่พบข้อมูลราคา")
			exit function
		end if
		if(isnumeric(pObjAmount.value)) then
			pObjAmount.value=n_formatnumber(pObjAmount.value,2)
			if(cdbl(pObjAmount.value<0) or cdbl(pObjAmount.value)>cdbl(pPrice)) then
				alert("ส่วนลดต้องมากกว่าหรือเท่ากับ 0  และต้องน้อยกว่าหรือเท่ากับราคา (" & n_formatnumber(pPrice,2) & " บาท)")
				if pObjAmount.type="text" and pObjAmount.disabled = false then
					pObjAmount.focus()
				end if
			else
				calPercent=true
				if(cdbl(pPrice)>0) then
					pObjPercent.value=n_formatnumber(((pObjAmount.value)/pPrice)*100,2)
				else
					pObjPercent.value=n_formatnumber(0,2)
				end if
			end if
		else
'			alert("ส่วนลดต้องเป็นตัวเลข")
'			if pObjAmount.type="text" and pObjAmount.disabled = false then
'				pObjAmount.focus()
'			end if
		end if
end function


function getAtb(pObj,pAtb)
	getAtb=pObj.options(pObj.selectedIndex).getAttribute(pAtb)
end function

function checkDate_require(pObj_day,pObj_month,pObj_year,pName,pFlag)
	pFlag=checkString(pObj_day,pName,pFlag)
	pFlag=checkString(pObj_month,pName,pFlag)
	pFlag=checkString(pObj_year,pName,pFlag)
	if pFlag=true  then
		checkDate_require=checkDate(pObj_day,pObj_month,pObj_year,pName,pFlag)
	end if
	checkDate_require = pFlag
end function

function checkDate_notrequire(pObj_day,pObj_month,pObj_year,pName,pFlag)
	if pFlag AND (trim(pObj_day.value) <> "" OR trim(pObj_month.value) <> "" OR trim(pObj_year.value) <> "") then
		pFlag=checkString(pObj_day,pName,pFlag)
		pFlag=checkString(pObj_month,pName,pFlag)
		pFlag=checkString(pObj_year,pName,pFlag)
		if pFlag=true  then
			pFlag = checkDate(pObj_day,pObj_month,pObj_year,pName,pFlag)
		end if
	end if
	
	checkDate_notrequire = pFlag
end function

function checkTime(pObj,pName,pFlag)
	dim tmpArr
	pFlag=checkString(pObj,pName,pFlag)
	if pFlag=true  then
		pObj.value = replace(pObj.value,".",":")
		tmpArr = split(pObj.value,":")
		if ubound(tmpArr)<>1 then
			pFlag = false
			alert("โปรดระบุ " & pName & "ให้ถูกต้อง HH:MM")
			pObj.focus
		else
			if isnumeric(tmpArr(0)) and isnumeric(tmpArr(1)) and isdate(pObj.value) then
				pObj.value = formatdatetime(pObj.value,vbShortTime)
			else
				pFlag = false
				alert("โปรดระบุ " & pName & "ให้ถูกต้อง HH:MM")
				pObj.focus
			end if
		end if
	end if
	checkTime=pFlag
end function

function checkDate(pObj_day,pObj_month,pObj_year,pName,pFlag)
	if pFlag=true and (isnumeric(pObj_day.value) or isnumeric(pObj_month.value) or isnumeric(pObj_year.value)) then
		
		if isnumeric(pObj_day.value) and isnumeric(pObj_month.value) and isnumeric(pObj_year.value)	then 
			if cint(pObj_day.value)<>day(dateserial(pObj_year.value,pObj_month.value,pObj_day.value)) then
				alert("ระบุ" & pName & " ให้ถูกต้อง")
				pObj_day.focus
				pFlag=false
			end if
		else
			pFlag=checkString(pObj_day,pName & " (วัน)",pFlag)
			pFlag=checkString(pObj_month,pName & " (เดือน)",pFlag)
			pFlag=checkString(pObj_year,pName & " (ปี)",pFlag)
		end if
	end if
	checkDate = pFlag
end function
'----------- เอาใว้เลือก checkbox ทั้งหมด -------------------
sub checkCheckBoxAll(pThis,pObj)
	select case checkArray(pObj)
		case 0
			
		case 1
			pObj.checked = pThis.checked
		case else
			dim i
			for each i in pObj
				i.checked = pThis.checked
			next
	end select
end sub
