String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}
String.prototype.isNumeric=function(){return this.match(/^[+-]?\d*[.]?\d*$/gi)!=null;}
String.prototype.isInteger=function(){return this.match(/^[+-]?\d*$/gi)!=null;}
String.prototype.round=function(n)
{
	var f=parseFloat(this);
	var s=String(Math.round(f*Math.pow(2,n))/Math.pow(2,n));
	var dx=s.indexOf(".");
		if(dx>0 && (s.length-dx)>2)s=s.substring(0,dx+3);
		return parseFloat(s);
}
String.prototype.isDate=function()
{ 
	//valid date format is yyyy-mm-dd
	
	//month(4,6,9,11) 
	var r1=/^([0-9]{4})(-)((([0]{0,1}(4|6|9))|11))(-)(([1-9])|([0][1-9])|([1-2][0-9])|30)$/g;

	//month(1,3,5,7,8,10,12)
	var r2=/^([0-9]{4})(-)(([0]{0,1}(1|3|5|7|8))|(10|12))(-)(([1-9])|([0][1-9])|([1-2][0-9])|30|31)$/g

	//not leap year month 2 
	var r3=/^(([0-9]{2}([02468][1235679])|([13579][01345789])))(-)(2|02)(-)(([1-9])|([0][1-9])|([1-2][0-8]))$/g;

	//leap year month 2 
	var r4=/^(([0-9]{2}([02468][048])|([13579][26])))(-)(2|02)(-)(([1-9])|([0][1-9])|([1-2][0-9]))$/g;

	return r1.test(this) || r2.test(this) || r3.test(this) || r4.test(this); 
} 
String.prototype.isUKDate=function()
 { 
 	//valid date format is dd/mm/yyyy
	
	// small	
 	var r1=/^(([1-9])|([0][1-9])|([1-2][0-9])|30)(\/)((([0]{0,1}(4|6|9))|11))(\/)([0-9]{4})$/g;

	// big
 	var r2=/^(([1-9])|([0][1-9])|([1-2][0-9])|30|31)(\/)(([0]{0,1}(1|3|5|7|8))|(10|12))(\/)([0-9]{4})$/g
 	
 	// leap 2
 	var r3=/^(([1-9])|([0][1-9])|([1-2][0-8]))(\/)(2|02)(\/)(([0-9]{2}([02468][1235679])|([13579][01345789])))$/g;
 	
 	// not leap 2
 	var r4=/^(([1-9])|([0][1-9])|([1-2][0-9]))(\/)(2|02)(\/)(([0-9]{2}([02468][048])|([13579][26])))$/g;
 	
	return r1.test(this) || r2.test(this) || r3.test(this) || r4.test(this); 
 } 
String.prototype.byteLength=function()
{
    var a=0,i=0;
    for(i=0;i<this.length;i++){a+=this.charCodeAt(i)>255 ? 2 : 1;}
    return a;
}
function TestEmils(source, arguments) {
    var emails = arguments.Value.split(",");
    for (i = 0; i < emails.length; i++) {
        if (!Vemail(emails[i])) {
            arguments.IsValid = false;
            return;
        }
    }
}
function Vemail(value) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}
function initCap(obj)
{
//	var s=obj.value.trim(); 
//	if(s=="")
//	{
//		obj.value=s;
//		return;
//	}
//	
//	var p=" ",r="",c="";
//	for(var i=0;i<s.length;i++)
//	{
//		c=s.charAt(i);
//		r+=p==" " ? c.toUpperCase() : c.toLowerCase();
//		p=c;
//	}
//	obj.value=r;
	
	var name = obj.value.trim();
	if (name.toUpperCase().indexOf("MAC")==0) {
		name = "Mac" + name.substring(3, 4).toUpperCase() + name.substring(4, name.length);
	}
	else if (name.toUpperCase().indexOf("MC")==0) {
		name = "Mc" + name.substring(2, 3).toUpperCase() + name.substring(3, name.length);
	}
	name = name.substring(0, 1).toUpperCase() + name.substring(1, name.length);
	//replace empty next value;
	for (var i = 0; i < name.length; i++) {
		if (name.substring(i, i + 1) == " " && i < name.length - 1) {
			var k = i + 1;
			name = name.substring(0, k - 1) + " " + name.substring(k, k + 1).toUpperCase() + name.substring(k + 1, name.length);
		}
	}
	obj.value=name;
}
function initFC(obj)
{
	var name = obj.value.trim();
	name = name.substring(0, 1).toUpperCase() + name.substring(1, name.length);
	obj.value=name;
}
function $(eltId,doc){if(!doc)doc=document;return doc.getElementById(eltId);};
function $$(tagName,doc){if(!doc)doc=document;return doc.createElement(tagName);};
var __IsIE=navigator.userAgent.toLowerCase().indexOf("msie")>-1;

var flag=false;

function DrawImage(ImgD,iwidth,iheight){

     var image=new Image();
     image.src=ImgD.src;
    if(image.width>0 && image.height>0){
     flag=true;
    if(image.width/image.height>= iwidth/iheight){
        if(image.width>iwidth){  
         ImgD.width=iwidth;
         ImgD.height=(image.height*iwidth)/image.width;
         }else{
         ImgD.width=image.width;  
         ImgD.height=image.height;
         }
        // ImgD.alt=image.width+"กม"+image.height;
         }
    else{
        if(image.height>iheight){  
         ImgD.height=iheight;
         ImgD.width=(image.width*iheight)/image.height;        
         }else{
         ImgD.width=image.width;  
         ImgD.height=image.height;
         }
        // ImgD.alt=image.width+"กม"+image.height;
         }
     }
} 
