var el_names = ["inchi","inchikey","mw","charge","smiles"];
//var val_names = ["InChI: ", "MW: ", "Charge: "];

// from AJAX article on root.cz

function send_xmlhttprequest( obsluha, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
	return false;
    }
    xmlhttp.open(method, url); 
    xmlhttp.onreadystatechange = function() { obsluha(xmlhttp);};

    if (headers) { 
	for (var key in headers)
	    { xmlhttp.setRequestHeader(key, headers[key]); }
    }
    
    xmlhttp.send(content);
    return true;

}




function inchi_ajax_handler( xmlhttp) {

    switch (xmlhttp.readyState) {

    case 1:
	show_progress( "sending data...");
	break;
    case 2:
	show_progress( "awaiting response...");
	break;
    case 3:
	show_progress( "reading response...");
	break;
    case 4:
	hide_progress()
	if (xmlhttp.responseXML.getElementsByTagName( "output")[0].getAttribute("result") == "success")  {
	    // the retrieval was successful
	    document.getElementById( "output").className = "small";

	    for (var i in el_names) {
		name = el_names[i];
		el = document.getElementById( name);
		inel = xmlhttp.responseXML.getElementsByTagName( name)[0];
		if (inel != null)
		    el.innerHTML = inel.firstChild.nodeValue;
	    }

	    mol = xmlhttp.responseXML.getElementsByTagName( "molfile")[0];
	    if (mol != null) {
		document.getElementById( 'outmolfile').setAttribute( 'href', mol.getAttribute( 'href'));
	    }

	    pict = xmlhttp.responseXML.getElementsByTagName( "picture")[0];
	    if (pict != null) {
		pict_path = pict.getAttribute( "src");
		if (pict_path != "") {
		    document.getElementById( 'outpng').className = "";
		    document.getElementById( 'outpng').src = pict_path;
		}
		else {
		    alert( "Picture generation failed :(");
		}
	    }
	    else {
		document.getElementById( 'outpng').className = 'hidden';
	    }

	    /*alert( xmlhttp.responseXML.getElementById( "output"));
	      alert( xmlhttp.responseXML.getElementById( "picture"));*/
	}
	else {
	    alert( "Something went wrong, sorry.");
	}
	break;
    }
}



function ask_inchi_to_mol() {

    var inchi = document.inchi_input.inchi_text.value;
    var coords = document.inchi_input.coords.checked;
    var smiles = document.inchi_input.smiles.checked;
    var pict = document.inchi_input.pict.checked;
    var info = document.inchi_input.info.checked;
    var molfile = document.inchi_input.molfile.checked;
    //send_xmlhttprequest( inchi_ajax_handler, 'GET', encodeURI( 'http://gentoo:8080/index?inchi='+inchi+"&gen_coords="+gen_coords));

    send_xmlhttprequest( inchi_ajax_handler, 'GET', encodeURI( '/ajax/inchi_ajax?inchi='+inchi+"&coords="+coords+"&info="+info+"&smiles="+smiles+"&pict="+pict+"&molfile="+molfile));
}



function ask_smiles_to_mol() {

    var smiles = document.smiles_input.smiles_text.value;
    var coords = document.smiles_input.coords.checked;
    var pict = document.smiles_input.pict.checked;
    var info = document.smiles_input.info.checked;
    var molfile = document.smiles_input.molfile.checked;
    //send_xmlhttprequest( inchi_ajax_handler, 'GET', encodeURI( 'http://gentoo:8080/index?inchi='+inchi+"&gen_coords="+gen_coords));



    send_xmlhttprequest( inchi_ajax_handler, 'GET', encodeURI( '/ajax/smiles_ajax?smiles='+smiles+"&coords="+coords+"&info="+info+"&pict="+pict+"&molfile="+molfile));
}




function show_progress( text) {

    prog = document.getElementById( "progress");
    if (prog != null) {
	prog.innerHTML = text;
	prog.className = "progress";
    }
}


function hide_progress() {
    prog = document.getElementById( "progress");
    if (prog != null) {
	prog.className = "hidden";
    }

}



function start_upload() {
    document.getElementById( "output").className = "hidden";
    document.getElementById( 'error').className = "hidden";
    if (document.forms[0].inchi_text.value || document.forms[0].smiles_text.value || document.forms[0].molfile_file.value) { 
	show_progress('Please, wait. The request is processed.');
	return true;
    }
    else {
	return false;
    }
}


function process_answer() {

    hide_progress();

    var a = answer; //document.getElementById('answer');

    if (a.error != null) {
	document.getElementById( "output").className = "hidden";
	if (document.forms[0].inchi_text.value || document.forms[0].smiles_text.value || document.forms[0].molfile_file.value) { 
	    
	    var er = document.getElementById( 'error');
	    er.innerHTML = "The conversion was not successful, please check your input or submit a bug report. We are sorry for the inconvenience.<br />The following error was reported:<br /><i>"+a.error+"</i>";
	    er.className = ""
	    return;
	}
	else 
	    return;
    }

    if (!(document.forms[0].inchi_text.value || document.forms[0].smiles_text.value || document.forms[0].molfile_file.value)) {
	return;
    }
       
    document.getElementById( "output").className = "small";
    document.getElementById( 'error').className = "hidden";

    for (var i in el_names) {
	name = el_names[i];
	el = document.getElementById( name);
	if (a[name] != null) {
	    el.innerHTML = a[name];
	}
    }

    var mol = a.molfile;
    if (mol != null) {
	document.getElementById( 'outmolfile').setAttribute( 'href', mol);
    }

    var name = a.outname;
    if (name != null) {
	document.getElementById( 'outname').innerHTML = name;
    }

    var pict = a.picture;
    if (pict != null) {
	if (pict == 'failed') {
	    alert( "Picture generation failed :(");
	    document.getElementById( 'outpng').className = 'hidden';
	}
	else {
	    document.getElementById( 'outpng').className = "";
	    document.getElementById( 'outpng').src = pict;
	}
    }
    else {
	document.getElementById( 'outpng').className = 'hidden';
    }
    
}



function set_onload() {

    document.getElementById( 'answer').setAttribute( 'onload',  "process_answer();");
    //document.getElementById( 'answer').onload = process_answer;

}


// -------------------- InChIKey checking support --------------------

function start_keycheck_upload() {
    document.getElementById( "output").className = "hidden";
    document.getElementById( 'error').className = "hidden";
    if (document.forms[0].inchi_key.value) { 
	//show_progress('Please, wait. The request is processed.');
	return true;
    }
    else {
	return false;
    }
}


function process_keycheck_answer() {
    hide_progress();
    var a = answer; //document.getElementById('answer');

    if (!document.forms[0].inchi_key.value) {
	return;
    }

    if (a.error != null) {
	document.getElementById( "output").className = "hidden";
	document.getElementById( "output").innerHTML = "";
	var er = document.getElementById( 'error');
	er.innerHTML = "<b>Error:</b>"+a.error;
	er.className = "";
	return;
    }

    if (a.verified == true) {
	document.getElementById( "output").innerHTML = "The submitted InChIKey is valid";
	document.getElementById( "output").className = "ok";
    }
    else {
	document.getElementById( "output").innerHTML = "The submitted InChIKey is <b>not</b> valid!";
	document.getElementById( "output").className = "error";
    }

    document.getElementById( 'error').className = "hidden";
    document.getElementById( 'error').innerHTML = "";
}



// -------------------- InChIKey generation support --------------------

function start_keygen_upload() {
    document.getElementById( "output").className = "hidden";
    document.getElementById( 'error').className = "hidden";
    if (document.forms[0].inchi_text.value) { 
	//show_progress('Please, wait. The request is processed.');
	return true;
    }
    else {
	return false;
    }
}


function process_keygen_answer() {
    hide_progress();
    var a = answer; //document.getElementById('answer');

    if (!document.forms[0].inchi_text.value) {
	return;
    }

    if (a.error != null) {
	document.getElementById( "output").className = "hidden";
	document.getElementById( "output").innerHTML = "";
	var er = document.getElementById( 'error');
	er.innerHTML = "<b>Error:</b>"+a.error;
	er.className = "";
	return;
    }
    if (a.key != null) {
	document.getElementById( "output").innerHTML = "<b>Result</b>:<br/> " + a.key;
	document.getElementById( "output").className = "ok";
    }

    document.getElementById( 'error').className = "hidden";
    document.getElementById( 'error').innerHTML = "";
}



