function ResetForm()
{    
    location.href = "index.cgi?LINK_LOC=reset";    
}

function GetURL ()
{
    
    var getUrl = "index.cgi?LINK_LOC=bookmark";                    
        
    getUrl += getUrlCompForEntryField("seq"); 
    getUrl += getUrlCompForEntryField("PRIMER5_START"); 
    getUrl += getUrlCompForEntryField("PRIMER5_END"); 
    getUrl += getUrlCompForEntryField("PRIMER3_START"); 
    getUrl += getUrlCompForEntryField("PRIMER3_END"); 
    getUrl += getUrlCompForEntryField("PRIMER_LEFT_INPUT"); 
    getUrl += getUrlCompForEntryField("PRIMER_RIGHT_INPUT");
    getUrl += getUrlCompForEntryField("PRIMER_PRODUCT_MIN"); 
    getUrl += getUrlCompForEntryField("PRIMER_PRODUCT_MAX"); 
    getUrl += getUrlCompForEntryField("PRIMER_NUM_RETURN");     
    getUrl += getUrlCompForEntryField("PRIMER_MIN_TM"); 
    getUrl += getUrlCompForEntryField("PRIMER_OPT_TM"); 
    getUrl += getUrlCompForEntryField("PRIMER_MAX_TM"); 
    getUrl += getUrlCompForEntryField("PRIMER_MAX_DIFF_TM");
    getUrl += getUrlCompForOptionsField($("PRIMER_ON_SPLICE_SITE")); 
    getUrl += getUrlCompForEntryField("SPLICE_SITE_OVERLAP_5END");         
    getUrl += getUrlCompForEntryField("SPLICE_SITE_OVERLAP_3END");
    getUrl += getUrlCompForCheckedField($("SPAN_INTRON")); 
    getUrl += getUrlCompForEntryField("MIN_INTRON_SIZE");
    getUrl += getUrlCompForEntryField("MAX_INTRON_SIZE");    
    getUrl += getUrlCompForCheckedField($("SEARCH_SPECIFIC_PRIMER"));    
    getUrl += getUrlCompForEntryField("ORGANISM");         
    getUrl += getUrlCompForOptionsField("PRIMER_SPECIFICITY_DATABASE");            
    getUrl += getUrlCompForOptionsField("PRIMER_SPECIFICITY_MISMATCH");            
    getUrl += getUrlCompForOptionsField("PRIMER_3END_SPECIFICITY_MISMATCH");
    getUrl += getUrlCompForOptionsField("MISMATCH_REGION_LENGTH");
    getUrl += getUrlCompForEntryField("PRODUCT_SIZE_DEVIATION");
    getUrl += getUrlCompForCheckedField($("ALLOW_TRANSCRIPT_VARIANTS"));
    getUrl += getUrlCompForOptionsField("HITSIZE");
    getUrl += getUrlCompForOptionsField("EVALUE"); 
    getUrl += getUrlCompForOptionsField("WORD_SIZE"); 
    getUrl += getUrlCompForOptionsField("MAX_CANDIDATE_PRIMER");
    getUrl += getUrlCompForEntryField("PRODUCT_MIN_TM");
    getUrl += getUrlCompForEntryField("PRODUCT_OPT_TM");
    getUrl += getUrlCompForEntryField("PRODUCT_MAX_TM");
    getUrl += getUrlCompForEntryField("PRIMER_MIN_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_OPT_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_MAX_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_MIN_GC");
    getUrl += getUrlCompForEntryField("PRIMER_MAX_GC");
    getUrl += getUrlCompForEntryField("GC_CLAMP");
    getUrl += getUrlCompForEntryField("POLYX");
    getUrl += getUrlCompForEntryField("SELF_ANY");
    getUrl += getUrlCompForEntryField("SELF_END");
    getUrl += getUrlCompForOptionsField("PRIMER_MISPRIMING_LIBRARY");     
    getUrl += getUrlCompForCheckedField($("NO_SNP"));
    getUrl += getUrlCompForCheckedField($("LOW_COMPLEXITY_FILTER"));
    getUrl += getUrlCompForEntryField("MONO_CATIONS");
    getUrl += getUrlCompForEntryField("DIVA_CATIONS");
    getUrl += getUrlCompForEntryField("CON_ANEAL_OLIGO");
    getUrl += getUrlCompForEntryField("CON_DNTPS");
    getUrl += getUrlCompForOptionsField("SALT_FORMULAR");    
    getUrl += getUrlCompForOptionsField("TM_METHOD");   
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MIN_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_OPT_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MAX_SIZE");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MIN_TM");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_OPT_TM");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MAX_TM");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MAX_GC");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_OPT_GC_PERCENT");
    getUrl += getUrlCompForEntryField("PRIMER_INTERNAL_OLIGO_MIN_GC");
    getUrl += getUrlCompForCheckedField($("PICK_HYB_PROBE"));    
    getUrl += getUrlCompForCheckedField($("nw1"));//NEWWIN
    getUrl += getUrlCompForCheckedField($("nw2"));//NEWWIN
    
    alert("Please bookmark the current page so that you can directly use the saved URL to do primer-blast with your current parameters");    
    
    if (navigator.userAgent.match(/safari/i)) {
        window.open(getUrl,"PrimerBookmark");
    }
    else {
        location.href = getUrl;    
    }    
}    

function AddOneOrgField()
{
    var newOrgField = " <input name=\"ORGANISM\" class=\"multiOrg\" size=\"55\" id=\"ORGANISM" + $("numOrg").value +
                       "\" sgDict=\"taxids_sg\" sgStyle=\"SG_green\" />";        
    utils.insertInHtml(newOrgField,$("orgs"));
    $("numOrg").value++;             
}

function AddOrgField()
{    
    AddOneOrgField();
    AddOneOrgField();   
    NSuggest_init();     
}

function checkFormDefaultDiff()
{
    var defValNodes = cssQuery(".checkDef");
	var count = 0;	
	for(i=0; i < defValNodes.length; i++) {	  
	  //var par = utils.getParent(defValNodes[i]);	
	  //if(utils.hasClass(par,"all") || utils.hasClass(par,$("selectedProg").value)) { 	    	    	    
	    if(defValNodes[i].type == "text" && defValNodes[i].value == "") {
	        defValNodes[i].value = defValNodes[i].getAttribute("defVal");
	    }
	    count += diffFromDefault(defValNodes[i]); 	    
	    if(defValNodes[i].type == "checkbox") {
	        utils.addEvent(defValNodes[i], "click", function() { diffFromDefault(this);  }, false); 
	    }
	    else {
	        utils.addEvent(defValNodes[i], "change", function() { diffFromDefault(this);  }, false); 
	    }
	    //alert(defValNodes[i].id + " " + par.className + " " + $("selectedProg").value + "count=" + count);
	  //}
	}
		
	if(count > 0) {
	    $("diffMes").style.display = "inline";
	    $("diffMesTop").style.display = "inline";
	    $("NUM_DIFFS").value = count; 
	}	
	if($("NUM_OPTS_DIFFS").value != "0") {  
	    //alert("Change toggle options");
        utils.addClass($("toggleOptions"), "showing");
        utils.removeClass($("toggleOptions"), "hiding");
        utils.addClass($("moreopts"), "shown");
        utils.removeClass($("moreopts"), "hidden");
	}
}	

function OnLoadHandler() {	    

    utils.addEvent($("resetlink"), "click", ResetForm,false);					   
						
    //utils.addEvent($("clearseq"), "click", clearSeq, false);
						
						

    var clinks = cssQuery("[class='clearlink']");
    for (var i = 0; i < clinks.length; i++) {
     utils.addEvent(clinks[i], "click", clearSeq, false);
   }
   var imgButtons = cssQuery("[class='prbutton']");
   for (var i = 0; i < imgButtons.length; i++) {    
     utils.addEvent(imgButtons[i], "click", function() {                    
                    if($("nw1").checked) $("searchForm").target = "Blast_Results_for_" + Math.floor(Math.pow(10,10) *Math.random());
                    else $("searchForm").target = "";                                        
                    $("searchForm").submit();}, false);    
      utils.addEvent(imgButtons[i], "mouseover", function() {this.src = this.getAttribute("mouseovImg");}, false);
      utils.addEvent(imgButtons[i], "mouseout", function() {this.src = this.getAttribute("mouseoutImg");}, false);
      utils.addEvent(imgButtons[i], "mousedown", function() {this.src = this.getAttribute("mousedownImg");}, false);
      utils.addEvent(imgButtons[i], "mouseup", function() {this.src = this.getAttribute("mouseupImg");}, false);
   }
   links = cssQuery("[name='getURL']");
   for (var i = 0; i < links.length; i++) {      
      utils.addEvent(links[i], "click",  GetURL, false);              
   }
   
   var newwins = cssQuery("[name='NEWWIN']");  
   for (var i = 0; i < newwins.length; i++) {    
      utils.addEvent(newwins[i], "click", function() {checkAll("NEWWIN",this.checked);}, false);
   }
   
   utils.addEvent($("AddOrg"), "click", AddOrgField, false);
   
   checkFormDefaultDiff();  						
}

utils.addEvent(window, 'load', OnLoadHandler, false); 

/* If user agent has "safari" in it, include safari-specific stylesheet. */ 
if (navigator.userAgent.match(/safari/i)) {
    document.write("<link rel='stylesheet' type='text/css' href='css/safari-blastn.css'  media='screen'/>");
}
