var fromSubscribePage = false;
var isVideoPlan = false;
function CalculatePrice() {
var isVideo = document.location.toString().toLowerCase().indexOf("video", 0) > 0;
if (isVideoPlan != undefined && isVideoPlan) {
isVideo = isVideoPlan;
}
//alert(document.location.toString().toLowerCase());
var objQuantity = document.getElementById("txtQuantity");
var quantity = objQuantity.value;
if (isNaN(quantity) || quantity < 1) {
alert("The quantity is invalid!");
objQuantity.value = "1";
objQuantity.focus();
return;
}
var size = document.getElementById("ddResolution").value;
var uploadSpeed = document.getElementById("ddUploadSpeed").value;
if (isVideo)
uploadSpeed = uploadSpeed / 2.0;
var days = document.getElementById("ddRetension").value;
var sumUploadBytesOneDay = (size * uploadSpeed) * 60.0 * 60 * 24 / 1024 / 1024; //one day in GB.
//var unitPrice = 0.09;
var sumUploadBandwidthOneMonth = sumUploadBytesOneDay * 30;
///
var GBsPerMonth = 0;
var nextGBsPerMonth = 0;
var unitPrice = 0;
var nextUnitPrice = 0;
for (var i = 0; i < bdPrices.length; i++) {
if (GBsPerMonth == 0) {
GBsPerMonth = bdPrices[i].baseBandwidth;
unitPrice = bdPrices[i].basePrice;
}
if (sumUploadBandwidthOneMonth <= bdPrices[i].baseBandwidth) {
nextGBsPerMonth = bdPrices[i].baseBandwidth;
nextUnitPrice = bdPrices[i].basePrice;
break;
}
else {
GBsPerMonth = bdPrices[i].baseBandwidth;
unitPrice = bdPrices[i].basePrice;
}
}
if (nextGBsPerMonth == 0) {
nextGBsPerMonth = GBsPerMonth;
nextUnitPrice = unitPrice;
}
//if (sumUploadBandwidthOneMonth <= GBsPerMonth)
// return unitPrice;
if (sumUploadBandwidthOneMonth >= nextGBsPerMonth)
unitPrice = nextUnitPrice;
else if (sumUploadBandwidthOneMonth > GBsPerMonth) //
{
//alert("unitPrice=" + unitPrice + " nextUnitPrice=" + nextUnitPrice + " GBsPerMonth=" + GBsPerMonth + " nextGBsPerMonth=" + nextGBsPerMonth );
//e.g.: (100, 1.0), (200, 0.8), the value is 140, then the total price should be:
// 100 * 1 + (200*0.8 - 100*1.0) * (140-100)/(200-100) = 124.0
// using this formular we can make sure the price is continuous.
var totalBandwidthPrice = (unitPrice * GBsPerMonth +
(nextUnitPrice * nextGBsPerMonth - unitPrice * GBsPerMonth) * (sumUploadBandwidthOneMonth - GBsPerMonth) / (nextGBsPerMonth - GBsPerMonth));
unitPrice = (totalBandwidthPrice / sumUploadBandwidthOneMonth);
}
///
var sumUploadBandwidthPrice = sumUploadBandwidthOneMonth * unitPrice;
document.getElementById("bdUnitPrice").innerHTML = "$" + unitPrice + "/GB";
document.getElementById("bdUsed").innerHTML = sumUploadBandwidthOneMonth.toFixed(2) + "GB";
document.getElementById("bdTotalPrice").innerHTML = "$" + sumUploadBandwidthPrice.toFixed(2);
var sumStorage = sumUploadBytesOneDay * days;
/*
for (var i = 0; i < storagePrices.length; i++) {
if (sumStorage < storagePrices[i].baseStorage || i == storagePrices.length - 1) {
unitPrice = storagePrices[i].basePrice;
break;
}
}
*/
var GBs = 0;
var nextGBs = 0;
unitPrice = 0;
nextUnitPrice = 0;
for (var i = 0; i < storagePrices.length; i++) {
if (GBs == 0) {
GBs = storagePrices[i].baseStorage;
unitPrice = storagePrices[i].basePrice;
}
if (sumStorage <= storagePrices[i].baseStorage) {
nextGBs = storagePrices[i].baseStorage;
nextUnitPrice = storagePrices[i].basePrice;
break;
}
else {
GBs = storagePrices[i].baseStorage;
unitPrice = storagePrices[i].basePrice;
}
}
if (nextGBs == 0) {
nextGBs = GBs;
nextUnitPrice = unitPrice;
}
if (sumStorage >= nextGBs)
unitPrice = nextUnitPrice;
else if (sumStorage > GBs) //
{
//alert("Storage: unitPrice=" + unitPrice + " nextUnitPrice=" + nextUnitPrice);
//e.g.: (100, 1.0), (200, 0.8), the value is 140, then the total price should be:
// 100 * 1 + (200*0.8 - 100*1.0) * (140-100)/(200-100) = 124.0
// using this formular we can make sure the price is continuous.
var totalStoragePrice = (unitPrice * GBs +
(nextUnitPrice * nextGBs - unitPrice * GBs) * (sumStorage - GBs) / (nextGBs - GBs));
unitPrice = (totalStoragePrice / sumStorage);
}
///
//alert("sumStorage=" + sumStorage + " sumUploadBandwidthOneMonth=" + sumUploadBandwidthOneMonth);
var sumStoragePrice = sumStorage * unitPrice;
document.getElementById("storageUnitPrice").innerHTML = "$" + unitPrice + "/GB";
document.getElementById("storageTotalPrice").innerHTML = "$" + sumStoragePrice.toFixed(2);
document.getElementById("storageUsed").innerHTML = sumStorage.toFixed(2) + "GB";
var sumMonthlyPrice = (sumUploadBandwidthPrice + sumStoragePrice);
var motionDect = document.getElementById("ckbMotionDect").checked;
if (motionDect) {
sumMonthlyPrice = sumMonthlyPrice * 0.5;
}
var payTypeRadio = document.getElementsByName("PayType");
if (payTypeRadio == null || payTypeRadio.length==0)
payTypeRadio = document.getElementsByName(payType); // defined in Subscribe.aspx
if (payTypeRadio[1].checked) {
sumMonthlyPrice = sumMonthlyPrice * 10;
//sumMonthlyPrice = parseFloat(sumMonthlyPrice.toFixed(1)); // toFixed(1): round to the closest value.
document.getElementById("payUnitDescr").innerHTML = "/Year";
if (sumMonthlyPrice < 15 && !isVideo)
sumMonthlyPrice = 15;
else if (sumMonthlyPrice < 25 && isVideo)
sumMonthlyPrice = 25;
else if (days > 90 && size > 200 && sumMonthlyPrice < 50) // over 3 months retention and over 1280x960 resolution,
sumMonthlyPrice = 50;
}
else {
//sumMonthlyPrice = parseFloat(sumMonthlyPrice.toFixed(2));
document.getElementById("payUnitDescr").innerHTML = "/Month";
if (sumMonthlyPrice < 1.5 && !isVideo)
sumMonthlyPrice = 1.5;
else if ( sumMonthlyPrice < 2.5 && isVideo )
sumMonthlyPrice = 2.5;
else if(days>90 && size>200 && sumMonthlyPrice<5) // over 3 months retention and over 1280x960 resolution,
sumMonthlyPrice = 5;
}
if (quantity > 1) {
var ratio = quantity * 1;
for (var i = 0; i < priceRatio.length; i++) {
if (quantity == priceRatio[i].cameraNum) {
ratio = priceRatio[i].ratio;
break;
}
}
sumMonthlyPrice = sumMonthlyPrice * ratio;
}
if (days >= 730)
sumMonthlyPrice = sumMonthlyPrice * 1.1;
var totalResult = sumMonthlyPrice.toFixed(2);
if (payTypeRadio[1].checked) {//yearly
totalResult = sumMonthlyPrice.toFixed(1) + "0";
}
document.getElementById("priceResult").innerHTML = totalResult;//
var speed = size * quantity * 8 * uploadSpeed;
if (speed > 1024) {
speed = speed / 1024.0;
speed = speed.toFixed(1) + "Mbps";
} else {
speed = speed.toFixed(1) + "Kbps";
}
document.getElementById("preBandwidth").innerHTML = speed;
// if it is from the /secure/camera/subscribe..., the two values $("#ddResolution_descr") and $("#ddUploadSpeed_descr") have been set already
if (!fromSubscribePage) {
var obj = document.getElementById("ddUploadSpeed");
var si = obj.selectedIndex;
$("#ddUploadSpeed_descr").val($("#ddUploadSpeed option:selected").text());
//alert($("#ddUploadSpeed option:selected").text());
}
var strRes = $("#ddResolution option:selected").text();
if(strRes+""=="")
{
strRes = $("#ddResolution_descr").val()+"";
}
if(strRes =="" ||
strRes.indexOf(" ")>0){
var i = strRes.indexOf(" ");
if (i > 0) {//if it likes 1024*768 (1MB)//remove (1MB)
strRes = strRes.substring(0, i);
}
}
$("#ddResolution_descr").val(strRes);
}