var sEl="";
var temp = "";
var vMM=0;
var formula="";
var DataError=false;
var nEl="";
var mP;
var aM="";
var aMM="";
var aG="";
NumberAtoms=new Array(0,0,0,0,0,0,0,0,0,0,0,0);
PercentOfMass=new Array(0,0,0,0,0,0,0,0,0,0,0,0);
aNA=new Array(0,0,0,0,0,0,0,0,0,0,0,0);
compound=new cmpd(NumberAtoms,aNA,vMM);
a=new mA(109);
a[1]=new El("H","1.00794",5);
a[2]=new El("He","4.002602",6);
a[3]=new El("Li","6.941",3);
a[4]=new El("Be","9.012182",6);
a[5]=new El("B","10.811",3);
a[6]=new El("C","12.011",3);
a[7]=new El("N","14.00674",5);
a[8]=new El("O","15.9994",4);
a[9]=new El("F","18.9984032",7);
a[10]=new El("Ne","20.1797",4);
a[11]=new El("Na","22.989768",6);
a[12]=new El("Mg","24.3050",4);
a[13]=new El("Al","26.981539",6);
a[14]=new El("Si","28.0855",4);
a[15]=new El("P","30.973762",6);
a[16]=new El("S","32.066",3);
a[17]=new El("Cl","35.4527",4);
a[18]=new El("Ar","39.948",3);
a[19]=new El("K","39.0983",4);
a[20]=new El("Ca","40.078",3);
a[21]=new El("Sc","44.955910",6);
a[22]=new El("Ti","47.88",2);
a[23]=new El("V","50.9415",4);
a[24]=new El("Cr","51.9961",4);
a[25]=new El("Mn","54.93805",5);
a[26]=new El("Fe","55.847",3);
a[27]=new El("Co","58.93320",5);
a[28]=new El("Ni","58.6934",4);
a[29]=new El("Cu","63.546",3);
a[30]=new El("Zn","65.39",2);
a[31]=new El("Ga","69.723",3);
a[32]=new El("Ge","72.61",2);
a[33]=new El("As","74.92159",5);
a[34]=new El("Se","78.96",2);
a[35]=new El("Br","79.904",3);
a[36]=new El("Kr","83.80",2);
a[37]=new El("Rb","85.4678",4);
a[38]=new El("Sr","87.62",2);
a[39]=new El("Y","88.90585",5);
a[40]=new El("Zr","91.224",3);
a[41]=new El("Nb","92.90638",5);
a[42]=new El("Mo","95.94",2);
a[43]=new El("Tc","98",0);
a[44]=new El("Ru","101.07",2);
a[45]=new El("Rh","102.90550",5);
a[46]=new El("Pd","106.42",2);
a[47]=new El("Ag","107.8682",4);
a[48]=new El("Cd","112.411",3);
a[49]=new El("In","114.82",2);
a[50]=new El("Sn","118.710",3);
a[51]=new El("Sb","121.757",3);
a[52]=new El("Te","127.60",2);
a[53]=new El("I","126.90447",5);
a[54]=new El("Xe","131.29",2);
a[55]=new El("Cs","132.90543",5);
a[56]=new El("Ba","137.327",3);
a[57]=new El("La","138.9055",4);
a[58]=new El("Ce","140.115",3);
a[59]=new El("Pr","140.90765",5);
a[60]=new El("Nd","144.24",2);
a[61]=new El("Pm","145",0);
a[62]=new El("Sm","150.36",2);
a[63]=new El("Eu","151.965",3);
a[64]=new El("Gd","157.25",2);
a[65]=new El("Tb","158.92534",5);
a[66]=new El("Dy","162.50",2);
a[67]=new El("Ho","164.93032",5);
a[68]=new El("Er","167.26",2);
a[69]=new El("Tm","168.93421",5);
a[70]=new El("Yb","173.04",2);
a[71]=new El("Lu","174.967",3);
a[72]=new El("Hf","178.49",2);
a[73]=new El("Ta","180.9479",4);
a[74]=new El("W","183.85",2);
a[75]=new El("Re","186.207",3);
a[76]=new El("Os","190.2",1);
a[77]=new El("Ir","192.22",2);
a[78]=new El("Pt","195.08",2);
a[79]=new El("Au","196.96654",5);
a[80]=new El("Hg","200.59",2);
a[81]=new El("Tl","204.3833",4);
a[82]=new El("Pb","207.2",2);
a[83]=new El("Bi","208.98037",5);
a[84]=new El("Po","209",0);
a[85]=new El("At","210",0);
a[86]=new El("Rn","222",0);
a[87]=new El("Fr","223",0);
a[88]=new El("Ra","226.0254",4);
a[89]=new El("Ac","227",0);
a[90]=new El("Th","232.0381",4);
a[91]=new El("Pa","213.0359",4);
a[92]=new El("U","238.0289",4);
a[93]=new El("Np","237.0482",4);
a[94]=new El("Pu","244",0);
a[95]=new El("Am","243",0);
a[96]=new El("Cm","247",0);
a[97]=new El("Bk","247",0);
a[98]=new El("Cf","251",0);
a[99]=new El("Es","252",0);
a[100]=new El("Fm","257",0);
a[101]=new El("Md","258",0);
a[102]=new El("No","259",0);
a[103]=new El("Lr","260",0);
a[104]=new El("Rf","261",0);
a[105]=new El("Db","262",0);
a[106]=new El("Sg","263",0);
a[107]=new El("Bh","262",0);
a[108]=new El("Hs","265",0);
a[109]=new El("Mt","266",0);

function cmpd(NumberAtoms,aNA,MMM,printString){
  this.NumberAtoms=NumberAtoms;
  this.aNA=aNA;this.MMM=MMM;
  this.printString=printString;
}

function ParseFormula(formula){
  var c=0;
  x=0;
  y=0;
  c1=0;
  marker1=0;
  marker2=-1;
  loopCount=0;
  DataError=false;
  multiplier=1;
  a1=new Array(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0);
  a2=new Array(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0);
  while (c<=formula.length - 1 && !DataError){
     AN=findAN(formula.substring(c,c+2));
     if(AN==-1){
        AN=findAN(formula.substring(c,c+1));
        y=1;
     }
     else y=2;
     
     if(AN>-1){
        marker2=-1;
        for(var c2=0;c2<=11;c2++){
            if(a2[c2]==AN){
               marker2=c2;
               break;
            }
        }
        if(marker2>=0){
           marker1=marker2;
           loopCount-=1;
        }
        else marker1=loopCount;
        c+= y;
        a2[marker1]=AN;
        var OK=true;
        var c3=0;
        while (OK && c+c3<=formula.length-1){
               AN=findNum(formula.substring(c+c3, c+c3+1));
               if(AN>=0) c3++;
               else OK=false
        }
        if(c3>0){
           if(marker2>=0) a1[marker1]+= parseInt(formula.substring(c, c+c3))*multiplier;
           else a1[marker1]=parseInt(formula.substring(c, c+c3))*multiplier;
        }
        else{
             if(marker2>=0) a1[marker1]+= 1*multiplier;
             else a1[marker1]=1*multiplier;
        }
        c+= c3;
        loopCount++;
     }
     else{
        AN="[]().".indexOf(formula.substring(c,c+1)); 
        if(AN==-1){
           DataError=true;
           break;
        }
        if(AN==0 || AN==2){
           if(AN==2){
              var zz=0;
              for(x=c;x<=formula.length-1;x++){
                  if(formula.substring(x,x+1)==")"){
                     zz=x;
                     break;
                  }
              }
           }
           else{
              for(x=c;x<=formula.length-1;x++){
                  if(formula.substring(x,x+1)=="]"){
                     zz=x;
                     break;
                  }
              }
           }
           var c4=1;
           OK=true;
           while (OK && zz+c4<=formula.length-1){
                  var AN1=findNum(formula.substring(zz+c4, zz+c4+1));
                  if(AN1>=0) c4++;
                  else OK=false;
           }
           if(c4>1){
              var z=parseInt(formula.substring(1+zz, c4+zz));
              multiplier=multiplier*z;
           }c++;
         }
         if(AN==1 || AN==3){
            var c5=1;
            OK=true;
            while (OK && c+c5<=formula.length-1){
                var AN1=findNum(formula.substring(c+c5, c+c5+1));
                if(AN1>=0) c5++;
                else OK=false;
            }
            if(c5>1){
               multiplier=Math.round(multiplier/parseInt(formula.substring(c+1, c+c5)));
            }
            c+=c5;
          }
          if(AN==4){
             var c6=1;
             OK=true;
             while (OK && c+c6<=formula.length-1){
                var AN1=findNum(formula.substring(c+c6, c+c6+1));
                if(AN1>=0) c6++;
                else OK=false;
             }
             if(c6>1){
                multiplier *= parseInt(formula.substring(c+1, c+c6));
             }
             c+=c6;
           }
        }
     }
     nEl=loopCount-1;
     for(var i=0;i<=nEl;i++){
        NumberAtoms[i]=a1[i];
        aNA[i]=a2[i];
     }
}

function findAN(aSym){
  var AN=0;
  if(aSym.length==1) aSym+="*";
  var x=sEl.indexOf(aSym);
  AN=(x==-1) ? x : (x/2)+1;
  return AN;
}

function findNum(ch){
  var numstring="0123456789";
  var x=numstring.indexOf(ch);
  return x;
}

function CalculateMass(){
  MMM=0;
  for(var c=0;c<=nEl;c++){
     var x =aNA[c];
     MMM+= a[x]["mass"]*NumberAtoms[c];
  }
}


function El(symbol, mass,SigDigits){
  this.symbol=symbol;
  this.mass=mass;
  this.SigDigits=SigDigits;
}


function mA(arraySize){
  this.length=arraySize;
  for(var c=0;c<=arraySize;c++){
    this[c]=0;
  }
  return this;
}


function makestrEl(){
   for(var c=1;c<=a.length;c++){
      sEl+=a[c]["symbol"];
      if(a[c]["symbol"].length==1) sEl+= "*";
   }
   return sEl;
}


function writeOut(){
  makestrEl();
  formula=document.MMC.ML.value;
  if(formula ==""){
      document.MMC.ML.value= "Kérem adja meg a formulát!";
   }
  else{
      ParseFormula(formula);
      if(!DataError){
          CalculateMass();
          Presorted=new Array("","","","","","","","","","","","");
           c1=5;
          var aAMA="";
          var SD=8;
          for(var c=0;c<=nEl;++c){
             if(a[aNA[c]]["symbol"]=="H" || a[aNA[c]]["symbol"]=="B" || a[aNA[c]]["symbol"]=="C" || a[aNA[c]]["symbol"]=="N" || a[aNA[c]]["symbol"]=="F" || a[aNA[c]]["symbol"]=="P" || a[aNA[c]]["symbol"]=="S" || a[aNA[c]]["symbol"]=="K" || a[aNA[c]]["symbol"]=="V" || a[aNA[c]]["symbol"]=="Y" || a[aNA[c]]["symbol"]=="I" || a[aNA[c]]["symbol"]=="U" || a[aNA[c]]["symbol"]=="O"){
                 a[aNA[c]]["symbol"]=a[aNA[c]]["symbol"]+" ";
             }
             if(NumberAtoms[c]<10){
                NumberAtoms[c]=" "+NumberAtoms[c]
             }
             PercentOfMass[c]=Math.round((parseFloat(a[aNA[c]]["mass"])*NumberAtoms[c]*100/MMM)*100)/100;
             if((PercentOfMass[c]*10)%10==0){
                 mP=".00";
             }
             else if((PercentOfMass[c]*100)%10==0){
                 mP="0";
             }
             else mP="";
             if(PercentOfMass[c]==100){
                PercentOfMass[c]=" "+PercentOfMass[c]+"% ";
             }
             else if(PercentOfMass[c]<1.0){
                PercentOfMass[c]=" 0"+PercentOfMass[c]+mP+"% ";
             }
             else if(PercentOfMass[c]<10.0){PercentOfMass[c]=" "+PercentOfMass[c]+mP+"% "}
			 else PercentOfMass[c]=PercentOfMass[c]+mP+"% ";
			 if(parseFloat(a[aNA[c]]["mass"])<10){aAMA=" "}else if(parseFloat(a[aNA[c]]["mass"])<100){aAMA=" "}else aAMA="";Presorted[c]=PercentOfMass[c]+" "+a[aNA[c]]["symbol"]+" "+NumberAtoms[c]+" "+aAMA+a[aNA[c]]["mass"];if(a[aNA[c]]["SigDigits"]==0){SD=0}else if(a[aNA[c]]["SigDigits"]==1){if(SD>1){SD=1}}else if(a[aNA[c]]["SigDigits"]==2){if(SD>2){SD=2}}else if(a[aNA[c]]["SigDigits"]==3){if(SD>3){SD=3}}else if(a[aNA[c]]["SigDigits"]==4){if(SD>4){SD=4}}else if(a[aNA[c]]["SigDigits"]==5){if(SD>5){SD=5}}else if(a[aNA[c]]["SigDigits"]==6){if(SD>6){SD=6}}else if(a[aNA[c]]["SigDigits"]==7){if(SD>7){SD=7}}else if(a[aNA[c]]["SigDigits"]>7){if(SD>7){SD=8}};c1++}var lG="";var leadMole="";if(1000/MMM<1&& parseInt((1000/MMM)*10)%10>0 ){lG="0"};if(parseInt(MMM/100)%10>0 && MMM/1000<1){leadMole="0"};var aGA="";var aMMA="";var aMA="";if(SD==0){aMMA=Math.round(MMM);aGA="1 gram = "+Math.round(1000/MMM/1000)+" Mole"}else if(SD==1){aMM=Math.round(MMM*10)/10;if(aMM*10%10==0){aMMA=aMM+".0"}else aMMA=aMM;aG=Math.round(10000/MMM/1000)/10;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==2){aMM=Math.round(MMM*100)/100;if(aMM*10%10==0){aMMA=aMM+".00"}else if(aMM*100%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(100000/MMM/1000)/100;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".00 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==3){aMM=Math.round(MMM*1000)/1000;if(aMM*10%10==0){aMMA=aMM+".000"}else if(aMM*100%10==0){aMMA=aMM+"00"}else if(aMM*1000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(1000000/MMM/1000)/1000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==4){aMM=Math.round(MMM*10000)/10000;if(aMM*10%10==0){aMMA=aMM+".0000"}else if(aMM*100%10==0){aMMA=aMM+"000"}else if(aMM*1000%10==0){aMMA=aMM+"00"}else if(aMM*10000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(10000000/MMM/1000)/10000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".0000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"000 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*10000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==5){aMM=Math.round(MMM*100000)/100000;if(aMM*10%10==0){aMMA=aMM+".00000"}else if(aMM*100%10==0){aMMA=aMM+"0000"}else if(aMM*1000%10==0){aMMA=aMM+"000"}else if(aMM*10000%10==0){aMMA=aMM+"00"}else if(aMM*100000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(100000000/MMM/1000)/100000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".00000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"0000 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"000 Mole"}if(aG*10000%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*100000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==6){aMM=Math.round(MMM*1000000)/1000000;if(aMM*10%10==0){aMMA=aMM+".000000"}else if(aMM*100%10==0){aMMA=aMM+"00000"}else if(aMM*1000%10==0){aMMA=aMM+"0000"}else if(aMM*10000%10==0){aMMA=aMM+"000"}else if(aMM*100000%10==0){aMMA=aMM+"00"}else if(aMM*1000000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(1000000000/MMM/1000)/1000000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".000000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"00000 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"0000 Mole"}if(aG*10000%10==0){aGA="1 gram = "+lG+aG+"000 Mole"}if(aG*100000%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*1000000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD==7){aMM=Math.round(MMM*10000000)/10000000;if(aMM*10%10==0){aMMA=aMM+".0000000"}else if(aMM*100%10==0){aMMA=aMM+"000000"}else if(aMM*1000%10==0){aMMA=aMM+"00000"}else if(aMM*10000%10==0){aMMA=aMM+"0000"}else if(aMM*100000%10==0){aMMA=aMM+"000"}else if(aMM*1000000%10==0){aMMA=aMM+"00"}else if(aMM*10000000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(10000000000/MMM/1000)/10000000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".0000000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"000000 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"00000 Mole"}if(aG*10000%10==0){aGA="1 gram = "+lG+aG+"0000 Mole"}if(aG*100000%10==0){aGA="1 gram = "+lG+aG+"000 Mole"}if(aG*1000000%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*10000000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"}else if(SD>7){aMM=Math.round(MMM*10000000000)/10000000000;if(aMM*10%10==0){aMMA=aMM+".00000000"}else if(aMM*100%10==0){aMMA=aMM+"0000000"}else if(aMM*1000%10==0){aMMA=aMM+"000000"}else if(aMM*10000%10==0){aMMA=aMM+"00000"}else if(aMM*100000%10==0){aMMA=aMM+"0000"}else if(aMM*1000000%10==0){aMMA=aMM+"000"}else if(aMM*10000000%10==0){aMMA=aMM+"00"}else if(aMM*10000000%10==0){aMMA=aMM+"0"}else aMMA=aMM;aG=Math.round(100000000000/MMM/1000)/100000000;if(aG*10%10==0){aGA="1 gram = "+lG+aG+".00000000 Mole"}if(aG*100%10==0){aGA="1 gram = "+lG+aG+"0000000 Mole"}if(aG*1000%10==0){aGA="1 gram = "+lG+aG+"000000 Mole"}if(aG*10000%10==0){aGA="1 gram = "+lG+aG+"00000 Mole"}if(aG*100000%10==0){aGA="1 gram = "+lG+aG+"0000 Mole"}if(aG*1000000%10==0){aGA="1 gram = "+lG+aG+"000 Mole"}if(aG*10000000%10==0){aGA="1 gram = "+lG+aG+"00 Mole"}if(aG*100000000%10==0){aGA="1 gram = "+lG+aG+"0 Mole"}else aGA="1 gram = "+lG+aG+" Mole"};for(i=0;i<nEl;i++){for(j=0;j<nEl-i;j++){if(Presorted[j]<Presorted[j+1]){temp=Presorted[j];Presorted[j]=Presorted[j+1];Presorted[j+1]=temp}}}
			 document.MMC.MW.value=aMMA;
             document.getElementById("MW_szamol").disabled = true;
             document.getElementById("MW_szamol").className = "calcbuttons_off";
			 
             //calc_V triggerelése, ha a V_szamol nincs kikapcsolva
             if(document.getElementById("V_szamol").enabled == true || document.getElementById("V_szamol").disabled == false && document.getElementById("V").value != ""){
                calc_V();
             }
             //calc_m triggerelése, ha a m_szamol nincs kikapcsolva
             if(document.getElementById("m_szamol").enabled == true || document.getElementById("m_szamol").disabled == false && document.getElementById("m").value != ""){
                calc_m();                
             }			 
             //calc_c triggerelése, ha a c_szamol nincs kikapcsolva
             if(document.getElementById("c_szamol").enabled == true || document.getElementById("c_szamol").disabled == false && document.getElementById("c").value != ""){
                calc_c();
             }			 
    }
	else FormulaError()
  }
};

function FormulaError(){document.getElementById("ML").value= "Hibás formula!";}
             
             
function ClearForm(){
  document.MMC.ML.value="";
  document.MMC.MW.value="";
  document.getElementById("MW_szamol").disabled = false;
  document.getElementById("MW_szamol").className = "calcbuttons";  
}
