// javasciprt only

function is_email(strsender){
	strsender = utrim(strsender);
	var strpattern = "^[a-z][\\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){	
	strsender = utrim(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){
	strsender = utrim(strsender);
	var splt = ((strsender.indexOf("\\") != -1) ? strsender.split("\\") : 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) || charcode == 45 || charcode == 46 || charcode == 44){			
		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) || charcode == 45 || charcode == 46 || charcode == 44){	
			if (!num){ return num; }
		}		
	}
	if (!eng && !th && num){
		if (isNaN(strsender)){ return false; }
	}
	return true;
}

function is_date(dd, mm, yy){
	var result;
	if (!dd || !mm || !yy){ return false; }
	dd = parseInt(dd); mm = parseInt(mm); yy = parseInt(yy);
	if (!dd || !mm || !yy){ return false; }
	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;
	}
}

//for file extension are htm or html only
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 clear_fckeditor(insname){
	var oEditor = FCKeditorAPI.GetInstance(insname);
	oEditor.EditorDocument.body.innerText = "";
}

function set_fckeditor_value(insname, valsender){
	var oEditor = FCKeditorAPI.GetInstance(insname);
	oEditor.EditorDocument.body.innerText = valsender;
}
