var http_request = false;
var http_request_timeout;


function create_dropdown() {
   
    var select = document.getElementById("shaplo");     
    
    //REMOVE ALL OPTIONS
    select.options.length = null; 


    var seldef = true;
    var tdef   = true;

    for (var i=0;i<hgrps.length;i++) {
	select.options[i] = new Option(hgrps[i][1],hgrps[i][0],seldef,tdef);
	seldef = false;
	tdef   = false;
    }
}

function set_info(hg) {

    var hid;
    var hindex;
    var pindex;

    //FIND SELECTED HAPLOGROUP 
    for (var e=0; e<hg.length;e++) {
	if (hg[e].selected) {
	    hid = hg[e].value;
	}
    }
	
    //FIND HAPLOGROUP IN ARRY hgrps. SET AS hindex
    for  (var i=0; i<hgrps.length;i++) {
	if (hid == hgrps[i][0]) {
	    hindex = i;
	    pid    =  hgrps[i][2]
	}
    }

    //FIND PARRENT HAPLOGROUP IN ARRY hgrps. SET AS pindex
    for  (var i=0; i<hgrps.length;i++) {
	if (pid == hgrps[i][0]) {
	    pindex = i;
	}
    }

    
    //REMOVE INFO
    document.getElementById("name").removeChild(document.getElementById("name").firstChild);
    document.getElementById("parrent").removeChild(document.getElementById("parrent").firstChild);
    document.getElementById("mutation").removeChild(document.getElementById("mutation").firstChild);

    //ADD INFO
    document.getElementById("name").appendChild(document.createTextNode(hgrps[hindex][1]));
    document.getElementById("parrent").appendChild(document.createTextNode(hgrps[pindex][1]));
    document.getElementById("mutation").appendChild(document.createTextNode(hgrps[hindex][3]));

}


function makeRequest(url,cform) {

    var submitstr = formData2QueryString(cform);

    http_request = false;

    if (window.XMLHttpRequest) { 
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
	    http_request.overrideMimeType('text/xml');
	}
    } else if (window.ActiveXObject) { 
	try {
	    http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    try {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	}
    }
    
    if (!http_request) {
	alert('Ende :( No XMLHTTP-Instance found');
	return false;
    }
    http_request.onreadystatechange = get_result;
    http_request.open('POST', url, true);
    http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http_request.send(submitstr);   
    http_request_timeout = window.setTimeout("abort_xhttp_request()", 20000); // 20 Sec
}



function get_result() {
    if (http_request.readyState == 4) {
	if (http_request.status == 200) {
	    document.getElementById("result").innerHTML = http_request.responseText;
	}
	else {
	    alert('XMLHtmlRequest failed : ' + http_request.status);
	}
    }
    window.clearTimeout(http_request_timeout);
}

function abort_xhttp_request() {
   http_request.abort();
   document.getElementById("result").innerHTML = "<b>XMLHtmlRequest-Timeout</b>";
}




// The var docForm should be a reference to a <form>
function formData2QueryString(docForm) {

    var strSubmitContent = '';
    var formElem;
    var strLastElemName = '';
	
    for (i = 0; i < docForm.elements.length; i++) {
		
	formElem = docForm.elements[i];
	switch (formElem.type) {
	    // Text fields, hidden form elements
	case 'text':
	case 'hidden':
	case 'password':
	case 'textarea':
	case 'select-one':
	    strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
		break;
				
	    // Radio buttons
	case 'radio':
	    if (formElem.checked) {
		strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
		    }
	    break;
	    
	    // Checkboxes
	case 'checkbox':
	    if (formElem.checked) {
		// Continuing multiple, same-name checkboxes
		if (formElem.name == strLastElemName) {
		    // Strip of end ampersand if there is one
		    if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
			strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
		    }
		    // Append value as comma-delimited string
		    strSubmitContent += ',' + escape(formElem.value);
		}
		else {
		    strSubmitContent += formElem.name + '=' + escape(formElem.value);
		}
		strSubmitContent += '&';
		strLastElemName = formElem.name;
	    }
	    break;
	    
	}
    }
    
    // Remove trailing separator
    strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
    //alert ("Parameter send:" + strSubmitContent);
    return strSubmitContent;
}


function display_abstract (num) {

    var ab = document.getElementById("abstract"+num);

    if (ab.style.display == "none") {
	ab.style.display = "inline";
    }
    else {
	ab.style.display = "none";
    }
}
