// JavaScript Document
var currentTime = new Date();
var file_function = "index.php";
//url=url+"&sid="+Math.random();
// initial AJAX
function newXmlHttp() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}
	return xmlHttp;
}

function script_popupPage(page, h, w) {
	windowprops = "height="
			+ h
			+ ",width="
			+ w
			+ ",location=no,"
			+ "scrollbars=yes, menubars=no, toolbars=no, resizable=no, status=no";
	window.open(page, "popup", windowprops);
}

function cancelSubmit() {
	window.event.returnvalue = false;
}

function sleep(mTime) {
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();

	while (sleeping) {
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if (alarmMSeconds - startingMSeconds > mTime)
			sleeping = false;
	}
}

function script_voteVocab(myform, vocab_id, vote_choice) {
	//alert('hi');
	if (!vote_choice) {
		var vote_choice_obj = myform.vote_choice;
		if (vote_choice_obj.value == undefined) {
			for ( var i = 0; i < vote_choice_obj.length; i++) {
				if (vote_choice_obj[i].checked) {
					vote_choice = vote_choice_obj[i].value;
					break;
				}
			}
		} else {
			vote_choice = vote_choice_obj.value;
		}
	}
	//alert('hi2');
	if (!vote_choice) {
		alert('กรุณาเลือกโหวตคะแนนก่อนค่ะ !!');
		return false;
	} else {
		//alert('hi3');
		var xmlHttp = newXmlHttp();
		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		var event = false;
		if (window.document.readyState == undefined) {
			event = true;
		}

		xmlHttp.onreadystatechange = function() {
			//alert("state = " + xmlHttp.readyState + " status =" + xmlHttp.status);
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				if (xmlHttp.responseText == "ok") {
					alert("ขอบคุณที่ช่วยโหวตคำศัพท์ค่ะ");
					
					if (document.getElementById('vocab_list')) {
						document.location.reload();
					}else{
						script_lookupVocab(myform);
					}
					
					

				} else {
					alert("ระบบผิดพลาดไม่สามารถโหวตคำศัพท์ได้ค่ะ !!");
				}
			}
		};

		var action = "voteVocab";
		var param = 'q=svs_manager/script_result&currentTime=' + currentTime
				+ '&action=' + action;
		param += '&vocab_id=' + vocab_id + '&vote_choice=' + vote_choice;
		xmlHttp.open("POST", file_function, event);
		xmlHttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8;");
		xmlHttp.send(param);
		//alert('hi4');
	}
}

function script_lookupVocab(myform) {
	

	var word = myform.word.value;
	//alert(word);
	if (!word) {
		alert('กรุณาใส่คำที่ต้องการค้นหาก่อนค่ะ !!');
		return false;
	} else {
		//alert('finish1');
		/*var submit = document.getElementById('lookupvocab_submit');
		document.getElementById('f_lookupvocab').onSubmit = cancelSubmit;
		submit.disabled = 1;*/
		var xmlHttp = newXmlHttp();
		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		//alert('finish2');
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				//alert("test : " + xmlHttp.responseText);
				document.getElementById('result_svs_lookup').style.display = "";
				document.getElementById('result_lookup').innerHTML = xmlHttp.responseText;
				//submit.disabled = 0;
				//document.getElementById('word').value = "";
				// document.getElementById('mycontent').innerHTML = document
				// .getElementById('svs_lookupvocab').innerHTML;
				// return xmlHttp.responseText;
			}
		};
		//alert('finish3');
		var action = "lookupVocab";
		var param = 'q=svs_manager/script_result&currentTime=' + currentTime
				+ '&action=' + action;
		param += '&word=' + word;
		xmlHttp.open("POST", file_function, true);
		xmlHttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8;");
		xmlHttp.send(param);
		//alert('finish4');
	}
}

function script_dropdown(obj_id) {
	var display = '';
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(obj_id).style.display == '') {
			display = 'none';
		}
		document.getElementById(obj_id).style.display = display;

	} else {
		if (document.layers) {
			if (document.obj_id.display == '') {
				display = 'none';
			}
			document.obj_id.display = display;
		} else {
			if (document.all.obj_id.style.display = '') {
				display = 'none';
			}
			document.all.obj_id.style.display = display;
		}
	}
	return false;
}

function script_suggestVocab(myform) {
	var validate_result = script_validateSuggestForm(myform);
	if (!validate_result) {
		return false;
	} else {
		var source_language = myform.source_language.value;
		var target_language = myform.target_language.value;
		var word = myform.word.value;
		var pos = myform.pos.value;
		var translation = myform.translation.value;
		var def = myform.def.value;
		var ex = myform.ex.value;
		var syn = myform.syn.value;
		var ant = myform.ant.value;
		var read = myform.read.value;
		var ipa = myform.ipa.value;
		var domain = myform.domain.value;
		var ref = myform.ref.value;

		var xmlHttp = newXmlHttp();
		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//				alert("res : "+xmlHttp.responseText);
				if (xmlHttp.responseText == "ok") {
					alert("ขอบคุณที่ช่วยกันแนะนำคำศัพท์เข้ามาค่ะ");
					return true;
				} else if (xmlHttp.responseText == "exist") {
					var msg = "คำศัพท์ที่ท่านแนะนำมีอยู่แล้ว กรุณาแนะนำคำศัพท์อื่น !!";
					document.getElementById("vocab_detail").style.display = "none";
					document.getElementById("alert_msg").innerHTML = msg;						
					alert(msg);
					return false;
				} else {
					alert("ระบบผิดพลาดไม่สามารถแนะนำคำศัพท์ได้ !!");
					return false;
				}		
			}

		};
		
		var action = "suggestVocab";
		var param = 'q=svs_manager/script_result/currentTime=' + currentTime
				+ '&action=' + action;
		param += '&source_language=' + source_language + '&target_language='
				+ target_language + '&word=' + word + '&pos=' + pos
				+ '&translation=' + translation + '&def=' + def + '&ex=' + ex
				+ '&syn=' + syn + '&ant=' + ant + '&read=' + read + '&ipa='
				+ ipa + '&domain=' + domain + '&ref=' + ref;
		xmlHttp.open("POST", file_function, true);
		xmlHttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8;");
		xmlHttp.send(param);
	}
}

function script_getAllPOS(language) {
	if (!language) {
		alert('กรุณาเลือกภาษาต้นทางก่อนค่ะ !!');
		document.getElementById("pos_list").innerHTML = "&nbsp; ";
		return false;
	} else {
		var xmlHttp = newXmlHttp();
		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				document.getElementById("pos_list").innerHTML = xmlHttp.responseText;
			}
		};
		var action = "getAllPOS";
		var param = 'q=svs_manager/script_result&currentTime=' + currentTime
				+ '&action=' + action;
		param += '&language=' + language;
		xmlHttp.open("POST", file_function, true);
		xmlHttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8;");
		xmlHttp.send(param);
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function script_validateSuggestForm(myform) {
	var source_language = myform.source_language.value;
	var target_language = myform.target_language.value;
	var word = trim(myform.word.value);
	var pos = myform.pos.value;
	var translation = trim(myform.translation.value);
	var def = trim(myform.def.value);
	var ex = trim(myform.ex.value);
	var syn = trim(myform.syn.value);
	var ant = trim(myform.ant.value);
	var read = trim(myform.read.value);
	var ipa = trim(myform.ipa.value);
	var domain = trim(myform.domain.value);
	var ref = trim(myform.ref.value);

	if (!source_language || !target_language || !word || !pos || !translation
			|| !def || !domain) {
		alert('กรุณากรอกข้อมูลในช่องที่มีเครื่องหมาย (*) ให้ครบถ้วน');
		return false;
	} else {
		return true;
	}
}

function script_validateExistingVocab(word, pos, translation, sourceLang,targetLang) {
	if (!word || !pos || !translation || !sourceLang || !targetLang) {
		alert('กรุณากรอกข้อมูลให้ครบถ้วนค่ะ !!');
		return false;
	} else {
		var xmlHttp = newXmlHttp();
		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.responseText == "exist") {
					var msg = "คำศัพท์ที่ท่านแนะนำมีอยู่แล้ว กรุณาแนะนำคำศัพท์อื่น !!";
					alert(msg);
					document.getElementById("vocab_detail").style.display = "none";
//					document.getElementById("alert_msg").innerHTML = "คำศัพท์ที่ท่านแนะนำมีอยู่แล้ว กรุณาแนะนำคำศัพท์อื่น !!";
				} else if(xmlHttp.responseText == "error"){
                    var msg = "กรุณาเลือกภาษาให้ถูกต้อง !!";
					alert(msg);
					//document.getElementById("vocab_detail").style.display = "none";
                }else {
//					document.getElementById("alert_msg").innerHTML = "";
					script_popupPage(xmlHttp.responseText, 400, 500);
					// return xmlHttp.responseText;
					// document.getElementById("validate").style.display =
					// "none";
					document.getElementById("vocab_detail").style.display = "";
					// document.getElementById("vocab_list").innerHTML =
					// xmlHttp.responseText;
				}
			}
		};
		var action = "validateVocab";
		var param = 'q=svs_manager/script_result/currentTime=' + currentTime
				+ '&action=' + action;
		param += '&word=' + word + '&pos=' + pos + '&translation='
				+ translation + '&sourceLang=' + sourceLang + '&targetLang='
				+ targetLang;
		xmlHttp.open("POST", file_function, true);
		xmlHttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8;");
		xmlHttp.send(param);
	}
}

function ajaxpage(url, containerid, param) {
	alert(param);
	var xmlHttp = newXmlHttp();
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange = function() {
		loadpage(xmlHttp, containerid);
	};
	var params = param;
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=UTF-8;");
	xmlHttp.send(params);
}

function loadpage(page_request, containerid) {
	if (page_request.readyState == 4
			&& (page_request.status == 200 || window.location.href
					.indexOf("http") == -1)) {
		document.getElementById(containerid).innerHTML = page_request.responseText;
	}
}

function loadobjs() {
	if (!document.getElementById)
		return;
	for (i = 0; i < arguments.length; i++) {
		var file = arguments[i];
		var fileref = "";
		if (loadedobjects.indexOf(file) == -1) { // Check to see if this
			// object has not already
			// been added to page before
			// proceeding
			if (file.indexOf(".js") != -1) { // If object is a js file
				fileref = document.createElement('script');
				fileref.setAttribute("type", "text/javascript");
				fileref.setAttribute("src", file);
			} else if (file.indexOf(".css") != -1) { // If object is a css
				// file
				fileref = document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref != "") {
			document.getElementsByTagName("head").item(0).appendChild(fileref);
			loadedobjects += file + " "; // Remember this object as being
			// already
			// added to page
		}
	}
}