function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function submitData(f, choice_num) {
	var xChoice = null;
	for (i = 0;i < choice_num;i++) {
		if (f.choice[i].checked) {
			xChoice = f.choice[i].value;		
			break;
		}
	}
	if (xChoice == null) {
		alert("กรุณาเลือกหัวข้อ.");
		return false;
	}

	var url = "pollresult.php?poll_id=" + f.poll_id.value + "&votes=" + xChoice;
	MM_openBrWindow(url,'result','width=650,height=480');
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function ltrim (s){
	return s.replace( /^\s*/, "" );
}

function rtrim (s){
	return s.replace( /\s*$/, "" );
}

function trim (s){
	return rtrim(ltrim(s));
}

function ValidFindDoctorForm(f) {
	if (trim(f.name.value)=="" && f.specialist.value=="#" && f.department.value=="#" && f.weekday.value=="#" && f.sex.value=="#") {
		alert("Please select data for searching.");
		f.name.focus();
		return false;
	}
	f.action.value = "FIND";
	return true;
}

function ValidForm(f) {
	if (trim(f.name.value)=="" && f.specialist.value=="#" && f.department.value=="#" && f.weekday.value=="#" && f.sex.value=="#") {
		alert("Please select data for searching.");
		f.name.focus();
		return false;
	}
	f.action.value = "FIND";
	return true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ViewSelectedDoctor(Obj) {
	if (Obj.value == "#") return;
	var theUrl = "doctorprofileorg.php?id=" + Obj.value;
	MM_openBrWindow(theUrl,'','scrollbars=yes,width=645,height=600');
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
/*
function ShowHideBrief(id) {
	obj = document.getElementById(id);
	if (obj.style.display == 'none') {
		obj.style.display = 'block';
	//	obj.title = "ซ่อนรายละเอียด";
	}
	else {
		obj.style.display = 'none';
	//	obj.title = "แสดงรายละเอียด";
	}
}
*/
function ViewSelectedDoctor(Obj) {
	if (Obj.value == "#") return;
	var theUrl = "doctorprofileorg.php?id=" + Obj.value;
	MM_openBrWindow(theUrl,'','scrollbars=yes,width=645,height=600');
}

function ValidQuickContactForm(f) {
	if (trim(f.message.value) == "") {
		alert("Please enter message.");
		f.message.focus();
		return false;
	}
	if (trim(f.email.value)=="") {
		alert ("Please enter email address.");
		f.email.focus();
		return false;
	} else {
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
		if (!filter.test(f.email.value)) {
			alert('Invalid email address.');
			f.email.select();
			return false;
		}
	}

	f.action.value = "SUBMIT";
	return true;
}

function CalculateBMI(f) {
	if (trim(f.weight.value)=="") {
		alert ("กรุณากรอกน้ำหนัก หน่วยเป็นกิโลกรัมเท่านั้น");
		f.weight.select();
		return false;
	}
	if (trim(f.height.value)=="") {
		alert ("กรุณากรอกส่วนสูง หน่วยเป็นเมตรเท่านั้น");
		f.height.select();
		return false;
	}
	
	var iresult = f.weight.value / (f.height.value * f.height.value);
	result = iresult + "";
	var pos = result.indexOf('.');
	if (pos >= 0) result = result.substring(0, pos+2);
	f.bmi.value = result;
	document.getElementById('result_tbl').style.display = "block";
	return true;
}

function isValidDouble(Obj){
    var allowed = '0123456789.';
    var string = Obj.value;
    for (var i=0; i< string.length; i++) {
        if (allowed.indexOf(string.charAt(i)) == -1) {
            alert('กรุณากรอกเลขทศนิยมเท่านั้น');
            Obj.select();
            return false;
        }
    }
    return true;
}

