/* Trout.com.au Author: Fred Wu */ $(document).ready(function(){ /*------------------------------------------------------------ Shared ------------------------------------------------------------*/ // $("a[@rel*=lightbox]").livequery(function(){ // $(this).lightBox({ // imageLoading: base_url + 'media/img/jquery.lightbox/lightbox-ico-loading.gif', // imageBtnClose: base_url + 'media/img/jquery.lightbox/lightbox-btn-close.gif', // imageBtnPrev: base_url + 'media/img/jquery.lightbox/lightbox-btn-prev.gif', // imageBtnNext: base_url + 'media/img/jquery.lightbox/lightbox-btn-next.gif', // imageBlank: base_url + 'media/img/jquery.lightbox/lightbox-blank.gif' // }); // }); $("a[href=#]").livequery(function(){ $(this).click(function(){ $("div#flash_msg p").fadeOut("slow"); return false; }); }); $("table a.remove").livequery(function(){ $(this).fadeTo(0, 0.3).mouseover(function(){ $(this).fadeTo(300, 1); }).mouseout(function(){ $(this).fadeTo(300, 0.3); }); }); $("#tabs > ul").livequery(function(){ $(this).tabs(); }); $("p.close-popup a").livequery(function(){ $(this).click(function(){ $(this).parent().parent().fadeOut(); }); }); $(".pagination_target").livequery(function(){ var total_num = $("#pagination_items_total").attr("rel"); /* var setPage = new RegExp('[\\?&]' + 'page' + '=([^]*)').exec(window.location.href); if(setPage[1]) { var current_page = setPage[1] - 1; } else { var current_page = ; } */ if (total_num > 0) { $("div#pagination").show().pagination(total_num, { // note: the pagination plugin's first page starts with 0 instead of 1 current_page: $("#pagination_current_page").attr("rel") - 1, num_display_entries: 4, num_edge_entries: 1, items_per_page: $("#pagination_items_per_page").attr("rel"), callback: function(page_id, jq){ var current_page = page_id + 1; if ($("#pagination_callback").length) { eval($("#pagination_callback").attr("rel"))(current_page); } return false; } }); } else { $("div#pagination").hide(); } }); $("a.del_parent").livequery(function(){ $(this).fadeTo(0, 0.3).mouseover(function(){ $(this).fadeTo(300, 1); }).mouseout(function(){ $(this).fadeTo(300, 0.3); }); }); $("a.del_child").livequery(function(){ $(this).fadeTo(0, 0).mouseover(function(){ $(this).fadeTo(300, 1); }).mouseout(function(){ $(this).fadeTo(300, 0); }); }); // colour picker $("input.colour_picker").livequery(function(){ var self = this; if (self.value.length == 6) { $(self).css('backgroundColor', '#' + self.value); } $(self).change(function(){ $(self).css('backgroundColor', '#' + self.value); }); $(self).ColorPicker({ onBeforeShow: function () { $(self).ColorPickerSetColor(self.value); }, onShow: function (colpkr) { $(colpkr).fadeIn("fast"); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut("fast"); return false; }, onChange: function (hsb, hex, rgb) { $(self).val(hex); $(self).css('backgroundColor', '#' + hex); }, onSubmit: function(hsb, hex, rgb) { $(self).val(hex); $(self).css('backgroundColor', '#' + hex); } }) .bind('keyup', function(){ $(self).ColorPickerSetColor(self.value); }); }); // product search $("form#product_search").validate({ errorElement: "span", errorLabelContainer: "div#search_error_msg", rules: { keyword: { required: true, minlength: 3 } }, messages: { keyword: { required: "Please enter some keywords." } } }); function show_msg(msg) { $("div#msg").text(msg).fadeIn().animate({opacity: 1.0}, 2000).fadeOut(); } function show_flash_msg(msg) { $("div#flash_msg").html("
" + msg + "
").fadeIn(); } function trim(str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); } /*------------------------------------------------------------ Page - Dashbaord ------------------------------------------------------------*/ $("body#admin_dashboard").livequery(function(){ reload_site_list(); }); $("a#add_site").livequery(function(){ $(this).click(function(){ $("div#add_site_div").fadeIn(); }); }); $("a#add_site_cancel").livequery(function(){ $(this).click(function(){ $("div#add_site_div").fadeOut(); }); }); $("ul#site_list a.del").livequery(function(){ $(this).click(function(){ if (confirm('Are you sure you want to remove this site?')) { $.post(base_url + "admin/remove_site", { permalink: $(this).attr("rel") }, function(){ reload_site_list(); }); } }); }); $("a#add_site_submit").livequery(function(){ $(this).click(function(){ $.post(base_url + "admin/add_site", { site_name: $(this).parent().prev().val() }, function(){ reload_site_list(); $("div#add_site_div").fadeOut(); }); }); }); function reload_site_list() { $.post(base_url + "admin/render_site_list_partial", { action: "reload" }, function(data){ $("ul#site_list").html(data); }); } /*------------------------------------------------------------ Page - Categories ------------------------------------------------------------*/ $("body.body_categories").livequery(function(){ // drag n' drop $(".draggable").livequery(function(){ $(this).draggable({ helper: 'clone', revert: 'invalid' }); }); $(".droppable").livequery(function(){ $(this).droppable({ accept: ".draggable", activeClass: 'droppable-active', hoverClass: 'droppable-hover', drop: function(ev, ui) { ui.draggable.fadeOut().fadeIn().fadeOut().fadeIn().appendTo($('ul', this)); $.post(this.location, { action: "move", child_id: ui.draggable.attr("rel"), parent_id: $(this).attr("rel") }); } }); }); // jEditable $("ul#categories li span").livequery(function(){ $(this).editable(base_url + 'admin/edit_category', { indicator : "
",
tooltip : "Double click to edit",
event : "dblclick",
submit : 'OK',
callback : function() { $(this).parent().fadeOut().fadeIn(); }
});
});
// add
$("li.add_category > a").livequery(function(){
$(this).click(function(){
var parent_id = $(this).attr("rel");
$("div.add_category_div").fadeOut();
$("div.add_category_div[rel=" + parent_id + "] input[name=name]").val("");
$("div.add_category_div[rel=" + parent_id + "] a").show();
$("div.add_category_div[rel=" + parent_id + "] span.loading").remove();
$("div.add_category_div[rel=" + parent_id + "]").fadeIn();
});
});
$("li.add_category a.add_category_cancel").livequery(function(){
$(this).click(function(){
$(this).parent().parent().fadeOut();
});
});
$("div.add_category_div a.add_category_submit").livequery(function(){
$(this).click(function(){
var self = this;
var parent_id = $(this).parent().parent().attr("rel");
$(self).hide().before("loading..."); // prevent from multiple clicks
$.post(base_url + 'admin/add_category', {
site_id: $("ul#categories").attr("rel"),
parent_id: parent_id,
category_name: $(this).parent().prev().val()
}, function(data){
$(self).parent().parent().fadeOut(function(){
$(self).parent().parent().parent().before(data);
});
});
});
});
// delete
$("a.del").livequery(function(){
$(this).click(function(){
if (confirm('Are you sure you want to remove this category?'))
{
$.post(this.location, {
action: "remove",
id: $(this).parent().attr("rel")
});
$(this).parent().fadeOut();
}
});
});
});
/*------------------------------------------------------------
Page - Products
------------------------------------------------------------*/
$("body.body_products").livequery(function(){
// cateogires
$("ul#parent_categories li:first-child a").livequery(function(){
var first_parent_category_id = $(this).attr("rel");
$("ul#parent_categories li a[rel=" + first_parent_category_id + "]").addClass("current");
$("ul#in_parent_category_" + first_parent_category_id).show();
});
invoke_category_list_partial(window.location.hash);
if (window.location.hash == 0 || window.location.hash == '')
{
var category_id = 0;
}
else
{
var category_id = window.location.hash.substring(1);
}
var setPage = new RegExp('[\\?&]' + 'page' + '=([^]*)').exec(window.location.href);
if(setPage != null)
{
var setpage = setPage[1];
}
else
{
var setpage = 1;
}
invoke_product_list_partial(category_id,setpage);
$("ul.child_categories").livequery(function(){
$("ul.child_categories li a[rel=" + category_id + "]").addClass("current");
var parent_id = $("ul.child_categories li a[rel=" + category_id + "]").parent().parent().attr("rel");
$("ul#parent_categories li a").removeClass("current");
$("ul#parent_categories li a[rel=" + parent_id + "]").livequery(function(){
$(this).addClass("current");
});
$("ul#in_parent_category_" + parent_id).show();
$("ul#in_parent_category_" + parent_id + " li").show("slide", { direction: "left" });
});
$("ul#parent_categories li a").livequery(function(){
$(this).click(function(){
var category_id = $(this).attr("rel");
$("ul.child_categories").hide();
$("ul.child_categories li a").removeClass("current");
$("ul#parent_categories li a").removeClass("current");
$("ul#parent_categories li a[rel=" + category_id + "]").addClass("current");
$("ul#in_parent_category_" + category_id).show();
$("ul#in_parent_category_" + category_id + " li").show("slide", { direction: "left" });
invoke_product_list_partial(category_id);
window.location.hash = category_id;
});
});
$("ul.child_categories li a").livequery(function(){
$(this).click(function(){
var category_id = $(this).attr("rel");
$("ul.child_categories li a").removeClass("current");
$("ul.child_categories li a[rel=" + category_id + "]").addClass("current");
invoke_product_list_partial(category_id);
window.location.hash = category_id;
});
});
// products
$("table#products a.remove").livequery(function(){
$(this).click(function(){
var self = this;
if (confirm('Are you sure you want to remove this product?'))
{
$.post(base_url + "admin/remove_product", {
product_id: $(self).parent().parent().attr("rel")
}, function(){
$(self).parent().parent().fadeOut();
});
}
});
});
$("table#products a.edit, a#add_new_product").livequery(function(){
$(this).click(function(){
var add_to = parseInt($("ul.child_categories li a.current").attr("rel"));
var product_id = $(this).parent().parent().attr("rel");
if (product_id == null)
{
$("div#edit_product h2").livequery(function(){
$(this).text("Add Product");
});
}
else
{
$("div#edit_product h2").livequery(function(){
$(this).text("Edit Product");
});
}
$.post(base_url + "admin/get_product", {
product_id: product_id,
site_id: $("#site_id").attr("rel"),
add_to: add_to
}, function(data){
$("div#edit_product").html(data);
});
$("div#edit_product").hide().fadeIn(500);
});
});
$("a#add_product_price_option").livequery(function(){
$(this).click(function(){
$(this).next().clone(true).insertBefore(this).hide().slideDown();
});
});
$("a.remove_product_price_option").livequery(function(){
$(this).click(function()
{
$(this).parent().slideUp(function()
{
$(this).children("input[type=text]").each(function()
{
$(this).val("");
});
})
});
});
// form trigger
$("a#edit_product_submit").livequery(function(){
$(this).click(function(){
$("form#edit_product_form").submit();
});
});
// form validation
$("form#edit_product_form").livequery(function(){
$(this).validate({
errorElement: "p",
errorLabelContainer: "div#error_msg",
rules: {
code: {
required: true,
maxlength: 20
},
name: {
required: true,
maxlength: 100
},
pack: {
digits: true
},
minimum: {
digits: true
},
category_id: {
required: true
}
},
messages: {
code: {
required: "Product code is required."
},
name: {
required: "Product name is required."
},
category_id: {
required: "Please select a category."
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
success: function(response, status){
$("div.popup").fadeOut();
show_flash_msg("Product has been successfully updated!");
var category_id = $("span#product_list_category_id").attr("rel");
invoke_category_list_partial(category_id);
invoke_product_list_partial(category_id);
}
});
}
});
});
});
function invoke_category_list_partial(hash)
{
var category_id = window.location.hash.substring(1);
$.post(base_url + base_section + "/render_category_list_partial", {
site_id: $("div#product_list_partial").attr("rel"),
category_id: category_id
}, function(data){
$("div#category_list_partial").hide().fadeIn().html(data);
});
}
function invoke_product_list_partial(category_id, current_page, persistent)
{
// keep current pagination page number
// otherwise reset to page 1
if (persistent != true)
{
$("#pagination_current_page").attr("rel", 1);
}
if (current_page == null)
{
current_page = $("#pagination_current_page").attr("rel");
}
else
{
$("#pagination_current_page").attr("rel", current_page);
}
$.post(base_url + base_section + "/render_product_list_partial" + location.search, {
site_id: $("div#product_list_partial").attr("rel"),
category_id: category_id,
// read parameters for pagination
current_page: current_page,
items_per_page: $("#pagination_items_per_page").attr("rel")
}, function(data){
$("div#product_list_partial").hide().fadeIn().html(data);
});
}
function invoke_product_list_partial_via_pagination(current_page)
{
invoke_product_list_partial($("#product_list_category_id").attr("rel"), current_page, true);
}
/*------------------------------------------------------------
Page - Reps
------------------------------------------------------------*/
$("body.body_reps").livequery(function(){
invoke_rep_list_partial();
$("a.remove").livequery(function(){
$(this).click(function(){
var self = this;
if (confirm('Are you sure you want to remove this rep?'))
{
$.post(base_url + "admin/remove_rep", {
rep_id: $(self).parent().parent().attr("rel")
}, function(){
$(self).parent().parent().fadeOut();
});
}
});
});
$("a.edit").livequery(function(){
$(this).click(function(){
$("html, body").animate({scrollTop: 0}, 200);
var self = this;
var rep = $(this).parent().parent();
var rep_id = rep.attr("rel");
var rep_name = rep.children(":eq(1)").text() + " " + rep.children(":eq(2)").text();
if (rep_id == null)
{
$("form#edit_rep_form h2").livequery(function(){
$(this).text("Add User");
});
}
else
{
$("form#edit_rep_form h2").livequery(function(){
$(this).text("Edit User");
});
}
$.post(base_url + "admin/get_rep", {
rep_id: rep_id,
site_id: $(self).attr("rel")
}, function(data){
$("div#edit_rep").html(data);
});
$("div#edit_rep").hide().fadeIn(500);
});
});
// form trigger
$("a#edit_rep_submit").livequery(function(){
$(this).click(function(){
$("form#edit_rep_form").submit();
});
});
// form validation
$("form#edit_rep_form").livequery(function(){
$(this).validate({
errorElement: "span",
rules: {
username: {
required: true,
rangelength: [4, 20]
},
password: {
rangelength: [4, 20]
},
password2: {
equalTo: "#password"
},
firstname: {
required: true,
maxlength: 50
},
surname: {
required: true,
maxlength: 50
},
position: {
maxlength: 50
},
branch: {
maxlength: 50
},
company: {
maxlength: 100
},
address: {
maxlength: 255
},
suburb: {
maxlength: 30
},
postcode: {
digits: true,
rangelength: [3, 4]
},
phone: {
rangelength: [8, 16]
},
fax: {
rangelength: [8, 16]
},
email: {
required: true,
email: true
}
},
messages: {
username: {
rangelength: "Username should be 4 to 20 characters long."
},
password: "Password should be 4 to 20 characters long.",
password2: "Passwords entered do not match.",
postcode: {
digits: "Postcode should only contain digits."
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
success: function(){
invoke_rep_list_partial();
$("div.popup").fadeOut();
show_flash_msg("Rep has been successfully updated!");
}
});
}
});
});
});
function invoke_rep_list_partial()
{
$.post(base_url + base_section + "/render_rep_list_partial", {
site_id: $("div#rep_list_partial").attr("rel")
}, function(data){
$("div#rep_list_partial").hide().fadeIn().html(data);
});
}
/*------------------------------------------------------------
Page - Orders
------------------------------------------------------------*/
$("body.body_orders").livequery(function(){
invoke_order_list_partial();
if ($.query.get('order_id') > 0)
{
window.location.hash = $.query.get('order_id');
}
if (window.location.hash != '#0' && window.location.hash != '')
{
$.post(base_url + base_section + "/render_order_details_partial", {
order_id: window.location.hash.substring(1)
}, function(data){
$("div#order_details_partial").html(data).fadeIn();
$("table#orders").hide();
$("div#pagination").livequery(function(){
$(this).hide();
});
});
}
$("table#orders tr.clickable").livequery(function(){
$(this).click(function(){
var order_id = $(this).attr("rel");
window.location.hash = order_id;
$.post(base_url + base_section + "/render_order_details_partial", {
order_id: order_id
}, function(data){
$("div#pagination").hide("slide", { direction: "right" });
$("table#orders").hide("slide", { direction: "right", callback: function(){
$("div#order_details_partial").html(data).show("slide", { direction: "left" });}
});
});
});
});
$("p a.order_list").livequery(function(){
$(this).click(function(){
if ($.query.get('order_id') > 0)
{
window.location = window.location.toString().split("?")[0];
}
else
{
window.location.hash = 0;
$("div#order_details_partial").hide("slide", { direction: "left", callback: function(){
$("table#orders").show("slide", { direction: "right" });
$("div#pagination").show("slide", { direction: "right" });
} });
}
return false;
});
});
// update purchase number
$("input#purchase_number").livequery(function(){
$(this).change(function(){
$("div#order_items div.loading").fadeIn();
var self = this;
$.post(base_url + base_section + "/update_purchase_number", {
order_id: window.location.hash.substring(1),
purchase_number: $(self).val()
}, function(data){
$("div#order_items div.loading").fadeOut();
});
});
});
// confirmation checkbox for dispatched order
$("input#dispatched").livequery(function(){
if ($(this).attr("checked") == true)
{
$(this).attr("disabled", "disabled");
}
$(this).change(function(){
if (confirm("Are you sure you want to confirm the dispatch of this order?"))
{
$(this).attr("disabled", "disabled");
$("div#order_items div.loading").fadeIn();
$.post(base_url + base_section + "/confirm_dispatched", {
order_id: window.location.hash.substring(1)
}, function(data){
$("div#order_items div.loading").fadeOut();
});
}
else
{
$(this).attr("checked", false);
}
});
});
});
function invoke_order_list_partial(current_page)
{
if (current_page == null)
{
current_page = $("#pagination_current_page").attr("rel");
}
else
{
$("#pagination_current_page").attr("rel", current_page);
}
$.post(base_url + base_section + "/render_order_list_partial", {
site_id: $("div#order_list_partial").attr("rel"),
// read parameters for pagination
current_page: current_page,
items_per_page: $("#pagination_items_per_page").attr("rel")
}, function(data){
$("div#order_list_partial").hide().fadeIn().html(data);
});
}
function invoke_order_list_partial_via_pagination(current_page)
{
invoke_order_list_partial(current_page);
}
/*------------------------------------------------------------
Page - Product
------------------------------------------------------------*/
$("body.body_product").livequery(function(){
var order_qty;
// calculate total
$("input.qty").change(function(){
var self = this;
var subtotal = 0;
order_qty = 0; // reset the order_qty because we're looping the qty on every 'change' event
$("input.qty").each(function(){
var price = parseCurrency($(this).parent().next().text());
var qty = parseInt(jQuery.trim($(this).val()));
if (isNaN(qty) || qty < 0)
{
qty = 0;
}
order_qty += qty;
subtotal += price * qty;
});
// reset to 0 if it's not a number or is a negative number
var current_qty = $(self).val();
if (isNaN(current_qty) || current_qty < 0)
{
$(self).val(0);
}
subtotal = toCurrency(subtotal);
var gst = toCurrency(subtotal * 0.1);
var total = toCurrency(subtotal * 1.1);
$("span#product_subtotal").text(subtotal);
$("span#product_gst").text(gst);
$("span#product_total").text(total);
$("p#total").fadeOut("fast").fadeIn();
});
// add to cart
$("form[name=add_to_cart]").submit(function(){
var minimum = $("span#minimum").text();
if (order_qty == 0)
{
$("#add_to_cart_error_msg").text("Please specify the amount you would like to purchase!").fadeIn();
}
else if (order_qty < minimum)
{
$("#add_to_cart_error_msg").text("Minimum order quantity is " + minimum + ".").fadeIn();
}
else
{
$(this).ajaxSubmit({
success: function(data){
show_flash_msg("Items are now added to your cart!");
$("#add_to_cart_error_msg").hide();
update_num_of_items_in_cart();
}
});
}
return false;
});
});
function parseCurrency(price)
{
return trim(price).substring(1).replace(",", "");
}
function toCurrency(price)
{
price = parseFloat(price);
return price.toFixed(2);
}
function update_num_of_items_in_cart()
{
$.post(base_url + base_section + "/update_num_of_items_in_cart", {}, function(data){
$("li#sub_nav_cart a span").hide().fadeIn().text(parseInt(data));
});
}
/*------------------------------------------------------------
Page - Cart
------------------------------------------------------------*/
$("body.body_cart").livequery(function(){
calculate_cart_total();
$("div#cart_items a.remove").click(function(){
if (confirm("Are you sure you want to remove this item from your cart?"))
{
var self = this;
$.post(base_url + base_section + "/remove_from_cart", {
item_id: $(self).parent().parent().attr("rel")
}, function(data){
$(self).parent().parent().fadeOut(function(){
calculate_cart_total();
update_num_of_items_in_cart();
});
show_msg("Item is removed from your cart.");
});
}
});
$("div#cart_info input[type=text], div#cart_info select").change(function(){
$("div#cart_info div.loading").fadeIn();
$.post(base_url + base_section + "/update_order_info", {
key: $(this).attr("name"),
value: $(this).val()
}, function(data){
$("div#cart_info div.loading").fadeOut();
});
});
$("div#cart_items input").change(function(){
var self = this;
$("div#cart_items div.loading").fadeIn();
$.post(base_url + base_section + "/update_order_qty", {
key: $(this).parent().parent().attr("rel"),
value: $(this).val()
}, function(data){
$("div#cart_items div.loading").fadeOut();
var item_subtotal = "$" + toCurrency(parseCurrency($(self).parent().prev().text()) * data);
$(self).parent().next().fadeOut("fast").fadeIn().text(item_subtotal);
calculate_cart_total();
});
});
$("a#clear").click(function(){
if (confirm("Are you sure you want to clear the cart?"))
{
$("div#cart_items div.loading").fadeIn();
$.post(base_url + base_section + "/clear_cart", {
id: $("div#cart_info").attr("rel")
}, function(data){
$("div#cart_items div.loading").fadeOut();
calculate_cart_total();
$("div#cart_items").fadeOut("fast", function(){
$(this).html("
",
tooltip : "Double click to edit",
event : "dblclick",
submit : 'OK',
callback : function() { $(this).parent().fadeOut().fadeIn(); }
});
});
// add
$("li.add_category > a").livequery(function(){
$(this).click(function(){
var parent_id = $(this).attr("rel");
$("div.add_category_div").fadeOut();
$("div.add_category_div[rel=" + parent_id + "] input[name=name]").val("");
$("div.add_category_div[rel=" + parent_id + "] a").show();
$("div.add_category_div[rel=" + parent_id + "] span.loading").remove();
$("div.add_category_div[rel=" + parent_id + "]").fadeIn();
});
});
$("li.add_category a.add_category_cancel").livequery(function(){
$(this).click(function(){
$(this).parent().parent().fadeOut();
});
});
$("div.add_category_div a.add_category_submit").livequery(function(){
$(this).click(function(){
var self = this;
var parent_id = $(this).parent().parent().attr("rel");
$(self).hide().before("loading..."); // prevent from multiple clicks
$.post(base_url + 'admin/add_category', {
site_id: $("ul#categories").attr("rel"),
parent_id: parent_id,
category_name: $(this).parent().prev().val()
}, function(data){
$(self).parent().parent().fadeOut(function(){
$(self).parent().parent().parent().before(data);
});
});
});
});
// delete
$("a.del").livequery(function(){
$(this).click(function(){
if (confirm('Are you sure you want to remove this category?'))
{
$.post(this.location, {
action: "remove",
id: $(this).parent().attr("rel")
});
$(this).parent().fadeOut();
}
});
});
});
/*------------------------------------------------------------
Page - Products
------------------------------------------------------------*/
$("body.body_products").livequery(function(){
// cateogires
$("ul#parent_categories li:first-child a").livequery(function(){
var first_parent_category_id = $(this).attr("rel");
$("ul#parent_categories li a[rel=" + first_parent_category_id + "]").addClass("current");
$("ul#in_parent_category_" + first_parent_category_id).show();
});
invoke_category_list_partial(window.location.hash);
if (window.location.hash == 0 || window.location.hash == '')
{
var category_id = 0;
}
else
{
var category_id = window.location.hash.substring(1);
}
var setPage = new RegExp('[\\?&]' + 'page' + '=([^]*)').exec(window.location.href);
if(setPage != null)
{
var setpage = setPage[1];
}
else
{
var setpage = 1;
}
invoke_product_list_partial(category_id,setpage);
$("ul.child_categories").livequery(function(){
$("ul.child_categories li a[rel=" + category_id + "]").addClass("current");
var parent_id = $("ul.child_categories li a[rel=" + category_id + "]").parent().parent().attr("rel");
$("ul#parent_categories li a").removeClass("current");
$("ul#parent_categories li a[rel=" + parent_id + "]").livequery(function(){
$(this).addClass("current");
});
$("ul#in_parent_category_" + parent_id).show();
$("ul#in_parent_category_" + parent_id + " li").show("slide", { direction: "left" });
});
$("ul#parent_categories li a").livequery(function(){
$(this).click(function(){
var category_id = $(this).attr("rel");
$("ul.child_categories").hide();
$("ul.child_categories li a").removeClass("current");
$("ul#parent_categories li a").removeClass("current");
$("ul#parent_categories li a[rel=" + category_id + "]").addClass("current");
$("ul#in_parent_category_" + category_id).show();
$("ul#in_parent_category_" + category_id + " li").show("slide", { direction: "left" });
invoke_product_list_partial(category_id);
window.location.hash = category_id;
});
});
$("ul.child_categories li a").livequery(function(){
$(this).click(function(){
var category_id = $(this).attr("rel");
$("ul.child_categories li a").removeClass("current");
$("ul.child_categories li a[rel=" + category_id + "]").addClass("current");
invoke_product_list_partial(category_id);
window.location.hash = category_id;
});
});
// products
$("table#products a.remove").livequery(function(){
$(this).click(function(){
var self = this;
if (confirm('Are you sure you want to remove this product?'))
{
$.post(base_url + "admin/remove_product", {
product_id: $(self).parent().parent().attr("rel")
}, function(){
$(self).parent().parent().fadeOut();
});
}
});
});
$("table#products a.edit, a#add_new_product").livequery(function(){
$(this).click(function(){
var add_to = parseInt($("ul.child_categories li a.current").attr("rel"));
var product_id = $(this).parent().parent().attr("rel");
if (product_id == null)
{
$("div#edit_product h2").livequery(function(){
$(this).text("Add Product");
});
}
else
{
$("div#edit_product h2").livequery(function(){
$(this).text("Edit Product");
});
}
$.post(base_url + "admin/get_product", {
product_id: product_id,
site_id: $("#site_id").attr("rel"),
add_to: add_to
}, function(data){
$("div#edit_product").html(data);
});
$("div#edit_product").hide().fadeIn(500);
});
});
$("a#add_product_price_option").livequery(function(){
$(this).click(function(){
$(this).next().clone(true).insertBefore(this).hide().slideDown();
});
});
$("a.remove_product_price_option").livequery(function(){
$(this).click(function()
{
$(this).parent().slideUp(function()
{
$(this).children("input[type=text]").each(function()
{
$(this).val("");
});
})
});
});
// form trigger
$("a#edit_product_submit").livequery(function(){
$(this).click(function(){
$("form#edit_product_form").submit();
});
});
// form validation
$("form#edit_product_form").livequery(function(){
$(this).validate({
errorElement: "p",
errorLabelContainer: "div#error_msg",
rules: {
code: {
required: true,
maxlength: 20
},
name: {
required: true,
maxlength: 100
},
pack: {
digits: true
},
minimum: {
digits: true
},
category_id: {
required: true
}
},
messages: {
code: {
required: "Product code is required."
},
name: {
required: "Product name is required."
},
category_id: {
required: "Please select a category."
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
success: function(response, status){
$("div.popup").fadeOut();
show_flash_msg("Product has been successfully updated!");
var category_id = $("span#product_list_category_id").attr("rel");
invoke_category_list_partial(category_id);
invoke_product_list_partial(category_id);
}
});
}
});
});
});
function invoke_category_list_partial(hash)
{
var category_id = window.location.hash.substring(1);
$.post(base_url + base_section + "/render_category_list_partial", {
site_id: $("div#product_list_partial").attr("rel"),
category_id: category_id
}, function(data){
$("div#category_list_partial").hide().fadeIn().html(data);
});
}
function invoke_product_list_partial(category_id, current_page, persistent)
{
// keep current pagination page number
// otherwise reset to page 1
if (persistent != true)
{
$("#pagination_current_page").attr("rel", 1);
}
if (current_page == null)
{
current_page = $("#pagination_current_page").attr("rel");
}
else
{
$("#pagination_current_page").attr("rel", current_page);
}
$.post(base_url + base_section + "/render_product_list_partial" + location.search, {
site_id: $("div#product_list_partial").attr("rel"),
category_id: category_id,
// read parameters for pagination
current_page: current_page,
items_per_page: $("#pagination_items_per_page").attr("rel")
}, function(data){
$("div#product_list_partial").hide().fadeIn().html(data);
});
}
function invoke_product_list_partial_via_pagination(current_page)
{
invoke_product_list_partial($("#product_list_category_id").attr("rel"), current_page, true);
}
/*------------------------------------------------------------
Page - Reps
------------------------------------------------------------*/
$("body.body_reps").livequery(function(){
invoke_rep_list_partial();
$("a.remove").livequery(function(){
$(this).click(function(){
var self = this;
if (confirm('Are you sure you want to remove this rep?'))
{
$.post(base_url + "admin/remove_rep", {
rep_id: $(self).parent().parent().attr("rel")
}, function(){
$(self).parent().parent().fadeOut();
});
}
});
});
$("a.edit").livequery(function(){
$(this).click(function(){
$("html, body").animate({scrollTop: 0}, 200);
var self = this;
var rep = $(this).parent().parent();
var rep_id = rep.attr("rel");
var rep_name = rep.children(":eq(1)").text() + " " + rep.children(":eq(2)").text();
if (rep_id == null)
{
$("form#edit_rep_form h2").livequery(function(){
$(this).text("Add User");
});
}
else
{
$("form#edit_rep_form h2").livequery(function(){
$(this).text("Edit User");
});
}
$.post(base_url + "admin/get_rep", {
rep_id: rep_id,
site_id: $(self).attr("rel")
}, function(data){
$("div#edit_rep").html(data);
});
$("div#edit_rep").hide().fadeIn(500);
});
});
// form trigger
$("a#edit_rep_submit").livequery(function(){
$(this).click(function(){
$("form#edit_rep_form").submit();
});
});
// form validation
$("form#edit_rep_form").livequery(function(){
$(this).validate({
errorElement: "span",
rules: {
username: {
required: true,
rangelength: [4, 20]
},
password: {
rangelength: [4, 20]
},
password2: {
equalTo: "#password"
},
firstname: {
required: true,
maxlength: 50
},
surname: {
required: true,
maxlength: 50
},
position: {
maxlength: 50
},
branch: {
maxlength: 50
},
company: {
maxlength: 100
},
address: {
maxlength: 255
},
suburb: {
maxlength: 30
},
postcode: {
digits: true,
rangelength: [3, 4]
},
phone: {
rangelength: [8, 16]
},
fax: {
rangelength: [8, 16]
},
email: {
required: true,
email: true
}
},
messages: {
username: {
rangelength: "Username should be 4 to 20 characters long."
},
password: "Password should be 4 to 20 characters long.",
password2: "Passwords entered do not match.",
postcode: {
digits: "Postcode should only contain digits."
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
success: function(){
invoke_rep_list_partial();
$("div.popup").fadeOut();
show_flash_msg("Rep has been successfully updated!");
}
});
}
});
});
});
function invoke_rep_list_partial()
{
$.post(base_url + base_section + "/render_rep_list_partial", {
site_id: $("div#rep_list_partial").attr("rel")
}, function(data){
$("div#rep_list_partial").hide().fadeIn().html(data);
});
}
/*------------------------------------------------------------
Page - Orders
------------------------------------------------------------*/
$("body.body_orders").livequery(function(){
invoke_order_list_partial();
if ($.query.get('order_id') > 0)
{
window.location.hash = $.query.get('order_id');
}
if (window.location.hash != '#0' && window.location.hash != '')
{
$.post(base_url + base_section + "/render_order_details_partial", {
order_id: window.location.hash.substring(1)
}, function(data){
$("div#order_details_partial").html(data).fadeIn();
$("table#orders").hide();
$("div#pagination").livequery(function(){
$(this).hide();
});
});
}
$("table#orders tr.clickable").livequery(function(){
$(this).click(function(){
var order_id = $(this).attr("rel");
window.location.hash = order_id;
$.post(base_url + base_section + "/render_order_details_partial", {
order_id: order_id
}, function(data){
$("div#pagination").hide("slide", { direction: "right" });
$("table#orders").hide("slide", { direction: "right", callback: function(){
$("div#order_details_partial").html(data).show("slide", { direction: "left" });}
});
});
});
});
$("p a.order_list").livequery(function(){
$(this).click(function(){
if ($.query.get('order_id') > 0)
{
window.location = window.location.toString().split("?")[0];
}
else
{
window.location.hash = 0;
$("div#order_details_partial").hide("slide", { direction: "left", callback: function(){
$("table#orders").show("slide", { direction: "right" });
$("div#pagination").show("slide", { direction: "right" });
} });
}
return false;
});
});
// update purchase number
$("input#purchase_number").livequery(function(){
$(this).change(function(){
$("div#order_items div.loading").fadeIn();
var self = this;
$.post(base_url + base_section + "/update_purchase_number", {
order_id: window.location.hash.substring(1),
purchase_number: $(self).val()
}, function(data){
$("div#order_items div.loading").fadeOut();
});
});
});
// confirmation checkbox for dispatched order
$("input#dispatched").livequery(function(){
if ($(this).attr("checked") == true)
{
$(this).attr("disabled", "disabled");
}
$(this).change(function(){
if (confirm("Are you sure you want to confirm the dispatch of this order?"))
{
$(this).attr("disabled", "disabled");
$("div#order_items div.loading").fadeIn();
$.post(base_url + base_section + "/confirm_dispatched", {
order_id: window.location.hash.substring(1)
}, function(data){
$("div#order_items div.loading").fadeOut();
});
}
else
{
$(this).attr("checked", false);
}
});
});
});
function invoke_order_list_partial(current_page)
{
if (current_page == null)
{
current_page = $("#pagination_current_page").attr("rel");
}
else
{
$("#pagination_current_page").attr("rel", current_page);
}
$.post(base_url + base_section + "/render_order_list_partial", {
site_id: $("div#order_list_partial").attr("rel"),
// read parameters for pagination
current_page: current_page,
items_per_page: $("#pagination_items_per_page").attr("rel")
}, function(data){
$("div#order_list_partial").hide().fadeIn().html(data);
});
}
function invoke_order_list_partial_via_pagination(current_page)
{
invoke_order_list_partial(current_page);
}
/*------------------------------------------------------------
Page - Product
------------------------------------------------------------*/
$("body.body_product").livequery(function(){
var order_qty;
// calculate total
$("input.qty").change(function(){
var self = this;
var subtotal = 0;
order_qty = 0; // reset the order_qty because we're looping the qty on every 'change' event
$("input.qty").each(function(){
var price = parseCurrency($(this).parent().next().text());
var qty = parseInt(jQuery.trim($(this).val()));
if (isNaN(qty) || qty < 0)
{
qty = 0;
}
order_qty += qty;
subtotal += price * qty;
});
// reset to 0 if it's not a number or is a negative number
var current_qty = $(self).val();
if (isNaN(current_qty) || current_qty < 0)
{
$(self).val(0);
}
subtotal = toCurrency(subtotal);
var gst = toCurrency(subtotal * 0.1);
var total = toCurrency(subtotal * 1.1);
$("span#product_subtotal").text(subtotal);
$("span#product_gst").text(gst);
$("span#product_total").text(total);
$("p#total").fadeOut("fast").fadeIn();
});
// add to cart
$("form[name=add_to_cart]").submit(function(){
var minimum = $("span#minimum").text();
if (order_qty == 0)
{
$("#add_to_cart_error_msg").text("Please specify the amount you would like to purchase!").fadeIn();
}
else if (order_qty < minimum)
{
$("#add_to_cart_error_msg").text("Minimum order quantity is " + minimum + ".").fadeIn();
}
else
{
$(this).ajaxSubmit({
success: function(data){
show_flash_msg("Items are now added to your cart!");
$("#add_to_cart_error_msg").hide();
update_num_of_items_in_cart();
}
});
}
return false;
});
});
function parseCurrency(price)
{
return trim(price).substring(1).replace(",", "");
}
function toCurrency(price)
{
price = parseFloat(price);
return price.toFixed(2);
}
function update_num_of_items_in_cart()
{
$.post(base_url + base_section + "/update_num_of_items_in_cart", {}, function(data){
$("li#sub_nav_cart a span").hide().fadeIn().text(parseInt(data));
});
}
/*------------------------------------------------------------
Page - Cart
------------------------------------------------------------*/
$("body.body_cart").livequery(function(){
calculate_cart_total();
$("div#cart_items a.remove").click(function(){
if (confirm("Are you sure you want to remove this item from your cart?"))
{
var self = this;
$.post(base_url + base_section + "/remove_from_cart", {
item_id: $(self).parent().parent().attr("rel")
}, function(data){
$(self).parent().parent().fadeOut(function(){
calculate_cart_total();
update_num_of_items_in_cart();
});
show_msg("Item is removed from your cart.");
});
}
});
$("div#cart_info input[type=text], div#cart_info select").change(function(){
$("div#cart_info div.loading").fadeIn();
$.post(base_url + base_section + "/update_order_info", {
key: $(this).attr("name"),
value: $(this).val()
}, function(data){
$("div#cart_info div.loading").fadeOut();
});
});
$("div#cart_items input").change(function(){
var self = this;
$("div#cart_items div.loading").fadeIn();
$.post(base_url + base_section + "/update_order_qty", {
key: $(this).parent().parent().attr("rel"),
value: $(this).val()
}, function(data){
$("div#cart_items div.loading").fadeOut();
var item_subtotal = "$" + toCurrency(parseCurrency($(self).parent().prev().text()) * data);
$(self).parent().next().fadeOut("fast").fadeIn().text(item_subtotal);
calculate_cart_total();
});
});
$("a#clear").click(function(){
if (confirm("Are you sure you want to clear the cart?"))
{
$("div#cart_items div.loading").fadeIn();
$.post(base_url + base_section + "/clear_cart", {
id: $("div#cart_info").attr("rel")
}, function(data){
$("div#cart_items div.loading").fadeOut();
calculate_cart_total();
$("div#cart_items").fadeOut("fast", function(){
$(this).html("