/**
 * calculate and return prices.
 * @param wnd link to main wnd (index page of layout)
 * @return object with all prices
 */
function calculatePrices(wnd)
{
  if(wnd.needTurnImage) {
    sizex = Number(Number(wnd.image.imageSizeY) - wnd.cropPos2 - wnd.cropPos4);
    sizey = Number(Number(wnd.image.imageSizeX) - wnd.cropPos1 - wnd.cropPos3);
  } else {
    sizex = Number(Number(wnd.image.imageSizeX) - wnd.cropPos2 - wnd.cropPos4);
    sizey = Number(Number(wnd.image.imageSizeY) - wnd.cropPos1 - wnd.cropPos3);
  }

  var price = new Object();
  price.art = wnd.image.price;
  price.total = Number(wnd.image.price);
  if(wnd.c_transfer==1) {
    price.c_transfer=wnd.c_transferPrice*(sizex+sizey);
    price.total += price.c_transfer;
  }
  if(wnd.c_transfer==2) {
    price.c_transfer=wnd.image.price/2+2.2*(sizex+sizey);
    wnd.c_transferPrice=price.c_transfer;
    price.total += price.c_transfer;
  }
  if(wnd.brushstrokes>0) {
    price.brushstrokes=wnd.brushstrokesPrice*(sizex+sizey);
    price.total += price.brushstrokes;
  }
  if(wnd.gal_wrap>0) {
    price.gal_wrap=wnd.gal_wrapPrice*(sizex+sizey);
    price.total += price.gal_wrap;
  }
  if(wnd.museum_wrap>0) {
    price.museum_wrap=wnd.museum_wrapPrice*(sizex+sizey);
    price.total += price.museum_wrap;
  }

  var tmpX = sizex;
  var tmpY = sizey;
  eval( prepareFrameDeltaString("fillet") );
  sizex = tmpX;
  sizey = tmpY;
  sizex += 2*wnd.bottomMatWidth+2*wnd.middleMatWidth+2*wnd.topMatWidth;
  sizey += 2*wnd.bottomMatWidth+2*wnd.middleMatWidth+2*wnd.topMatWidth;
  eval( prepareMatDeltaString("bottomMat") );
  eval( prepareMatDeltaString("middleMat") );
  eval( prepareMatDeltaString("topMat") );
  eval( prepareFrameDeltaString("liner") );

  //calculate frame price
  if(wnd.frame.type=='cframe')
  {
    if(wnd.frame.width){
    price.frame = wnd.frame.price;
  sizex += 2 * wnd.frame.width - 0.5;
  sizey += 2 * wnd.frame.width - 0.5;
  price.total += price.frame;}
  }
  else
    eval( prepareFrameDeltaString("frame") );

  //calculate glass price
  if( wnd.glass && wnd.glass.id ) {
    var divisor = wnd.inchesInSquareFoot;
    if(wnd.measureUnit=='cm')
    divisor = wnd.cmInSquareMetre;  
    //var divisor = wnd.
    var glassW = sizex;
    var glassH = sizey;
    if(wnd.frame.width > 0 && (wnd.frame.type == 'cframe' || wnd.frame.type == 'frame')) {
      glassW -= 2 * wnd.frame.width;
      glassH -= 2 * wnd.frame.width;
    }
    if(wnd.pricing['glass']==1)
      price.glass = roundPrice((glassW + glassH) * wnd.glass.price);
    else
      price.glass = roundPrice(((glassW * glassH)/divisor) * wnd.glass.price);
    price.total += price.glass;
  }

  if(wnd.framing_kit ) {
    price.crop = wnd.cropPrice;
    price.total += price.crop;
  }

  if(wnd.cropPos1!=0 || wnd.cropPos2!=0 || wnd.cropPos3!=0 || wnd.cropPos4!=0 ) {
    price.crop = wnd.cropPrice;
    price.total += price.crop;
  }

  if(wnd.groove > 0) {
    price.groove = wnd.groovePrice;
    price.total += price.groove;
  }

  price.art = roundPrice( price.art );
  price.total = roundPrice( price.total );
  sizex = roundSize(sizex);
  sizey = roundSize(sizey);
  return price;
}

function prepareFrameDeltaString(obj)
{
 var sub;
  if(obj!="fillet")sub=1;
  else sub=0;
 return "if( wnd."+obj+".width  > 0 ){\n"+
  "var wasteP=0;"+
  "if(wnd.pricing['"+obj+"']==1){if(wnd.waste==1&&'"+obj+"'=='frame')wasteP=wnd."+obj+".width*8*wnd."+obj+".price/2;price."+obj+" = (sizex + sizey) * wnd."+obj+".price;}\n"+
  "else price."+obj+" = (sizex + sizey + 4*wnd."+obj+".width-" + sub + ") * wnd."+obj+".price;\n"+
  "if(wnd.pricing['"+obj+"']==4)price."+obj+" /= 12;"+
  "sizex += 2*wnd."+obj+".width-0.5;\n"+
  "sizey += 2*wnd."+obj+".width-0.5;\n"+
  "price.total += Number(price."+obj+" + wasteP);\n"+
  "price."+obj+" = roundPrice(price."+obj+");"+
  "}\n";
}
function prepareMatDeltaString(obj) {
  return "if( wnd."+obj+"Width  > 0 ){\n"+
  "price."+obj+" = calculateSquare(sizex,sizey,wnd.pricing['"+obj+"']) * wnd."+obj+".price;\n"+
  "price.total += Number(price."+obj+");\n"+
  "price."+obj+" = roundPrice(price."+obj+");"+
  "}\n";
}
function calculateSquare(bigX,bigY,pricing) {
  return pricing == 1 ? (bigX+bigY) : (bigX*bigY);
}

/** round double value to 2 value after coma. */
function roundPrice(val)
{
  return Math.round(val*100)/100;
}
/** round double value to 2 value after coma. */
function roundSize(val)
{
  return Math.round(val*100)/100;
}

function arrayToParams(arr, name, startSimbolIfNoEmpty)
{
    var str = "";
    for(var i=0; i<arr.length; i++){
        if( ! arr[i] )
            continue;
        if( i>0 )
            str += "&";
        str += name+"["+i+"]="+arr[i];
    }
    return str.length>0 ? startSimbolIfNoEmpty+str : str;
}

function extractArrayFromParamsArray(parArr, name)
{
    var i=1;
    var arr = new Array();
    while( true ){
        if( parArr[name+"["+i+"]"] ){
            arr[i] = parArr[name+"["+i+"]"];
        }else{
            break;
        }
        i++;
    }
    return arr;
}

function URLEncode( plaintext )
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" +                  // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+";             // x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "+";
            } else {
      if(ch!='/')
      {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
      }
      else
        encoded += "/";
            }
        }
    } // for

    return encoded;
}

function createWnd(url, name)
{   
    if(!name) name='';
    var wnd = window.open(url, name,'scrollbars=yes, menubar=yes, toolbar=no, status=yes, directories=yes, resizable=yes, location=yes, width=780, height=450, top=0, left=0');
    wnd.focus();
}

function getDesc(p)
{
//Generation string
  var prices = calculatePrices(p);
  var is_even = '';
  
  var Strgen = "<table border=0 cellspacing=2 cellpadding=2 width=70%><tr><td width=100%><table border=0 cellspacing=0 cellpadding=0 width=100% align=center>" +
  "<tr nowrap>" +
  "<td>Screen Size: </td>" +
  "<td>" +
  " " + sizex + " x " + sizey +
  "</td>" +
  "</tr>" +
  "<tr nowrap>" +
  "<td>" +
  "Image: " +
  "</td>" +
  "<td>" +
  " " + p.image.title + "</td>" +
  "</tr>";
  if(p.c_transfer > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Canvas Transfer: " +
    " " + "</td>" +
    "<td align=right>" + format_price(prices.c_transfer) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.brushstrokes > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Brushstrokes: " +
    " " + "</td>" +
    "<td align=right>" + format_price(prices.brushstrokes) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.museum_wrap > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Museum Wrap: " +
    " " + "</td>" +
    "<td align=right>" + format_price(prices.museum_wrap) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.gal_wrap > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Gallery Wrap: " +
    " " + "</td>" +
    "<td align=right>" + format_price(prices.gal_wrap) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.frame.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" + "<td>" + "Frame: ";
    if(p.frame.type=='cframe') {
      is_even = is_even == '' ? '1' : '';
    Strgen += " (" + p.frame.frameWidth +"x"+ p.frame.frameHeight + " " + p.frame.width + p.measureUnit + ")" + "</td>";
    } else {
      is_even = is_even == '' ? '1' : '';
    Strgen += " (" + p.frame.width + p.measureUnit + ")" + "</td>";
    }
    is_even = is_even == '' ? '1' : '';
    Strgen += "<td> " +
    p.frame.name + "</td>" +
    "</tr>";
  }
  if(p.liner.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Liner: " + " (" + p.liner.width + p.measureUnit + ")" + "</td>" +
    "<td>" +
    p.liner.name + "</td>" +
    "</tr>";
  }
  if(p.topMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Top Mat: " + " (" + p.topMatWidth + p.measureUnit + ")" + "</td>" +
    "<td> " +
    p.topMat.name + "</td>" +
    "</tr>";
  }
  if(p.middleMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Middle Mat: " + " (" + p.middleMatWidth + p.measureUnit + ")" + "</td>" +
    "<td>" +
    p.middleMat.name + "</td>" +
    "</tr>";
  }
  if(p.bottomMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Bottom Mat: " + "(" + p.bottomMatWidth + p.measureUnit + ")" + "</td>" +
    "<td>" +
    p.bottomMat.name + "</td>" +
    "</tr>";
  }
  if(p.fillet.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td>" +
    "Fillet: " + " (" + p.fillet.width + p.measureUnit + ")" + "</td>" +
    "<td>" +
    p.fillet.name + "</td>" +
    "</tr>";
  }
  if(p.groove > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap><td>" +
    "V-Groove: " + "</td>" +
    "<td>" +
    "&nbsp;" + p.grooveDeltaX*0.125 + p.measureUnit + "</td>" +
    "<td align=right>";
  if(prices.groove > 0)
    Strgen += "" + format_price(prices.groove) + p.moneySuffix;
  else
    Strgen += " ;";
    Strgen += "</td></tr>";
  }
  if((p.cropPos1!=0 || p.cropPos2!=0 || p.cropPos3!=0 || p.cropPos4!=0) && p.cropPrice>0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap><td class='mainText'>" +
    "Crop: " + "</td>" +
    "<td>" +
    "&nbsp;" + "<td align=right>" + format_price(prices.crop) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.glass && p.glass.id) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr nowrap>" +
    "<td> Glass: </td>" +
    "<td>" +
    p.glass.name + "</td>" +
    "</tr></table>";
  }
  
  /*var Strgen = "<table border=0 cellspacing=2 cellpadding=2 width=100%><tr><td width=100%><table border=0 cellspacing=0 cellpadding=0 width=100% class='grayfont' align=center>" +
  "<tr class='stripes1' nowrap>" +
  "<td  class='mainText'>Size: </td>" +
  "<td class='mainText'>" +
  " " + sizex + " x " + sizey +
  "</td>" +
  "<td align=right nowrap  class='mainText'>" + p.moneyPrefix + format_price(prices.total) + p.moneySuffix + "</td>"+
  "</tr>" +
  "<tr class='stripes" + is_even + "' nowrap>" +
  "<td  class='mainText'>" +
  "Art: " +
  " " + p.image.imageSizeX + " x " + p.image.imageSizeY
    + "</td>" +
  "<td  class='mainText'>" +
  " " + p.image.title + "</td>" +
  "<td align=right class='mainText'>" + format_price(prices.art) + p.moneySuffix + "</td>"+
  "</tr>";
  if(p.c_transfer > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Canvas Transfer: " +
    " " + "</td>" +
      "<td>" +
    " " + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.c_transfer) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.brushstrokes > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Brushstrokes: " +
    " " + "</td>" +
      "<td>" +
    " " + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.brushstrokes) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.museum_wrap > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Museum Wrap: " +
    " " + "</td>" +
      "<td>" +
    " " + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.museum_wrap) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.gal_wrap > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Gallery Wrap: " +
    " " + "</td>" +
      "<td>" +
    " " + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.gal_wrap) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.frame.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" + "<td class='mainText'>" + "Frame: ";
    if(p.frame.type=='cframe') {
      is_even = is_even == '' ? '1' : '';
    Strgen += " (" + p.frame.frameWidth +"x"+ p.frame.frameHeight + " " + p.frame.width + p.measureUnit + ")" + "</td>";
    } else {
      is_even = is_even == '' ? '1' : '';
    Strgen += " (" + p.frame.width + p.measureUnit + ")" + "</td>";
    }
    is_even = is_even == '' ? '1' : '';
    Strgen += "<td class='mainText'> " +
    p.frame.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.frame) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.liner.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Liner: " + " (" + p.liner.width + p.measureUnit + ")" + "</td>" +
    "<td class='mainText'>" +
    p.liner.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.liner) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.topMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Top Mat: " + " (" + p.topMatWidth + p.measureUnit + ")" + "</td>" +
    "<td class='mainText'> " +
    p.topMat.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.topMat) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.middleMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Middle Mat: " + " (" + p.middleMatWidth + p.measureUnit + ")" + "</td>" +
    "<td class='mainText'>" +
    p.middleMat.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.middleMat) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.bottomMatWidth > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Bottom Mat: " + "(" + p.bottomMatWidth + p.measureUnit + ")" + "</td>" +
    "<td class='mainText'>" +
    p.bottomMat.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.bottomMat) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.fillet.width > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'>" +
    "Fillet: " + " (" + p.fillet.width + p.measureUnit + ")" + "</td>" +
    "<td class='mainText'>" +
    p.fillet.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.fillet) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.groove > 0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap><td class='mainText'>" +
    "V-Groove: " + "</td>" +
    "<td class='mainText'>" +
    "&nbsp;" + p.grooveDeltaX*0.125 + p.measureUnit + "</td>" +
  "<td align=right class='mainText'>";
  if(prices.groove > 0)
    Strgen += "" + format_price(prices.groove) + p.moneySuffix;
  else
    Strgen += " ;";
    Strgen += "</td></tr>";
  }
  if((p.cropPos1!=0 || p.cropPos2!=0 || p.cropPos3!=0 || p.cropPos4!=0) && p.cropPrice>0) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap><td class='mainText'>" +
    "Crop: " + "</td>" +
    "<td class='mainText'>" +
    "&nbsp;" + "<td align=right class='mainText'>" + format_price(prices.crop) + p.moneySuffix + "</td>"+
    "</tr>";
  }
  if(p.glass && p.glass.id) {
    is_even = is_even == '' ? '1' : '';
    Strgen += "<tr class='stripes" + is_even + "' nowrap>" +
    "<td class='mainText'> Glass: </td>" +
    "<td class='mainText'>" +
    p.glass.name + "</td>" +
    "<td align=right class='mainText'>" + format_price(prices.glass) + p.moneySuffix + "</td>"+
    "</tr></table>";
  }*/
  return Strgen;
}

function format_price(price) {
  var zero='';
  var intP=Math.floor(price);
  var realP=Math.round((price-intP)*100);
  if(realP<10)
    zero='0';
  return intP+"."+zero+realP;
}
