$(document).ready(function(){
//this is what allows a product to be draggable.
$(".pbimage").draggable({
opacity: 0.5,
helper: 'clone'
});
$(".drop").droppable({
accept: ".draggablediv",
activeClass: 'droppable-active',
tolerance: 'touch',
over: function() {
$("#shoppingcarticon").fadeTo("fast", 0.6);
},
out: function() {
$("#shoppingcarticon").fadeTo("fast", 1);
},
drop: function(ev, ui) {
dropProduct( ev, ui );
}
});
// custom fields that we may need to set
if ( $( "#neucart_cart_value_custom" ).length )
{
if ( $( "#carttotal" ).length )
$( "#neucart_cart_value_custom" ).text( $( "#carttotal" ).text() );
else
custom_carttotal_absent();
}
// custom fields that we may need to set
if ( $( "#neucart_cart_qty_custom" ).length )
{
if ( $( "#cartqty" ).length )
$( "#neucart_cart_qty_custom" ).text( $( "#cartqty" ).text() );
else
custom_cartqty_absent();
}
custom_neucart_pageload();
});
function detailsClicked( cartkey ) {
var elemId = "#celldetails" + cartkey;
var showHideId = "#showhide" + cartkey;
var expand = $( elemId ).html() == "";
var detailText = expand ? $( elemId ).attr( "dtext" ) : "";
$( elemId ).html( detailText );
$( showHideId ).html( expand ? neucart_hide_details : neucart_show_details );
}
function checkoutQuantityChange( cartkey, count ) {
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: 'chk_change_quantity',
id: cartkey,
quantity: count },
function ( data ) {
if ( data == "false" )
return false;
// cart price, item total
var qpk = data.split( "|" );
$( "#summary_carttotal" ).text( qpk[ 0 ] );
var itemtotal = qpk[ 1 ];
$( "#totalprice_" + cartkey ).text( neucart_currency_symbol + itemtotal );
}
);
return true;
}
function checkoutSpecInstrChange( cartkey, instructions ) {
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: 'chk_change_spec_inst',
id: cartkey,
inst: instructions }
);
}
function checkoutCartMemoChange( instructions ) {
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: 'chk_change_cart_memo',
inst: instructions }
);
}
function checkoutOptionChanged( itemkey, optionkey, optionvalue ) {
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "chk_option_changed",
id: itemkey,
op: optionkey,
val: optionvalue
}, function ( data ) {
// it is blank when the price of the options didn't change.
if ( data == "" )
return;
var arr = data.split( "|" );
var iprice = prettifyNumber( arr[ 0 ] );
var tprice = prettifyNumber( arr[ 1 ] );
var cprice = prettifyNumber( arr[ 2 ] );
$( "#unitprice_" + itemkey ).text( neucart_currency_symbol + iprice );
$( "#totalprice_" + itemkey).text( neucart_currency_symbol + tprice );
$( "#summary_carttotal" ).text( cprice );
}
);
}
// this function is called from drag/drop. the product is being "dropped"
// into the cart. this calls "add product", which actually adds the item
// into memory and the ui cart
function dropProduct( ev, ui ) {
productid = ui.draggable.attr("p_id");
productsku = ui.draggable.attr("p_sku");
productname = ui.draggable.attr("p_name");
productprice = ui.draggable.attr("p_price");
productInstructionLabel = ui.draggable.attr("p_specInstrLabel");
addProduct( ev, ui, productid, productname, productprice, productsku, productInstructionLabel, "", null );
}
// it would appear that "ev" is never used.
// this function is called directly from the "add to cart" link
// on the product detail page. also called from function above.
function addProduct(ev, ui, productid, productname, productprice, productsku, productInstructionLabel, productInstrInstr, productOptions ) {
productqty = 1;
if ( productOptions == null )
productOptions = "";
else
{
// create a pipe-sep string with key:value items between pipes
var optns = "";
for ( var id in productOptions )
optns = optns.concat( id, ":", productOptions[ id ], "|" );
productOptions = optns;
}
if ( productInstructionLabel == null )
productInstructionLabel = "";
if ( productInstrInstr == null )
productInstrInstr = "";
var topOrig = showWorking( neucart_adding_product );
var atc_link = document.getElementById( 'atc_link_' + productid );
var linkOrig = showWorkingLink( atc_link );
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "addproduct",
id: productid,
name: productname,
price: productprice,
sku: productsku,
options: productOptions,
specname: productInstructionLabel,
specinst: productInstrInstr },
function( data ) {
// the function echos "false" string or "quantity|total|cartguid|unitprice".
if ( data == "false" ) {
restoreCart( topOrig );
restoreLink( linkOrig, atc_link );
return;
}
// quantity, price, key (unique key used to store items in cart)
var qpk = data.split( "|" );
$( "#cartqty" ).text( qpk[ 0 ] );
$("#carttotal").text( qpk[ 1 ] );
var cartkey = qpk[ 2 ];
productprice = qpk[ 3 ];
// if there are custom cart fields, update those.
update_custom_totals( qpk[ 0 ], qpk[ 1 ] );
var elemId = "#ddcproduct" + cartkey;
// if the item exists, increase the quantity. otherwise,
// add to the list.
if ( $(elemId).text() != "" )
{
// increase the quantity. update the attribute AND
// update the text
var newQty = parseFloat( $( elemId ).attr( "qty" ) ) + 1;
$( elemId ).attr( "qty", newQty );
$( elemId + " > #cell2").html( newQty );
}
else
{
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "get_options_name",
id: cartkey },
function( rslt ) {
//add a new row.
elemId = elemId.replace( "#", "" );
var detailsText = "";
if ( rslt != "" )
{
detailsText =
"
[" +
'' + neucart_show_details + ']' +
'
';
}
var newrowtext =
'' +
'
' +
'
' + productqty + '
' +
'
' + neucart_currency_symbol +
prettifyNumber( productprice ) +
'
' +
'
' +
'
';
$( "#listitems" ).prepend( newrowtext );
}
);
}
restoreCart( topOrig );
restoreLink( linkOrig, atc_link );
}
);
}
function vote_for_review( isHelpful, id ) {
isHelpful = isHelpful ? 1 : 0;
$.get( "includes/ajax/cust_review.php", {
sid: "",
action: "vote_for_review",
id: id,
helpful: isHelpful }
);
$( "#helpful_review_" + id ).fadeOut( 100, function() {
document.getElementById( 'helpful_review_' + id ).innerHTML = neucart_thanks_for_review;
// update the counts to show the customer just reviewed. it's AJAX so we don't really know if
// the update occurred (and it may fail if this is from an IP address that already voted)
// but just update this anyway without returning a result. it's not critical.
cur_val = document.getElementById( 'number_of_helpful_' + id ).innerHTML;
document.getElementById( 'number_of_helpful_' + id ).innerHTML = parseInt( cur_val ) + isHelpful;
cur_val = document.getElementById( 'number_of_total_' + id ).innerHTML;
document.getElementById( 'number_of_total_' + id ).innerHTML = parseInt( cur_val ) + 1;
$( "#helpful_review_" + id ).fadeIn( 400 );
});
}
function submit_review( showImmediately, prodid ) {
stars = document.getElementById( 'review_stars' ).value;
title = document.getElementById( 'review_title' ).value;
text = document.getElementById( 'review_text' ).value;
name = document.getElementById( 'reviewer_name' ).value;
loca = document.getElementById( 'reviewer_location' ).value;
crid = 'new';
if ( jQuery.trim( title ).length == 0 ) {
alert( neucart_review_blank_title );
return;
}
if ( jQuery.trim( text ).length == 0 ) {
alert( neucart_review_blank_text );
return;
}
if ( jQuery.trim( name ).length == 0 ) {
alert( neucart_review_blank_name );
return;
}
if ( jQuery.trim( loca ).length == 0 ) {
alert( neucart_review_blank_loca );
return;
}
if ( document.getElementById( 'edit_your_review' ) != null )
crid = $( "#edit_your_review" ).attr( 'crid' );
$.get( "includes/ajax/cust_review.php", {
sid: "",
action: "submit_review",
pid: prodid,
rstars: stars,
rtitle: title,
rtext: text,
rname: name,
rloca: loca,
revid: crid }
);
$( "#your_review" ).fadeOut( 300, function() {
$( "#your_review_submitted" ).fadeIn( 400 );
$( "#your_review" ).remove();
});
}
function update_custom_totals( qty, total ) {
// if these fields exist, update them. this is for customers who
// have added custom fields on their pages that must be updated
if ( document.getElementById( 'neucart_cart_value_custom' ) != null )
$( "#neucart_cart_value_custom" ).text( total );
if ( document.getElementById( 'neucart_cart_qty_custom' ) != null )
$( "#neucart_cart_qty_custom" ).text( qty );
}
function showWorking( text ) {
var topRow = "#toplistrow";
var orig = $( topRow ).html();
var newHtml = '' + text + '
';
$( topRow ).html( newHtml );
$( topRow ).css( "background", "yellow" );
return orig;
}
function showWorkingLink( atc_link ) {
if ( atc_link == null )
return null;
atc_orig = atc_link.innerHTML;
atc_link.innerHTML = neucart_adding_product;
return atc_orig;
}
function restoreLink( text, atc_link ) {
if ( atc_link == null )
return;
$( atc_link ).fadeTo( 500, 0, function() {
$( atc_link ).html( neucart_click_to_add_another );
$( atc_link ).fadeTo( 500, 1 );
} );
}
function restoreCart( topRowOrigText ) {
$("#addhowto").css( "display", "none");
$("#parentlist").css( "display", "none");
$("#parentlist").fadeIn( "fast" );
$("#shoppingcarticon").fadeTo( "fast", 1 );
$( "#toplistrow" ).css( "background", "#ccc" );
$( "#toplistrow" ).html( topRowOrigText );
}
function prettifyNumber( number ) {
var splits = number.split( '.' );
var digits = splits[ 0 ];
if ( digits.length > 3 )
{
var thou = digits.substr( 0, digits.length - 3 )
digits = thou.concat( ",", digits.substr( digits.length - 3 ) );
}
if ( splits.length > 1 )
return digits.concat( ".", splits[ 1 ] );
else
return digits.concat( ".", "00" );
}
function increaseQty( product ) {
prodparts = product.split( "|" );
cartkey = prodparts[ 0 ];
var topOrig = showWorking( neucart_increasing_quantity );
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "quantity_increase",
key: cartkey },
function( data ) {
if ( data == "false" ) {
restoreCart( topOrig );
return;
}
// quantity, price, key... returned from func
var qpk = data.split( "|" );
$( "#cartqty" ).text( qpk[ 0 ] );
$("#carttotal").text( qpk[ 1 ] );
// if there are custom cart fields, update those.
update_custom_totals( qpk[ 0 ], qpk[ 1 ] );
var newQty = qpk[ 2 ];
// increase the quantity. update the attribute AND
// update the text
var elemId = "#ddcproduct" + cartkey;
$( elemId ).attr( "qty", newQty );
$( elemId + " > #cell2").html( newQty );
restoreCart( topOrig );
}
);
}
function removeProduct( productid ) {
var topOrig = showWorking( neucart_decreasing_quantity );
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "quantity_decrease",
key: productid },
function( data ) {
if ( data == "false" ) {
restoreCart( topOrig );
return;
}
// quantity, price, key... returned from func
var qpk = data.split( "|" );
$( "#cartqty" ).text( qpk[ 0 ] );
$("#carttotal").text( qpk[ 1 ] );
// if there are custom cart fields, update those.
update_custom_totals( qpk[ 0 ], qpk[ 1 ] );
var newQty = qpk[ 2 ];
var elemId = "#ddcproduct" + productid;
if ( newQty > 0 )
{
// decrease the quantity. update the attribute AND
// update the text
$( elemId ).attr( "qty", newQty );
$( elemId + " > #cell2").html( newQty );
}
else
{
// disable the link and then
// remove altogether.
$( elemId + " > #cell0 > a").attr( 'onclick', '' );
$( elemId ).fadeOut( 500, function() {
$( elemId ).remove();
if ( qpk[ 0 ] == 0 )
$( "#addhowto" ).fadeIn( "fast" );
}
);
}
restoreCart( topOrig );
}
);
}
function emptyCart(){
if ( !confirm( neucart_empty_cart_confirm ) )
return;
var topOrig = showWorking( neucart_emptying_cart );
$.get( "includes/ajax/updatecart.php", {
sid: "",
action: "empty_cart" },
function( data ) {
if ( data == "false" ) {
restoreCart( topOrig );
return;
}
// array of items
var arr = data.split( "|" );
for ( i = 0; i < arr.length; i++ )
{
// disable the link and then
// remove altogether.
var elemId = "#ddcproduct" + arr[ i ];
$( elemId + " > #cell0 > a").attr( 'onclick', '' );
$( elemId ).fadeOut( 500, function() {
$( elemId ).remove();
}
);
}
$( "#cartqty" ).text( 0 );
$("#carttotal").text( neucart_currency_symbol + "0.00" );
restoreCart( topOrig );
$( "#addhowto" ).fadeIn( "fast" );
// if there are custom cart fields, update those.
update_custom_totals( 0, neucart_currency_symbol + "0.00" );
}
);
}