function openModalDialog(url, width, height) {
	if (!window.showModalDialog) {
		window.showModalDialog = function(sUrl, vArguments, sFeatures) {
			var wFeatures   = sFeatures.replace(/dialog/gi,"").replace(/:/gi,"=").replace(/;/gi,",")
			var modalWindow = window.open (sUrl, "_blank", wFeatures);
			modalWindow.setFocusToWindow = function () {
				modalWindow.focus();
			}
			modalWindow.setInterval("setFocusToWindow()", 100);
		}
	}
	window.showModalDialog(url, self, "dialogHeight: " + height + "px; dialogWidth: " + width + "px; center: yes; help: no; status: no");
	return false;
}

function openPopup(url, width, height) {
	var mywindow = window.open(url, "mywindow","toolbar=no,location=0,status=0,scrollbars=1,width=" + width + ",height=" + height + "");
	mywindow.moveTo(screen.availWidth / 2 - (width / 2), screen.availHeight / 2 - (height / 2));
	return false;
}

function checkAll(element) {
	var form = element.form
	var i = 0;
	for(i = 0; i < form.length; i++) {
		if (form[i].type == 'checkbox' && form[i].name != 'checkall') {
			form[i].checked = element.checked;
		}
	}
}

function bookmarksite(title, url) {
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if (window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
	} 
	else if (document.all) // ie
		window.external.AddFavorite(url, title);
}

function getCheckedValue(radioObj) {
	if (!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if (radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function vote_poll(poll_id) {
	var opt = document.forms["form_poll"].poll_opt;
	var opt_id = getCheckedValue(opt);
	if (opt_id == '') {
		return false;
	}
	$.ajax({
	  	type: "POST",
	  	url: "ajax_vote_poll.php",
	  	data: "poll_id=" + poll_id + "&opt_id=" + opt_id,
		success: onVotePollSuccess
	});
	return false;
}

function onVotePollSuccess(msg) {
	if (msg == 'success')
		alert('โหวดเรียบร้อย ขอบคุณครับ');
	else if (msg == 'wait')
		alert('คุณได้โหวดไปแล้ว กรุณารออีก 1 ชั่วโมง');
	else
		alert('คุณต้องทำการเข้าสู่ระบบก่อน');
}