//var animSpeed = "normal";
var animSpeed = 150;

var quoteObj = {
  uid: 0,
  pid: 0,
  fmt: 0,
  qty: 0,
  pgs: 0,
  ppr: 0,
  prt: 0,
  cvr_ppr: 0,
  cvr_prt_out: 0,
  cvr_prt_in: 0,
  cvr_lam: 0,
  cvr_lam_out: 0,
  cvr_lam_in: 0,
  i1_pgs: 0,
  i1_ppr: 0,
  i1_prt_out: 0,
  i1_prt_in: 0,
  i2_pgs: 0,
  i2_ppr: 0,
  i2_prt_out: 0,
  i2_prt_in: 0,
  trm: 0
};

function getQuotePrice(email_quote) {
  var url = "news_quote.php";
  //var params = 'uid='+quoteObj.uid+'&pid='+quoteObj.pid+'&fmt='+quoteObj.fmt+'&qty='+quoteObj.qty+'&pgs='+quoteObj.pgs+'&ppr='+quoteObj.ppr+'&prt='+quoteObj.prt+'&cvr_ppr='+quoteObj.cvr_ppr+'&cvr_prt_out='+quoteObj.cvr_prt_out+'&cvr_prt_in='+quoteObj.cvr_prt_in+'&cvr_lam='+quoteObj.cvr_lam+'&cvr_lam_out='+quoteObj.cvr_lam_out+'&cvr_lam_in='+quoteObj.cvr_lam_in+'&i1_pgs='+quoteObj.i1_pgs+'&i1_ppr='+quoteObj.i1_ppr+'&i1_prt_out='+quoteObj.i1_prt_out+'&i1_prt_in='+quoteObj.i1_prt_in+'&i2_pgs='+quoteObj.i2_pgs+'&i2_ppr='+quoteObj.i2_ppr+'&i2_prt_out='+quoteObj.i2_prt_out+'&i2_prt_in='+quoteObj.i2_prt_in+'&trm='+quoteObj.trm;
  var params = ''; // not required when useJSON is set to 1
  var useJSON = 1;
  getQuotePrices(email_quote, url, params, useJSON);
}

function getInitialValues() {
  $("#format").val(7);
  $("#qty").val(100);
  $("#pages").val(16);
  $("#paperspec").val(17);
  $("#pageprint").val(2);
  $("#coverlamination_no").attr("checked", "checked");
  $("#coverlaminationoptions").hide(animSpeed);
  $("#trimming").attr("checked", "checked");
  $("[name=finishing]").attr("checked", "");
  $("#finish6").attr("checked", "checked");

  quoteObj.uid = $("#uid").val();
  quoteObj.pid = $("#pid").val();
  quoteObj.fmt = $("#format").val();
  quoteObj.qty = $("#qty").val();
  quoteObj.pgs = $("#pages").val();
  quoteObj.ppr = $("#paperspec").val();
  quoteObj.prt = $("#pageprint").val();
  quoteObj.cvr_ppr = $("#coverpaperspec").val();
  quoteObj.cvr_prt_out = $("#coverprint_out").val();
  quoteObj.cvr_prt_in = $("#coverprint_in").val();
  quoteObj.cvr_lam = $("#coverlamination_yes:checked").length;
  quoteObj.cvr_lam_out = $("#coverlaminate_out").val();
  quoteObj.cvr_lam_in = $("#coverlaminate_in").val();
  quoteObj.i1_pgs = $("#insert1pages").val();
  quoteObj.i1_ppr = $("#insert1paperspec").val();
  quoteObj.i1_prt_out = $("#insert1print_out").val();
  quoteObj.i1_prt_in = $("#insert1print_in").val();
  quoteObj.i2_pgs = $("#insert2pages").val();
  quoteObj.i2_ppr = $("#insert2paperspec").val();
  quoteObj.i2_prt_out = $("#insert2print_out").val();
  quoteObj.i2_prt_in = $("#insert2print_in").val();
  quoteObj.trm = $("#trimming:checked").length;
  populateInsert2Pages();
  populateInsert1Pages();
  getQuotePrice(false);
}

function onCoverLaminationChange(value) {
  if (value==1) {
    $("#coverlaminationoptions").show(animSpeed);
  } else {
    $("#coverlaminationoptions").hide(animSpeed, function() {
      $(".radio.coverlaminate[value=0]").attr("checked", "checked");
      $("#coverlaminate_out").val(0);
      $("#coverlaminate_in").val(0);
    });
    quoteObj.cvr_lam_out = 0;
    quoteObj.cvr_lam_in = 0;
  }
  quoteObj.cvr_lam = value;
  getQuotePrice(false);
}

function onCoverLamInChange(value) {
  quoteObj.cvr_lam_in = value;
  getQuotePrice(false);
}

function onCoverLamOutChange(value) {
  quoteObj.cvr_lam_out = value;
  getQuotePrice(false);
}

function onCoverPaperChange(value) {
  if (value == 0) {
    $("#coveroptions").hide("normal");
  } else {
    if (quoteObj.cvr_ppr == 0) {
      $("#coveroptions").show("normal", function() {
        $("#coverprint_out").focus();
      });
      var select = document.getElementById("pages");
      if (select.selectedIndex === 0) {
        select.selectedIndex = 1;
        onPagesChange(select.options[1].value);
      }
    }
  }
  quoteObj.cvr_ppr = value;
  getQuotePrice(false);
}

function onCoverPrintInChange(value) {
  quoteObj.cvr_prt_in = value;
  getQuotePrice(false);
}

function onCoverPrintOutChange(value) {
  quoteObj.cvr_prt_out = value;
  getQuotePrice(false);
}

function onFinishingClick() {
  getQuotePrice(false);
}

function onFormatChange(value) {
  quoteObj.fmt = value;
  getQuotePrice(false);
}

function onInsert1PagesChange(value) {
  if (value == 0) {
    $("#insert1options").hide("normal");
  } else {
    if (quoteObj.i1_pgs == 0) {
      $("#insert1options").show("normal", function() {
        $("#insert1paperspec").focus();
      });
    }
  }
  quoteObj.i1_pgs = value;
  getQuotePrice(false);

  populateInsert2Pages();
}

function onInsert1PaperSpecChange(value) {
  quoteObj.i1_ppr = value;
  getQuotePrice(false);
}

function onInsert1PrintInChange(value) {
  quoteObj.i1_prt_in = value;
  getQuotePrice(false);
}

function onInsert1PrintOutChange(value) {
  quoteObj.i1_prt_out = value;
  getQuotePrice(false);
}

function onInsert2PagesChange(value) {
  if (value == 0)   {
    $("#insert2options").hide("normal");
  } else {
    if (quoteObj.i2_pgs == 0) {
      $("#insert2options").show("normal", function() {
        $("#insert2paperspec").focus();
      });
    }
  }
  populateInsert1Pages();
}

function onInsert2PaperSpecChange(value) {
  quoteObj.i2_ppr = value;
  getQuotePrice(false);
}

function onInsert2PrintInChange(value) {
  quoteObj.i2_prt_in = value;
  getQuotePrice(false);
}

function onInsert2PrintOutChange(value) {
  quoteObj.i2_prt_out = value;
  getQuotePrice(false);
}

function onPagePrintChange(value) {
  quoteObj.prt = value;
  getQuotePrice(false);
}

function onPagesChange(value) {
  if (value == 4) {
    var select = document.getElementById("coverpaperspec");
    if (select.selectedIndex > 0) {
      select.selectedIndex = 0;
      onCoverPaperChange(select.options[0].value);
    }
  }
  quoteObj.pgs = value;
  getQuotePrice(false);
  
  populateInsert2Pages();
  populateInsert1Pages();
}

function onPaperSpecChange(value) {
  quoteObj.ppr = value;
  getQuotePrice(false);
}

function onQtyChange(el) {
  if (isNaN(el.value)) {
    el.value = 0;
  } else {
    if ((el.value > maxQty[quoteObj.pid]) && (maxQty[quoteObj.pid] > 0)) {
      el.value = maxQty[quoteObj.pid];
    } else {
      el.value = Number(el.value);
    }
  }
  quoteObj.qty = el.value;
  getQuotePrice(false);
}

function onTrimmingClick(value) {
  quoteObj.trm = boolToInt(value);
  getQuotePrice(false);
}

function onBodyLoad() {

  getInitialValues();
  getQuotePrice(false);

}

$(document).ready(function() {

  $("#loading_section").hide(100, function() {
    $("#product_section").show(animSpeed);
  });

  //select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
    showEmailPopup(e);
	});
	
	//if close button is clicked
	$('.window .close, .window .closeX').click(function (e) {
    hideEmailPopup(e);
	});
  
});

function populateInsert1Pages() {
  var idx;
  var available = quoteObj.pgs - 8 - quoteObj.i2_pgs;
  var pagesOptions = '<option value="' + 0 + '">' + 'None' + '</option>';
  for (idx = 4; idx <= available; idx += 4) {
    pagesOptions = pagesOptions + '<option value="' + idx + '">' + idx + ' Pages</option>';
  }
  
  idx = document.getElementById("insert1pages").selectedIndex;
  document.getElementById("insert1pagesdiv").innerHTML = '<select class="narrow" id="insert1pages" name="insert1pages" onChange="onInsert1PagesChange(this.value)">' + pagesOptions + '</select>';
  var select = document.getElementById("insert1pages");
  if (idx < select.length) {
    select.selectedIndex = idx;
  } else {
    select.selectedIndex = 0;
    onInsert1PagesChange(select.options[0].value);
  }
}

function populateInsert2Pages() {
  var idx;
  var available = quoteObj.pgs - 8 - quoteObj.i1_pgs;
  var pagesOptions = '<option value="' + 0 + '">' + 'None' + '</option>';
  for (idx = 4; idx <= available; idx += 4) {
    pagesOptions = pagesOptions + '<option value="' + idx + '">' + idx + ' Pages</option>';
  }
  
  idx = document.getElementById("insert2pages").selectedIndex;
  document.getElementById("insert2pagesdiv").innerHTML = '<select class="narrow" id="insert2pages" name="insert2pages" onChange="onInsert2PagesChange(this.value)">' + pagesOptions + '</select>';
  var select = document.getElementById("insert2pages");
  if (idx < select.length) {
    select.selectedIndex = idx;
  } else {
    select.selectedIndex = 0;
    onInsert2PagesChange(select.options[0].value);
  }
}

function submitForm() {
  document.quoteform.submit();
}

function validate_form(thisform) {
  with (thisform) {
    if (validate_required_jq($("#qty:visible"), "You must enter a quantity!") == false) {
      $("#qty:visible").focus().select();
      return false;
    }
    if ($("#email2").val() != null && $("#email2").val() != "") {
      if (validate_email($("#email2"), "E-mail address is not valid!") == false) {
        $("#email2").focus().select();
        return false;
      }
    }
  }
  return true;
}

