var pmConn;
var cachedSubmitterString = "";

function clearSubmitterList(id) {
	if (id == 'a' ){
		document.getElementById("gnsubmitters").innerHTML = "";
	}
	else {
		document.getElementById("spsubmitters").innerHTML = "";
	}
}


function selectSubmitterGn() {
	if (document.getElementById("submittersList") != null) {
		var selector = document.getElementById("submittersList");
		var selection = selector.options[selector.selectedIndex].value;
		if (selection != "") {
			document.geneDisambiguation.gnname.value = selection;
			clearSubmitterList('a');
		}
	}
}


function selectSubmitterSp() {
	if (document.getElementById("submittersList") != null) {
		var selector = document.getElementById("submittersList");
		var selection = selector.options[selector.selectedIndex].value;
		if (selection != "") {
			document.geneDisambiguation.spname.value = selection;
			clearSubmitterList('b');
		}
	}
}



function Process (searchString,name) {

	if (searchString.length < 2) {
        	clearSubmitterList(name);
        	cachedSubmitterString = "";
        	return;
    	}

	if (cachedSubmitterString == searchString) {
        	return;

	}

    	findSubmitter(searchString,name);
    	cachedSubmitterString = searchString;
}


function suggestSubmitter(name) {
    var searchString;
    if (name == 'a') {
	searchString = document.geneDisambiguation.gnname.value;
	Process (searchString,name);
    }
    else {
        searchString = document.geneDisambiguation.spname.value;
	Process (searchString,name);

    }
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
// Usage value = getCheckedValue(document.forms['radioExampleForm'].elements['number'])


function getSelectedCheckbox(buttonGroup) {
	// Go through all the check boxes. return an array of all the ones
	// that are selected (their position numbers). if no boxes were checked,
	// returned array will be empty (length will be zero)
	var retArr = new Array();
	var lastElement = 0;
	if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
		for (var i=0; i<buttonGroup.length; i++) {
			if (buttonGroup[i].checked) {
				retArr.length = lastElement;
				retArr[lastElement] = i;
				lastElement++;
			}
		}
	} 
	else { // There is only one check box (it's not an array)
		if (buttonGroup.checked) { // if the one check box is checked
			retArr.length = lastElement;
			retArr[lastElement] = 0; // return zero as the only array value
		}
	}
	return retArr;
} 
// Ends the "getSelectedCheckbox" function


function getSelectedCheckboxValue(buttonGroup) {
	// return an array of values selected in the check box group. if no boxes
	// were checked, returned array will be empty (length will be zero)
	var retArr = new Array(); // set up empty array for the return values
	var selectedItems = getSelectedCheckbox(buttonGroup);
	if (selectedItems.length != 0) { // if there was something selected
		retArr.length = selectedItems.length;
		for (var i=0; i<selectedItems.length; i++) {
			if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
				retArr[i] = buttonGroup[selectedItems[i]].value;
			}
			else{ // It's not an array (there's just one check box and it's selected)
				retArr[i] = buttonGroup.value;// return that value
			}
		}
	}
	return retArr;
}
// Ends the "getSelectedCheckBoxValue" function



function makeSuggestionBox(submittersList,pname) {
    if (submittersList) {
	var re = new RegExp("<Id>([^<]+)<\/Id>","g");
	var m = null;
	var iOption = 0;
	if (document.getElementById("submittersList") != null) {
		document.getElementById("submittersList").options.length = 0;
	}
	else {

		if (pname == 'a' ){
			var selector = "<select id='submittersList' size=5 name='submittersList' onClick='selectSubmitterGn();'></select>\n";
			document.getElementById("gnsubmitters").innerHTML = selector;
		}
		else {
			var selector = "<select id='submittersList' size=5 name='submittersList' onClick='selectSubmitterSp();'></select>\n";
			document.getElementById("spsubmitters").innerHTML = selector;
		}
	}
	while(m = re.exec(submittersList)) {
		document.getElementById("submittersList").options[iOption] = new Option(m[1]);
		document.getElementById("submittersList").options[iOption].value = m[1];
		iOption++;
	}
    }
    else {
    	if (pname == 'a'){
              document.getElementById("gnsubmitters").innerHTML = "<font color='red'>Warning: Unkwnow Gene symbols (short names) or there might be no name associated with your species </font>";
	}
	else {
	      document.getElementById("spsubmitters").innerHTML = "<font color='red'>Warning: Either there are no species asssociated with your query gene or unknown species' common name or Scientific name: [genus species] Carl linnaeus form</font>";
	}
    }

}

function findSubmitter(searchString,name) {

    if( pmConn && pmConn.readyState != 0 ){
        pmConn.abort();
    }

    pmConn = getConn();

    radiovalue = getCheckedValue(document.forms['geneDisambiguation'].elements['radiobutton']);
    var checkBoxV = getSelectedCheckboxValue(document.forms['geneDisambiguation'].elements['denovo']);
    gnname = document.geneDisambiguation.gnname.value;
    spname = document.geneDisambiguation.spname.value;
    gnname = gnname.replace("Enter gene symbol",""); 
    spname = spname.replace("Enter species name",""); 
    
    //radiovalue = 'as';

    if( pmConn ) {
    	if (name == 'a'){
        	pmConn.open('GET','/gi/submittergnnameAll.php?searchString='+ searchString +'&radio='+ radiovalue +'&species='+ spname +'&checkbox='+ checkBoxV , true);
	}
	else {
		pmConn.open('GET','/gi/submitterspname.php?searchString='+ searchString +'&gene=' +gnname +'&checkbox='+ checkBoxV , true);
	}
	pmConn.onreadystatechange = function() {
        	if( pmConn.readyState == 4) {
			makeSuggestionBox(pmConn.responseText,name);
		}
        };
    	pmConn.send(null);
    }
}

function getConn() {
    var c = null;
    try {
        c = new ActiveXObject('Msxml2.XMLHTTP.4.0');
    } catch(e) {
        try {
            c = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(oc) {
            c = null;
        }
    }
    if( !c && typeof XMLHttpRequest != 'undefined') {
        c = new XMLHttpRequest();
    }
    return c;
}



