// javasciprt only

function is_email(strsender){
	var strpattern = "^[a-z0-9][\\w\\-\\.]*\\w+@[a-z0-9][a-z0-9\\-]*(\\.[a-z0-9]{2,4})+$";
	var regex = new RegExp(strpattern, "i");
	return regex.test(strsender);
}

function is_url(strsender){	
	var strpattern = "^((http|https|ftp)://)?(www\\.)?[a-z][a-z0-9\\-]*(\\.[a-z0-9][a-z0-9\\-]*)+(\\.[a-z0-9]{2,4})+(/[\\w\\-]+)*(/[\\w\\-]+\\.[a-z0-9]+)?";
	var regex = new RegExp(strpattern, "i");
	if (strsender.substring(strsender.length - 1) == "/"){ strsender = strsender.substring(0, strsender.length - 1); }
	return regex.test(strsender);	
}

function is_file(strsender){
	var splt = strsender.split("\\");
	var filename = splt.reverse().shift();
	var pospoint = filename.indexOf(".");
	return (pospoint != -1 && pospoint != (filename.length - 1))
}

function check_keyasc(eng, th, num){
	var e = (e) ? e : ((event) ? event : null);
	var charcode = (e.charCode || e.keyCode);
	if ((charcode >= 65 && charcode <= 90) || (charcode >= 97 && charcode <= 122)){			
		if (e.returnValue){ e.returnValue = eng; } else { return eng; }
	}
	if ((charcode >= 161 && charcode <= 255) || (charcode >= 3585 && charcode <= 3675)){			
		if (e.returnValue){ e.returnValue = th; } else { return th; }
	}
	if (charcode >= 48 && charcode <= 57){			
		if (e.returnValue){ e.returnValue = num; } else { return num; }
	}
}

function check_char(strsender, eng, th, num){
	strsender = utrim(strsender);
	if (strsender == ""){ return false; }	
//	var sp_char = "`~!@#$%^&*()_-+=|\\{[}]:;\"\'<,>.?/í";
	var i, charcode;
	var strlen = strsender.length;
	for (icount = 0; icount < strlen; icount++){
		charcode = strsender.charCodeAt(icount);
		if ((charcode >= 65 && charcode <= 90) || (charcode >= 97 && charcode <= 122)){			
			if (!eng){ return eng; }
		}
		if ((charcode >= 161 && charcode <= 255) || (charcode >= 3585 && charcode <= 3675)){			
			if (!th){ return th; }
		}
		if (charcode >= 48 && charcode <= 57){	
			if (!num){ return num; }
		}		
	}
	return true;
}

function is_date(dd, mm, yy){
	var result;
	dd = parseInt(dd); mm = parseInt(mm); yy = parseInt(yy);
	if (yy > 2400) { yy -= 543;}
	if (((yy % 4) == 0 && (yy % 100) != 0) || ((yy % 400) == 0)) {
		result = true;
	} else {
		result = false;
	}
	switch (mm) {
		case 4: case 6: case 9: case 11:
			if (dd > 30) {return false; }			
			return true;
			break;
		case 2:
			if (dd > 29) {
				return false;
			} else if (dd == 29) {
				if (!result) {return false;}
			}
			return true;
			break;
		default:
			return true;
			break;
	}
}

function is_over_dimension(fpath, wid, hei){
	var answer = false;
	var img = new Image();	
	img.src = "file://"+fpath;
	if (img.width > wid || img.height > hei){ answer = true; }
	img = null;
	return answer;
}

function get_fileURL(strsender){
	var qstr = strsender.indexOf("?");
	if (qstr != -1){ strsender = strsender.substring(0, qstr); }
	var splt = strsender.split("/");
	var filename = splt.reverse().shift();
	return filename;
}

function utrim(strsender) { 
	return strsender.replace(/^\s+|\s+$/g, ''); 
}

function check_picext(strsender, arrext){
	var chkhas = false;
	var pointpos = strsender.lastIndexOf(".");
	if (pointpos != -1){
		strsender = strsender.substring(pointpos + 1).toLowerCase();
		for (icount = 0; icount < arrext.length; icount++){
			if (strsender == arrext[icount].toLowerCase()){ chkhas = true; break; }
		}
	}
	return chkhas;
}

function goto_page(strsender){
	window.location.href = strsender;
}

function goto_page_con(strpage, strcon){
	var ans = confirm(strcon);
	if (ans){ window.location.href = strpage; }
}

function submit_form(objfrm, straction, querystr, usrfn){
	if (straction){ objfrm.action = straction; }
	if (querystr){ objfrm.action += "?"+querystr; }
	var ans = usrfn;
	if (ans == undefined || ans){ objfrm.submit(); }
}

function submit_form_con(objfrm, straction, querystr, strcon, usrfn){
	var ans = confirm(strcon);
	if (ans){
		if (straction){ objfrm.action = straction; }
		if (querystr){ objfrm.action += "?"+querystr; }
		var ans = usrfn;
		if (ans == undefined || ans){ objfrm.submit(); }
	}
}

function lock_key(){
	var e = (e) ? e : ((event) ? event : null);
	var charcode = (e.charCode || e.keyCode);
	if (charcode != 8 && charcode != 46){
		if (e.returnValue){ e.returnValue = false	; } else { return false; }
	}
	return true;
}

function popup_window(fpage, wid, hei, scr, type, argu){ //0=new window,1=check window already,2=open modal dialog,3=open modeless dialog
	var scaletop = (screen.height - hei) / 2;
	var scaleleft = (screen.width - wid) / 2;
	wid += 19;
	var stroptional_1 = "width="+wid+",height="+hei+",left="+scaleleft+",top="+scaletop+","+
		"location=no,menubar=no,resizable=no,toolbar=no,status=no,scrollbars="+scr;			
	var stroptional_2 = "dialogWidth:"+wid+"px;dialogHeight:"+hei+"px;dialogLeft:"+scaleleft+"px;dialogTop:"+scaletop+"px;"+
		"center:yes;status:no;resizable:no;scroll:"+scr+";";
	if (!type){ type = 0; }
	switch (type){
		case 0:
			winpop0 = window.open(fpage, "_blank", stroptional_1);
			winpop0.focus();
			break;
		case 1:
			if (typeof(winpop1) == "undefined" || winpop1.closed || (!winpop1.open)){
				winpop1 = window.open(fpage, "_blank", stroptional_1);
			} else {
				winpop1.location.href = fpage;
				winpop1.focus();
			}
			break;
		case 2:
			if (!argu){ argu = ""; }
			winpop2 = showModalDialog(fpage, argu, stroptional_2);
		 	break;
		case 3:
			if (!argu){ argu = ""; }
			if (typeof(winpop3) == "undefined" || winpop3.closed || (!winpop3.open)){
				winpop3 = showModelessDialog(fpage, argu, stroptional_2);
			} else {
				winpop3.location.href = fpage;
				winpop3.focus();
			}
			break;
	}
}

function get_fckeditor_value(insname){
	var oEditor = FCKeditorAPI.GetInstance(insname);
	return oEditor.EditorDocument.body.innerText;
}

function focus_fckeditor(insname){
	var oEditor = FCKeditorAPI.GetInstance(insname);
	return oEditor.EditorDocument.body.focus();
}

function show_swf(_file, _w, _h, _id, _var, _Div){
	var strFlash
	strFlash = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'"
	strFlash += " width='" + _w + "' height='" + _h + "' id='"+ _id +"' name='"+ _id +"' />"
	strFlash += "<param name='movie' value='" + _file + "' /><param name='quality' value='high' />"
	strFlash += "<param name='allowScriptAccess' value='sameDomain'><param name='menu' value='false' />"
	strFlash += "<param name='flashvars' value='"+ _var +"' /><param name='swfliveconnect' value='true' />"
	strFlash += "<embed src='"+_file+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' "
	strFlash += " width='" + _w + "' height='" + _h + "' id='"+ _id +"' name='"+ _id +"' menu='false' allowScriptAccess='sameDomain'"
	strFlash += " flashvars='"+ _var +"' swfliveconnect='true' ></embed></object>"		
	var obj =document.getElementById(_Div);
	obj.innerHTML= strFlash;
}

function format_number(strsender, decpos, roundtype){ //1=up,2=down,3=currency
	if (isNaN(strsender)){ strsender = "0"; }
	if (isNaN(decpos)){ decpos = 0; }
	if (roundtype != 1 && roundtype != 2 && roundtype != 3){ roundtype = 3; }
	strsender = strsender.replace(",", "");
	arr_str = strsender.split(".");
	var val_int = arr_str[0];
	var val_dec = add_string_repeat("0", decpos);
	if (arr_str.length > 1){;
		if (arr_str[1].length > decpos){			
			var lastnum = arr_str[1].substring(decpos, (decpos + 1));
			switch (true){
				case (roundtype == 2 || (roundtype == 3 && lastnum < 5) || lastnum == "0"): val_dec = arr_str[1].substring(0, decpos); break;
				default:
					val_dec = (parseInt(arr_str[1].substring(0, decpos)) + 1).toString();
					if (val_dec.length > decpos){
						val_int = (parseInt(val_int) + 1).toString();
						val_dec = "";
					} else if (val_dec.length < decpos){
						val_dec = add_string_repeat("0", (decpos - val_dec.length))+val_dec;
					}
					break;
			}
		} else if (arr_str[1].length < decpos){
			val_dec = arr_str[1]+add_string_repeat("0", (decpos - arr_str[1].length));
		}
	}
	var len_int = arr_str[0].length;
	for (i = 1; i <= len_int; i++){
		if (!(i % 3) && i != len_int){ val_int = arr_str[0].substring(0, (len_int - i))+","+val_int.substring(len_int - (3 * (i / 3))); }
	}
	return val_int+(val_dec ? ("."+val_dec) : "");
}

function add_string_repeat(strsender, numrep){
	if (isNaN(numrep) || numrep <= 0){ return ""; }
	return strsender+add_string_repeat(strsender, (numrep - 1));
}

function IsIDCardNo(strTmp)
{
	var bFlag = true;
	var iSumDigit = 0; 
	var iCheckDigit = 0;
	var invalidIDCard = new Array("1111111111119","2222222222227","3333333333335","4444444444443","5555555555551","6666666666660","7777777777778","8888888888886");
	
	if ( strTmp.length != 13 ){ bFlag = false; }
	else if ((strTmp.charCodeAt(0) == 48) || (strTmp.charCodeAt(0) == 57) ){ bFlag = false; }
	else {
		for( var i = 0; i < invalidIDCard.length; i++ ) {
			if (strTmp == invalidIDCard[i]) {
				bFlag = false;
				break;
			}
		}
	}

	if (bFlag == true) {
		for( var i = 0; i < strTmp.length - 1; i++ ) { 
			if (((strTmp.charCodeAt(i)<48) || (strTmp.charCodeAt(i)>57)) ){
				bFlag = false;
				break;
			}
			if (i != 12) {
				iSumDigit += parseInt(strTmp.charAt(i),10) * (13 - i); 
			}
		}

		if (bFlag == true) {    	        
			iCheckDigit = parseInt(strTmp.charAt(12),10); 
			iSumDigit = (iSumDigit % 11);
			
			iSumDigit = (11 - iSumDigit)
			if (iSumDigit == 11) 
				iSumDigit = 1;
			else if (iSumDigit == 10) 
				iSumDigit = 0;
			
			if (iCheckDigit == iSumDigit)
				bFlag = true;
			else
				bFlag = false;
		}
	}
	return bFlag;
}

function numberOnly( event )
{
	var key = event.keyCode;
	//alert(key);
	if ( !( ( key > 95 && key < 106 ) || key == 8 || key == 9 || key == 37 || key == 39  || key == 110   || key == 190  ||  ( key > 47 && key < 58 )) )
	{
		if ( window.event )
		{
			event.returnValue = null;
		}
		else
		{
			event.preventDefault();
		};
	};
};

function hilight_text(obj){
	document.getElementById(obj).style.background = '#00FF00';	
}