<!--
/************************************************************************/
// Helper Functions
// By...
//	    Zico - Internet Solutions, MWEB
/************************************************************************/

/*************************** String Helper functions ***************************/

function IsEmailCorrect(szEmail) {
	var i, j;
	szEmail = Trim(szEmail);
	if((i=szEmail.indexOf("@")) <= 0)
		return false;
	if((j=szEmail.substring(i + 1).indexOf(".")) == -1)
		return false;
	j += i + 1;
	if(szEmail.substring(i + 1, j).length <= 0)
		return false;
	if(szEmail.substring(j + 1).length <= 0)
		return false;
	for(i=0; i<szEmail.length; i++) {
		if(szEmail.charAt(i) > 'z' || szEmail.charAt(i) < '-' || ("/:;<=>?[\\]^`".indexOf(szEmail.charAt(i)) != -1))
			return false;
	}
	return true;
}

function IsEmpty(szStr) {
	for(var i=0; i<szStr.length; i++) {
		if(szStr.charAt(i) == ' ')
			szStr = szStr.substring(i-- + 1, szStr.length);
		else
			break;
	}
	if(szStr.length)
		return false;
	else
		return true;
}

function IsAlphaNumeric(szStr) {
	szStr = szStr.toLowerCase();
	for(var i=0; i<szStr.length; i++) {
		if(!((szStr.charAt(i) >= 'a' && szStr.charAt(i) <= 'z') || (szStr.charAt(i) >= '0' && szStr.charAt(i) <= '9')))
			return false;
	}
	return true;
}

function IsNumber(szStr) {
	for(var i=0; i<szStr.length; i++) {
		if("0123456789+-.".indexOf(szStr.charAt(i)) == -1)
			return false;
	}
	return true;
}

function Trim(szStr) {
	return TrimLeft(TrimRight(szStr));
}

function TrimLeft(szStr) {
	for(var i=0; i<szStr.length; i++) {
		if(szStr.charAt(i) == ' ')
			szStr = szStr.substring(i-- + 1, szStr.length);
		else
			break;
	}
	return szStr;
}

function TrimRight(szStr) {
	for(var i=szStr.length - 1; i>=0; i--) {
		if(szStr.charAt(i) == ' ')
			szStr = szStr.substring(0, i);
		else
			break;
	}
	return szStr;
}


/*************************** Day Month and Year Helper functions ***************************/

/*
	Initialize Day, Month and Year comboboxes with Thai or English style and display the current
	day, month and year if necessary.
	Ex 1.. fill-up the day month and year comboboxes with the english style
		- InitDate(document.frm.cmbMonth, document.frm.cmbDay, document.frm.cmbYear, false, null);

	Ex 2.. fill-up the day month and year comboboxes with the thai style and display the current day month and year
		- InitDate(document.frm.cmbMonth, document.frm.cmbDay, document.frm.cmbYear, true, new Date());
*/
function InitDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault) {
	var i;
	var dt = new Date();
	var arrThMonth = new Array("���Ҥ�", "����Ҿѹ��", "�չҤ�", "����¹", "����Ҥ�", "�Զع�¹", "�á�Ҥ�", "�ԧ�Ҥ�", "�ѹ��¹", "���Ҥ�", "��Ȩԡ�¹", "�ѹ�Ҥ�");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	cmbMonth.options[0] = new Option(" -- ", 0);
	for(i=0; i<arrThMonth.length; i++) {
		cmbMonth.options[i + 1] = new Option((bThai)? arrThMonth[i]:arrEnMonth[i], i + 1);
		if(dtDefault != null)
			if(dtDefault.getMonth() + 1 == i)
				cmbMonth.selectedIndex = i;
	}

	cmbDay.options[0] = new Option(" -- ", 0);
	for(i=1; i<=31; i++) {
		cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			if(dtDefault.getDate() == i)
				cmbDay.selectedIndex = i;
	}

	cmbYear.options[0] = new Option(" -- ", 0);
	for(i=dt.getYear()-3; i<dt.getYear() + 3; i++) {
		cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, (bThai)? i + 543:i);
		if(dtDefault != null)
			if(dtDefault.getYear() == i)
				cmbYear.selectedIndex = cmbYear.options.length - 1;
	}
}

/*
	Validate the selected day month and year for leap year, worng date and correct them if necessary.
	Ex 1..
		- <select name="cmbDay" onChange="dateValidation(this, this.form.cmbMonth, this.form.cmbYear);"></select>
*/
function dateValidation(cmbDay, cmbMonth, cmbYear) {
	if(cmbDay.options.selectedIndex == 0 || cmbMonth.options.selectedIndex == 0 || cmbYear.options.selectedIndex == 0)
		return;

	var nDay = cmbDay.options[cmbDay.options.selectedIndex].value;
	var nMonth = cmbMonth.options[cmbMonth.options.selectedIndex].value;
	var nYear = cmbYear.options[cmbYear.options.selectedIndex].value;
	var nMaxDay = 0;

	if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
		nMaxDay = 30;
	else if(nMonth == 2) {
		if(nYear % 4 == 0 && (nYear % 100 != 0 || nYear % 400 == 0))
			nMaxDay = 29;
		else
			nMaxDay = 28;
	}
	else
		nMaxDay = 31;

	if(nDay > nMaxDay) {
		cmbDay.options.selectedIndex = nMaxDay;
	}
}

/*generage string for sql syntax */
/*   input "abc'def' output "abc''def''"     */
function chkSingleQuote(szString)
{
		var szTemp = "";
		var nLength = 0;
		
		szString = Trim(szString); 
		nLength = Trim(szString).length;
		
		if(szString.lastIndexOf("'")<0){
			szTemp = szString;
		} else {
			for (i=0;i<=nLength;i++){
				if(szString.charAt(i)=="'"){
					szTemp = szTemp + szString.substring(0,i+1) + "'";
					szString = szString.substring(i+1,szString.length);
					nLength = Trim(szString).length;
					i=0;
				}	
			}
		}
		return szTemp;
}

/*generate currency format */
function GetCurrency(szInput) {
	var sztmp;
	var szSubfix;
	var i, n;
	var bDot;

	if(szInput.length <= 3 || szInput.indexOf(",") != -1)
		return szInput;

	szSubfix = "";
	if((i = szInput.indexOf(".")) != -1) {
		bDot = true;
		szSubfix = szInput.substring(i + 1);
		szInput = szInput.substring(0, i);
		if(szSubfix.length >= 2)
			szSubfix = szSubfix.substring(0, 2);
		else
			szSubfix += "0";
	}
	else
		bDot = false;

	sztmp = "";
	n = szInput.length - 1;
	i = 0;
	while(n >= 0) {
		sztmp += szInput.charAt(n--);
		if(++i >= 3 && n >= 0) {
			sztmp += ",";
			i = 0;
		}
	}
	if(bDot)
		sztmp = ReverseString(sztmp) + "." + szSubfix;
	else
		sztmp = ReverseString(sztmp);
	return sztmp;
}

function ReverseString(szInput) {
	var sztmp;
	var i;

	i = 0;
	sztmp = "";
	for(i = szInput.length; i>=0; i--)
		sztmp += szInput.substring(i - 1, i);

	return sztmp;
}

function  Chk_DeleteId_char(frm,chkname) 
{ 
	var del_id = "";
	for (var i = 0; i < frm.elements.length; i++)  
	{ 
		if(frm.elements[i].name == chkname )	 
		{ 
			if (frm.elements[i].checked == true ) {
				if(frm.elements[i].value.length > 0) {
					del_id = del_id+"'"+frm.elements[i].value+"',";
				}
			}
		}       
	} 
	return del_id;
}	

function  Chk_DeleteId(frm,chkname) 
{ 
	var del_id = "";
	var nCount = 0;
	for (var i = 0; i < frm.elements.length; i++)  
	{ 
		if(frm.elements[i].name == chkname )	 
		{ 
			if (frm.elements[i].checked == true ) {
				if(frm.elements[i].value.length > 0) {
					if(nCount > 0) {
						del_id = del_id+	",";
					}
					del_id = del_id+frm.elements[i].value;
					nCount++;
				}
			}
		}       
	} 
	return del_id;
}	

function Chk_Count(frm,chkname)
{
	var count = 0;
	for (var i = 0; i < frm.elements.length; i++)  
	{ 
		if(frm.elements[i].name == chkname )	 
		{ 
			if (frm.elements[i].checked == true ) {
				if(frm.elements[i].value.length > 0) {
					count++;
				}
			}
		}       
	} 
	return count;
}

function searchStock(frm)
{
	frm.submit();	
}

function setSelectObject(obj){
	with(document.cscform){
		obj.select();
	}
}

function setFocusObject(obj){
	with(document.cscform){
		obj.focus();
	}
}

function setTextValue(obj){
    with(document.cscform){
		obj.value = " ";
	}
}

function setTextValue(obj,replace){
	if(replace == undefined){ replace = " "; }
    with(document.cscform){
		obj.value = replace;
	}
}

function Replace(source,oldword,newword){
	var index = 0;
	var first = "";
	var last = "";
	while(source.indexOf(oldword) > -1){
		index = source.indexOf(oldword);
		first = source.substring(0,index);
		last = source.substring(index+oldword.length,source.length);
		source = first + newword + last;
	}
	return source;
}

function addDate(olddate,delim,oldform,type,amount){
	var tempFormat = oldform.split(delim);
	var tempDate = olddate.split(delim);
	var newDate = "";
	for(i=0;i<tempFormat.length;i++){
		if(type == tempFormat[i]){
			tempDate[i] = parseInt(tempDate[i]) + parseInt(amount);
		}
		newDate += tempDate[i] + "/";
	}
	newDate = newDate.substring(0,newDate.length-1);	
	return newDate;
}

function subDate(olddate,delim,oldform,type,amount){
	var tempFormat = oldform.split(delim);
	var tempDate = olddate.split(delim);
	var newDate = "";
	for(i=0;i<tempFormat.length;i++){
		if(type == tempFormat[i]){
			tempDate[i] = parseInt(tempDate[i]) - parseInt(amount);
		}
		newDate += tempDate[i] + "/";
	}
	newDate = newDate.substring(0,newDate.length-1);	
	return newDate;
}

function changDateFormat(olddate,delim,oldform,newform){
	var tempFormat = oldform.split(delim);
	var tempDate = olddate.split(delim);
	for(i=0;i<tempFormat.length;i++){
		alert(tempFormat[i] + "-->" + tempDate[i]);
		newform = Replace(newform,tempFormat[i],tempDate[i]);
	}
	return newform;
}

function checkDateFormat(DateObj){
	var day,month,year;
	var cur = new Date();
	var curyear = String.valueOf(cur.getYear());
	alert(curyear);
	if(DateObj.value.length != 6){
		alert("��سҡ�͡�ѹ������١��ͧ (ddmmyy)");
		return ;
	} else {
		day = DateObj.value.substring(0,2);
		month = DateObj.value.substring(2,4);
		year = DateObj.value.substring(4,6);
		alert(day+"/"+month+"/"+curyear.substring(0,2)+year);
	}
}
//-->