var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;
// Get base url
var url = document.location.href;
var xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
var root_url = window.location.protocol + "//" + window.location.host;
var checkDem=0;

function showhide(id) {
el = document.getElementById(id);
els = dom ? el.style : el;
  if (dom){
    if (els.display == "none") {
        els.display = "";
     } else {
        els.display = "none";
      }
    }
  else if (ns4){
    if (els.display == "show") {
        els.display = "hide";
      } else { 
      els.display = "show";
         }
  }
}

function redirect(mess,url,el) {
	alert(mess);
	getHTML(url,'',el);
}


function sendFrom (url,fromID,el) {	
	pars = Form.serialize($(fromID));	
	url=encodeURI(url);
	getHTML(url,pars,el);
}

// khong the phong to cua so
function openPopUp(url, windowName, w, h, scrollbar) {
   var winl = (screen.width - w) / 2;
   var wint = (screen.height - h) / 2;
   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar ;
   win = window.open(url, windowName, winprops);
   if (parseInt(navigator.appVersion) >= 4) { 
       	win.window.focus(); 
   } 
}

//tai noi dung tu mot page
function getHTML(url, pars, el, callback) {
	var complete = function(originalRequest) {		
	};	
	if (callback)
		complete = callback;

	if (url.substring(0, 1) == '/') {
		url = root_url + url;
	}
	// Does URL begin with http?
	else if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}

	var reportError = function(request) {
		try {
			$(el).innerHTML = "Sorry. There was an error.";
		}
		catch (e) {
			alert("Sorry. There was an error.");
		}
	};
	$(el).innerHTML = '<div style="position: relative; margin-left: auto; margin-right: auto; text-align: center; padding: 5px 5px 5px 5px"><img src="images/loading.gif"></div>';	
	var myAjax = new Ajax.Updater(
		{success: el},
		url,
		{
			method: 'get',
			parameters: pars,
			evalScripts: true,
			//asynchronous: false,
			onFailure: reportError,
			onComplete: complete
		}
	);
}

function InsertEmText(text)
{
	osh.InsertText(text);
	return false;
}

function getMem(p) {
	getHTML('ajax/_member.php?p='+p, '', 'box_member');	
}

function addCommentCandidate(mem_id) {
	
var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;

var email = $('comment_email').value;

var content = $('content').value;

if (email == '') 	{
	alert('Vui lòng nhập email của bạn');
	Form.Element.focus('comment_email');
	return false;
}
		
if (email != '' && email.match(re)==null) {
	alert('Email của bạn không hợp lệ.');
	Form.Element.focus('comment_email');
	return false;
}

if (content == '') 	{
	alert('Vui lòng nhập nhận xét của bạn về thí sinh này.');
	Form.Element.focus('content');
	return false;
}

pars = Form.serialize($("fPostComment"));
getHTML("ajax/_get_comment_candidate.php?do=add&mem_id="+mem_id, pars, "ext_post_comment");
return false;
}

function addCommentExam(exam_id) {

var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;

var email = $('comment_email').value;

var content = $('content').value;

if (email == '') 	{
	alert('Vui lòng nhập email của bạn');
	Form.Element.focus('comment_email');
	return false;
}
		
if (email != '' && email.match(re)==null) {
	alert('Email của bạn không hợp lệ.');
	Form.Element.focus('comment_email');
	return false;
}

if (content == '') 	{
	alert('Vui lòng nhập nhận xét của bạn về bài dự thi này.');
	Form.Element.focus('content');
	return false;
}

pars = Form.serialize($("fPostComment"));
getHTML("ajax/_get_comment_exam.php?do=add&exam_id="+exam_id, pars, "ext_post_exam");
return false;
}

function setVoteValue(num) {
$('num_vote').value = num;
}

GB_myShow = function(caption, url, /* optional */ height, width, callback_fn) {
  var num_vote = $('num_vote').value;	
	if(num_vote) {
		var options = {
        caption: caption,
        height: height || 250,
        width: width || 480,
        fullscreen: false,
        show_loading: true,
        callback_fn: callback_fn
    }
		url = url+"&num_vote="+num_vote;
    var win = new GB_Window(options);
    return win.show(url);	
	}else {
		alert("Vui lòng chọn số điểm đánh giá của bạn");	
		return false;
	}
}

function viewResult(mem_id) {
	Element.scrollTo('comment');
	getHTML('ajax/_get_result.php?mem_id='+mem_id, '', 'comment');
}

function changeTab (id, url) {	
	getHTML(url,'','box_member');
}

function showStatus () {
var dem=0;
for ( i=0;i < document.fMain.elements.length ; i++ ){
	if ( document.fMain.elements[i].type=="checkbox" && document.fMain.elements[i].checked==true ){
		dem++;
		window.status = "Số thí sinh đang được chọn là "+dem;
	}	
}
}

function doAddValue32() {	
	var dem=0;
	var value = "";
	var fav = "";
	for ( i=0;i < document.fMain.elements.length ; i++ ){
		if ( document.fMain.elements[i].type=="checkbox" && document.fMain.elements[i].checked==true ){
			dem++;
			value = value+','+document.fMain.elements[i].value;
		}
		if ( document.fMain.elements[i].type=="radio" && document.fMain.elements[i].checked==true ){
			fav = document.fMain.elements[i].value;
		}
	}
	if(dem==32 && fav!="") {		
		value = value.slice(1,value.length);
		$('num_vote').value=value;
		GB_myShow ('Đánh giá','../vote.php?method_id=4&do=vote32&fav='+fav);
	}else {
		alert ('Bạn vui lòng chọn đủ 32 thí sinh để đánh giá, và chọn thí sinh mà bạn yêu thích nhất.');	
		return;
	}
}