var http_request = false;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var prevsearch = '';

function searchFocus(from, inout){
	if (inout == "in"){
		from.className = "s_focus";
		if (from.value == "type your search here"){
			from.value = "";
		}
	}else{
		from.className = "s_nofocus";
		if (from.value == ""){
			from.value = "type your search here";
		}
	}
}

function submitSearch(){
	document.searchform.submit();
	return false;
}

function hover(what){
	if (what.className){
		what.className = null;
	}else{
		what.className = 'row_over';
	}
}

function hover_server(what){
	if (what.className == 'chosen'){
		// do nothing
	}else{
		if(what.className){
			what.className = null;
		}else{
			what.className = 'row_over';
		}
	}
}

function choose_s(which, what){
	trArray = document.getElementById('servertable').tBodies[0].rows;
	// alert('found ' + trArray.length);
	i=0;

	while(trArray[i]){
		// alert('checking ' + trArray[i].nodeName);
		if (trArray[i].className == 'chosen'){
			trArray[i].className = null;
		}
		i++;

	}
	
	checkbox = document.getElementById("server_" + which);
	createCookie('default_server',which,180);
	// alert ('choosing ' + which);
	checkbox.checked = true;
	what.className = "chosen";
}

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

function goDownload(){
	var captcha = document.getElementById('captcha_guess');
	if (captcha.value == ''){
		alert ('You must enter a validation code to continue.');
		captcha.focus();
	}else{
		window.location = "/connect/?sess=" + Math.round(Math.random() * 1000000000000000, 0);
	}
}


function doNoRefRedirect(url){
  // alert('hello!');
  hide_referer = window.open ("about:blank", "hide_referer", "statusbar=0,toolbar=0,location=0,height=600,width=800,scrollbars=1");
  
  hide_referer.document.write("<html>");
  hide_referer.document.write("<head>");
  hide_referer.document.write("</head>");
  hide_referer.document.write("<body>");
  hide_referer.document.write("<meta http-equiv=\"refresh\" content=\"0;url=" + url + "\">");
  hide_referer.document.write("</body>");
  hide_referer.document.write("</html>");
  hide_referer.document.close ();
}

function makeSyncRequest(doiturl){
	http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			  http_request.overrideMimeType('text/xml');
			  // See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			  http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			  try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
			  } catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	  

	http_request.onreadystatechange = function() { alertContents(http_request); };
	http_request.open('GET', doiturl);
	http_request.send(null);	
}

function alertContents(hrequest){
		if (hrequest.readyState == 4) {
		if (hrequest.status == 200) {
			/* alert( hrequest.responseText); */
		} else {
			alert('There was a problem with the request.');
		}
	}
}