/* iframe rezise function to make sure iframe height fits the contents - does not work cross-domain! */
function calcHeight(thisFrame, currentHeight) {
    //find the height of the internal page
    var newHeight=document.getElementById(thisFrame).contentWindow.document.body.scrollHeight;
    //change the height of the iframe
    if (navigator.appVersion.indexOf("MSIE") != -1) { //if it is IE
        document.getElementById(thisFrame).height = newHeight;
    }
    else { document.getElementById(thisFrame).height = (newHeight-currentHeight)+20; }    
}

var savedFrameSrc = '';
function saveFrameSrc(iframe) {
        savedFrameSrc = document.getElementById(iframe).src;
        //alert(savedFrameSrc);
}
function getFrameSrc(iframe) {
    if (savedFrameSrc != '') {
        document.getElementById(iframe).src = savedFrameSrc;
    }
}

function getParamFromURL() {
    var urlString = String(window.location);
    var paramString;
    if (urlString.indexOf("SEQUENCE=") != -1) {  
        //only plug values into searchbox if there is the sequence parameter passed
            paramString = urlString.substring(urlString.indexOf("SEQUENCE=")+9, urlString.length);
            document.getElementById('seqText').value = paramString;
            cleanSeqData();
        }
    else if ( (urlString.indexOf('seqserver.html') != -1) && (urlString.indexOf("patent=") != -1) ) {
        //only load the sequenceserver search page with results if patent parameter is passed
            paramString = urlString.substring(urlString.indexOf("patent=")+7, urlString.length);
            document.getElementById('seqServerFrame').src = "http://www.patentlens.net/sequenceserver/search?patent="+paramString;
        }
}    

function getHTML(fragment_url) {
    //var blastHelp = document.getElementById('blastHelp');
    //blastHelp.src = url;	
    var xmlhttp = null;                                  // Initialize the AJAX variable.
    if (window.XMLHttpRequest) {                         // Does this browser have an XMLHttpRequest object?
        xmlhttp=new XMLHttpRequest();                     // Yes -- initialize it.
    } else {                                             // No, try to initialize it IE style
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   //  Wheee, ActiveX, how do we format c: again?
    }                                                    // End setup Ajax.
    if (xmlhttp==null) {                                 // If we couldn't initialize Ajax...
        alert("Your browser doesn't support AJAX.");      // Sorry msg.                                               
        return false                                      // Return false, couldn't set up ajax
    }
    
    var element = document.getElementById('helpText');
    toggleHelpAreaSize('clickOpen');
    
    element.innerHTML = '<p style="text-align: center;"><em>Sorry. There was a problem loading this file ...</em></p>';
    xmlhttp.open("GET", fragment_url);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            element.innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);		
}
	        
function insertNthChar(string,chr,nth) {
    var output = '';
    for (var i=0; i<string.length; i++) {
    if (i>0 && i%60 == 0)
        output += chr;
        output += string.charAt(i);
    }
    return output;
}
    
function cleanSeqData() {
    seqText = document.getElementById('seqText');
    
    if (seqText.value.charAt(0) == '>') { //if there is no sequence header data provided - as in the sample, preserve the first line
        var allLines = seqText.value.split("\n");
        var headerLine = allLines[0];
        var contentLines = '';
    
        var i = 1; //put other lines together, not including first line				
        for (i=1; i<allLines.length; i++) { 
            contentLines = contentLines+allLines[i];
        }
        var cleanedContents = contentLines.replace(/([^a-zA-Z])/g, '');
        cleanedContents = insertNthChar(cleanedContents,"\r",60) ; //make sure the string is wrapped after 60 column (important for firefox)
        seqText.value = headerLine+'\n'+(cleanedContents.toUpperCase());
    }

    else { //if there is no sequence header data provided clean all
        var cleanedContents = seqText.value.replace(/([^a-zA-Z])/g, '');
        cleanedContents = insertNthChar(cleanedContents,"\r",60) ; //make sure the string is wrapped after 60 column (important for firefox)
        seqText.value = cleanedContents.toUpperCase();
    }
}

function checkSelection () {
    program = document.getElementById('PROGRAM');
    database = document.getElementById('DATALIB');
    sequence = document.getElementById('seqText');
    upload = document.getElementById('seqUpload');
    loc = location.href;
    selectionOK = false;
    
    if (sequence.value != '' || upload.value != '') {  
        if ( !loc.match('megablast.html')) {
        //make sure at either sequence is entered or file path is supplied before checking for db/program compatibility. for BLAST ONLY.
            if ((program.selectedIndex == 0 || program.selectedIndex == 3 || program.selectedIndex ==4) && (database.selectedIndex >= 4)) { 
                    //blastn, tblastn, tblastx, only with nt databases
                selectionOK = false;
                alert ("Incorrect Selection!\nPlease ensure the PROGRAM and DATABASE selection is compatible (see help for details)");
                program.focus();
            }
            else if ((program.selectedIndex == 1 || program.selectedIndex == 2) && (database.selectedIndex < 4)) { 
                    //blastp, blastx, only with aa databases
                selectionOK = false;
                alert ("Incorrect Selection!\nPlease ensure the PROGRAM and DATABASE selection is compatible (see help for details)");
                program.focus();
            }
            else { 
                selectionOK = true; 
                /*alert ("Good Selection!");*/
            }
        }
        else { selectionOK = true; } //no need to check program/database compatibility if it is MEGABLAST
    }
    else {
        alert ('Please enter your sequence data!');
        sequence.focus();
    }
    return selectionOK; 
}
    
    
function toggleShowHide(element) {
    if (document.getElementById(element).style.display =='block') {
        document.getElementById(element).style.display ='none';
    }
    else {
        document.getElementById(element).style.display='block';
    }
}
 
function toggleHelpAreaSize(command) {
    if (command == 'clickClose') {
        document.getElementById('helpArea').style.display ='none';
        document.getElementById('bodyBlock').style.height ='100%';
        document.getElementById('helpOpen').style.display ='block';
        document.getElementById('helpClose').style.display ='none';
    }
    else if (command == 'clickOpen') {
        document.getElementById('helpArea').style.display ='block';
        document.getElementById('helpArea').style.height='25%';
        document.getElementById('bodyBlock').style.height ='75%';
        document.getElementById('helpOpen').style.display ='none';
        document.getElementById('helpClose').style.display ='block';
//         if (document.getElementById('helpText').innerHTML =='') {
//             //document.getElementById('helpText').innerHTML = '<p style="font-weight:bold; font-size:12px">HELP INFORMATION</p><p>Help files will appear here when you click the help links </p>';
//             getHTML('docs/index.html');
//         }
    }
}

function toggleDisabled(elementBtn, elementDisable, elementEnable) {
    document.getElementById(elementEnable).disabled = true;
    document.getElementById(elementDisable).disabled = false;
    document.getElementById(elementBtn.id).checked = true;
}
 
function customReset() {
    document.getElementById('buttonsTop').style.display='block'; 
    document.getElementById('moreOptions').style.display='none';
    document.getElementById('seqText').disabled = false;
    document.getElementById('seqUpload').disabled = true;
    document.getElementById('seqUpload').value = '';
}
 
function check_use_megablast(element) {
    // Checks blast program for megablast compat. and activates/inactivates the megablast option
    var this_select = element.value;
    var megablast = document.MainBlastForm.MEGABLAST;
    if (this_select == 'blastn') {
        //megablast.checked  = true;
        megablast.disabled = false;
    } else {
        megablast.checked  = false;
        megablast.disabled = true;
    }
}
 
