/**
* Created with JetBrains WebStorm.
* User: tangledupinblue
* Date: 2013/04/20
* Time: 3:52 PM
* To change this template use File | Settings | File Templates.
* test
*/
function Litecart() {
this.lineItems = [];
//this.postToUrl = "";
this.currency = "$";
// this.firstNamePostId = "firstName";
// this.lastNamePostId = "lastName";
// this.emailPostId = "email";
// this.orderInfoPostId = "orderDetails";
// this.additionalPostParams = [];
this.imageDirectory = "";
//this.postFormId = "userData";
this.addItem = function(item, price) {
var itemFound = false;
for (index = 0; index < this.lineItems.length; ++index) {
if (this.lineItems[index].item.toString() == item.toString()) {
console.log("found {0}".format(item));
itemFound = true;
this.lineItems[index].qty += 1;
}
}
if (!itemFound) {
var newLineItem = new LitecartLineItem(item,1,price);
console.log("added {0}".format(item));
this.lineItems.push(newLineItem);
}
};
this.removeItem = function(item) {
for (index = 0; index < this.lineItems.length; ++index) {
var lineItem = this.lineItems[index];
if (lineItem.item.toString() == item.toString()) {
console.log("removing {0}".format(lineItem.item));
if (lineItem.qty > 1)
{
lineItem.qty -= 1;
}
else
{
this.lineItems.splice(index,1);
}
}
}
};
this.toTable = function() {
var tbl = "
";
return tbl;
};
// this.generateDetailsForm = function() {
// var tbl = "";
// return tbl;
// };
this.toText = function() {
var txt = "";
var priceSummer = 0;
//txt += "ItemQtyPrice\n";
for (index = 0; index < this.lineItems.length; ++index) {
var li = this.lineItems[index];
txt += "{0} x{1} at {2} each\n".format(
li.item.toString().padRight(20," "), li.qty.toString().padLeft(4," "), (this.currency + " " + li.price.toFixed(2)).padLeft(10," "));
priceSummer += li.qty * li.price;
}
txt += "\nTotal Order Value of {1} {0}\n".format(priceSummer.toFixed(2), this.currency);
return txt;
};
}
function LitecartLineItem(item, qty, price) {
this.qty = qty;
this.item = item;
this.price = price;
return this;
}
function clearCart() {
console.log("clear cart");
cart.lineItems.splice(0,cart.lineItems.length);
sessionStorage.setItem("litecart",JSON.stringify(cart));
}
function refreshCart() {
var cartdiv = document.getElementById("cart");
cartdiv.innerHTML = cart.toTable();
sessionStorage.setItem("litecart",JSON.stringify(cart));
}
function enterContactDetails() {
var cartdiv = document.getElementById("cart");
cartdiv.innerHTML = cart.generateDetailsForm();
}
function addToCart(item,price) {
console.log(item);
cart.addItem(item,price);
refreshCart();
}
function removeFromCart(item) {
cart.removeItem(item);
refreshCart();
}
function setControlValuesOnForm(inputControlId) {
var targetEl = document.getElementById(inputControlId);
console.log("writing to: " + targetEl.id);
targetEl.value = cart.toText();
}
//formatting strings
//first, checks if it isn't implemented yet
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
//padding strings
if (!String.prototype.padLeft) {
String.prototype.padLeft = function(len, fillWith) {
var fillLen = len - this.length;
if (fillLen > 0) {
return repeatString(fillWith,fillLen) + this;
} else {
return this.substr(0,len);
}
}
}
//padding strings
if (!String.prototype.padRight) {
String.prototype.padRight = function(len, fillWith) {
var fillLen = len - this.length;
if (fillLen > 0) {
return this + repeatString(fillWith,fillLen);
} else {
return this.substr(0,len);
}
}
}
function repeatString(repeat, numberTimes) {
var repeated = "";
for(var i = 0; i < numberTimes; i++) {
repeated += repeat;
}
return repeated;
}
//helper function to clone a given object instance
function copyObject(from, to) {
for (var key in from) {
//copy all the fields
to[key] = from[key];
}
}
function getCurrentFilePath() {
var scriptEls = document.getElementsByTagName( 'script' );
var thisScriptEl = scriptEls[scriptEls.length - 1];
var scriptPath = thisScriptEl.src;
alert(scriptPath);
return scriptPath.substr(0, scriptPath.lastIndexOf('/') + 1);
}
//console.log( [scriptPath, scriptFolder] );
console.log("starting litecart");
try {
var obj = JSON.parse(sessionStorage.getItem("litecart"));
}
catch (e) {
}
var cart = new Litecart();
if (obj != null) {
copyObject(obj,cart);
}
console.log(JSON.stringify(cart));