//*
function isNum(args)
{
	args = args.toString();

	if (args.length == 0)
	return false;

	for (var i = 0;  i<args.length;  i++)
	{
		if (
		    (args.substring(i,i+1) < "0" || args.substring(i, i+1) > "9") 
		     && (args.substring(i, i+1) != ".")
			 && (args.substring(i, i+1) != "e")
			 && (args.substring(i, i+1) != "E")
			 && (args.substring(i, i+1) != "-")
		   )
			{
			   return false;
			}
	}

	return true;
}

function clearForm(tranform)
{
    tranform.value = "";
    tranform.focus();	
}

function getselectedbutton(buttongroup)
	{
	 for (var i=0; i <buttongroup.length;i++)
		{
		if (buttongroup[i].checked)
			{
			return buttongroup[i].value;
			break;
			}
		}
	alert("you must select a value from the radio buttons");
	return(false);
	}
	
//Get all characters BEFORE the search string
function getFront(mainStr,searchStr){
    foundOffset=mainStr.indexOf(searchStr);
    if(foundOffset==-1){
       return null;
    }
    return mainStr.substring(0,foundOffset);
}

//Get all characters AFTER the search string
function getEnd(mainStr,searchStr){
    foundOffset=mainStr.indexOf(searchStr);
    if(foundOffset==-1){
       return null;
    }
    return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}

function replaceString(mainStr,searchStr,replaceStr){
    var front=getFront(mainStr,searchStr);
    var end=getEnd(mainStr,searchStr);
    if(front!=null && end!=null){
       return front+replaceStr+end;
    }
    return null;
}

//Insert string BEFORE the search string
function insertString(mainStr,searchStr,insertStr){
    var front=getFront(mainStr,searchStr);
    var end=getEnd(mainStr,searchStr);
    if(front!=null && end!=null){
       return front+insertStr+searchStr+end;
    }
    return null;
}

//Delete String
function deleteString(mainStr,deleteStr){
    return replaceString(mainStr,deleteStr,"");
}


function GetSingleSeq(strSeqs)
{
    var myArray = new Array;
	if (strSeqs.indexOf("\r\n") != -1)
	{
		myArray = strSeqs.split("\r\n");
		//alert("good");
	}
	else if (strSeqs.indexOf("\n") != -1)
	{
		myArray = strSeqs.split("\n");
		//alert("bad");
	}
	else
	{
	    return strSeqs;
	}
		
	var retstr = "";

	var arrSeqs = new Array();
	
    var myReg = /[\d\s]+/;	
	var flag = 0;
	for (var i=0; i<(myArray.length); i++)
	{
	   if (myArray[i].indexOf(">")!=-1)
       {
			if (flag == 1) break;   //The Line with ">"
			flag =1;
	   }
	   else
	   {
	       retstr += myArray[i];
           retstr = retstr.replace(myReg, "");		   
	   }
	}
	
	return retstr;		
}

function GetMultiSeqs(strSeqs)
{
    var myArray = new Array;
	if (strSeqs.indexOf("\r\n") != -1)
		myArray = strSeqs.split("\r\n");
	else if (strSeqs.indexOf("\n") != -1)
		myArray = strSeqs.split("\n");
	
	
	var arrSeqs = new Array();
	var str = "";
	var index="";
	
    var myReg = /[\d\s]+/;	
	for (var i=0; i<(myArray.length); i++)
	{
	   if (myArray[i].indexOf(">")!=-1)
       {
	      if (index == "")
			index=myArray[i];
		  else
		  {
		     arrSeqs[index] = str;
			 index = myArray[i];
			 str ="";
		  }
	   }
	   else
	   {
	       str += myArray[i];
           str = str.replace(myReg, "");		   
	   }
	}
	arrSeqs[index] = str;
	
	return arrSeqs;
}
//*/

function dnaexchange()
{
     //alert("good");
	 //	var seq = document.seqed.seqs.value;
	 	var retstr = document.seqed.ret.value;
		
		document.seqed.seqs.value = retstr;

}

function dnacal(cform)
{
//*
    var theOligo = new Oligo();
	var	theComplement = new Oligo();
	
    var strSeqs = cform.seqs.value;
	var arrSeqs = GetMultiSeqs(strSeqs);
	
	//WriteFile("log.txt",strSeqs);
	
	var temp;
	var CompString;
	
	var retstr = "";
	var flag = 0;
	retstr += "Name Length(bp) GC(%) MW(Dalton)\r\n";
	
	//document.dnamw.ret.value = "cal";
	for (var iKey in arrSeqs)
	{
  	    retstr += iKey;
		retstr += " ";
		
		temp=CheckBase(arrSeqs[iKey]);
		if (temp == -1)
		{
		    flag = 1;
		    break;
		}
		
		theOligo.Sequence = temp;
		theOligo.OligoCalculate();
		
	    var val = document.dnamw.dnastat.options[document.dnamw.dnastat.selectedIndex].value;
		var opt = 0;
	    if (val.indexOf("DNA") >= 0)  opt	= 1;
	
	    CompString = MakeComplement(temp, opt);
		theComplement.Sequence = CompString;
		theComplement.OligoCalculate();
		
		var mw = 0;

		if (!theOligo.hasIUpacBase)
		{
		    if (val.indexOf("ds") <0)
			{
			   mw = theOligo.mwValmin;
			}
			else
			{
				mw = theOligo.mwValmin + theComplement.mwValmin;		
			}
		}
		else
		{
		    if (val.indexOf("ds") <0)
			{
				mw = theOligo.mwValmin+" to "+ theComplement.mwValmax;
			}
			else
			{
				var min= theOligo.mwValmin+ theComplement.mwValmin;
				var max= theOligo.mwValmax+ theComplement.mwValmax;
			    mw = min+" to "+max;
			}	
		}

		retstr += theOligo.Sequence.length;
		retstr += " ";		
		
        retstr += theOligo.gcValmin;	
		retstr += " ";		
        retstr += mw;
		retstr += "\r\n";
		}

	
	if (flag == 0)
		document.dnamw.ret.value =retstr;
	else
		document.dnamw.ret.value ="";	
		//*/
		
		//cform.ret.value = "cal";
}

//*
function IsBase(theBase) {
	if ((theBase == "A") ||
		(theBase == "G") ||
		(theBase == "C") ||
		(theBase == "U") ||
		(theBase == "T")) {
			return 1;
	}
	return 0;
}



function CheckBase(theString) {
	var returnString = "";
	var cnt = 0;
	var rcnt = 0;
	var cha="";
	//alert("theString is: " + thString);
	theString = theString.toUpperCase();
	theString = RemoveNonPrintingChars(theString);
	for ( var i = 0; i < theString.length; i++) {
		cha=theString.charAt(i);
		if (IsIUpacBase(cha) || IsBase(cha))
		{
			returnString +=cha;
			cnt++;
		}else if(cha!=" " && cha!="\n"){
			alert("base # "+(cnt+1)+" :"+cha+" is not a valid base! You can use the sequence editor on this website (http://www.endmemo.com/bio/seqedit.php) to remove space, numbers of the sequence.");
			return -1;
		}
	}
	return returnString;
}

function CheckDNARNA (cha)
{
	if (IsBase(cha))
		return 1;
	else
		return 0;
}

function calcodon()
{
    var seq = document.codon.seq.value;
	seq = seq.toUpperCase();
	var letter = seq.charAt(seq.length-1);
	var len = seq.length;
	if (len > 3) 
	{
	   seq = letter;
       document.codon.ret.value = "";						   
	}
	
	var seq2 = "";
	
	for ( var i = 0; i < seq.length; i++)
	{
		var cha=seq.charAt(i);
		if (cha == "U")  seq2 += "T";
	    else seq2 += cha;
	}	
	//alert(seq2);
	
	if (!CheckDNARNA(letter))
	{
		//alert(letter);	 
		seq = seq.substring(0,seq.length-1);			  
        document.codon.seq.value = seq;
        return;		
	}
	else
	{
        document.codon.seq.value = seq;	
	    if (len == 3)
		{
		    var retstr = "";
			//alert(seq2);
            if (seq2 == "ATT" || seq2 == "ATC" || seq2 == "ATA")
                retstr = "Isoleucine | Ile | I";
            if (seq2 == "CTT" || seq2 == "CTC" || seq2 == "CTA" || seq2 == "CTG" || seq2 == "TTA" || seq2 == "TTG")
                retstr = "Leucine | Leu | L";
            if (seq2 == "GTT" || seq2 == "GTC" || seq2 == "GTA" || seq2 == "GTG")
                retstr = "Valine |Val | V";
           if (seq2 == "TTT" || seq2 == "TTC")
                retstr = "Phenylalanine |Phe | F";
           if (seq2 == "ATG")
                retstr = "Methionine | Met | M";
           if (seq2 == "TGT" || seq2 == "TGC")
                retstr = "Cysteine | Cys | C";
           if (seq2 == "GCT" || seq2 == "GCC" || seq2 == "GCA" || seq2 == "GCG")
                retstr = "Alanine | Ala | A";
           if (seq2 == "GGT" || seq2 == "GGC" || seq2 == "GGA" || seq2 == "GGG")
                retstr = "Glycine | Gly | G";
           if (seq2 == "CCT" || seq2 == "CCC" || seq2 == "CCA" || seq2 == "CCG")
                retstr = "Proline | Pro | P";
           if (seq2 == "ACT" || seq2 == "ACC" || seq2 == "ACA" || seq2 == "ACG")
                retstr = "Threonine | Thr | T";
           if (seq2 == "TCT" || seq2 == "TCC" || seq2 == "TCA" || seq2 == "TCG" || seq2 == "AGT" || seq2 == "AGC")
                retstr = "Serine | Ser | S";
           if (seq2 == "TAT" || seq2 == "TAC")
                retstr = "Tyrosine | Tyr | Y";
		   if (seq2 == "TGG")
                retstr = "Tryptophan | Trp | W";
		   if (seq2 == "CAA" || seq2 == "CAG")
                retstr = "Glutamine | Gln | Q";
		   if (seq2 == "AAT" || seq2 == "AAC")
                retstr = "Asparagine | Asn | N";
		   if (seq2 == "CAT" || seq2 == "CAC")
                retstr = "Histidine | His | H";
		   if (seq2 == "GAA" || seq2 == "GAG")
                retstr = "Glutamic acid | Glu | E";
		   if (seq2 == "GAT" || seq2 == "GAC")
                retstr = "Aspartic acid | Asp | D";
		   if (seq2 == "AAA" || seq2 == "AAG")
                retstr = "Lysine | Lys | K";
           if (seq2 == "CGT" || seq2 == "CGC" || seq2 == "CGA" || seq2 == "CGG" || seq2 == "AGA" || seq2 == "AGG")
                retstr = "Arginine | Arg | R";
           if (seq2 == "TAA" || seq2 == "TAG" || seq2 == "TGA")
                retstr = "Stop codons";

            document.codon.ret.value = retstr;
			return;			
		}
		else if (len >3)
		{
			document.codon.seq.value = letter;	
            document.codon.ret.value = "";					
			return;
		}
		else
		{
            document.codon.ret.value = "";		
		    return;
		}
	}
	
}

function IsIUpacBase(theBase) {
	if ((theBase == "M") ||
		(theBase == "R") ||
		(theBase == "W") ||
		(theBase == "S") ||
		(theBase == "Y") ||
		(theBase == "K") ||
		(theBase == "V") ||
		(theBase == "H") ||
		(theBase == "D") ||
		(theBase == "B") ||
		(theBase == "N")) 
	{
			return 1;
	}
	return 0;
}

function RemoveNonPrintingChars(theString) 
{
	var returnString = ""
	for ( var i = 0; i < theString.length; i++) {
		if ( theString.charAt(i) > " ") {
			returnString += theString.charAt(i);
		}
	}
	return returnString
}

function Oligo()
{
	this.aCount = 0;
	this.cCount = 0;
	this.gCount = 0;
	this.tCount = 0;
	this.uCount = 0;
	this.iCount = 0;
	this.mCount = 0;
	this.rCount = 0;
	this.wCount = 0;
	this.sCount = 0;
	this.yCount = 0;
	this.kCount = 0;
	this.vCount = 0;
	this.hCount = 0;
	this.dCount = 0;
	this.bCount = 0;
	this.nCount = 0;
	
	this.gcValmin = 0;
	this.gcValmax = 0;
	this.mwValmin = 0;
	this.mwValmax = 0;

	this.eA_MWmin=0;
	this.eC_MWmin=0;
	this.eG_MWmin=0;
	this.eT_MWmin=0;
	this.eU_MWmin=0;
	this.eI_MWmin=0;
	
	this.eA_MWmax=0;
	this.eC_MWmax=0;
	this.eG_MWmax=0;
	this.eT_MWmax=0;														  
	this.eU_MWmax=0;														  
	this.eI_MWmax=0;														  
	this.eGC_min=0;
	this.eGC_max=0;
								
	
	this.aaCount = 0;
	this.atCount = 0;
	this.taCount = 0;
	this.caCount = 0;
	this.gtCount = 0;
	this.ctCount = 0;
	this.gaCount = 0;
	this.cgCount = 0;
	this.gcCount = 0;
	this.ggCount = 0;
	this.IUpairVals_min=new Array(0,0,0);
	this.IUpairVals_max=new Array(0,0,0);
	this.hasIUpacBase=0;
	
	this.seqArray;
	this.revSeqArray;
	
	this.GC = GC;
	this.MW = MW;
	this.OligoCount = OligoCount;
    this.OligoCalculate = OligoCalculate;
	
	this.Sequence="";
	this.revSequence="";
}

function OligoCalculate() 
{
	this.OligoCount();

	if(!this.hasIUpacBase) {
		this.gcValmin = this.GC("min");
		this.gcValmax = this.gcValmin;
	} else {
		this.gcValmin = this.GC("min");
		this.gcValmax = this.GC("max");
	}
	
	if(!this.hasIUpacBase) {
		this.mwValmin = this.MW("min");
		this.mwValmax = this.mwValmin;
	} else {
		this.mwValmin = this.MW("min");
		this.mwValmax = this.MW("max");
	}	
}

function OligoCount(){
	this.aCount = CountChar("A",this.Sequence);
	this.cCount = CountChar("C",this.Sequence);
	this.gCount = CountChar("G",this.Sequence);
	this.tCount = CountChar("T",this.Sequence);
	this.uCount = CountChar("U",this.Sequence);
	this.iCount = CountChar("I",this.Sequence);
	this.mCount = CountChar("M",this.Sequence);
	this.rCount = CountChar("R",this.Sequence);
	this.wCount = CountChar("W",this.Sequence);
	this.sCount = CountChar("S",this.Sequence);
	this.yCount = CountChar("Y",this.Sequence);
	this.kCount = CountChar("K",this.Sequence);
	this.vCount = CountChar("V",this.Sequence);
	this.hCount = CountChar("H",this.Sequence);
	this.dCount = CountChar("D",this.Sequence);
	this.bCount = CountChar("B",this.Sequence);
	this.nCount = CountChar("N",this.Sequence);
	//Effective a,c,g,t count for different calculations
	//effective counts for A260min

	this.eA_A260min=this.aCount;
	this.eU_A260min=this.uCount;
	this.eI_A260min=this.iCount;
	this.eC_A260min=this.cCount+this.mCount+this.sCount+this.yCount+this.vCount
						+this.hCount+this.bCount+this.nCount;
	this.eG_A260min=this.gCount+this.rCount;
	this.eT_A260min=this.tCount+this.wCount+this.kCount+this.dCount;
	
	this.eA_A260max=this.aCount+this.mCount+this.rCount+this.wCount+this.vCount
					+this.hCount+this.bCount+this.nCount;
	this.eC_A260max=this.cCount;
	this.eG_A260max=this.gCount+this.sCount+this.kCount+this.dCount;
	this.eT_A260max=this.tCount+this.yCount;			
	
	//effective counts for MW()
	this.eA_MWmin=this.aCount+this.rCount;
	this.eC_MWmin=this.eC_A260min;
	this.eU_MWmin=this.eU_A260min;
	this.eI_MWmin=this.eI_A260min;
	this.eG_MWmin=this.gCount;
	this.eT_MWmin=this.eT_A260min;
	
	this.eA_MWmax=this.aCount+this.mCount+this.wCount+this.hCount;
	this.eC_MWmax=this.cCount;
	this.eG_MWmax=this.gCount+this.rCount+this.sCount+this.kCount+this.vCount+
					this.dCount+this.bCount+this.nCount;
	this.eT_MWmax=this.tCount+this.yCount;				
	
	this.eGC_min=this.gCount + this.cCount +this.sCount;
	this.eGC_max=(this.Sequence.length)-this.aCount-this.tCount-this.wCount-this.uCount;	
}

function CountChar(theChar, theSequence) 
{
	var returnValue = 0
	for ( var i = 0; i < theSequence.length; i++) {
		if (theSequence.charAt(i) == theChar) {
			returnValue ++;
		}
	}
	return returnValue;
}

function MW(choice) {
	var mw;
	if (this.Sequence.length > 0) {
		if (this.isDeoxy) {
			if(choice=="min"){
				mw=     313.21 * this.eA_MWmin + 
						329.21 * this.eG_MWmin + 
						289.18 * this.eC_MWmin + 
						304.2  * this.eT_MWmin + 
						290.169 * this.eU_MWmin + 
						314. * this.eI_MWmin 
						- 61.96;
			}else{
				mw=     313.21 * this.eA_MWmax + 
						329.21 * this.eG_MWmax + 
						289.18 * this.eC_MWmax + 
						304.2 * this.eT_MWmax + 
						290.169 * this.eU_MWmax +
						314. * this.eI_MWmax
						- 61.96;
			}
		} else { // is riboNucleotide
			if(choice=="min"){
				mw=     329.21 * this.eA_MWmin + 
						345.21 * this.eG_MWmin + 
						305.18 * this.eC_MWmin + 
						320.2  * this.eT_MWmin + 
						306.169 * this.eU_MWmin +
						330  * this.eI_MWmin
						+159;
					
			}else{
				mw=     329.21 * this.eA_MWmax + 
						345.21 * this.eG_MWmax + 
						305.18 * this.eC_MWmax + 
						320.2 * this.eT_MWmax + 
						306.169 * this.eU_MWmax +
						330  * this.eI_MWmax
						+159; 
			}
		}
		return mw;
	}
	return "";
}

function GC(choice)
{
	if (this.Sequence.length > 0) {
		if(choice=="min"){
			return Math.round( 100 * this.eGC_min /this.Sequence.length )
		}else{
			return Math.round(100*this.eGC_max/this.Sequence.length);
		}
	}
	return "";
}

function MakeComplement(theSequence, isDNA) {
	var returnString="";
	var i;
	var temp;
	for( i=theSequence.length-1; i>=0; i--) {
		temp=theSequence.charAt(i);
		switch (temp) {
			case "A" :
				if (isDNA) {
					temp="T";
				} else {
					temp="U";
				}
				break;
			case "T" :
				temp="A";
				break;
			case "U" :
				temp="A";
				break;
			case "G" :
				temp="C";
				break;
			case "C" :
				temp="G";
				break;
			case "M" :
				temp="K";
				break;
			case "K" :
				temp="M";
				break;
			case "R" :
				temp="Y";
				break;
			case "Y" :
				temp="R";
				break;
			case "W" :
				temp="W";
				break;
			case "S" :
				temp="S";
				break;
			case "V" :
				temp="B";
				break;
			case "B" :
				temp="V";
				break;
			case "H" :
				temp="D";
				break;
			case "D" :
				temp="H";
				break;
			default : break;
		}
		returnString=returnString+temp;
	}
	return returnString;
}
//*/

function dnacopynumgetinputseqlength()
{
    //alert("works");
    var theOligo = new Oligo();
	
    var strSeqs = document.dnacp.seqs.value;
	var seq = GetSingleSeq(strSeqs);
	//alert("seq is: " + seq);
	
	var temp;
	
	var retstr = "";
	var flag = 0;
		
	temp=CheckBase(seq);

	if (temp == -1)
	{
		flag = 1;
		return;
	}

	theOligo.Sequence = temp;
	
	var len = theOligo.Sequence.length;
	//alert(len);
	if (len > 0)
	{
		document.dnacp.seqlen.value = len;
	}
}


function cpcal(cform,c)
{
   var mwfix = 0;
   var strSeqs = document.dnacp.seqs.value;
   var mw = document.dnacp.lengthA.value;
   var mol = document.dnacp.lengthB.value;
   var cp = document.dnacp.lengthC.value;
   var seqlength = document.dnacp.seqlen.value;

   if (strSeqs == "" && seqlength == "")
      return;
	  
   if (c != "4" && strSeqs != "")
   {
    var theOligo = new Oligo();
	var	theComplement = new Oligo();
	
	var seq = GetSingleSeq(strSeqs);
	//alert("seq is: " + seq);
	
	//WriteFile("log.txt",strSeqs);
	
	var temp;
	var CompString;
	
	var retstr = "";
	var flag = 0;
    //document.dnacp.lengthA.value = 30;
    //document.dnacp.lengthB.value = 40;
    //document.dnacp.lengthC.value = 50; 		
	temp=CheckBase(seq);
    //document.dnacp.lengthC.value = 50; 		
//*
		if (temp == -1)
		{
		    flag = 1;
		    //break;
			//alert("Seq is not valid");
			return;
		}
      //document.dnacp.lengthC.value = 50; 		

//*		
		theOligo.Sequence = temp;
		theOligo.OligoCalculate();
		
	    var val = cform.dnastat.options[cform.dnastat.selectedIndex].value;
		var opt = 0;
	    if (val.indexOf("DNA") >= 0)  opt = 1;
		
		//alert(temp);
	
	    CompString = MakeComplement(temp, opt);
		theComplement.Sequence = CompString;
		theComplement.OligoCalculate();
		
		//alert(CompString);
		
		//Sequence mw per mole
		//var mwfix = 0;
     
	   //document.dnacp.lengthC.value = 50; 		
		
		//*
		if (!theOligo.hasIUpacBase)
		{
		    //alert("good");
		    if (val.indexOf("ds") <0)
			{
			   mwfix = theOligo.mwValmin;
			}
			else
			{
			    //alert('ee');
				mwfix = theOligo.mwValmin + theComplement.mwValmin;		
				//mwfix = 2 * theOligo.mwValmin;		
			}
		}
		else
		{
		    //alert("bad");
		    if (val.indexOf("ds") <0)
			{
				mwfix = (theOligo.mwValmin + theComplement.mwValmax) /2;
				//mwfix = theOligo.mwValmin;
			}
			else
			{
				var min= theOligo.mwValmin+ theComplement.mwValmin;
				var max= theOligo.mwValmax+ theComplement.mwValmax;
				//var min= 2 * theOligo.mwValmin;
				//var max= 2 * theOligo.mwValmax;
			    mwfix = (min + max)/2;
			}	
		}
	}
	else
	{
	    var slen = document.dnacp.seqlen.value;
	    var val = cform.dnastat.options[cform.dnastat.selectedIndex].value;
		 
		if (val.indexOf("ds") <0)
		{
			mwfix = slen * 325;
		}
		else
		{
		    mwfix = slen * 650;
		}
		 //alert(mwfix);		 
	}


	//document.dnacp.lengthC.value = mwfix; 		
	
//*	
	if (c == "1")
	{
	    if (isNum(mw))
		{
		    //echo mw;
		    //alert(mw);
			//alert(mwfix);
	        document.dnacp.lengthB.value = (mw * 1)/mwfix;
	        document.dnacp.lengthC.value = Math.round((mw * 6.02 * 100000000000000)/mwfix);
		}
		else
		{
				mw = mw.substring(0,mw.length-1);
				document.dnacp.lengthA.value = mw;		
		}
	}
	//*
	else if ( c== "2")
	{
	    if (isNum(mol))
		{
		    //document.dnacp.lengthA.value = (mol * mwfix) / 1000000000;
		    document.dnacp.lengthA.value = (mol * mwfix);
		    document.dnacp.lengthC.value = Math.round(mol * 6.02 * 100000000000000);			
		}
		else
		{
				mol = mol.substring(0,mol.length-1);
				document.dnacp.lengthB.value = mol;		
		}
		
	}
	//*/
	else if ( c== "3")
	{
	    if (isNum(cp))
		{
		    document.dnacp.lengthA.value = (1 * cp * mwfix)/(6.02 * 100000000000000);
		    document.dnacp.lengthB.value = cp /(6.02 * 100000000000000);			
		}
		else
		{
				cp = cp.substring(0,cp.length-1);
				document.dnacp.lengthC.value = Math.round(cp);		
		}	
	}		
	else if (c=="4")
	{
	    if (isNum(mw))
		{
	        document.dnacp.lengthB.value = (mw * 1)/mwfix;
	        document.dnacp.lengthC.value = Math.round((mw * 6.02 * 100000000000000)/mwfix);		    
		}
	    else if (isNum(mol))
		{
		    //document.dnacp.lengthA.value = (mol * mwfix) / 1000000000;
		    document.dnacp.lengthA.value = (mol * mwfix);
		    document.dnacp.lengthC.value = Math.round(mol * 6.02 * 100000000000000);			
		}
	    else if (isNum(cp))
		{
		    document.dnacp.lengthA.value = (1 * cp * mwfix)/(6.02 * 100000000000000);
		    document.dnacp.lengthB.value = cp /(6.02 * 100000000000000);			
		}
		
		if (isNum(seqlength))
		{
		    document.dnacp.seqs.value = "";
		}
		
	}
}

//Sequence Editor
function tidyup()
{
    var optnumberline = 0;
	if (document.seqed.optnumline.checked == true)
	{
      optnumberline = 1;
	}
	  
	var linenum = document.seqed.linenum.value;
	var spacenum = document.seqed.spacenum.value;
	
	    var str=GetSingleSeq(document.seqed.seqs.value);
		str = str.toUpperCase();
		str=Removeuseless(str);

	   var revstr='';
	   if (optnumberline == 1)	   
	      revstr=' 1 ';
	   var k=0;
	   for (i =0; i<str.length; i++)
	   {
		  revstr+=str.charAt(i);
		  k+=1;
		  if (linenum >1)
		  {
		      if (optnumberline == 1)
				if (k==Math.floor(k/linenum)*linenum) {revstr+='\n' + k};
		      else
				if (k==Math.floor(k/linenum)*linenum) {revstr+='\n'};
		  }
		  if (spacenum > 1)
		  {
			  if (k==Math.floor(k/spacenum)*spacenum) {revstr+=' '};
		  }
	   };

	   document.seqed.seqs.value=revstr;
}

//Sequence Editor
function untidy()
{
	var str=GetSingleSeq(document.seqed.seqs.value);
	str = str.toUpperCase();
	str=Removeuseless(str);

	document.seqed.seqs.value=str;
}

//seqeunce editor
function Removeuseless(str) {
   str = str.split(/\d/).join("");
   str = str.split(/\W/).join("");
   return str;
   }

function dnarev()
{
    var strSeqs = GetSingleSeq(document.seqed.seqs.value);

    var theOligo = new Oligo();
	var	theComplement = new Oligo();

	var temp;
	var CompString;
	
	var retstr = "";
	var flag = 0;
	temp=CheckBase(strSeqs);
	if (temp == -1)
	{
		flag = 1;
		return;
	}
	theOligo.Sequence = temp;
	
	var val = document.seqed.dnastat.options[document.seqed.dnastat.selectedIndex].value;
	var opt = 0;
	if (val.indexOf("DNA") >= 0)  opt	= 1;
	
	CompString = MakeComplement(temp, opt);
	
	document.seqed.seqs.value = CompString;
}

function dnacut()
{
    var fnum = document.seqed.cutf.value-1;
	var rnum = document.seqed.cutr.value-1;
	var seq = GetSingleSeq(document.seqed.seqs.value);
	seq=Removeuseless(seq);
	
	if (isNum(fnum)  && isNum(rnum))
	{
	    if (fnum < 0) alert("Error! Start position must be >=1.");
		if (rnum <fnum) alert("Error! End position should >= start position.");
	}
	else
	{
		alert("Error! Please check the start and end cut positions, Make sure that only number allowed.");
	}
	
 	var temp;
	
	var retstr = "";
	var flag = 0;
	temp=CheckBase(seq);
	if (temp == -1)
	{
		flag = 1;
		alert("Error! Sequence contains unrecognized characters.");
		return;
	}
	
	if (rnum > (temp.length-1)) alert("Error! End position larger than the sequence length!");
	
	for (var i=0; i<=rnum; i++)
	{
	    var ch = temp.charAt(i);
	    if (i>=fnum)
			retstr += ch;
	}
		
	document.seqed.seqs.value = retstr;	
}

function dnaline()
{
    var num = document.seqed.linenum.value;
	//var seq = document.seqed.seqs.value;
	var seq = GetSingleSeq(document.seqed.seqs.value);
	
	//document.seqed.seqs.value = seq;
	//return;
 
    //alert("begin");
	
 	if (isNum(num))
	{
        ;
	}
	else
	{
		alert("Error! Please input a number for the line.");
	}
	
 	var temp;
	
	var retstr = "";
	var flag = 0;
	temp=CheckBase(seq);
	if (temp == -1)
	{
		flag = 1;
		alert("Error! Sequence contains unrecognized characters.");
		return;
	}
	
	//alert(temp);
	
	if (num > (temp.length)) alert("Error! Line number larger than the sequence length!");
	
 	for (var i=0; i<temp.length; i++)
	{
	    var ch = temp.charAt(i);
	    if ((i+1)%num == 0)
		{
			retstr += ch;
		    if (i>0)
			{
				retstr += "\r\n";
			}
		}
		else
			retstr += ch;
	}
  
	document.seqed.seqs.value = retstr;	
}

   

