function toppage(){
    window.location = "/index.html";
}

function receiveMousePick(file, model, chain, res, atom, altloc, resName, atomName, x, y, z){
    window.show_pickuped(file, model, chain, res, atom, altloc, resName, atomName, x, y, z);
}

function show_pickuped(file, model, chain, res, atom, altloc, resName, atomName, x, y, z) {
    var atom = new String(atomName);
    var newatomName = atom.replace(/^\s+|\s+\$/g, "");
    document.jv_form.model_name.value = model;
    document.jv_form.chain_name.value = chain;
    document.jv_form.res_no.value = res;
    document.jv_form.res_name.value = resName;
    
    if(chain==cchain){
	if(cmodel==model){
	    resid = 'res'+res;
	}else if(cmodel==-1 && model==''){
	    resid = 'res'+res;
	}else{
	    resid = '';
	}
    }else{
	resid = '';
    }

    if(model!=''){
	select_residue(res+':'+chain+' and */'+model, res, resid, 1);
    }else{
	select_residue(res+':'+chain, res, resid, 1);
    }
}

function show_interface(pdburl, cmd){
    if(jogl){
	document.jv.executeCommand('zap');
	document.jv.loadPDB(pdburl);
	document.jv.executeCommand(cmd);
    }else{
	document.jv.getSubApplet().executeCommand('zap');
	document.jv.getSubApplet().loadPDB(pdburl);
	document.jv.getSubApplet().executeCommand(cmd);
    }
}

function showBindingState(pdburl, cmd, bd, model, chain){
    cbd = bd;
    cmodel = model;
    cchain = chain;
    
    cmd = cmd += coloring_cmd(ccolor);
    //window.alert(cmd);
    if(jogl){
	document.jv.executeCommand('zap');
	document.jv.loadPDB(pdburl);
	document.jv.executeCommand(cmd);
    }else{
	document.jv.getSubApplet().executeCommand('zap');
	document.jv.getSubApplet().loadPDB(pdburl);
	document.jv.getSubApplet().executeCommand(cmd);
    }
    unselect_residues();

    err = '';
    for(var i=1;i<=max_clstid;i++){
	try{
	    BpInfo=(document.all)?document.all("bpinfo"+i).style:document.getElementById("bpinfo"+i).style;
	    BpInfo.display = "none";
	}catch(e){
	    err = e;
	}
    }

}

function coloring_cmd(color){
    if(color==0){ //n_bp
	cmd = '';
	for(var i=0;i<nint.length;i++){
	    if(nint[i].length == 0){ continue; }
	    scmd = "";
	    for(var j=0;j<nint[i].length;j++){
		if(cbd == 0){
		    if(cmodel == -1){
			scmd += nint[i][j]+":"+cchain;
		    }else{
			scmd += nint[i][j]+":"+cchain+" and */"+cmodel;
		    }
		    scmd += ",";
		    
		}else{ //use mapping
		    qresno = nint[i][j];
		    resno = res_mapper[cbd][qresno];
		    if(resno){
			if(cmodel == -1){
			    scmd += resno+":"+cchain;
			}else{
			    scmd += resno+":"+cchain+" and */"+cmodel;
			}
			scmd += ",";
			
		    }
		}
	    }
	    if(scmd.substring(scmd.length-1, scmd.length) == ',' ){
		scmd = scmd.substring(0, scmd.length-1);
	    }
	    if(scmd==''){continue;}
	    cmd += "select "+ scmd + ";";
	    if(i>9){
 		cmd += "color atoms "+colortable[9]+';';
	    }else{
		cmd += "color atoms "+colortable[i]+';';
	    }
	} 
	ccolor=0;
    }else if(color==1){ //group
	cmd = 'color group;';
	if(cmodel == -1){ cmd = "select *:"+cchain+";"+cmd; }
	else{ cmd = "select *:"+cchain+"/"+cmodel+";"+cmd; }
	ccolor=1;
    }else if(color==2){ //temparature
	cmd = 'color temperature';
	if(cmodel == -1){ cmd = "select *:"+cchain+";"+cmd; }
	else{ cmd = "select *:"+cchain+"/"+cmodel+";"+cmd; }
	ccolor=2;
    }else if(color==3){
	cmd = '';
	for(var i=min_rid;i<=max_rid;i++){
	    if(cbd == 0){
		resno = i;
	    }else{
		resno = res_mapper[cbd][i];
	    }
		
	    if(cmodel == -1){
		cmd += "select "+i+":"+cchain+";";
	    }else{
		cmd += "select "+i+":"+cchain+" and */"+cmodel+";";
	    }
	    cmd += "color atoms [255,"+disorder[i]+","+disorder[i]+"];";
		
	}
	ccolor=3;
    }else{
	cmd = 'color [204,204,153];';
	if(cmodel == -1){ cmd = "select *:"+cchain+";"+cmd; }
	else{ cmd = "select *:"+cchain+"/"+cmodel+";"+cmd; }
	ccolor=999;
    }
    return cmd;
}

function coloring(color){
    cmd = coloring_cmd(color);
    if(jogl){
	document.jv.executeCommand(cmd);
    }else{ //Applet_veiwer'
	document.jv.getSubApplet().executeCommand(cmd);
    }
}


function jv_cmd(cmd){
    if(jogl){
	document.jv.executeCommand(cmd);
    }else{ //Applet_veiwer'
	document.jv.getSubApplet().executeCommand(cmd);
    }
}

function select_residue(cmd, qresno, res_id, pickup){
    //window.alert(cmd);
    if(cbd != 0 && pickup == 0){
	cmd = ''
	resno = res_mapper[cbd][qresno];
	if(resno){
	    if(cmodel == -1){
		cmd += resno+":"+cchain;
	    }else{
		cmd += resno+":"+cchain+" and */"+cmodel;
	    }
	}else{
	    if(cmodel == -1){
		cmd += qresno+":"+cchain;
	    }else{
		cmd += qresno+":"+cchain+" and */"+cmodel;
	    }
	}
    }

    if(jogl){
	document.jv.executeCommand('select all;spacefill off;');
	document.jv.executeCommand('select '+cmd+';spacefill on;');
    }else{
	document.jv.getSubApplet().executeCommand('select all;spacefill off;');
	document.jv.getSubApplet().executeCommand('select '+cmd+';spacefill on;');
    }
    
    unselect_residues();

    try{
	tMenu=(document.all)?document.all(res_id).style:document.getElementById(res_id).style;
	tMenu.border = "2px solid red";
    }catch(e){
	return e;
    }

    if(pickup == 0){
	document.jv_form.model_name.value = cmodel;
	document.jv_form.chain_name.value = cchain;
	document.jv_form.res_no.value = qresno;
	document.jv_form.res_name.value = '';
    }
}

function unselect_residues(){
    var err;
    err = '';
    for(var i=min_rid;i<max_rid+1;i++){
	try{
	    tMenu=(document.all)?document.all("res"+i).style:document.getElementById("res"+i).style;
	    tMenu.border = "0px solid red";
	}catch(e){
	    err = e;
	}
    }
    return err;
}
    

function change_display(id_show, id_hide){
    tMenu=(document.all)?document.all(id_show).style:document.getElementById(id_show).style;
    tMenu.display = "block";
    tMenu=(document.all)?document.all(id_hide).style:document.getElementById(id_hide).style;
    tMenu.display = "none";
}
    
function checkJS(){
    window.alert("Worked!");
}
    
function escramblec(user,domain){
    var a,b,c,d,e,f,g,h,i;
    a='<a href=\"mai';
    b=user;
    c='\">';
    a+='lto:';
    b+='@';
    f='';
    b+=domain;       
    e='Contact</a>';
    document.write(a+b+c+e);
}

function escrambles(user,domain){
    var a,b,c,d,e,f,g,h,i;
    a='<a href=\"mai';
    b=user;
    c='\" class=\"header_menu\">';
    a+='lto:';
    b+='@';
    f='';
    b+=domain;       
    e='Contact</a>';
    document.write(a+b+c+e);
}

function changeBindingPartnerState(chkboxid, chain, mdl){
    if(mdl=="0"){
	cmd = "select *:"+chain+";";
    }else{
	cmd = "select *:"+chain+"/"+mdl+";";
    }
    
    if(bpstate[chkboxid]){
	cmd += "cartoon on;";
	bpstate[chkboxid] = false;
    }else{
	cmd += "cartoon off;";
	bpstate[chkboxid] = true;
    }
    
    jv_cmd(cmd);
}

function changeBindingPartnerShape(chain, mdl, shape){
    if(mdl=="0"){
	cmd = "select *:"+chain+";";
    }else{
	cmd = "select *:"+chain+"/"+mdl+";";
    }
    
    cmd += "spacefill off;";
    cmd += shape+" on;";
    jv_cmd(cmd);
}

function changeShape(shape, elm){
    cmd = 'spacefill off;'+shape+' on ;';
    if(cmodel == -1){ cmd = "select *:"+cchain+";"+cmd; }
    else{ cmd = "select *:"+cchain+"/"+cmodel+";"+cmd; }
    jv_cmd(cmd);
    cshape = shape;
    
    if(elm){
	for(var i=0;i<3;i++){
	    st =(document.all)?document.all("shape"+i).style:document.getElementById("shape"+i).style;
	    st.border = "1px solid white";
	}
	elm.style.border  = "1px solid blue";
    }
}