
  var side, side1, side2, spaceWidth, extendWidth, dif, toolfix;
  var pType = 2; pLine = 0, pAlign = "center", pUse = 1;

  var path = window.location.href;
  path = path.substr(0, path.lastIndexOf("/")+1);

  var let1 = "ÇÃÅÂÄÁÏÐÑÒæÉì"
  var let2 = "ÇÃÅÂÄÆÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÝÞßáãäåæíÉì";
  var let3 = "óðõñöòúø";

  defaultFont = "tahoma,ms sans serif,verdana,arial,helvetica";

  //poetry
  var fname = "Simplified Arabic", fsize = 14, fbold = false, fitalic = false, fcolor = "black";
  var bkcolor = "", bkimage = "";
  var obstyle = "double", obsize=0, obcolor="gray";
  var ibchar = "", ibcolor = "black";

  //title
  var fname2 = "Simplified Arabic", fsize2 = 14, fbold2 = true, fitalic2 = false, fcolor2 = "black";
  var bkcolor2 = "", bkimage2 = "";
  var obstyle2 = "double", obsize2=4, obcolor2="gray";

  //poet
  var fname3 = "Simplified Arabic", fsize3 = 12, fbold3 = true, fitalic3 = false, fcolor3 = "black";
  var aAlign = "left";

  for (seli = 1; seli <= 50; seli++) {
    var oOption = document.createElement("OPTION");
    selImg.options.add(oOption);
    oOption.innerText = seli;
    var oOption = document.createElement("OPTION");
    selImg2.options.add(oOption);
    oOption.innerText = seli;
  }


  function trim(t) {
    t = t.replace(/^[ ]+/,"");
    t = t.replace(/[ ]+$/,"");
    return t;
  }

  function textWidth(t) {
    testWidth.innerHTML= t.replace(/ /gi, "&nbsp;");
    return ((testWidth.clientWidth - 2) * 0.75);
  }

  function strReplace(t, s1, s2, c) {
    var i = 0, len1 = s1.length; len2 = s2.length;
    while (1) {
      i = t.indexOf(s1, i);
      if (i == -1)
        break;
      else {
        t = t.substr(0, i) + s2 + t.substr(i+len1);
        i += len2;
        if (--c == 0) break;
     }
    }
    return t
  }

  function count(str, ch) {
    var c = 0;
    for (var i = 0; i < str.length; i++)
      c += (str.charAt(i) == ch);
    return c;
  }

  function space(n) {
    if (n <= 1)
      return " ";
    return  " " + space( n - 1);
  }

  function createExtend(t) {
    var alef = "ÇÃÅÂ";
    var s = "";
    var befor = "", ch = "";
    t = t.replace(/Çááå/g,"@");
    for (i = 0; i < t.length; i++) {
      ch = t.charAt(i);
      if (let2.indexOf(ch.charAt(0)) != -1 || ch.charAt(0) == "Á") {
        if (befor != "")
          if (let1.indexOf(befor.charAt(0)) == -1 && let2.indexOf(ch.charAt(0)) != -1
            && (befor.charAt(0) != "á"
            || alef.indexOf(ch.charAt(0)) == -1))
            s += "^";
        befor = ch;
        s += ch;
      }
      else {
        s += ch;
        if (let3.indexOf(ch.charAt(0)) == -1)
          befor = "";
      }
    }
    s = s.replace(/@/g,"Çááå");
    return s;
  }

  function extend(n) {
    if (n <= 1)
      return "Ü";
    return  "Ü" + extend(n-1);
  }

  function justifyBYextend(t, w) {
    var ex = 0, i, d, n, p, m;
    t = createExtend(t);
    ex = count(t, "^");
    if (ex == 0) return t;
    d = Math.floor((w - textWidth(t.replace(/\^/g,""))) / extendWidth);
    t = t.split(" ");
    n = 0;
    for (i = 0; i < t.length; i++)
      n += (t[i].indexOf("^") != -1);
    p = Math.floor(d / n);
    m = d % n;
    for (i = 0; i < t.length; i++) {
      ex = count(t[i], "^");
      if (ex == 0)
        continue;
      d = p + (m-- > 0);
      n = Math.floor(d / ex);
      if (n > 0)
        t[i] = t[i].replace(/\^/g, "^" + extend(n));
      if (d % ex > 0)
        t[i] = strReplace(t[i], "^", extend(1), d % ex);
    }
    t = t.join(" ");
    t = t.replace(/\^/g, "");
    return t;
  }

  function justifyBYspace(t, w, dir) {
    var sp = 0, i, d, tw, n;
    sp = count(t, " ");
    if (sp != 0) {
      d = Math.floor((w - textWidth(t)) / spaceWidth);
      n = Math.floor(d / sp);
      if (n > 0)
        t = t.replace(/ /g, space(n + 1));
      if (d % sp > 0)
        t = strReplace(t, space(n + 1), space(n + 2), d % sp);
    }
    tw = w - textWidth(t);
    if (tw > 0) {
      i = t.lastIndexOf(" ");
      if (i == -1)
        if (dir == 0)
          return t;
        else
          i = ((dir==2)?-1:t.length-1);
      tw -= dif;
      t = t.substr(0, i+1) + toolfix.replace(/letter-spacing:normal/,"letter-spacing:"+tw+"pt ") + t.substr(i+1);
    }
    return t;
  }

  function justify(t, w, dir) {
    if (pUse == 1) t = justifyBYextend(t, w);
    return justifyBYspace(t, w, dir);
  }

  function doJustify () {
    var L, R, MTW, i, j, cr, txt;

    toolfix = "<span style='font-size:1pt;letter-spacing:normal;visibility:hidden;'>ii</span>";
    testWidth.innerHTML = toolfix;
    dif = (testWidth.clientWidth - 2) * 0.75;
    toolfix = toolfix.replace(/ /g,"^");

    testWidth.style.font = (fitalic?"italic":"normal")+" normal "+(fbold?"bold ":"normal ")+fsize+"pt "+fname+","+defaultFont;

    txt = txt = poetry.value;
    txt = txt.replace(/[\^@Ü]/g,"");
    txt = txt.replace(/[ ]+/g," ");
    txt = txt.replace(/^[\r\n]+/,"");
    txt = txt.replace(/[\r\n]+$/,"");
    txt = txt.replace(/\n/g,"");
    txt = txt.replace(/[ ]*\r[ ]*/g,"\r");
    txt = txt.replace(/[ ]*=[ ]*/g,"=");
    txt = trim(txt);
    poetry.value = txt.replace(/=/g," = ");
    txt += "\r";

    extendWidth = textWidth("Ô" + "ÜÜ" + "Ô");
    extendWidth -= textWidth("Ô" + "Ü" + "Ô");
    spaceWidth = textWidth("Ô" + "&nbsp;&nbsp;" + "Ô");
    spaceWidth -= textWidth("Ô" + "&nbsp;" + "Ô");

    var nbsp = "";
    while (1) {
      nbsp += "&nbsp;";
      if (textWidth(nbsp) > 12) break;
    }

    cr = 0;
    j = 0;
    MTW = textWidth(txt.replace(/\r/g,"<br>").replace(/=/g,"<br>"));

    side = "";
    cr = 0;
    j = 0;
    var sn = 0;
    while (1) {
      cr = txt.indexOf("\r", cr);
      if (cr == -1) break;
      if (txt.substring(j, cr) != "") {
        i = txt.indexOf("=", j);
        if (i == -1 || i > cr) i = cr; //no '=' in this line
        L = txt.substring(j, i);
        L = justify(L, MTW, ((i==cr)?0:1));
        L = L.replace(/ /g, "&nbsp;");
        L = L.replace(/\^/g, " ");

        R = txt.substring(i+1, cr);
        R = justify(R, MTW, 2);
        R = R.replace(/ /g, "&nbsp;");
        R = R.replace(/\^/g, " ");

        if (pType == 0) {
          if (i != cr)
            side += L + nbsp + ibchar + nbsp + R + "<br>";
          else
            side += "<div align=center>" + L + "</div>";
        }
        else if (pType == 1) {
          if (i != cr) {
            side += "<div style='margin-left:"+Math.round(MTW)+"pt'>" + L + "</div>";
            side += "<div style='margin-right:"+Math.round(MTW)+"pt'>" + R + "</div>";
          }
          else {
            side += "<div align=center>" + L + "</div>";
          }
        }
        else if (pType == 2) {
          if (i != cr) {
            side += "<div style='margin-left:"+Math.round(MTW/1.5)+"pt'>" + L + "</div>";
            side += "<div style='margin-right:"+Math.round(MTW/1.5)+"pt'>" + R + "</div>";
          }
          else {
            side += "<div align=center>" + L + "</div>";
          }
        }
        else if (pType == 3) {
          side += L + "<br>";
          if (i != cr) {
            side += R + "<br>";
            side += "<br>";
          }
        }
        else if (pType == 4) {
          side += L + "<br>";
        }
      }
      else {
        side += "<br>";
      }
      cr += 1;
      j = cr;
    }
    side = side.replace(/(<br>)+$/i, "");
    return ToHTML();
  }


  function ToHTML() {
    var  titlestyle, poetrystyle, middlestyle="";

    poetstyle = "font:"+(fitalic3?"italic":"normal") + " normal " + (fbold3?"bold ":"normal ") + fsize3 + "pt " + fname3;
    poetstyle += "; color:" + fcolor3;

    titlestyle = "; font:"+(fitalic2?"italic":"normal") + " normal " + (fbold2?"bold ":"normal ") + fsize2 + "pt " + fname2;
    titlestyle += "; color:" + fcolor2;
    titlestyle += "; background-image:url(" + bkimage2 + "); background-color:" + bkcolor2;
    titlestyle += "; border:"+ obsize2 +" " + obstyle2 +" " + obcolor2;

    poetrystyle = "font:"+(fitalic?"italic":"normal") + " normal " + (fbold?"bold ":"normal ") + fsize + "pt " + fname;
    poetrystyle += "; color:" + fcolor;
    poetrystyle += "; background-image:url(" + bkimage + "); background-color:" + bkcolor;
    poetrystyle += "; border:" + obsize +" " + obstyle +" " + obcolor;
    poetrystyle += "; line-height:" + ((pLine==0)?"200%":"350%");

    middlestyle = "color:" + ibcolor;

    HTML = "<table cellspacing='20' cellpadding='0' dir='rtl' border='0' width='0' style='" + poetrystyle + "'>";
    HTML += "<tr>";
    HTML += "<td width=0 align=center valign=top nowrap>" + side + "</td>";
    HTML += "</tr></table>";

    here.align = pAlign;

    here.innerHTML = HTML;


  }
