//
// Created by Harm Jan Plat.
//
//Group email: Checks checkboxes from every user.
var checkflag = "false";
function check(option) {
	var i = 0;
	if (option == 'editor'){
		checkName = 'contentEditorlist[]'
	}
	else{
		checkName = 'all'
	}
	if (checkflag == "false") {
		while(temp = document.getElementById(i)){
			if ((temp.name == checkName) || (checkName == 'all')){
				temp.checked = true;
			}
			i++
		}
		checkflag = "true";
		if (checkName == 'all'){
			return "Uncheck All"; 
		}
		else {
			return "Uncheck editors"	
		}
	}
	else {
		while(temp = document.getElementById(i)){
			if ((temp.name == checkName) || (checkName == 'all')){
				temp.checked = false;
			}
			i++
		}
		checkflag = "false";
		if (checkName == 'all'){
			return "Check All"; 
		}
		else {
			return "Check editors"	
		}
	}
}

//Editor Repositorie: Opens window for lock-info from locked files. 
function runFileInfo(date, time, user, unlock){
	window.open("/~zuurman/extensions/externalWikiSettings.php?date="+date+"&user="+user+"&time="+time+"&unlock="+unlock , 'LockInfo','width=268,height=180,scrollbars=no,toolbar=no,location=no');

} 

//Editor Repositorie: open confirm pop-up for deleting a entrie from database.
function deleteFile(fileId, php_self){
	var x = window.confirm("Deleting this file means it will be removed entirely from the server. Are you sure?");
	if (x){
		window.location=php_self+"?title=Special:ControlPanel&tab=repos&removeEntrie="+fileId
	}
	else{
		window.location=php_self+"?title=Special:ControlPanel&tab=repos"
	}
}

//Pubmed Search; Runs: /extensions/control_Panel/nierCheckPubmedAjaxFunctions.php
function pubmedAjaxFunction(POST)
  {
  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 false;
        }
      }
    }
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4)
        {
 	if (POST == 'removeAuthor' || POST == 'addAuthor' || POST == 'updateDB'){
 		window.location.reload()
 	}
 	else{
		document.getElementById(POST).innerHTML=xmlHttp.responseText;
		document.getElementById('loadingGif').innerHTML=""	
 		}
 	}
      }
	// runs loading screen in front of window.
	document.getElementById('loadingGif').innerHTML="<div id=\"loading\"><img src=\"/~zuurman/extensions/nierStylesheets/styleItems/load.gif\"></div>"
	if (POST == 'searchAuthor'){
		author = document.getElementById('addAuthor')
		QUERIE = POST+'&querie='+author.value
	}
	else if (POST == 'updateDB'){
		i = 0
		flag = true
		repl = new RegExp("^,")
  		query= ''
		while((temp = document.getElementById(i+'checkPubmed')) && (flag)){
			query += ","+temp.value 
			i++
			if (i >= 650){
				alert("You want to download more then 650 publications at the same time. The first 650 will be downloaded. Please run Pubmed update again to complete update.")
				flag = false
			}
		}
		if (i > 50){
			var x=window.confirm("You are going to upload "+i+" journals. This will take some time. Are you sure?")
			if (x){
				confirm = true
				}
			else{
				window.location.href=window.location.href
			}
		}
		if (query == ''){
			alert("There are no publications to update! Database is up to date or run \"check Pubmed\" ")
			window.location.reload()
		}
		query = query.replace(repl, '')
		QUERIE = POST+'&pmids='+query+'&amount='+i
		
	}
	else if (POST == 'addAuthor'){
		i = 0
		query= ''	
		repl = new RegExp("^,")
  		while(temp = document.getElementById(i+'searchAuthor')){
			query += ","+temp.value 
			i++
		}
		if (i > 50){
			var x=window.confirm("You are going to upload "+i+" journals. This will take some time. Are you sure?")
			if (x){
				confirm = true
				}
			else{
				window.location.href=window.location.href
			}
		}		
		query = query.replace(repl, '')	
		author = document.getElementById('addAuthor')
		QUERIE = POST+'&pmids='+query+'&author='+author.value+'&amount='+i
	}

	else if (POST == 'removeAuthor'){

		selectField=document.getElementById('personlist')
		userid = selectField.value
		QUERIE = POST+'&userid='+userid
	}
	
	else if (POST == 'checkPubmed'){
		QUERIE = POST
	}
	 
    file = "/~zuurman/extensions/externalWikiSettings.php?Pubmed="+QUERIE;
    xmlHttp.open("GET", file ,true);
    xmlHttp.send(null);
  }

// Runs /extensions/control_Panel/lockReposFiles.php
function ajaxLockFile(lock, fileid, user, locktype, fileName)
  {
  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 false;
        }
      }
    }
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4){
  		window.location.reload()
 	}
      }
	if ((locktype == 'lock') && (lock > 0)){
		var x = window.confirm("Are you sure you want to lock "+fileName+" for "+lock+"hrs?")
		
	}
	else if (locktype == 'unlock'){
		var x=window.confirm("Are you sure you wish to unlock the "+fileName+"?")
		if (x){
			lock = 0
		}
		else{
			window.location.reload()
		}
	}	
    file = "/~zuurman/extensions/externalWikiSettings.php?fileId="+fileid+"&wgUser="+user+"&locktime="+lock;
    xmlHttp.open("GET", file ,true);
    xmlHttp.send(null);
  }


function deleteCheck(user){

var x=window.confirm("Are you sure you wish to delete "+user+"?")
	if (x){
		window.location.href=window.location.href+'&ManFunction=delete&EditUser='+user
	}
	else{
	}
} 
