function swapBook(level, date) {
	dynamItLoad('/account/swap.dT?level=' + level  + '&date=' + date, null, 400, 'auto', function() {
		initSwapDialog(level, date);
		activateSwappers();
	});
}

function changePassword() {
	dynamItLoad('/account/password.dT', null, 300, 'auto', null);
}

function changeShipping() {
	dynamItLoad('/account/modshipping.dT', null, 300, 'auto', null);
}
function changeBilling() {
	dynamItLoad('/account/modbilling.dT', null, 300, 'auto', null);
}

function collapseAdvanced() {
	$("#advSearch").slideUp('slow');
}

function expandAdvanced() {
	$("#advSearch").slideDown('slow');
}

function noticeBrowse(first) {
	dynamItLoad('/news/list.dT?first=' + first, 'noticeList', null, null, null);
}

var lastNotice = null;
function noticeLoad(notice) {
	dynamItLoad('/news/load.dT?nid=' + notice, 'noticeBody', null, null, function() {

		if(lastNotice) $('#notice_' + lastNotice).removeClass();
		$('#notice_' + notice).addClass('selectedrow');
		lastNotice = notice;

	});
}


function noticeCouponLoad(notice, coupon) {
    dynamItLoad('/news/load.dT?cid=' + coupon, 'noticeBody', null, null, function() {

        if(lastNotice) $('#notice_' + lastNotice).removeClass();
        $('#notice_' + notice).addClass('selectedrow');
        lastNotice = notice;
    });
}


/*** CART AND WISHLIST FUNCTIONS ***/
function addLevelToCart(levels) {
	var url = '/cart/addlevel.dT';
	if(typeof(levels) == 'object') {
		url += '?level[]=' + levels.join('&level[]=');
	} else {
		url += '?level=' + levels;
	}
	url += '&cart=1';
	dynamItSend(url);
}

function addLevelToWishlist(levels) {

	var url = '/cart/addlevel.dT';
	if(typeof(levels) == 'object') {
		url += '?level[]=' + levels.join('&level[]=');
	} else {
		url += '?level=' + levels;
	}
	url += '&cart=0';
	dynamItSend(url);

}

function addBookToCart(book) {
	var url = '/cart/addguide.dT';
	if(typeof(book) == 'object') {
		url += '?book[]=' + book.join('&book[]=');
	} else {
		url += '?book=' + book;
	}
	url += '&cart=Cart';
	dynamItSend(url);
}


function addBookToWishlist(book) {
	var url = '/cart/addguide.dT';
	if(typeof(book) == 'object') {
		url += '?book[]=' + book.join('&book[]=');
	} else {
		url += '?book=' + book;
	}
	url += '&cart=Wishlist';
	dynamItSend(url);
}

function doAddBookToCart(book, turnoff) {
	var url = '/cart/add.dT';
	if(typeof(book) == 'object') {
		url += '?book[]=' + book.join('&book[]=');
	} else {
		url += '?book=' + book;
	}
	url += '&no_guides=' + ( (turnoff) ? 1:0 );
	dynamItSend(url);
	dynamItCloseDialog();
}

function doAddBookToWishlist(book, turnoff) {
	var url = '/wishlist/add.dT';
	if(typeof(book) == 'object') {
		url += '?book[]=' + book.join('&book[]=');
	} else {
		url += '?book=' + book;
	}
	url += '&no_guides=' + ( (turnoff) ? 1:0 );
	dynamItSend(url);
	dynamItCloseDialog();
}

function moveBatchToCart(f) {
	f.action = '/wishlist/move.dT';
	dynamItPublish(f);
}

function moveBookToCart(book) {
	dynamItSend('/wishlist/move.dT?book[]=' + book);
}

function moveLevelToCart(level) {
	dynamItSend('/wishlist/move.dT?level[]=' + level);
}

function removeBatchFromWishlist(f) {
	f.action = '/wishlist/remove.dT';
	dynamItPublish(f);
}


function removeBatchFromCart(f) {
	f.action = '/cart/remove.dT';
	dynamItPublish(f);
}

function addBatchToCart(f) {
	var booklist = new Array();
	$(f).find("input[name='book[]']").each(function() {
		if(this.checked) {
			booklist.push($(this).val());
		}
	});

	addBookToCart(booklist);
}

function addBatchToWishlist(f) {
	var booklist = new Array();
	$(f).find("input[name='book[]']").each(function() {
		if(this.checked) {
			booklist.push($(this).val());
		}
	});

	addBookToWishlist(booklist);
}

function applyCoupon(coupon) {
    var url = '/cart/addcoupon.dT?coupon='+escape(coupon);
    dynamItSend(url);
}

function removeCoupon(coupon) {
    var url = '/cart/removecoupon.dT?coupon='+escape(coupon);
    dynamItSend(url);
}

function renewLevel(levels) {

	var url = '/cart/addlevel.dT?lengthopt=3';
	if(typeof(levels) == 'object') {
		url += '&level[]=' + levels.join('&level[]=');
	} else {
		url += '&level=' + levels;
	}

	dynamItSend(url);

}

function renewBatchLevels(f) {
	f.action = '/cart/addlevel.dT';
	dynamItPublish(f);
}

function addLevelBatchToCart(f) {
	f.action = '/cart/addlevel.dT';
	dynamItPublish(f);
}

/*** END CART AND WISHLIST ***/



function changeQty(level, book, qtyf, unit, proc, months) {

	if(qtyf.value == '') return;

	var qty = parseInt(qtyf.value, 10);
	qtyf.value = qty;

	var q = '';
	if(qty <= 0) {
		if(book != '') q = 'book[]=' + book;
		if(level != '') q = 'level[]=' + level;

		dynamItSendOnConfirm('/cart/remove.dT?' + q, 'Remove this item from your cart?');
	} else {
		if(book != '') q = 'book=' + book;
		if(level != '') q = 'level=' + level;
		q += '&qty=' + qty;
		q += '&unit=' + unit;
		q += '&proc=' + proc;
        q += '&months=' + months;

		dynamItSend('/cart/quantity.dT?' + q);
	}
}

function couponsNeedRefesh() {
    // TODO: Possibly refresh panel with new data
    $("tr.coupondata").each(function() {
        $(this).hide();
    });

    $("tr.couponrefresh").fadeIn();
}


function changeOpt(level, opt) {
	var url = '/cart/update.dT';
	url += '?level=' + level;
	url += '&lengthopt=' + opt;

	location.href = url;
}

function findBook(q) {
	if(q == '') {
		$("#swapOrigList").show();
		$("#swapSearchList").html('').hide();
		return;
	}

	var url = '/account/findbook.dT?q=' + urlencode(q);
	dynamItLoad(url, 'swapSearchList', null, null, function() {
		$("#swapOrigList").hide();
		$("#swapSearchList").show();
		activateSwappers();
	});

}


function findCustomerNo() {
	dynamItLoad('/register/help.dT', null, 350, 'auto', null);
}


function expandLeftNav(obj, target) {
	$("#" + obj).toggleClass('selected');
	$("#" + target).toggle('slow');
}




function adminRemovePackage(id) {
	dynamItSendOnConfirm('/admin/removepkg.dT?pid=' + id, 'Are you sure you wish to delete this package?');
}


function deleteFromWeb(id) {
	dynamItSendOnConfirm('/admin/doDeleteFromWeb.dT?isbn=' + id, 'Would you like to delete ISBN <b>'+id+'</b> from the website?<br/><br/>NOTE: Future SAP synchronizations may re-enable this item unless the master record is hidden or deleted.');
}

var globalTimeout = null;
function queue(fcn, interval) {

	if(globalTimeout) clearTimeout(globalTimeout);
	globalTimeout = setTimeout(fcn, interval);
}

function dequeue(hide) {

	if(globalTimeout) clearTimeout(globalTimeout);
	$(hide).hide();
}

/** SWAP **/
	var selectedBook = null;
	var swapLevel = null;
	var swapDate = null;

	function initSwapDialog(level, date) {
		swapLevel = level;
		swapDate = date;
	}

	function activateSwappers() {
		$('.availableBook').click(function() {

			if(selectedBook) $('#' + selectedBook).css('backgroundColor', '#ffffff');

			var val = $(this).attr('id');
			selectedBook = val;
			$(this).css('backgroundColor', '#BEF3F7');
		});
	}

	function makeTheSwap() {

		if(selectedBook) {
			var url = '/account/makeswap.dT';
			url += '?level=' + swapLevel;
			url += '&date=' + swapDate;
			url += '&book=' + selectedBook;

			location.href = url;
		}

	}
/*********/


function aboutGuides() {
	dynamItLoad('/home/aboutguides.dT', null, '400', 'auto', null);
}

function cartProcessingOptChange( addon, setup ) {
    if ( addon == 1 && setup == 1 ) {
        var select = dynamItElem("processing");
        select.value='0';
        dynamItLinkOnConfirm('/cart/processing.dT?addon='+addon, 'Your processing profile options have not been defined.<br/><br/>Would you like set them up now?');
    } else {
        location.href='/cart/processing.dT?addon='+addon;
    }
}

function marcRecord(isbn, filename, placeholder) {
    var args = 'isbn='+isbn+'&filename='+filename+'&placeholder='+(placeholder?1:0);
    
    if ( getCookie('marc')=='1' ) {
        if ( placeholder ) {
            // Log the event with google
            analyticsEvent('MARC Record', 'Download Placeholder', filename);
            window.open('/books/downloadmarcplace.dT?'+args, 'marcdownload');
        } else {
            // Log the event with google
            analyticsEvent('MARC Record', 'Download Standard', filename);
            window.open('/books/downloadmarcstd.dT?'+args, 'marcdownload');
        }
    } else {
        dynamItLoad('/books/marc_warning.dT?'+args,null,350,275,null);
    }
}

function jlgGuideDownload(itemcode, type) {
    if ( type == 1 ) {
        analyticsEvent('JLG Guide', 'Download', 'Student:'+itemcode);
    } else if ( type == 2 ) {
        analyticsEvent('JLG Guide', 'Download', 'Teacher:'+itemcode);
    } else {
        analyticsEvent('JLG Guide', 'Download', 'Default:'+itemcode);
    }
}

function memberPriceWarning(isbn) {
    var args = "isbn="+isbn;
    
    if ( getCookie('memberpricewarning')=='1' ) {
        return true;
    } else {
        dynamItLoad('/cart/member_price_warning.dT?'+args,null,350,275,null);
        return false;
    }
    
}

function base_convert (number, frombase, tobase) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philippe Baumann
    // +   improved by: Rafal Kukawski (http://blog.kukawski.pl)
    // *     example 1: base_convert('A37334', 16, 2);
    // *     returns 1: '101000110111001100110100'

    return parseInt(number + '', frombase | 0).toString(tobase | 0);
}

function time () {
    // http://kevin.vanzonneveld.net
    // +   original by: GeekFG (http://geekfg.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: metjay
    // +   improved by: HKM
    // *     example 1: timeStamp = time();
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
    
    return Math.floor(new Date().getTime()/1000);
}