/*-----
Javascript Document for Aminal Genome Size Database
Author: James Nicol, (www.glossopteris.com), 2005
-----*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { window.onload = func; } 
  else { window.onload = function() { oldonload(); func(); } }
}

function prepareLinks(){
	nav = document.getElementById("content_text");
	links = nav.getElementsByTagName("a");
	for (var i=0; i<links.length; i++){ //this section applies behaviours to the style buttons
		if (links[i].getAttribute("id")=="c_r_t"){
			links[i].onmouseover = function(){ MM_swapImage('c_r_t','','images/customise_results_table_02.jpg',1); }
			links[i].onmouseout = function(){ MM_swapImgRestore(); }
		}
		if (links[i].getAttribute("id")=="d_t_e"){
			links[i].onmouseover = function(){ MM_swapImage('d_t_e','','images/data_to_excel_02.jpg',1); }
			links[i].onmouseout = function(){ MM_swapImgRestore(); }
		}
	}
}

function url_build(control,type){
	select_name = control.name.split("-");
	if(select_name[0]=='delete'){
		window.location = 'edit.php?type='+type+'&action=delete&id='+control.options[control.selectedIndex].value+'';
	}else if(select_name[0]=='edit'){
		window.location = 'edit.php?type='+type+'&action=edit&id='+control.options[control.selectedIndex].value+'';
	}else if(select_name[1]=='add'){
		window.location = 'edit.php?type='+type+'&action=add&format='+control.options[control.selectedIndex].value+'';
	}else if(select_name[0]=='edit_ref'){
		window.location = 'edit.php?type='+type+'&action=edit&id='+control.options[control.selectedIndex].value+'';
	}else if(select_name[0]=='del_ref'){
		window.location = 'edit.php?type='+type+'&action=del&id='+control.options[control.selectedIndex].value+'';
	}
}

function input_update(control){
	select_name = control.getAttribute("name").split("-");
	input_name = select_name[0];
	//document.write(input_name);
	document.getElementById(input_name).value = control.options[control.selectedIndex].value;
	control.selectedIndex = 0;
}

function category_build(control) {
	select_name = control.getAttribute("name").split("-");
	input_name = select_name[0];
	if(!document.getElementById(input_name)) return false;
	var input = document.getElementById(input_name);
	if(control.options[control.selectedIndex].value=='') return false;
	var category;
	if (input.value==''){
		category = control.options[control.selectedIndex].value;
	}else{
		if(input.value.indexOf(control.options[control.selectedIndex].value)!=-1) return false;
		category = input.value+', '+control.options[control.selectedIndex].value;
	}
	input.value = category;
	control.selectedIndex = 0;
}


function validateEmail() {
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	if (re.test(document.dataRequestForm.request_email.value)) {
		return true
	}
	document.getElementById("email_error").innerHTML = '<font color=\"#FF0000\">Please enter a valid email address</font><br />';
	return false
}


/*----- Functions for Cookies -----*/
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 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;
}


/*----- Functions for some dynamic effects -----*/

function showQuote(language){
	if (language=='French'){
		document.getElementById("quote_text").innerHTML="\"Il est hors de doute que l’étude systématique, de la teneur absolue du noyau en acide désoxyribonucléique, à travers de nombreuses espèces animales, puisse fournir des suggestions intéressantes en ce qui concerne le problème de l’évolution.\""
		document.getElementById("quote_link").innerHTML="<a href=\"javascript:showQuote('English')\">Translate</a>"
	}else{
		document.getElementById("quote_text").innerHTML="\"The systematic study of the absolute nuclear DNA content across numerous animal species will, without doubt, provide many interesting suggestions concerning the question of evolution.\""
		document.getElementById("quote_link").innerHTML="<a href=\"javascript:showQuote('French')\">Original</a>"
	}
}


/*----- Image rollover functions from Dreamweaver -----*/
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


/*----- AJAX scripts for dynamic drop downs on Search Page -----*/

var http_request = false;
var menu_name = "";

function startRequest(Control) {
	if (Control.options[Control.selectedIndex].value==""){
	}else{
		if (Control.name=='phylum;sub_phylum'){ menu_name='class'; }
		else if (Control.name=='class'){ menu_name='order_name'; }
 		else if (Control.name=='order_name'){ menu_name='family'; }
		else if (Control.name=='family'){ menu_name='species'; }
		url = 'menu_data.php?field='+Control.name+'&value='+Control.options[Control.selectedIndex].value+'&menu='+menu_name;
		makeRequest(url, menu_name);
	}
}

function makeRequest(url, div_id) {
	http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
        	http_request.overrideMimeType('text/xml');
        }
   	} 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 = updateMenu;
	menu_name = div_id;
    http_request.open('GET', url, true);
    http_request.send(null);
}

function getobjnostyle(elementID){
if (document.getElementById)
return document.getElementById(elementID)
else if (document.all)
return document.all.elementid
}
	
function updateMenu() {
  	if (http_request.readyState == 4) {
      	if (http_request.status == 200) {
        	if (menu_name=='class'){
				getobjnostyle(menu_name).innerHTML = http_request.responseText;
				getobjnostyle('order_name').innerHTML = '<select name="order_name" size="3"><option value="">[-- none --]</option></select>';
				getobjnostyle('family').innerHTML = '<select name="family" size="3"><option value="">[-- none --]</option></select>';
				getobjnostyle('species').innerHTML = '<select name="species" size="3"><option value="">[-- none --]</option></select>';
			}else if (menu_name=='order_name'){
				getobjnostyle(menu_name).innerHTML = http_request.responseText;
				getobjnostyle('family').innerHTML = '<select name="family" size="3"><option value="">[-- none --]</option></select>';
				getobjnostyle('species').innerHTML = '<select name="species" size="3"><option value="">[-- none --]</option></select>';
			}else if (menu_name=='family'){	
				getobjnostyle(menu_name).innerHTML = http_request.responseText;
				getobjnostyle('species').innerHTML = '<select name="species" size="3"><option value="">[-- none --]</option></select>';
			}else{
				getobjnostyle(menu_name).innerHTML = http_request.responseText;
			}
		} else {
          	alert('There was a problem with the request.');
        }
   	}
}

function enableInput(SearchSign, SearchValue) {
	if (SearchSign.options[SearchSign.selectedIndex].text == "between") { SearchValue.disabled = false; }
	else { SearchValue.disabled = true; }
}

function setEnable() {
	enableInput(document.advanced_search.c_value_sign, document.advanced_search.c_value_2)
}

/*----- Script for Contact Window -----*/

var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 100;	// maximum image size.

if (document.getElementById || document.all){
	document.write('<div id="contactpageid">');
	document.write('</div>');
}

function getcontactpageobj(){
	if (document.getElementById){ return document.getElementById("contactpageid").style; }
	else if (document.all){ return document.all.contactpageid.style; }
}

function getcontactpageobjnostyle(){
	if (document.getElementById){ return document.getElementById("contactpageid"); }
	else if (document.all) { return document.all.contactpageid; }
}

function showcontactpage(){
	
	newHTML = '<div id="contactpageid_div">';
	newHTML += '<div id="contactclose"><a href="javascript:hidecontactpage();"><img src="images/contact_close.gif" alt="Close"></a></div>';
	newHTML += '<h3>Contact Details</h3>';
	newHTML += '<p class="ryan_name">Dr. T. Ryan Gregory</p>';
	newHTML += '<table>';
	newHTML += '<tr><td valign="top"><strong>Mailing Address:</strong></td><td>Dept. of Integrative Biology<br>University of Guelph<br>50 Stone Rd. E.<br>Guelph, Ontario<br>N1G 2W1 Canada.</td></tr>';
	newHTML += '<tr><td><strong>Email:</strong</td><td><a href="mailto:&#x64;&#x61;&#x74;&#x61;&#x62;&#x61;&#x73;&#x65;&#x40;&#x67;&#x65;&#x6e;&#x6f;&#x6d;&#x65;&#x73;&#x69;&#x7a;&#x65;&#x2e;&#x63;&#x6f;&#x6d;">&#x64;&#x61;&#x74;&#x61;&#x62;&#x61;&#x73;&#x65;&#x40;&#x67;&#x65;&#x6e;&#x6f;&#x6d;&#x65;&#x73;&#x69;&#x7a;&#x65;&#x2e;&#x63;&#x6f;&#x6d;</a></td></tr><table>';
	newHTML += '<center><p>For general questions and comments, please use the <a href="forum">discussion forum</a>.</p></center>';
	newHTML += '</div>';
 
	getcontactpageobjnostyle().innerHTML = newHTML;
	getcontactpageobj().visibility="visible";
	fadeInDiv('contactpageid',0);
	
}

function hidecontactpage(){
	getcontactpageobj().visibility="hidden"
}

function fadeInDiv(divId, opacity) {
  if (opacity <= 100) {
	setOpacity(divId, opacity);
	opacity += 5;
	window.setTimeout("fadeInDiv('"+divId+"',"+opacity+")", 25);
  }
}

function setOpacity(divId, opacity) {
  var div = document.getElementById(divId);
  opacity = (opacity == 100)?99.999:opacity;
  div.style.filter = "alpha(opacity:"+opacity+")";
  div.style.KHTMLOpacity = opacity/100;
  div.style.MozOpacity = opacity/100;
  div.style.opacity = opacity/100;
}

/*----- Addition of Load Events -----*/
addLoadEvent(MM_preloadImages('images/home_button_f2.jpg','images/search_button_f2.jpg','images/summary_button_f2.jpg','images/faq_button_f2.jpg','images/refs_button_f2.jpg','images/submit_data_button_f2.jpg','images/links_button_f2.jpg'));
addLoadEvent(prepareLinks);
