// JavaScript Document

function fnOpenLocation(targeturl){
	self.document.location = targeturl;
}

function start_voting() { 
	var cat_string = document.forms['rounds'].category_id.value; 
	if(cat_string != ''){
		var cat_array = cat_string.split(",");
		var cat_complete = parseInt(cat_array[0]);
		var cat_id = parseInt(cat_array[1]);
		var newurl = new String('');
		
		if(cat_complete==1){
			newurl = "start_review.php?cat_id="+cat_id;
		}else{
			newurl = "vote.php?category_id="+cat_id+"&start=1";
		}
		self.document.location = newurl;
	}else{
		alert('Please select a category for review or voting');
	}
}

function continue_voting(){
	self.document.location = 'vote.php';
}

function fnVoteReview(i,catID){
	var voteCount = parseInt(i);
	if(voteCount==0){
		alert('You have no votes to review in this category');
	}else{
		self.document.location = 'start_review.php?cat_id='+catID;
	}
}

function fnPauseVoting(stat_id){
	self.document.location = "rounds.php?stat_id=" + stat_id;
}

function code() {
	if(pageLoaded){
		var key = event.keyCode - 48;
		var voteForm = document.forms['vote_form'];
		var set = voteForm.set.value;
	
		if ( (key >= 1) && (key <= 7)) {
			var rGrp = voteForm.elements['vote'];
			for(var i=0; i < rGrp.length; i++){
				if(!rGrp[i].disabled && key == i+1){
					rGrp[i].click();
					voteForm.set.value = 1;
					break;
				}
			}
		}
	  
		if (key == -35) {         	 
			if (voteForm.set.value==1) {
				voteForm.submit()
			}
		} 
	}
}

function enterkey(){
	var low_val;
	var high_val;
	if (event.keyCode != 8) {
		key = event.keyCode - 48;
		if (key > 48) {
			low_val = 48;
			high_val = 55;
		} else {
			low_val = 1;
			high_val = 7;
		}
		if ( ( (event.keyCode == 13) && (document.vote_form.vote.value=="") ) || ( (key < low_val) && (event.keyCode != 13) ) || ( (key > high_val) && (event.keyCode != 13) ) ) { 	
			window.alert("You must enter a vote between 1 and 7!")  
			return false;
		}
	}
	return true;
}

function testSubmit() {
  if (document.vote_form.set.value == 1) {
    return true;
  }
  else {
      window.alert("You must enter a vote to continue!")
      return false;
  }
}

function flip() {
  document.vote.flip.value=" Submit Changes ";
  return true;
}

function fnPageControl(key,cat_id,page,order){
	self.document.location = 'vote_review.php?key='+key+'&cat_id='+cat_id+'&page='+page+'&x_order='+order +"&exact_score_filter="+document.getElementById('exact_score_filter').value;
}

function fnPageLoaded(){
	var thisform = document.forms['vote'];
	if(document.getElementById){
		document.getElementById("page_loading").innerHTML='';
	}
	thisform.cmd_exit.disabled = false;
	thisform.x_order[0].disabled = false;
	thisform.x_order[1].disabled = false;
	thisform.x_order[2].disabled = false;
}

function fnLockPage(i){
	var oForm = document.forms['vote'];
	switch(i){
	case 1:
		if(oForm.cmd_first){
			oForm.cmd_first.disabled = true;
			oForm.cmd_previous.disabled = true;
			oForm.cmd_next.disabled = true;
			oForm.cmd_last.disabled = true;
		}
		for(var x=0; x < oForm.x_order.length; x++){
			oForm.x_order[x].disabled = true;
		}
		oForm.cmd_reset.disabled = false;
		oForm.cmd_submit.disabled = false;
		oForm.cmd_exit.disabled = true;
		break;
	case 2:
		oForm.reset();
		if(oForm.cmd_first){
			oForm.cmd_first.disabled = false;
			oForm.cmd_previous.disabled = false;
			oForm.cmd_next.disabled = false;
			oForm.cmd_last.disabled = false;
		}
		for(var x=0; x < oForm.x_order.length; x++){
			oForm.x_order[x].disabled = false;
		}
		oForm.cmd_reset.disabled = true;
		oForm.cmd_submit.disabled = true;
		oForm.cmd_exit.disabled = false;
		
		break;
	case 3:
		oForm.mod.value = 1;
		oForm.submit();
		break;
	default:
		self.document.location = history.go(0);
	}
}

function init() {
	document.onkeydown = keyDown
}

function keyDown() {
	var keycode = event.keyCode;
//  Please leave this for testing purposes !!!
//	var realkey = String.fromCharCode(event.keyCode);
//	alert("keycode: " + keycode + "\nrealkey: " + realkey);
/*	if(keycode==13){
		self.close();
	}
	if(keycode==27){
		self.close();
	}
*/
	if(keycode!=''){
		fnUpdateStats();
	}
}

function fnAdjustTiming(){
	document.forms['delay'].submit();
}

function fnChangeCategory(){
	document.forms['delay'].change_category.value=1;
	document.forms['delay'].slide.value=0;
	document.forms['delay'].submit();
}

function fnChangeRound(){
	document.forms['delay'].change_round.value=1;
	document.forms['delay'].slide.value=0;
	document.forms['delay'].submit();
}

function fnJumpToSlide(i){
	slide = (parseInt(i)-1);
	document.forms['delay'].slide.value=slide;
	document.forms['delay'].submit();
}

function fnPauseSlideShow(){
	clearTimeout(timeoutID);
	if(document.getElementById){
		document.getElementById("pause_message").innerHTML='<strong><font size="2" face="Arial, Helvetica, sans-serif">To resume the slide show click \"Next\".</font></strong>';
	}
}

function fnSlideShowControl(ctl,int,num,id,rnd,key){
	var control = parseInt(ctl);
	var slide = parseInt(num);
	if(control==0){
		slide = slide-2;
		if(slide < 1){
			slide = 0;
		}
	}
	self.document.location = 'slide_show.php?key='+key+'&time='+int+'&slide='+slide+'&category_id='+id+'&sel_round='+rnd;
}

function fnViewObject(mode,objID,appView,retPage){
	if(!appView){
		fnPauseSlideShow();
	}
	targeturl = './view_object.php?obj_id='+objID+'&ret='+retPage;
	fnOpenLocation(targeturl);
}

function fnViewApp(mode,objID,retPage){
	targeturl = './view_object.php?app_id='+objID+'&ret='+retPage;
	fnOpenLocation(targeturl);
}

function fnUpdateStats(){
	self.document.forms['stat_tracker'].submit();
	//history.back();
}

function fnExitVoteReview(url){
	self.document.location = url;
}
