function ra_de(i_rad) {
  		var pi = Math.PI;
		var o_deg = (i_rad*(180/pi));
		
		return o_deg;
}

function de_ra(i_deg) {
  		var pi = Math.PI;
		var o_rad = (i_deg*(pi/180));
		
		return o_rad;
}

function drawHexMap(i_base, i_mx,i_cellside,i_type, i_rp, i_brd, i_div, i_frame, i_terr) 
{
   halfpiece = 35; 
	
    b = i_base;
    t = i_type;
    s = i_cellside;
    g = 2*b - 1;
   
    hexY = 0;
    hexX = 0;
    q = g + 1;
    e = Math.min(i_mx,g);
    af = g-e;                           
    o = 3*s/2;
    h = Math.sqrt(s*s - (s*s/2/2));
    rep = Math.abs(i_rp);

    br = i_div[4];
    hr = i_div[5];
    // choose a color for the polygon
    cp = new Array();
    cp[0] = i_div[0];
    cp[1] = i_div[1];
    cp[2] = i_div[2];
    
    sbase = s;
    hbase = h;
    
    imgx=0;
    imgy=0;
    cellr=0;
    cellf=0;
    polyz=0;
    jotn = new Array();
    jotx = new Array();
    joty = new Array();
    jotr = new Array();
    jnc=0;

    while(rep>=0) 
    {
        mult = 1+Math.abs(i_rp) - rep;
        hadj = hbase*(mult-1)*(g-1);
        sadj = sbase;
        //sadj = (q)*sbase*(mult-1)*3/2;
        //hadj = (i_rp >= 0) ? hbase*(mult-1): hbase*rep;
        high = g;
        draw = 0;
        aft = 0;
        for(r=0; r<high; r++) 
        {
            hexY =  3*sbase/2;
            hexY *= (draw);
            hexY += o + sadj;
            j = Math.floor(r-b+1);
            i = Math.abs(j);
            ba = (af<=i) ? (i-af) : ((af-i)%2);
            xb = o + h*ba + hadj;
            mf = Math.min(e,g-i);
            cb = (i_rp < 0) ? b - 1 + mult -1 : b -1 + rep;
            cb += (j < 0) ? -ba : ba;
            if (r>=b-1) cb += ba;
            sf = (rep>0) ? 0+r%2 : 0;
            aft += sf;
            for(f=sf; f < mf; f++) 
            {
              polyz++;
              hexX = xb;
              hexX += 2*h*f;
              c = cb;
              c += f;
              if (t==1) 
              {
                  polyx = [(hexY-s/2),(hexY+s/2),(hexY+s),(hexY+s/2),(hexY-s/2),(hexY-s)];
                  polyy = [(hexX-h),(hexX-h),hexX,(hexX+h),(hexX+h),hexX];
                  imgx = hexY - halfpiece;
                  imgy = hexX - halfpiece;
              } else {
                  polyx = [(hexX-h),(hexX-h),hexX,(hexX+h),(hexX+h),hexX];
                  polyy = [(hexY-s/2),(hexY+s/2),(hexY+s),(hexY+s/2),(hexY-s/2),(hexY-s)];
                  imgy = hexY - halfpiece;
                  imgx = hexX - halfpiece;
              }
          // draw the polygon
          //  clrd = cp[(c%3)];
            jg.setColor(br);
           // jg.fillPolygon(polyx,polyy);
                      	 cellrep = (polyz>72) ? 1 : 0;
                 	 cellbas = polyz - (cellrep*72) - 1 + aft;
                 	 cellalp = Math.floor((cellbas)/6);
                 	 cellnum = 12 - cellbas%6 - cellrep*6;
                 	 cellf = jg.cellalpha[cellalp]
                 	 cellr = cellnum.toString();
                 	 celltxt = cellf + cellr;
                 if(i_terr == "yes") {
                       	 inHex = getTerrain(celltxt);
                 	 hexImg = inHex.split("#");
                    for(el=1;el<hexImg.length;el++) {
                 	 drawImg = hexImg[0]+hexImg[el];
                 	 jg.drawImage(drawImg,imgx, imgy, 70, 70);
                    }
                   inHex = getNotes(celltxt);
                   if(inHex != "none") {
                   	   jotn[jnc] = inHex;
                   	   jotr[jnc] = celltxt;
                   	   jotx[jnc] = (jnc ==0 ) ? imgx/2 : imgx;
                   	   joty[jnc] = (jnc ==0 ) ? imgy/2 : imgy;
                   	   if(celltxt == "m12") {
                   	   	   joty[jnc] = imgy/2;
                   	   	   jotx[jnc] = imgx-35;
                   	   }
                   	   jnc++;
                   }
                 }
            if (i_brd == "yes") {
                 inHex = getRedForces(celltxt);
                 if(inHex != "none") {
                 	 hexImg = inHex.split("#");
                     for(el=1;el<hexImg.length;el++) {
                 	 drawImg = hexImg[0]+hexImg[el];
                 	 jg.drawImage(drawImg,imgx, imgy, 70, 70);
                      }
                 }
                  inHex = getBluForces(celltxt);
                  if(inHex != "none") {
                 	 hexImg = inHex.split("#");
                     for(el=1;el<hexImg.length;el++) {
                 	 drawImg = hexImg[0]+hexImg[el];
                 	 jg.drawImage(drawImg,imgx, imgy, 70, 70);
                      }
                   }
            }
                  jg.drawPolygon(polyx,polyy);
                  if (i_frame == "yes") {
                 	 jg.setColor(hr);
                 	 jg.drawString(celltxt,hexY-8,hexX-8);
                   }
                   jl = jotn.length;
                   if(jl >= 1) {
                   	   for(nc = 0;nc<jl;nc++) {
                   	      if((nc == 0) || (jotr[nc] == "m12")) {
                   	          jg.setFont('verdana,geneva,helvetica,sans-serif', Number.toString(10), Font.PLAIN);
                   	          jg.setColor("maroon");
                   	      }
                   	      else { 
                   	       	  jg.setFont('verdana,geneva,helvetica,sans-serif', String.fromCharCode(0x31, 0x32, 0x70, 0x78), Font.PLAIN);
                   	       	  jg.setColor("white");
	     		      }
                   	       jg.drawString(jotn[nc],jotx[nc],joty[nc]);
                   	   }
                   }
                   jg.setFont('verdana,geneva,helvetica,sans-serif', String.fromCharCode(0x31, 0x32, 0x70, 0x78), Font.PLAIN);
         }
      draw++;
    }
    rep--;
    }
    jg.paint();
  }      



