/*
	Switch a form text control into a file form control
	and adjust the link to enable inverse operation

*/

function switchToBrowse(a, p){
	e1 = $(p);
	e1.style.display = "none";
	e1.nextSibling.nextSibling.nextSibling.style.display = "none";
	e1.disabled = true;
	e2 = $(p + "file");
	e2.style.display = "inline";
	e2.disabled = false;
	a.innerHTML = "enter PDB";
	a.onclick = function(){ switchToText(this, this.parentNode.parentNode.htmlFor); }
	
}

/*
	Switch a form file control into a text form control
	and adjust the link to enable inverse operation

*/

function switchToText(a, p){
	e1 = $(p);
	e1.style.display = "inline";
	e1.nextSibling.nextSibling.nextSibling.style.display = "inline";
	e1.disabled = false;
	e2 = $(p + "file");
	e2.style.display = "none";
	e2.disabled = true;
	a.innerHTML = "upload file";
	a.onclick = function(){ switchToBrowse(this, this.parentNode.parentNode.htmlFor); }
	e1.focus();
}


function toggleLigSelection(id){
	var newState = $('l'+id+'0').checked;
	var i = 0;
	while($('l'+id+i)){
			$('l'+id+i).checked = newState;
			i++;
	}
}

function toggleChainsSelection(id,e){
	var newState = e.checked;
	var boxes = document.getElementsByName("c"+id+"[]");
	for(var i=0;i<boxes.length;i++){
		boxes[i].checked = newState;
	}
}

function processCommentForm(){
	if($('comment-form-name').value == ''){
		$('comment-form-name-err').innerHTML = 'Please specify a name';
		return false;
	} else {
		$('comment-form-name-err').innerHTML = '';
	}
	if($('comment-form-institution').value == ''){
                $('comment-form-institution-err').innerHTML = 'Please specify an institution';
                return false;
        } else {
		$('comment-form-institution-err').innerHTML = '';
	}
	$('comment-form-submit').disabled = true;
    	$('comment-form-submit').value = "Sending.  Please Wait...";

	ajaxCaller.getPlainText('process_comment.php?'+serialize($('comment-form')), function my_callback_function(text){
		if(text != 'OK')
			alert('There was an error processing your request.');
		else
			alert('Thank You!');
		$('comment-form-submit').disabled = false;
	        $('comment-form-submit').value = "Submit Comment";
		hideCommentForm();

	});
	
	return false;
}


function toggleSelGroup(p,sel){
	for(var i=1;i<=3;i++){
		var el = $('grp'+p+i);
		if(el != null)
			el = el.getElementsByTagName('input');
		else
			continue;
		var s  = ( sel == i ) ? false : true;
		for(var j=0;j<el.length;j++){
			el[j].disabled = s;
		}
	}
	var b = $('grp'+p+'3').getElementsByTagName('button')[0];
	if(sel == 3){
		b.disabled = false;
	} else {
		b.disabled = true;
	}
}

var jmol_select_none = 'select not ( hetero and not water ); spacefill off; color atoms cpk; select none;';

function getSelectionForJmolApplet(p,type,value){
  $('loader'+p).style.display = 'inline';
  ajaxCaller.shouldEscapeVars = false;
  var options = new Object();
  options['id'] = id;
  options['t'] = type;
  options['p'] = p;
  options['v'] = escape(value);
  ajaxCaller.get('get_selection.php', options, function my_callback_function(text, headers, callingContext){
			    $('loader'+callingContext).style.display = 'none';
			    if(text.substr(0,5) == 'ERROR'){
				jmolScript(jmol_select_none,p-1);
			      	alert('There was an error processing your request.' + "\n" + text.substr(6));
			    } else {
			      	jmolScript(text,p-1); // execute received script in jMol Applet for the specified protein
			    }
		 }, false, p);
}

function getLigandsValues(form, name){
	var cbs = form.elements['l'+name+'[]'];
	if(cbs[0].checked)
		return 'all';
	var ligs = new Array();
	for(var i=0;i<cbs.length;i++){
		if(cbs[i].checked)
			ligs.push(cbs[i].value);
	}
	if(!ligs.length){
		return '';
	}
	return ligs.join(',');
}

function getLigSelectionForJmolApplet(p,form){
	var value = getLigandsValues(form,p);
	if(!value){
		jmolScript(jmol_select_none,p-1);
		return;
	}
	return getSelectionForJmolApplet(p,'b',value);
}

function getCavValue(form,name){
	var rbs = form.elements['v'+name];
	for(var i=0;i<rbs.length;i++){
		if(rbs[i].checked)
			return rbs[i].value;
	}
	// if no value is checked we check the firse one
	rbs[0].checked = true;
	return rbs[0].value;
}

function getCavSelectionForJmolApplet(p, form){
	var value = getCavValue(form, p);
	if(value!='none')
		return getSelectionForJmolApplet(p,'c',value);
	else {
		jmolScript(jmol_select_none,p-1);
		return true;
	}
}

function getResSelectionForJmolApplet(p){
	var value = util.trim($('r'+p).value);
	if(value != '')
		return getSelectionForJmolApplet(p,'r',value);
	else {
		jmolScript(jmol_select_none,p-1);
	}
}

function checkValidSelection(){
	var atoms1 = jmolGetPropertyAsArray("atomInfo","selected",0);
	var atoms2 = jmolGetPropertyAsArray("atomInfo","selected",1);

	if(!atoms1.length || !atoms2.length){
		alert('Please provide a non-empty selection for both the structures.');
		return false;
	} else {
		return true;
	}
}

function checkValidSelectionBatch(){
	// almost equal to the above function but works whent there is only one applet
	var atoms1 = jmolGetPropertyAsArray("atomInfo","selected",0);
	
	if(!atoms1.length){
		alert('Please provide a non-empty atoms selection.');
		return false;
	} else {
		return true;
	}
}
