﻿// JScript File

		function ltrim(String) {
			var i = 0;
			var j = String.length - 1;

			if (String == null)
				return (false);

			for (i = 0; i < String.length; i++)
			{
				if (String.substr(i, 1) != ' ' &&
					String.substr(i, 1) != '\t')
					break;
			}

			if (i <= j)
				return (String.substr(i, (j+1)-i));
			else
				return ('');
		}
		
		function rtrim(String) {
			var i = 0;
			var j = String.length - 1;

			if (String == null)
				return (false);

			for(j = String.length - 1; j >= 0; j--)
			{
				if (String.substr(j, 1) != ' ' &&
					String.substr(j, 1) != '\t')
				break;
			}

			if (i <= j)
				return (String.substr(i, (j+1)-i));
			else
				return ('');
		}
		
		function trim(String) {
			if (String == null)
				return ("");
			return rtrim(ltrim(String));
		}

		function emailValidate(email) {
				if (email == "") {return true};
				em_val = /^\w+@\w+(\.\w+?)/ ;
				return (em_val.test(email));
		}
		
		function isDigit(Expression) {
			Expression = Expression.toLowerCase();
			RefString = "0123456789";

			if (Expression.length < 1) 
				return (false);

			for (var i = 0; i < Expression.length; i++) 
			{
				var ch = Expression.substr(i, 1);
				var a = RefString.indexOf(ch, 0);
				if (a == -1)
					return (false);
			}
			return(true);
		}
		


//สำหรับให้ TextBox กรอกตัวที่กำหนดให้ได้เท่านั้น
//<asp:TextBox ID="txt_IdentNo" runat="server" Width="150px" onkeypress="allowOnly('0..9',this);" MaxLength="13"></asp:TextBox>
//allowOnly('0..9',this) ,allowOnly('0..9|.',this)

		function allowOnly(Expression,obj) {
		   // onkeypress ใช้ | คั่นระหว่าง a..z from 0..9 และสัญลักษณ์
			Expression = Expression.toLowerCase();
			Expression = replace(Expression, 'a..z', 'abcdefghijklmnopqrstuvwxyz');
			Expression = replace(Expression, '0..9', '0123456789');
			Expression = replace(Expression, '|', '');

			var ch = String.fromCharCode(window.event.keyCode);
			ch = ch.toLowerCase();
			Expression = Expression.toLowerCase();
			var a = Expression.indexOf(ch);
			if (a == -1) 
				window.event.keyCode = 0;

			  if (window.event.keyCode==46 && Expression.indexOf('.')) 
			  {
			        if( obj.value.indexOf('.') >= 0 )
			        {
			            window.event.keyCode = 0
			        }
			   } 

		}
		

		function replace(Expression, Find, Replace) {
			var temp = Expression;
			var a = 0;

			for (var i = 0; i < Expression.length; i++) 
			{
				a = temp.indexOf(Find);
				if (a == -1)
					break
				else
					temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length));
			}

			return temp;
		}	
		
//สำหรับตรวจสอบรหัสบัตรประชาชน     วิธีใช้
//<asp:TextBox ID="txt_IdentNo" runat="server" Width="150px" onkeypress="allowOnly('0..9',this);" MaxLength="13"></asp:TextBox>
//<asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="CustomValidator" ControlToValidate="txt_IdentNo" SetFocusOnError="True" ValidateEmptyText="True" ClientValidationFunction="tg_checkiden">รหัสบัตรประชาชนผิด</asp:CustomValidator>
function tg_checkiden(sender, args) {
    var id = args.Value;
    if(id.length != 13) 
    {
        //alert('รหัสบัตรประชาชนผิด');    
        args.IsValid = false;
        return;
    }
    for(i=0, sum=0; i < 12; i++)
        sum += parseFloat(id.charAt(i))*(13-i);
    if((11-sum%11)%10!=parseFloat(id.charAt(12))) 
    {
        //alert('รหัสบัตรประชาชนผิด');
        args.IsValid = false;
        return;
    }
    args.IsValid = true;
}


//สำหรับตรวจสอบรูปแบบ วันที่     วิธีใช้
//<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
//<asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="tg_isdatecalendar" ControlToValidate="TextBox1" ErrorMessage="CustomValidator"></asp:CustomValidator><br />
  function tg_isdatecalendar(sender, args) {
             var val = args.Value;
             var format = 'dd/MM/yyyy';
	//getDateFromFormat อยู่ใน date.js
	var date=getDateFromFormat(val,format);
	
	if (date==0) { 
	       args.IsValid = false;
        return;
	 }
	args.IsValid = true;
	}
	
	
//  Function Check_Keypress_Number  

function Check_Date(e){
	if (e.keyCode == 13)	{
		e.returnValue = true;
	}	else if (e.keyCode < 47 || e.keyCode > 57){
		e.returnValue = false;
		alert("กรุณากรอกตัวเลข และ / เท่านั้น \n input numeric and / only");
	}
}// End Function Check_Keypress_Number

  
function Check_Money(e){
//alert(e.keyCode);
	if (e.keyCode == 46 || e.keyCode == 13)	{
		e.returnValue = true;
	}	else if (e.keyCode < 48 || e.keyCode > 57){
		e.returnValue = false;
		alert("กรุณากรอกตัวเลข และ . เท่านั้น \n input numeric and . only");
	}
}

//<input name="s_salary_req" type="text" class="Input1" maxlength="7" onkeypress='Check_Money(event)' value="">
function Check_Number(e){
	if (e.keyCode == 13)	{
		e.returnValue = true;
	}	else if (e.keyCode < 48 || e.keyCode > 57){
		e.returnValue = false;
		alert("กรุณากรอกตัวเลข เท่านั้น \n input numeric only");
	}
}	


/*
ตรวจสอบ maxlength ใช้กับ textbox แบบ multiline เพราะไม่สามารถใช้ property maxlength ได้
ใช้คู่กับ custom validate กันกรณี copy ลง textbox 
<asp:TextBox ID="txt_mateaddress" runat="server" Height="60px" TextMode="MultiLine"
Width="330px" MaxLength="100" onkeypress="maxLength('10',this);"></asp:TextBox></td>		
*/		
function maxLength(Expression,obj) {
		   	        
		   	        if( obj.value.length >= Expression )
			        {
			            //obj.value=obj.value.substring(0,Expression)
			            window.event.keyCode = 0
			        }
}
//onkeyup="checkLength('50',this);" 
//onBlur="return ismaxlength(this)" 



/*
ตรวจสอบว่าเป็นตัวเลขหรือไม่
<asp:CustomValidator ID="ctv_matessf" runat="server" ClientValidationFunction="checkNumber"
ControlToValidate="txt_matessf" Display="Dynamic" ErrorMessage="CustomValidator"
SetFocusOnError="True" ValidateEmptyText="True">ตัวเลขเท่านั้น</asp:CustomValidator>
*/
function checkNumber(sender, args) {
        var val = args.Value;
    	if (val.length!=0) { 
		    args.IsValid=isDigit(val);
            return;
	    }
	    args.IsValid = true;
}
/*
ตรวจสอบว่าเป็นรหัสไปรษณีย์หรือไม่ ใช้คูกับ AjaxProvince
<asp:TextBox ID="txt_tumbon" runat="server" Width="80px" MaxLength="50"></asp:TextBox>&nbsp; 
<span class="Htext">รหัสไปรษณีย์</span>&nbsp;<asp:TextBox ID="txt_postcode" runat="server" Width="50px" MaxLength="5" SkinID="txtInteger"></asp:TextBox>&nbsp;
<asp:CustomValidator ID="ctv_postcode" runat="server" ClientValidationFunction="checkPostcode"
ControlToValidate="txt_postcode" Display="Dynamic" ErrorMessage="CustomValidator"
SetFocusOnError="True" ValidateEmptyText="True">ตัวเลขเท่านั้น</asp:CustomValidator>	
*/
function checkPostcode(sender, args) {
//debugger;
        var val = args.Value;
    	if (val.length!=0) { 
    	    if (isDigit(val)==true && val.length==5)
    	    {
    	        args.IsValid=true;
    	    }else{args.IsValid=false;}
            return;
	    }
	    args.IsValid = true;
}

