jQuery(function(){

	if ( typeof global_Current_ProductCode != "undefined" && jQuery("[name^=SELECT___] ").length )
	{

/* Copyright (c) 2009 Michael Manning (actingthemaggot.com) Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.*/
(function(A){A.fn.extend({currency:function(B){var C={s:",",d:".",c:2};C=A.extend({},C,B);return this.each(function(){var D=(C.n||A(this).text());D=(typeof D==="number")?D:((/\./.test(D))?parseFloat(D):parseInt(D)),s=D<0?"-":"",i=parseInt(D=Math.abs(+D||0).toFixed(C.c))+"",j=(j=i.length)>3?j%3:0;A(this).text(s+(j?i.substr(0,j)+C.s:"")+i.substr(j).replace(/(\d{3})(?=\d)/g,"$1"+C.s)+(C.c?C.d+Math.abs(D-i).toFixed(C.c).slice(2):""));return this})}})})(jQuery);jQuery.currency=function(){var A=jQuery("<span>").text(arguments[0]).currency(arguments[1]);return A.text()};

		function recal(){
			if(jQuery('select[name^="SELECT___"]').length ){
				//add up all selected options
				jQuery('select[name^="SELECT___"]').each(function(){
				var recal_var = 0;
				recal_var = jQuery(this).find("option:selected").text();	
			
					if (recal_var.indexOf('[Subtract -$') >0){
					price_result = recal_var.split('$')[1].slice(0,-1).replace(/,/g,'');
					current_val=current_val-price_result;
					}
					else if (recal_var.indexOf('[Add $') >0){
					price_result = parseFloat(recal_var.split('$')[1].slice(0,-1).replace(/,/g,''));
					current_val=current_val+price_result;
					}
				}); //each function
			} //if length

			if (jQuery('input[name^="SELECT___"][type="radio"]').length ){
				//add up all radio
				jQuery('input[name^="SELECT___"][type="radio"]:checked').each(function(){
				var recal_var = 0;
				recal_var = jQuery.trim(jQuery(this).next("span").text());

					if (recal_var.indexOf('[Subtract -$') >0){
					price_result = recal_var.split('$')[1].slice(0,-1).replace(/,/g,'');
					current_val=current_val-price_result;
					}
					else if (recal_var.indexOf('[Add $') >0){
					price_result = parseFloat(recal_var.split('$')[1].slice(0,-1).replace(/,/g,''));
					current_val=current_val+price_result;
					}
				}); //
			} //if length

			if (jQuery('input[name^="SELECT___"][type="checkbox"]').length ){
				//add up all radio
				jQuery('input[name^="SELECT___"][type="checkbox"]:checked').each(function(){
				var recal_var = 0;
				recal_var = jQuery.trim(jQuery(this).next("span").text());
	
					if (recal_var.indexOf('[Subtract -$') >0){
					price_result = recal_var.split('$')[1].slice(0,-1).replace(/,/g,'');
					current_val=current_val-price_result;
					}
					else if (recal_var.indexOf('[Add $') >0){
					price_result = parseFloat(recal_var.split('$')[1].slice(0,-1).replace(/,/g,''));
					current_val=current_val+price_result;
					}
				}); //each function
			} //if length


			if( product_orig_price == 0 && jQuery('#updated_price_table').length)
			{
			jQuery('table#updated_price_table').remove();
			jQuery('#options_table').before('<table id="updated_price_table" cellspacing="0" cellpadding="3" border="0"><tr><td><span class="updated_price_text"><b>Price With Selected Options: </span><span class="updated_price">$'+jQuery.currency(current_val,{s:",",d:".",c:2})+ '</span></td></tr></table>');
			} else {
				if (jQuery('#updated_price_table').length == 0 )
				{
				jQuery('#options_table').before('<table id="updated_price_table" cellspacing="0" cellpadding="3" border="0"><tr><td><span class="updated_price_text"><b>Price With Selected Options: </span><span class="updated_price">$'+jQuery.currency(current_val,{s:",",d:".",c:2})+ '</span></td></tr></table>');
				} else {
				jQuery('.updated_price').html('$' + jQuery.currency(current_val,{s:",",d:".",c:2}));
				}	
			}	
			current_val=product_orig_price;

		}

	var parent = jQuery('input[type="radio"][name^="SELECT___"],input[type="checkbox"][name^="SELECT___"]').parent('td');
	parent.contents().filter(function() { return this.nodeType != 1; }).wrap('<span></span>');
	var current_val;
	var price_result;
	var product_orig_price = parseFloat(jQuery('font.pricecolor.colors_productprice:not(.pricecolorsmall)').find('.price1:last').text().replace('$','').replace(/,/g,''));
	if (isNaN(product_orig_price)){ product_orig_price = 0}
	current_val = product_orig_price;
	recal();
		jQuery('select[name^="SELECT___"],input[name^="SELECT___"]').change(function(){
		recal();
		}); //change

	jQuery("a[href^='javascript:change_option']").click(function(){
		var result_href = jQuery(this).attr('href').match(/\'(.*?)\'/)[1];
		var result_val = jQuery(this).attr('href').match(/\,(.*?)\)/)[1];
		change_option(result_href,result_val);
		recal();
		return false;
	});
	}
}); //doc


jQuery(function(){
	if (typeof global_Current_ProductCode != 'undefined' && jQuery('a[onclick*="/BulkDiscounts.asp?ProductID="]').length){
	jQuery("a[onclick*='/BulkDiscounts.asp?ProductID=']").attr('id','quantity_dis');
	var pieces = jQuery('#quantity_dis').attr('onclick').toString().split('?')[1].split('=');
	var parts = [];
	for (var ii = 0; ii < pieces.length; ii++) {
	var value = parseFloat(pieces[ii]);
	if (!isNaN(value)) parts.push(value);
	}
	var dis_url='/BulkDiscounts.asp?ProductID=' +parts[0]+ '&ProductCode=' +global_URL_Encode_Current_ProductCode+ '&Orig_Price=' +parts[1];	
		jQuery.ajax({
		url: dis_url,
		type: 'GET',
		cache: false,
		success: function(data){
			var discount_table = jQuery(data).find('table.colors_backgroundneutral');
			jQuery('.colors_pricebox:last').after(discount_table);
			jQuery('table.colors_backgroundneutral').css('width','100%').css('margin-top','10px');
                        jQuery('table.colors_backgroundneutral').find('td.colors_backgroundneutral:gt(1)').closest('tr').remove();
                        jQuery('table.colors_backgroundneutral').find('td.bgcolor1.colors_background1.colors_background1_text[colspan="3"]').closest('tr').remove();
			} //success function
		}); 
	} 
});
