//* prototype function *//
String.prototype.trim = function() {
    return this.replace(/^\s*|\s*$/g, ""); // Strip leading and trailing white-space
}
String.prototype.normalize_space = function() {
    return this.replace(/^\s*|\s(?=\s)|\s*$/g, ""); // Replace repeated spaces, newlines and tabs with a single space
}
String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; } // Zero-Fill string
String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; } // VB-like string
Number.prototype.zf = function(l) { return this.toString().zf(l); } // Zero-Fill string
//* prototype function *//

function Set_Action(url,form,target_form) {
	if (form == 'All' ) {
	
	}
	else {
		document.all[form].action = url;
		if (target_form) { document.all[form].target = target_form;	}
		document.all[form].submit() ;
		document.all[form].target = "";
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function Select_All() {
	for (var i=0;i<document.forms[0].elements.length;i++) {
		var e = document.forms[0].elements[i];
		if (e.type =='checkbox') {
			e.checked = 1 ;
		}
	}
}

function Unselect_All() {
	for (var i=0;i<document.forms[0].elements.length;i++) {
		var e = document.forms[0].elements[i];
		if (e.type =='checkbox') {
			e.checked = 0 ;
		}
	}
}

function Clear_All() {
	for (var i=0;i<document.forms[0].elements.length;i++) {
		var e = document.forms[0].elements[i];
		if (e.type =='text' || e.label == 'text') {
			e.value = "" ;
		}
		else if (e.name.substring(0,3) == "cbo") {
			e.selectedIndex = 0;
		}
	}
}

function NewWindow(mypage,myname,w,h,scroll,status,fullscreen){	
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+scroll+',';
	settings +='status='+status+',';	
	settings +='channelmode='+fullscreen+',';		
	settings +='resizable=yes';
	win=window.open(mypage,myname,settings);
//	win.resizeTo(w, h);
//	win.moveTo(winl, wint);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
} 

function SendTab() {
	if(window.event.keyCode==13) window.event.keyCode=9;
}

function AddSameControl(cltName, count) {
	var td = document.getElementById("td" + cltName);
	var hdfNo = document.getElementById("hdf" + cltName + "No");	
	var tbl = document.getElementById("tbl" + cltName);
	var tblAdd = document.createElement("tbody");
	var HTML_tbl = tbl.innerHTML;	
	// Find all textbox id
	var found_id = HTML_tbl.indexOf('id=');
	var foundC_id;
	var valueArray = new Array();
	while (found_id != -1) {
		foundC_id = HTML_tbl.indexOf(' ', found_id);
		var id = HTML_tbl.substring(found_id + 3, foundC_id);
		if (id.substring(0,3) == "txt") {
			var value = document.getElementById(id).value
			if (document.getElementById(id).tagName == "TEXTAREA") {
				valueArray[valueArray.length] = value;	
			}
			else {
				valueArray[valueArray.length] = (value.indexOf(" ") != -1 ? 'value="' + value + '"' : 'value=' + value);				
			}
		}
		found_id = HTML_tbl.indexOf('id=', foundC_id);
	}	
	// end
	if (parseInt(hdfNo.value) + 1 == count) return;		
	tblAdd.id = "tbl" + cltName + "_" + hdfNo.value;	
	tblAdd.innerHTML = "*";
	tblAdd.innerHTML += "<table border=0 cellSpacing=0 cellPadding=0>" + HTML_tbl.replace(/_0/gi, "_" + hdfNo.value);
	hdfNo.value = parseInt(hdfNo.value) + 1;		
	tblAdd.innerHTML = tblAdd.innerHTML.replace(".1 ", "." + (parseInt(hdfNo.value)));
	// clear value 
	tblAdd.innerHTML = tblAdd.innerHTML.replace(/selected/gi, "");
	tblAdd.innerHTML = tblAdd.innerHTML.replace(/checked/gi, "");	
	for (var count=0; count<valueArray.length; count++) {
		tblAdd.innerHTML = tblAdd.innerHTML.replace(valueArray[count],' ');
	}
	// end
	/*alert(tblAdd.innerHTML);
	var HTML = tblAdd.innerHTML;
	var found_input = HTML.indexOf("INPUT");
	var found_id = HTML.indexOf('value="', found_input);
	var foundC_id;
	while (found_id != -1) {
		foundC_id = HTML.indexOf('" ', found_id);
		var value = HTML.substring(found_id, foundC_id);
		tblAdd.innerHTML = HTML.replace(value,'value=""');
		found_id = HTML.indexOf(found_id, foundC_id);
	}
	tblAdd.innerHTML = tblAdd.innerHTML.replace("1.", (parseInt(hdfNo.value) + 1) + ".");	
	*/	
	td.appendChild(tblAdd);
	td.innerHTML =	td.innerHTML.replace("*", "");
}

function RemoveSameControl(cltName, count) {
	var td = document.getElementById("td" + cltName);
	var hdfNo = document.getElementById("hdf" + cltName + "No");
	hdfNo.value = parseInt(hdfNo.value) - 1;				
	var tblRemove = document.getElementById("tbl" + cltName + "_" + hdfNo.value);	
}

function GET_AllData() {
	for (var count=0; count<topic_name.length; count++) {
		var divShow = document.getElementById("div" + topic_id[count]);
		var divTemp = document.getElementById("divTemp_" + topic_id[count]);
		divTemp.appendChild(divShow);		
	}
}

function SET_AllData() {
	var divAllData = document.getElementById("div" + topic_id[topic_id.length-1]);	
	divAllData.align = "left";
	divAllData.style.overflow = "";
	for (var count=0; count<topic_name.length-1; count++) {
		var divShow = document.getElementById("div" + topic_id[count]);
		divShow.style.position = "";
		divShow.style.top = "";
		divShow.style.left = "";
		divShow.style.height = "0";
		divShow.style.overflow = "";
		divShow.style.visibility = "visible";		
		divAllData.appendChild(divShow);
	}
}

function change_topic(stopic_id) {
	var tdBody = document.getElementById("tdBody");
	for (var count=0; count<topic_name.length; count++) {
		var tdMenu = document.getElementById("td" + topic_id[count]);
		if (!tdMenu) { break; }
		// incase Register show only last page
		var cmdRegister = document.getElementById("cmdRegister");
		if (cmdRegister) {
			if (topic_name.length == stopic_id) { cmdRegister.style.visibility = "visible"; }
			else { cmdRegister.style.visibility = "hidden"; }
		}
		// end
		// incase Focal Point have Region
		var cboUser_Group = document.getElementById("cboUser_Group");
		var tblRegion = document.getElementById("tblRegion");
		var cboRegion = document.getElementById("cboRegion");		
		if (cboUser_Group && tblRegion && cboRegion) {
			if (cboUser_Group.value == 2) {
				if (Login_Group == "Admin") { // Position Tab
					tblRegion.style.visibility = "visible";						
				}
				else {
					tblRegion.style.visibility = "hidden";
					cboRegion.value = "";
				}					
			}
			else {
				tblRegion.style.visibility = "hidden";
				cboRegion.value = "";			
			}	
		}
		// end
		var divShow = document.getElementById("div" + topic_id[count]);
		if (stopic_id == count+1) {
			tdMenu.bgColor = "47AC47";
			tdMenu.style.color = "FFFFFF";
			tdMenu.innerHTML =  topic_name[count];
			divShow.style.position = "";
			divShow.style.top = "";
			divShow.style.left = "";
			divShow.style.height = "100%";
			divShow.style.overflow = "auto";
			divShow.style.visibility = "visible";
			tdBody.appendChild(divShow);
			
			var control = topic_clt[count].split(",");
			var control_type = topic_clt_type[count].split(",");
			for (var countClt=0; countClt<control.length; countClt++) {
				setControl(control[countClt], control_type[countClt]);
			}
		}
		else {
			tdMenu.bgColor = "";			
			tdMenu.innerHTML = "<a href='javascript:change_topic(" + (count  + 1) + ")'>" + topic_name[count] + "</a>";
			divShow.style.position = "absolute";
			divShow.style.top = "0";
			divShow.style.left = "0";
			divShow.style.visibility = "hidden";
			var divTemp = document.getElementById("divTemp_" + topic_id[count]);
			divTemp.appendChild(divShow);
		}
	}
	var divPage = document.getElementById("divPage");
	divPage.innerHTML = "Page (" + stopic_id + "/" + topic_id.length + ")";
	var hdfPage = document.getElementById("hdfPage");
	hdfPage.value = stopic_id;
	var cmdNext = document.getElementById("cmdNext");
	var cmdPrev = document.getElementById("cmdPrev");
	if (stopic_id == 1) { cmdPrev.disabled = "disabled";  cmdNext.disabled="";}
	else if (stopic_id == topic_id.length) { cmdPrev.disabled = ""; cmdNext.disabled = "disabled"; }
	else { cmdPrev.disabled = ""; cmdNext.disabled = ""; }
	
	 // In case All Data tab	 
	if (window.name != "popup") { return; }
	
	if (topic_name[stopic_id-1] == "Overall") { 
		document.body.scroll = "auto";
		window.resizeTo(830,600);	
		SET_AllData() 
	}
	else {
		if (window.name == "popupR") {
			document.body.scroll = "";
			window.resizeTo(680,640);		
		}
		else if (window.name == "popup") {
			document.body.scroll = "";
			window.resizeTo(800,600);					
		}
	}
}

function Manage_Page(type) {
	var hdfPage = document.getElementById("hdfPage");
	var stopic_id;
	if (type=="Prev") {
		stopic_id = parseInt(hdfPage.value)-1;
		change_topic(stopic_id);
	}
	else if (type=="Next") {
		stopic_id = parseInt(hdfPage.value)+1;
		change_topic(stopic_id);		
	}
}

function Check_Header(control, oldvalue) {
	oldvalue = "," + oldvalue; 
	var chk = document.getElementsByName(control.name);
	for (var count=0; count < chk.length; count++) {
		if (control.alt == chk[count].value) {
			if (chk[count].checked) { }
			else { 
				chk[count].checked = true; 
				oldvalue += (oldvalue.indexOf("," + chk[count].value + ",") != -1 ? "" : chk[count].value + ",");
				// incase have 2 header
				if (chk[count].alt != chk[count].value) {
					//alert(chk[count].alt);
					var chk1 = document.getElementsByName(control.name);
					for (var count1=0; count1 < chk1.length; count1++) {
						if (chk[count].alt== chk1[count1].value) {
							if (chk1[count1].checked) { }
							else { 
								chk1[count1].checked = true; 
								oldvalue += (oldvalue.indexOf("," + chk1[count1].value + ",") != -1 ? "" : chk1[count1].value + ",");		
							}					
						}
					}
					oldvalue += (oldvalue.indexOf("," + chk[count].alt + ",") != -1 ? "" : chk[count].alt + ",");
				}
			}
		}
	}
	oldvalue += (oldvalue.indexOf("," + control.alt + ",") != -1 ? "" : control.alt + ",");	
	return oldvalue; 
}

function Uncheck_Header(control, oldvalue) {
	oldvalue = "," + oldvalue;	
	var chk = document.getElementsByName(control.name);
	for (var count=0; count < chk.length; count++) {
		var found = false;		
		if (control.value == chk[count].alt) {
			chk[count].checked = false;
			oldvalue = oldvalue.replace("," + chk[count].value + ",", ",");
			var chk1 = document.getElementsByName(control.name);
			for (var count1=0; count1 < chk1.length; count1++) {
				if (chk1[count1].alt == chk[count].value) {
					chk1[count1].checked = false;
					oldvalue = oldvalue.replace("," + chk1[count1].value + ",", ",");
				}
				if (!found) {
					if (control.alt == chk1[count1].value) { found = true; }
				}
			}
			if (!found) { oldvalue = oldvalue.replace("," + control.alt + ",", ","); }
		}
	}
	return oldvalue.substring(1);
}

function getControl(control, control_type) {
	if (control_type == "chk") {
		var hdf = document.getElementById("hdf" + control.name.substring(3, control.name.length-2));
		if (control.checked) {
			var value = Check_Header(control, hdf.value);
			hdf.value = (value.indexOf("," + control.value + ",") != -1 ? hdf.value : control.value + "," + value.substring(1)); // in case have header sub
			if (hdf.value.substring(hdf.value.length-2) == ",,") { hdf.value = hdf.value.substring(0, hdf.value.length-1); }
		}
		else {
			hdf.value = Uncheck_Header(control, hdf.value);
			hdf.value = "," + hdf.value;
			hdf.value = hdf.value.replace("," + control.value + ",",",");
			hdf.value = hdf.value.substring(1);
		}
	}
	else if (control_type == "rdb") {
		var hdf = document.getElementById("hdf" + control.name.substring(3));
		hdf.value = control.value;
	}
}

function sortNumbers(a, b) { return a - b }

function setControl(hdf_id, control_type) {
	var hdf = document.getElementById(hdf_id);
	if (!hdf) { return; }
	if (hdf.value.length == 0) { return; }
	if (control_type == "chk") {
		var hdf_array = hdf.value.split(",");
		hdf_array.pop();
		hdf_array = hdf_array.sort(sortNumbers);
		var hdf_count = 0;
		var chk = document.getElementsByName("chk" + hdf_id.substring(3) + "[]");
		for (var count=0; count < chk.length; count++) {
			if (hdf_array[hdf_count] == chk[count].value) {
				chk[count].checked = true;
				hdf_count++;
			}
		}
	}
	else if (control_type == "rdb") {
		var hdf = document.getElementById(hdf_id);
		if (!hdf) { return; }
		if (hdf.value.length == 0) { return; }
		var rdb = document.getElementsByName("rdb" + hdf_id.substring(3));
		for (var count=0; count < rdb.length; count++) {
			if (rdb[count].value == hdf.value) { rdb[count].checked = 1; }
		}
	}
}

function Change_Group(clt, Action) {
	var tblEducation = document.getElementById("tblEducation");
	var divBirthyear0 = document.getElementById("divBirthyear0");
	var divBirthyear1 = document.getElementById("divBirthyear1");
	var divBirthyear2 = document.getElementById("divBirthyear2");	
	var divPage = document.getElementById("divPage");
	
	// Researcher
	if (clt.value == 1) {
		if (Action == "Show") {
			topic_id = Array("Profile", "Position", "Info1", "Info2", "Info3", "Info4", "Remarks", "Abstract", "Overall");
			topic_name = Array("Profile", "Position", "Info (1)", "Info (2)", "Info (3)", "Info (4)", "Remarks", "Abstract", "Overall");
			topic_clt = Array("", "", "hdfClassification", "hdfST_Activity", "", "", "", "", "");
			topic_clt_type = Array("", "", "chk", "chk", "", "", "", "", "");	
			divPage.innerHTML = "Page (1/9)";
		}
		else {
			topic_id = Array("Profile", "Position", "Info1", "Info2", "Info3", "Info4", "Remarks");
			topic_name = Array("Profile", "Position", "Info (1)", "Info (2)", "Info (3)", "Info (4)", "Remarks");
			topic_clt = Array("", "", "hdfClassification", "hdfST_Activity", "", "", "");
			topic_clt_type = Array("", "", "chk", "chk", "", "", "");	
			divPage.innerHTML = "Page (1/7)";
		}
		change_topic(1);
	}
	// Focal Point = 2, Visitor = 4
	else if (clt.value == 2 || clt.value == 4) {
		for (var count = 2; count < topic_id.length; count++) {
			var tdMenu = document.getElementById("td" + topic_id[count]);
			if (tdMenu) { tdMenu.innerHTML = ""; }
		}
		topic_id = Array("Profile", "Position");
		topic_name = Array("Profile", "Position");
		topic_clt = Array("", "");
		topic_clt_type = Array("", "");
		divPage.innerHTML = "Page (1/2)";
	}
}

function emailCheck(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
/*	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid.");
		return false;
	}*/
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	return true;
}

function Validate_Thaikey(keyCode) {
	if ((keyCode >= 3585 && keyCode <= 3642) || (keyCode >= 3648 && keyCode <= 3673) || (keyCode == 32) || (keyCode == 46) || (keyCode == 44)) { // 44 = ","  , 46 = "."
		return true;
	}
	else {
		//alert("กรุณากรอกภาษาไทย");		
		return false
	}
}

function Validate_NotThaikey(keyCode) {
	if ((keyCode >= 3585 && keyCode <= 3642) || (keyCode >= 3648 && keyCode <= 3673) || (keyCode == 32)) {
		return false;
	}
	else {
		return true
	}
}

function Validate_Engkey(keyCode) {
	if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (keyCode == 32)) {
		return true;
	}
	else {
		//alert("กรุณากรอกภาษาไทย");		
		return false
	}
}

function Validate_Username(keyCode) {
	// a-z, . , 0-9
	if ((keyCode >= 46 && keyCode <= 57)  || (keyCode >= 97 && keyCode <= 122) || (keyCode == 46)) {
		return true;
	}
	else {
		return false
	}
}