function update_size(el){
	$(el).parent().parent().find('a').removeClass('sel');
	$(el).addClass('sel');
	var key = $(el).attr('key');
	var val = $(el).attr('value');
	var price = Number($(el).attr('price'));
	var base = Number($('#product_baseprice').val());
	var sale = Number($('#product_saleprice').val());
	var txt = $(el).text();
	update_itemprice(price,base,sale);
	var avail_extra = (price) ? ' (+$'+(price).toFixed(2)+')' : '';
	
	if (val == 'nostock'){
		$('#msg_available').html('<b>'+txt+'</b> is Currently Unavailable');
		$('#btn_addcart').removeClass('bg-school-color').addClass('disabled').removeClass('orderReady');
		$('#'+key).val('');
	} else {
		$('#msg_available').html('<b>'+txt+'</b> is Available'+avail_extra);
		$('#btn_addcart').addClass('bg-school-color').removeClass('disabled').addClass('orderReady');
		$('#'+key).val(val);
	}

}

function update_itemprice(price,base,sale){
	var baseprice = price+base
	$('#item_price').html('$'+(baseprice));

	if (sale){
		var pct = (Math.round((sale/base)*100))/100;
		var price = price*pct;
		var newprice = '$'+(price+sale).toFixed(2);
		$('#item_saleprice').text(newprice);
	}

	if (isNaN(baseprice)) $('#item_price').html('---');
}

function item_viewlarge(el){
	pagePopup('img', $(el).attr('img'));
}

function item_remove(el){
	var vars = new Object();
	var url = ROOT_URL+$(el).attr('href');
	var pid = $(el).attr('pid');
	$('#tr_'+pid).fadeTo(1,0.4);
	vars.pid = pid;
	$.post(url, vars, ajaxResponse, 'json');
}

function item_update(el){
	var vars = new Object();
	var url = ROOT_URL+$(el).attr('href');
	var pid = $(el).attr('pid');
	vars.qty = $('#qty_'+pid).val();
	vars.pid = pid;
	$.post(url, vars, ajaxResponse, 'json');
}

function item_add(el){
	var frm = $(el).closest('form');
	if (!$('#btn_addcart').hasClass('orderReady')){
		alert('Please choose an in-stock size to add this item to your cart');
		return;
	};	
	$('#msg_addcart').html('The item is being added to your cart...');
	$('#btn_addcart').hide();
	formSend($(frm));
}

function wishlist_add(el){
	var frm = $(el).closest('form');
	$(frm).attr('action', 'checkout/modify/wishlist');	
	formSend($(frm));
}

function limitChars(el, limit, infodiv, defaultmsg){
	var textid= $(el).attr('id');
	if (!infodiv) infodiv = textid+'_msg';
	var text = $('#'+textid).val(); 	
	var textlength = text.length;
	if (textlength == 0){
		$('#' + infodiv).html('');
		$('#' + infodiv).html('You have '+limit+' characters remaining.');
		//$('#'+textid).val(defaultmsg);
		return false;
	} else if(textlength > limit){
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters remaining.');
		return true;
	}
}

function checkLinkHash(el){
	var pcs = $(el).attr('href').split('#');
	$(el).closest('ul').find('li').removeClass('sel');
	$(el).closest('li').addClass('sel');
	if (pcs[1]){
		var pos = $('#'+pcs[1]).offset();
		var offset = $('#product_left').offset();
		var top = pos.top - offset.top;
		//$('#sidemenu_bb').css('margin-top', top);
	}
}

function fadePage(bool){
	if (bool){
		var ww = $('#tofade').width();
		var hh = $('#tofade').height();
		$('#fade').css({width:ww, height:hh});
		$('#fade').fadeTo(1,0.9).show();
	} else {
		$('#fade').fadeTo(1,0).hide();
		$('#fade').unbind('click');	}
}

function pagePopup(type,str, title){
	switch(type){
		case 'img':			
			html = '<p class="imgbox"><a class="bigclosex" onclick="hideCart(\'popup_bb\');return false" href="#">X</a><img src="'+str+'" border="0" onclick="hideCart(\'popup_bb\');return false"  class="pointer" /></p>';			
		break;
		case 'promo':
			var html = '';
			var html = '<a href="#" onclick="hideCart(\'popup_bb\');return false" class="popup_close">Close and Continue Shopping</a>';
			html += '<img src="'+str+'" border="0" onclick="hideCart(\'popup_bb\');return false"  class="pointer"  style="margin-top:70px" />';
			$('#fade').bind('click', function(){ hideCart('popup_bb')});			
		break;
		case 'error':
			html = '<div class="errorbox"><a class="bigclosex" onclick="hideCart(\'popup_bb\');return false" href="#">X</a><h1>Error Encountered</h2><p>'+str+'</p></div>';			
		break;
		case 'alert':
			html = '<div class="alertbox"><a class="bigclosex" onclick="hideCart(\'popup_bb\');return false" href="#">X</a><h1>'+title+'</h2><p>'+str+'</p></div>';			
		break;
		default:
		return;
	}
	
	$('#popup_bb').html(html);
	fadePage(1);
	$('#popup_bb').show();
}

function togglePopup(id, vars){
	switch(id){
		case 'free_shipping':
			var div = 'popup_bb';
			var type = 'promo';
			var pvar = 'img/promo/freeshipping_75_popup.gif';			
		break;
		case 'product':
			var div = 'popup_bb';
			var type = 'img';
			var pvar = vars;
		break;
	}	
		
	if ($('#'+div).attr('popup') == id && $('#'+div).css('display') == 'block'){
		hideCart(div);
	} else {
		$('#'+div).attr('popup', id);
		pagePopup(type, pvar);
	}
}

function showTip(id, el){
	var hh = $('#'+id).height();
	var pos = $(el).position();
	$('#'+id).show().css({ left:pos.left+15, top:(pos.top-hh-15)});	
}

function closeTip(el){
	$(el).closest('div.tooltip').hide();
}

function hideShipPromo(){
	$('#shipping_upsell').hide();
}

function popupCart(){
	$('#msg_addcart').html('');
	$('#btn_addcart').show();
	showCart();
}

function showCart(id){
	if (!id) id = 'cart';
	$('#'+id).show();
	fadePage(1);
}

function hideCart(id){
	if (!id) id = 'cart';
	$('#'+id).hide();
	fadePage(0);
}

function getPrice(id){
	var t = $('#'+id).text();
	var p =  Number(t.substr(1));
	if (isNaN(p)) p =  Number(t.substr(2));
	if (isNaN(p)) return 0;	
	return p;	
}

function setShippingChoice(el){
	$('#shipmenu li').removeClass('sel');	
	$(el).addClass('sel');
	var method = $(el).attr('value');
	var price = $(el).attr('price');
	$('#shipping_method_id').val(method);
	$('#shipping_cost').val($(el).attr('cost'));
	$('#order_shipping').html(price);
	//update on server
	var url = ROOT_URL+'checkout/modify/shipping_method';
	var data = new Object();
	data.method = method;	
	data.price = price;
	$.post(url, data);	
	calcTotal();
}

function applyDiscount(id){
	var url = ROOT_URL+'checkout/modify/'+id;
	var data = new Object();
	data.code = $('#'+id).val();
	data.ship = $('#shipping_method').val();
	$.post(url, data, ajaxResponse, 'json');
}

function updateCertificate(n){
	VARS.certificate =  Number(n).toFixed(2);
	$('#order_certificate').html('-$'+VARS.certificate);
	if (n > 0){		
		$('#tr_certificate').show();			
	} else {
		$('#tr_certificate').hide();
	}
	calcTotal();
}

function updateDiscount(n){
	VARS.disc = Number(n).toFixed(2);
	$('#order_discount').html('-$'+n);
	if (n > 0){		
		$('#tr_discount').show();
		$('#discounts_bb').show();
		$('#discounts_msg').hide();		
	} else {
		$('#tr_discount').hide();
	}
	calcTotal();
}

function toggleGiftwrap(bool){
	var ct = 0;
	if (bool){
		$('#order_product tr.checkout input.qty').each(function(i){
			ct += Number($(this).val());
		});
		var price = '$'+(ct*4).toFixed(2);
		$('#order_giftwrap').html(price);
		$('#tr_giftwrap').show();
	} else {
		$('#order_giftwrap').html('$0.00');
		$('#tr_giftwrap').hide();		
	}
	$('#giftwrap').attr('checked', bool);
	calcTotal();
}





