<!--
/************************************************************************/
// Helper Functions
// By...
//	    Zico - Internet Solutions, MWEB, Tuesday, December 05, 2000
// Modified:
//		+ April 24, 2001 by Wuttichai Nanekrangsan
//			- Add IsInteger
//			- Add IsDecimal
//		+ April 25, 2001 by Zico
//			- Change IsEmailCorrect function
//			- Add CountStr
//		+ April 27, 2001 by Wuttichai Nanekrangsan
//			- Add InitLongRangeDate -> used in credit apply, since year maybe rage upto 100 years
//		+ Wednesday, May 9, 2001 9:03 PM by Zico
//			- Add DMYFromNow
//			- Add DMYToNow
//		+ Thursday, May 10, 2001 10.54 AM by Wuttichai Nanekrangsan
//			- Added disable and enable and skip
//		+ Friday, May 18, 2001 6.47 PM by Wuttichai Nanekrangsan
//			- Added IsMonthYear
//		+ Thursday, May 24, 2001 2.45 PM by Wuttichai Nanekrangsan
//			- Added IsOnlyNumber -- no comma and dot allow
//		+ Tuesday, May 29, 2001 11.46 AM by Wuttichai Nanekrangsan
//			- Added special InitLongRangeDate named InitShorterLongRangeDate cuz html messed up in premium offer and policy service
/************************************************************************/

/*************************** String Helper functions ***************************/

function IsEmailCorrect(szEmail) {
	var i;
	var strtmp = Trim(szEmail);

	for (i=0; i<strtmp.length; i++) {
		if (strtmp.charAt(i) > 'z' || strtmp.charAt(i) < '-' || ("/:;<=>?[\\]^` ".indexOf(strtmp.charAt(i)) != -1))
			return false;
	}
	if (CountStr(strtmp, "@") != 1 || strtmp.indexOf("..") != -1)
		return false;

	if (strtmp.charAt(0) == '@' || strtmp.charAt(strtmp.length - 1) == '@')
		return false;

	while ((i = strtmp.indexOf(".")) != -1) {
		if (i <= 0 || i >= (strtmp.length - 1))
			return false;
		if (strtmp.charAt(i - 1) == '@' || strtmp.charAt(i + 1) == '@')
			return false;
		strtmp = strtmp.substring(i + 1);
	}

	return true;
}

// return true if szStr contains space or empty. Otherwise, return false
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 IsEngString(szStr) {
	szStr = szStr.toLowerCase();
	for(var i=0; i<szStr.length; i++) {
		if(!((szStr.charAt(i) >= 'a' && szStr.charAt(i) <= 'z') || szStr.charAt(i) == ' ' || szStr.charAt(i) == '.'))
			return false;
	}
	return true;
}

function IsThaiString(szStr) {
	for(var i=0; i<szStr.length; i++) {
		if(szStr.charCodeAt(i) <= 128 && szStr.charAt(i) != ' ')
			return false;
	}
	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') || ("0123456789+-.,".indexOf(szStr.charAt(i)) >= 0)))
			return false;
	}
	return true;
}

function IsNumeric(szStr) {
	for(var i=0; i<szStr.length; i++) {
		if("0123456789+-.,".indexOf(szStr.charAt(i)) == -1)
			return false;
	}
	return true;
}

function CountStr(strSrc, strFind) {
	var i, nCount = 0;

	while ((i = strSrc.indexOf(strFind)) != -1) {
		nCount++;
		strSrc = strSrc.substring(i + strFind.length, strSrc.length);
	}

	return nCount;
}

function Left(szStr, nChar)
{
	if(szStr.length > nChar)
		return szStr.substring(0, nChar);
	else
		return szStr
}

function Right(szStr, nChar)
{
	if(szStr.length > nChar)
		return szStr.substring(szStr.length - nChar, szStr.length);
	else
		return szStr;
}

// Because of the build-in function ".replace" cannot replace any string with the empty string
// the only way we can do is the following...
function RemoveStr(szStr, szRemove)
{
	var sztmp, i;

	while((i = szStr.indexOf(szRemove)) != -1) {
		sztmp = szStr.substring(0, i);
		sztmp += szStr.substring(i + szRemove.length, szStr.length);
		szStr = sztmp;
	}

	return szStr;
}

// Remove white space on the both side;
function Trim(szStr) {
	return TrimLeft(TrimRight(szStr));
}

// Remove white space on the left side;
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;
}

// Remove white space on the right side;
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;
}

// Is there the szStr contain any space ?
function ContainSpace(szStr) {
	if(szStr.indexOf(' ') != -1)
		return true;
	return false;
}

// Conver two or more space to the single space
function CompactSpace(szStr) {
	var nSpace = 0;
	var sztmp;
	for(var i=0; i<szStr.length; i++) {
		if(szStr.charAt(i) == ' ') {
			nSpace++;
		}
		else
			nSpace = 0;

		if(nSpace >= 2) {
			nSpace = 0;
			sztmp = szStr.substring(0, i);
			szStr = sztmp + szStr.substring(i + 1, szStr.length);
			i--;
		}
	}
	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, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		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) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option(" -- ", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option(" -- ", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-4; i<n + 3; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					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;
	}
}

/*
	Force the specified Day, Month and Year couldn't be selects date before the specified Date or Now!
	Ex ..
		- DMYFromNow(cmbDay, cmbMonth, cmbYear, new Date('Jan 1, 2000'));
		- DMYFromNow(cmbDay, cmbMonth, cmbYear, null);
*/
function DMYFromNow(cmbDay, cmbMonth, cmbYear, dtmComp) {
	if (cmbDay == null || cmbMonth == null || cmbYear == null)
		return;
	if (dtmComp == null)
		dtmComp = new Date();

	var dtmtmp = new Date(cmbYear.options[cmbYear.selectedIndex].value, cmbMonth.options[cmbMonth.selectedIndex].value - 1, cmbDay.options[cmbDay.selectedIndex].value);
	if (dtmtmp < dtmComp) {
		cmbDay.selectedIndex = dtmComp.getDate();
		cmbMonth.selectedIndex = dtmComp.getMonth() + 1;
		for (var i=0; i<cmbYear.options.length; i++) {
			if (cmbYear.options[i].value == dtmComp.getYear()) {
				cmbYear.selectedIndex = i;
				break;
			}
		}
	}
}

function DMYToNow(cmbDay, cmbMonth, cmbYear, dtmComp) {
	if (cmbDay == null || cmbMonth == null || cmbYear == null)
		return;
	if (dtmComp == null)
		dtmComp = new Date();

	var dtmtmp = new Date(cmbYear.options[cmbYear.selectedIndex].value, cmbMonth.options[cmbMonth.selectedIndex].value - 1, cmbDay.options[cmbDay.selectedIndex].value);
	if (dtmtmp > dtmComp) {
		cmbDay.selectedIndex = dtmComp.getDate();
		cmbMonth.selectedIndex = dtmComp.getMonth() + 1;
		for (var i=0; i<cmbYear.options.length; i++) {
			if (cmbYear.options[i].value == dtmComp.getYear()) {
				cmbYear.selectedIndex = i;
				break;
			}
		}
	}
}

/*
Get Max Day of the given month and year
*/
function GetMaxDay(nMonth, nYear) {
	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;

	return nMaxDay;
}

/*
format the given data in currency format ex. xx,xxx,xxx.xxx
*/
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;
}

/*
just reverse the given string (GetCurrency's helper function)
*/
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 DoOpenPicServer(picName)
{
  var winHandle = window.open("","picwin","resizable=yes,scrollbars=yes,status=yes,width=450,height=400")
  if(winHandle != null){
    var htmlString = "<html><head><title>Preview...</title></head>"  
          htmlString += "<body><center><img src='"+   picName + "'>"
          htmlString += "<form><input type=\"button\" value=\"  Close  \" onClick=\"window.close();\"></form></center></body></html>"
    winHandle.document.open()
    winHandle.document.write(htmlString)
    winHandle.document.close()
    }	
  //if(winHandle != null) winHandle.focus() //brings window to top
  //return winHandle 	  
}

function DoOpenPicLocal(picName)
{
     /*alert(picName);
     mywin = window.open(picName,"picwin","scrollbars=yes,status=yes,width=400,height=400");  */
	if ( picName == "" ) {
		alert("Please select the picture file first!");
		return ;
	}
	var strPic = '';
	//alert(picName);
   	for(var i=0; i<picName.length; i++) {
		   //alert( picName.charAt(i));
		   if(   picName.substring(i ,i+1)  == "\\"  ) 
		   {
		        strPic = strPic  + "/" ;
		   }
		   else

		    strPic = strPic + picName.charAt(i);
		
	} 

   //alert(strPic);

   var winHandle = window.open("","picwin","resizable=yes,scrollbars=yes,status=yes,width=400,height=400")
   if(winHandle != null){
    var htmlString = "<html><head><title>Preview...</title></head>"  
          htmlString += "<body><center><img src='file://"+   strPic + "'>"
          htmlString += "<form><input type=\"button\" value=\"  Close  \" onClick=\"window.close();\"></form></center></body></html>"
    winHandle.document.open()
    winHandle.document.write(htmlString)
    winHandle.document.close()
    } 	
}

function MM_openMailToWindow(theURL) { //v2.0
  var features = "width=577,height=382,screenx=0,screeny=0,top=0,left=0";
  var winName = "popmail";
  window.open(theURL,winName,features);
}

function IsInteger(szStr) {
	for(var i=0; i<szStr.length; i++) {
		// dot is not allow for integer
		if("0123456789+-,".indexOf(szStr.charAt(i)) == -1)
			return false;
	}
	return true;
}

function IsDecimal(szStr, leading, decimal) {
	var dotCount = 0;
	for(var i=0;i<szStr.length;i++) {
		if("0123456789+-.,".indexOf(szStr.charAt(i)) == -1) {
			return false;
		}
		else { // need to check if there are more than one dot
			if(".".indexOf(szStr.charAt(i)) != -1)
				dotCount++;
		}
	}

	if(dotCount > 1)  // more than one dot
		return false;

	if(decimal < 1 && dotCount > 0)
		return false;

	var dotIndex = szStr.indexOf(".");
	if(dotIndex == -1) { // no dot, consider only leading and leght of number
		if(szStr.length > leading)
			return false;
	}
	else {
		if(dotIndex > leading)  // integer number is more than leading allow
			return false;
		if((szStr.length - (dotIndex+1)) > decimal) // decimal number is more than decimal allow
			return false;
	}

	return true;
}

/*
		just like InitDate, only some slightly changes
*/
function InitLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more) {
	var i, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		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) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
		else {
			cmbMonth.selectedIndex = 0;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option("ระบุวันที่", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
		else {
			cmbDay.selectedIndex = 0;
		}
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option("ระบุปี", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-below; i<n+more+1; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					cmbYear.selectedIndex = cmbYear.options.length - 1;
		}
		if(dtDefault == null)
			cmbYear.selectedIndex = 0;
	}
}

/*
		just like InitDate, only some slightly changes, special for premium offer and policy service in is
*/
function InitShorterLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more) {
	var i, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		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) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
		else {
			cmbMonth.selectedIndex = 0;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option("วันที่", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
		else {
			cmbDay.selectedIndex = 0;
		}
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option("ปี", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-below; i<n+more+1; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					cmbYear.selectedIndex = cmbYear.options.length - 1;
		}
		if(dtDefault == null)
			cmbYear.selectedIndex = 0;
	}
}

// boom added
function skip(e) {
	this.blur();
}

function disable(component) {
	component.disabled = true;
	// netscape also add this
	component.onfocus = skip;
}

function enable(component) {
	component.disabled = false;
	// netscape also add this
	component.onfocus = null;
}
// end boom added

// balloon added

// get all checkbox value (String formate)
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;
}	

// get all checkbox value (String formate)
function  Chk_DeleteId(frm,chkname) 
{ 
	var del_id = "";
	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) {
					if (count > 0)
					{
						del_id = del_id + ","
					}
					del_id = del_id+frm.elements[i].value;
					count++;
				}
			}
		}       
	} 
	return del_id;
}	

// count checkbox checked
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 IsMonthYear(szStr) {
	if(szStr.length != 7)
		if(szStr.length != 6)
			return false;

    var szMonth;
	var szSlash;
	var szYear;

	if(szStr.length == 7) {
		szMonth = szStr.substring(0, 2);
		szSlash = szStr.substring(2, 3);
		szYear = szStr.substring(3,  7);
	}
	else if(szStr.length == 6) {
		szMonth = szStr.substring(0, 1);
		szSlash = szStr.substring(1, 2);
		szYear = szStr.substring(2, 6);
	}

	var nMonth = parseInt(szMonth);
	if(nMonth < 1 || nMonth > 12)
		return false;

	 var x = IsInteger(szYear) && (szSlash == "/");
	 return x;
}

function IsOnlyNumber(szStr) {
	for(var i=0;i<szStr.length;i++) {
		// except number, nothing is allow
		if("0123456789".indexOf(szStr.charAt(i)) == -1)
			return false;
	}
	return true;
}

// Calendar Dialog Box
function popCalendar(fielddate, fieldmonth, fieldyear, fieldschedule)
{
	w = window.open("/include/popCalendar.asp","Calendar","width=180, height=180");
	field_date = fielddate;
	field_month = fieldmonth;
	field_year = fieldyear;
	field_schedule = fieldschedule;
	if ( w.opener == null )
		w.opener = self;
	w.focus();
}

// end 
//-->

