
function validateSize(width,height)
{
  w = width;
  h = height;
  if (width > height) {
    h = width;
    w = height;
  }
  if (w <= 41 && h <= 63 && w >= 9.5 && h >= 12.8) {
    return 1;
  }
  else if (w > 41 || h > 63){
    return -2;
  }
  else {
    return -1;
  }
}

function getPrice(width,height)
{
  w = width;
  h = height;
  if (width > height) {
    h = width;
    w = height;
  }

    if (w < 12) {
      w = 12;
    }
    if (h < 12) {
      h = 12;
    }

    h = h - 12.8;
    w = w - 9.5;

    return ((w+h)*2.5) + 14;

}

function applyDiscount(width,height,price)
{
  percentageSavings = 1;

  if ((width * height) >= 190) {  // A3
    percentageSavings = 0.95;
  }
  if ((width * height) >= 380) {  // A2
    percentageSavings = 0.88;
  }
  if ((width * height) >= 750) {  // A1
    percentageSavings = 0.78;
  }
  if ((width * height) >= 1500) { // A0
    percentageSavings = 0.68;
  }

  return (price * percentageSavings);
}

function getPostage(width,height,price)
{
  var w = width;
  var h = height;
  if (width > height) {
    h = width;
    w = height;
  }
  var postage = 5;
  if (h >= 10) {
    var a = h-10;
    postage = parseInt(a/3) + 5;
    if (h == w) {
      if (parseInt((h+1-10)/3) != postage) {
        postage = parseInt((h+1-10)/3) + 5;
      }
    }
  }
  if (w <= 40 && h <= 60 && w >= 8 && h >= 8) {
    return postage+5;
  }
  else {
    // canvas too big
    return -1;
  }
}
function calculatePrice()
{

  var linkunits = document.getElementById('units').value

  if (document.getElementById('standard_size')) {

    if (document.getElementById('standard_size').disabled == true) {
      var width1 = (Math.round(parseFloat(document.getElementById('width').value)*10))/10;
      var height1 = (Math.round(parseFloat(document.getElementById('height').value)*10))/10;

      var linkwidth = width1;
      var linkheight = height1;

      // convert height and width to inches if they are in cm.
      if (document.getElementById('units').value == 1) {
        height1 = height1 / 2.54;
        width1 = width1 / 2.54;
      }

    }
    else {
      if (document.getElementById('standard_size').value == 1) {
        var height1 = 33.1;
        var width1 = 46.8;
      }
      else if (document.getElementById('standard_size').value == 2) {
        var height1 = 23.4;
        var width1 = 33.1;
      }
      else if (document.getElementById('standard_size').value == 3) {
        var height1 = 16.5;
        var width1 = 23.4;
      }
      else if (document.getElementById('standard_size').value == 4) {
        var height1 = 11.7;
        var width1 = 16.5;
      }
      else if (document.getElementById('standard_size').value == 5) {
        var height1 = 8.3;
        var width1 = 11.7;
      }
else if (document.getElementById('standard_size').value == 6) {
		  var width1 = 32;
		  var height1 = 24;
		}
		else if (document.getElementById('standard_size').value == 7) {
		  var width1 = 32;
		  var height1 = 20;
		}
		else if (document.getElementById('standard_size').value == 8) {
		  var width1 = 28;
		  var height1 = 20;
		}
		else if (document.getElementById('standard_size').value == 9) {
		  var width1 = 24;
		  var height1 = 24;
		}
		else if (document.getElementById('standard_size').value == 10) {
		  var width1 = 24;
		  var height1 = 20;
		}
		else if (document.getElementById('standard_size').value == 11) {
		  var width1 = 24;
		  var height1 = 16;
		}
		else if (document.getElementById('standard_size').value == 12) {
		  var width1 = 20;
		  var height1 = 16;
		}
		else if (document.getElementById('standard_size').value == 13) {
		  var width1 = 16;
		  var height1 = 16;
		}
		else if (document.getElementById('standard_size').value == 14) {
		  var width1 = 16;
		  var height1 = 12;
		}
		else if (document.getElementById('standard_size').value == 15) {
		  var width1 = 12;
		  var height1 = 12;
		}
      var linkwidth = width1;
      var linkheight = height1;

      linkunits = 2;
    }
  }
  else {

        var width1 = (Math.round(parseFloat(document.getElementById('width').value)*10))/10;
        var height1 = (Math.round(parseFloat(document.getElementById('height').value)*10))/10;

        var linkwidth = width1;
        var linkheight = height1;

        // convert height and width to inches if they are in cm.
        if (document.getElementById('units').value == 1) {
          height1 = height1 / 2.54;
          width1 = width1 / 2.54;
        }

  }

  var width2 = width1;
  var height2 = height1;

  width2 = (Math.round(width2*100))/100;
  height2 = (Math.round(height2*100))/100;

  if (document.getElementById('depth2').value == 1) {
    height1 += 1.2;
    width1 += 1.2;
  }

  if (document.getElementById('depth2').value == 2) {
    height1 = height1 + 2.8;
    width1 = width1 + 2.8;
  }

  var validSize = validateSize(width1,  height1);


  if (validSize >= 0) {
    var canvas = applyDiscount(width2, height2,getPrice(width1, height1));
    //canvas = canvas + 10;  //add standard £10 postage
    //canvas = canvas*percentageSavings;
    canvas = (Math.round(canvas*100))/100;
  }
  else {
    var canvas = -1;
  }

  if ((document.getElementById('width').value == "") || (document.getElementById('height').value == "")) {
    document.getElementById('canvas_cost').value = "";
  }
  else if ((canvas <= 0) && ((!document.getElementById('standard_size')) || (document.getElementById('standard_size').disabled == true))){
    document.getElementById('canvas_cost').value = "invalid size!";
    if (document.getElementById('startOrderLink')) {
      document.getElementById('startOrderLink').innerHTML = "";
    }
    if (validSize == -1) {
      document.getElementById('canvas_cost').value = "size too small!";

    }
    else if (validSize == -2) {
      document.getElementById('canvas_cost').value = "size too large!";

    }
    //javascript:disableIt(document.forms[0].step3);

  }

  else {

    //javascript:enableIt(document.forms[0].step3);

    if ( (document.getElementById('standard_size')) && ((document.getElementById('standard_size').disabled == true) || (document.getElementById('standard_size').value > 0))) {
      if (canvas.toFixed(2) > 50) {
        document.getElementById('canvas_cost').value = "£"+canvas.toFixed(2)+" (free delivery)";
      }
      else {
        document.getElementById('canvas_cost').value = "£"+canvas.toFixed(2)+" + £5 p&p";
      }
      if (document.getElementById('startOrderLink')) {
        document.getElementById('startOrderLink').innerHTML = "<input src=\"http://www.photocanvasprinting.com/images/startyourorderbutton.gif\" alt=\"Start your order\" type=\"image\" value=\"custom\" name=\"Submit\"/>";
        //document.getElementById('startOrderLink').href= "startyourorder?width="+linkwidth+"&height="+linkheight+"&depth="+document.getElementById('depth2').value+"&units="+linkunits+"&SID";
      }
    }
    else {
      document.getElementById('canvas_cost').value = "";
      if (document.getElementById('startOrderLink')) {
        document.getElementById('startOrderLink').innerHTML = "";
      }

    }

//    if ((document.getElementById('units').value == 1) && (document.getElementById('depth2').value == 1)) {
//      document.getElementById('canvas_cost').value = "Price for "+document.getElementById('width').value+"cm x "+document.getElementById('height').value+"cm x 1.5cm is £"+canvas;
//    }
//    else if ((document.getElementById('units').value == 1) && (document.getElementById('depth2').value == 2)) {
//      document.getElementById('canvas_cost').value = "Price for "+document.getElementById('width').value+"cm x "+document.getElementById('height').value+"cm x 3.6cm is £"+canvas;
//    }
//    else if ((document.getElementById('units').value == 2) && (document.getElementById('depth2').value == 1)) {
//      document.getElementById('canvas_cost').value = "Price for "+document.getElementById('width').value+"\" x "+document.getElementById('height').value+"\" x 0.6\" is £"+canvas;
//    }
//    else if ((document.getElementById('units').value == 2) && (document.getElementById('depth2').value == 2)) {
//      document.getElementById('canvas_cost').value = "Price for "+document.getElementById('width').value+"\" x "+document.getElementById('height').value+"\" x 1.4\" is £"+canvas;
//    }
  }
}





function extracheck(obj)
{
  return !obj.disabled;
}

function disableIt(obj)
{
  obj.disabled = true;
  //var z = (obj.disabled) ? 'disabled' : 'enabled';
  //alert(obj.type + ' now ' + z);
}

function enableIt(obj)
{
  obj.disabled = false;
  var z = (obj.disabled) ? 'disabled' : 'enabled';
  //alert(obj.type + ' now ' + z);
}

function disableButton(obj) {
  obj.disabled=true;
  obj.value = "Please Wait!";
}


function SubDisable(dform) {
  if (document.getElementById) {
   for (var sch = 0; sch < dform.length; sch++) {
    if (dform.elements[sch].type.toLowerCase() == "submit") {
      dform.elements[sch].disabled = true;
      dform.elements[sch].value = "Please Wait!";
    }
   }
  }
  return true;
}




function popUp(URL) {
  window.open(URL, '', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=450,height=600');
}


function selectStandardSizeType() {

  document.getElementById('standard_size').disabled=false;

  document.getElementById('units').disabled=true;
  document.getElementById('width').disabled=true;
  document.getElementById('height').disabled=true;
}

function selectCustomSizeType() {

  document.getElementById('units').disabled=false;
  document.getElementById('width').disabled=false;
  document.getElementById('height').disabled=false;

  document.getElementById('standard_size').disabled=true;
}


function selectPixelSizeType() {

  document.getElementById('megapixelsize').disabled=true;

  document.getElementById('pixelwidth').disabled=false;
  document.getElementById('pixelheight').disabled=false;

  if ((document.getElementById('pixelwidth').value >0) && (document.getElementById('pixelheight').value >0)) {
    document.getElementById('canvaswidth').disabled=false;
    document.getElementById('canvasunits').disabled=false;
    document.getElementById('canvasdepth').disabled=false;
    selectAutoHeight();
  }
  else {
    document.getElementById('canvaswidth').disabled=true;
    document.getElementById('canvasheight').disabled=true;
    document.getElementById('canvasunits').disabled=true;
    document.getElementById('canvasdepth').disabled=true;
  }
}

function selectCameraSizeType() {

  document.getElementById('pixelwidth').disabled=true;
  document.getElementById('pixelheight').disabled=true;

  document.getElementById('megapixelsize').disabled=false;

  if (document.getElementById('megapixelsize').value >0) {
    document.getElementById('canvaswidth').disabled=false;
    document.getElementById('canvasunits').disabled=false;
    document.getElementById('canvasdepth').disabled=false;
    selectAutoHeight();
  }
  else {
    document.getElementById('canvaswidth').disabled=true;
    document.getElementById('canvasheight').disabled=true;
    document.getElementById('canvasunits').disabled=true;
    document.getElementById('canvasdepth').disabled=true;
  }
}



function selectAutoHeight() {

  if (document.getElementById('autoheight').checked) {
    document.getElementById('canvasheight').disabled=true;
  }
  else {
    if (((document.getElementById('megapixelsize').disabled==false) && (document.getElementById('megapixelsize').value >0)) || ((document.getElementById('pixelwidth').disabled==false) && (document.getElementById('pixelwidth').value >0) && (document.getElementById('pixelheight').value >0))) {
      document.getElementById('canvasheight').disabled=false;
    }
    else {
      document.getElementById('canvasheight').disabled=true;
    }
  }
}


function getPixelWidth() {
  if (document.getElementById('megapixelsize').disabled==true) {
    //use entered pixel width
    var apixelwidth = document.getElementById('pixelwidth').value
  }
  else {
    //determine pixel width from megapixel size
    if (document.getElementById('megapixelsize').value==1) {      // 1Mp
      var apixelwidth = 1184;
    }
    else if (document.getElementById('megapixelsize').value==2) { // 2Mp
      var apixelwidth = 1672;
    }
    else if (document.getElementById('megapixelsize').value==3) { // 3Mp
      var apixelwidth = 2048;
    }
    else if (document.getElementById('megapixelsize').value==4) { // 4Mp
      var apixelwidth = 2364;
    }
    else if (document.getElementById('megapixelsize').value==5) { // 5Mp
      var apixelwidth = 2644;
    }
    else if (document.getElementById('megapixelsize').value==6) { // 6Mp
      var apixelwidth = 2896;
    }
    else if (document.getElementById('megapixelsize').value==7) { // 7Mp
      var apixelwidth = 3128;
    }
    else if (document.getElementById('megapixelsize').value==8) { // 8Mp
      var apixelwidth = 3344;
    }
    else if (document.getElementById('megapixelsize').value==9) { // 10Mp
      var apixelwidth = 3740;
    }
  }

  return apixelwidth;

}


function getPixelHeight() {
  if (document.getElementById('megapixelsize').disabled==true) {
    //use entered pixel height
    var apixelheight = document.getElementById('pixelheight').value
  }
  else {
    //determine pixel height from megapixel size
    if (document.getElementById('megapixelsize').value==1) {      // 1Mp
      var apixelheight = 888;
    }
    else if (document.getElementById('megapixelsize').value==2) { // 2Mp
      var apixelheight = 1254;
    }
    else if (document.getElementById('megapixelsize').value==3) { // 3Mp
      var apixelheight = 1536;
    }
    else if (document.getElementById('megapixelsize').value==4) { // 4Mp
      var apixelheight = 1773;
    }
    else if (document.getElementById('megapixelsize').value==5) { // 5Mp
      var apixelheight = 1983;
    }
    else if (document.getElementById('megapixelsize').value==6) { // 6Mp
      var apixelheight = 2172;
    }
    else if (document.getElementById('megapixelsize').value==7) { // 7Mp
      var apixelheight = 2346;
    }
    else if (document.getElementById('megapixelsize').value==8) { // 8Mp
      var apixelheight = 2508;
    }
    else if (document.getElementById('megapixelsize').value==9) { // 10Mp
      var apixelheight = 2805;
    }
  }

  return apixelheight;
}


function showAutoHeight() {

  if ((document.getElementById('autoheight').checked) && (document.getElementById('canvaswidth').disabled==false) && (document.getElementById('canvaswidth').value > 0)) {

    var acanvasheight = calculateAutoHeight();

    document.getElementById('canvasheight').value = acanvasheight;
    document.getElementById('canvasheight').disabled=true;
  }
}


function calculateAutoHeight() {

  var acanvaswidth = parseFloat(document.getElementById('canvaswidth').value);
  var adepthid = document.getElementById('canvasdepth').value;

  var acanvasdepth = 0.0;

  if (document.getElementById('canvaswrap').checked) {

    if (adepthid == 1) {
      //acanvasdepth = 3;
      acanvasdepth = 4.5;
    }
    else if (adepthid == 2) {
      //acanvasdepth = 7.2;
      acanvasdepth = 7.5;
    }

    if (document.getElementById('canvasunits').value == 2) {  // inches selected
      if (adepthid == 1) {
        //acanvasdepth = 1.2;
        acanvasdepth = 1.8;
      }
      else if (adepthid == 2) {
        //acanvasdepth = 2.8;
        acanvasdepth = 3;
      }
    }

  }



  var apixelwidth  = getPixelWidth();
  var apixelheight = getPixelHeight();

  var afactor = apixelheight / apixelwidth;

  acanvaswidth+=acanvasdepth;


  var acanvasheighttemp = acanvaswidth * afactor;

  acanvasheighttemp = acanvasheighttemp - acanvasdepth;

  var acanvasheight = (Math.round(acanvasheighttemp * 10)) / 10;

  return acanvasheight;
}


function alterCanvasUnits() {
  var cmPerInch = 2.54;
  var acanvasheight = document.getElementById('canvasheight').value;
  var acanvaswidth = document.getElementById('canvaswidth').value;
  if (document.getElementById('canvasunits').value == 1) {  // cm selected
    acanvasheight = (Math.round((acanvasheight * cmPerInch) * 10)) / 10;
    acanvaswidth = (Math.round((acanvaswidth * cmPerInch) *10)) / 10;
  }
  else if (document.getElementById('canvasunits').value == 2) {  // inches selected
    acanvasheight = (Math.round((acanvasheight / cmPerInch) * 10)) / 10;
    acanvaswidth = (Math.round((acanvaswidth / cmPerInch) * 10)) / 10;
  }

  document.getElementById('canvaswidth').value = acanvaswidth;

  if (document.getElementById('autoheight').checked) {
    showAutoHeight();
  }
  else {
    document.getElementById('canvasheight').value = acanvasheight;
  }
}


function displayResults() {

  if ((document.getElementById('canvaswidth').disabled==false) && (document.getElementById('canvaswidth').value > 0)) {
    var cmPerInch = 2.54;

    if (document.getElementById('canvasunits').value == 1) {  // cm selected
      var unitSymbol="cm";
    }
    else if (document.getElementById('canvasunits').value == 2) {  // inches selected
      var unitSymbol="\"";
    }
    //document.getElementById('chosenCanvasSize').innerHTML = document.getElementById('canvaswidth').value + unitSymbol + " x " + document.getElementById('canvasheight').value + unitSymbol + "&nbsp;&nbsp;&nbsp;<a href=\"http://www.photocanvasprinting.com/prices?units="+document.getElementById('canvasunits').value+"&width="+document.getElementById('canvaswidth').value+"&height="+document.getElementById('canvasheight').value+"&depth="+document.getElementById('canvasdepth').value+"&typ=cust&SID\" class=\"normal_link\">click here to get a price for this size</a>";
    document.getElementById('chosenCanvasSize').innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"right\"><tr><td class=\"normal_text\" width=\"120\">" + document.getElementById('canvaswidth').value + unitSymbol + " x " + document.getElementById('canvasheight').value + unitSymbol + "</td><td><input type=\"hidden\" id=\"canvasheight2\" name=\"canvasheight2\" value=\""+document.getElementById('canvasheight').value+"\"/><input src=\"http://www.photocanvasprinting.com/images/showpriceforcustomsize.gif\" alt=\"Show price for custom size\" type=\"image\" value=\"custom\" name=\"custom\"/></td></tr></table>";



    var apixelwidth  = getPixelWidth();
    var apixelheight = getPixelHeight();


    var recommendedHeight = apixelheight / 100;
    var recommendedWidth = apixelwidth / 100;

    if (document.getElementById('canvaswrap').checked) {
      var adepthid = document.getElementById('canvasdepth').value;

      if (adepthid == 1) {
        //recommendedHeight = recommendedHeight - 1.2;
        //recommendedWidth = recommendedWidth - 1.2;
        recommendedHeight = recommendedHeight - 1.8;
        recommendedWidth = recommendedWidth - 1.8;
      }

      if (adepthid == 2) {
        //recommendedHeight = recommendedHeight - 2.8;
        //recommendedWidth = recommendedWidth - 2.8;
        recommendedHeight = recommendedHeight - 3;
        recommendedWidth = recommendedWidth - 3;
      }

    }

    if (document.getElementById('canvasunits').value == 1) {  // cm selected
      recommendedHeight = recommendedHeight * cmPerInch;
      recommendedWidth = recommendedWidth * cmPerInch;
    }

    recommendedHeight = (Math.round(recommendedHeight *10)) /10;
    recommendedWidth = (Math.round(recommendedWidth *10)) /10;

    //document.getElementById('recommendedCanvasSize').innerHTML = recommendedWidth + unitSymbol + " x " + recommendedHeight + unitSymbol + "&nbsp;&nbsp;&nbsp;<a href=\"http://www.photocanvasprinting.com/prices?units="+document.getElementById('canvasunits').value+"&width="+recommendedWidth+"&height="+recommendedHeight+"&depth="+document.getElementById('canvasdepth').value+"&typ=rec&SID\" class=\"normal_link\">click here to get a price for this size</a>";
    document.getElementById('recommendedCanvasSize').innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"right\"><tr><td class=\"normal_text\" width=\"120\">" + recommendedWidth + unitSymbol + " x " + recommendedHeight + unitSymbol + "</td><td><input type=\"hidden\" id=\"reccanvaswidth\" name=\"reccanvaswidth\" value=\""+recommendedWidth+"\"/><input type=\"hidden\" id=\"reccanvasheight\" name=\"reccanvasheight\" value=\""+recommendedHeight+"\"/><input src=\"http://www.photocanvasprinting.com/images/showpriceforrecommendedsize.gif\" alt=\"Show price for recommended size\" type=\"image\" value=\"recommended\" name=\"recommended\"/></td></tr></table>";

    if ((recommendedHeight < document.getElementById('canvasheight').value) || (recommendedWidth < document.getElementById('canvaswidth').value)) {
      if (document.getElementById('autoheight').checked) {
        var resultText = "Your chosen canvas print size is larger than our recommended print size.  If you decide to print your image at your chosen size it could result in a blurry and grainy print."
      }
      else {
        var autoHeight = calculateAutoHeight();
        if ((document.getElementById('canvasheight').value > (autoHeight*0.95)) && (document.getElementById('canvasheight').value < (autoHeight*1.05))) {
          var resultText = "Your chosen canvas print size is larger than our recommended print size.  If you decide to print your image at your chosen size it could result in a blurry and grainy print."
        }
        else {
          var resultText = "Your chosen canvas print size is larger than our recommended print size.  If you decide to print your image at your chosen size it could result in a blurry and grainy print.<br/><br/>Your chosen canvas size is also not of the same proportions as your image, this may cause your final print to be cropped.  If you do not wish your image to be cropped, please ensure you check the auto calculate canvas height option."
        }
      }
    }
    else {
      if (!document.getElementById('autoheight').checked) {
        var autoHeight = calculateAutoHeight();
        if ((document.getElementById('canvasheight').value > (autoHeight*0.95)) && (document.getElementById('canvasheight').value < (autoHeight*1.05))) {
          var resultText = "We see no problems with your chosen canvas size";
        }
        else {
          var resultText = "Your chosen canvas size is also not of the same proportions as your image, this may cause your final print to be cropped.  If you do not wish your image to be cropped, please ensure you check the auto calculate canvas height option."
        }
      }
      else {
        var resultText = "We see no problems with your chosen canvas size";
      }
    }

    document.getElementById('resultDescription').innerHTML = resultText;

  }
  else {
    document.getElementById('chosenCanvasSize').innerHTML = "";
    document.getElementById('recommendedCanvasSize').innerHTML = "";
    document.getElementById('resultDescription').innerHTML = "";
  }

}

function clickOnUpload() {

  document.getElementById('uploading').innerHTML = "<font color=ff0000><b>Please Wait Uploading!</b></font>";

}