/**************************************************************************************************/
/***
/***	TERNSTYLE'S CUSTOM FUNCTIONS FOR REACTGEAR & VOLUSION JAVASCRIPT DOCUMENT - (ReactGear & Volusion)
/***	-----------------------------------------------------------------------
/***	Written by Matthew Praetzel. Copyright (c) 2010 Matthew Praetzel.
/***	-----------------------------------------------------------------------
/***	All Rights Reserved. Any use of these functions & scripts without written consent is prohibited.
/***
/**************************************************************************************************/

/*-----------------------
	Initialize
-----------------------*/
//var price,couponCopy = 'Your order qualifies for free shipping, which cannot be combined with any other discounts. If you would like to enter a discount, please select a paid shipping option first. If you have any questions, please review our <a href="http://www.reactgear.com/help_answer.asp?ID=15#18" target="_blank">coupon policy</a> or <a href="http://www.reactgear.com/contact-us-a/142.htm" target="_blank">contact us</a>.';
//
var couponCopy = 'We noticed that your order qualifies for free shipping and you have a coupon code. You may choose to use the Coupon Code or to receive Free Shipping, but we cannot honor both on the same order.<ul><li>To use the coupon code, please select a paid shipping method</li><li>If you prefer to receive free shipping, please remove the coupon code</li></ul>If you have any questions, please review our <a href="http://www.reactgear.com/help_answer.asp?ID=15#18" target="_blank">coupon policy</a> or <a href="http://www.reactgear.com/contact-us-a/142.htm" target="_blank">contact us</a>.',price=false;

//
jQuery(document).ready(function () {
	//alerts
	jQuery(document.body).append('<div id="alerts_cn" style="display:none;"><div id="errors">Errors:<br /><br /><br /><br /><br /></div><div id="alerts">Messages:<br /><br /><br /></div><br /><br /><a id="alerts_cl" href="javascript:hideAlerts();">Close</a></div>');
	//get price
	var t = jQuery('font.pricecolor b font').add('font.pricecolor b');
	t.each(function () {
		if(jQuery(this).html().indexOf('$') != -1) {
			price = this;
		}
	});
	//coupons
	//jQuery('input[name=CouponCode]').bind('keyup',coupons).bind('blur',coupons).parent().find('b').css({ display:'block' });
	if(jQuery('form[name=OnePageCheckoutForm]').get(0)) {
		var p = jQuery('#span_Shopping_Cart_UnEditable td');
	}
	else {
		var p = jQuery('font.carttext b');
	}
	p.each(function () {
		if(jQuery(this).html().indexOf('%') != -1) {
			coupons();
		}
	});
	//if(jQuery('#promo').get(0)) {
	//	coupons();
	//}
	//address fields
	jQuery('input[name^=BillingAddress]').add('input[name^=ShipAddress]').bind('keyup',function () {
		//jQuery(document.body).append('<div style="display:none">'+jQuery(this).val()+'</div>');
		if(jQuery('select[name=ShipTo]').get(0) || (!jQuery('select[name=ShipTo]').get(0) && this.name.indexOf('Ship') !== -1)) { 
			var s = jQuery(this).val().toLowerCase();
			var r = /p([\s]*)([.]?)([\s]*)o([\s]*)([.]?)([\s]*)box/g;
			if(r.test(s)) {
				alerts('Error:<br /><br />We cannot ship to PO Boxes. Please provide a street address for shipment.<br /><br />');
				jQuery(this).val('');
			}
		}
	});
	//free shipping
	freeShipping();
	
	//sku table
	jQuery("#sku tr").mouseover(function() {jQuery(this).addClass("over");}).mouseout(function() {jQuery(this).removeClass("over");});
	jQuery("#sku tr:even").addClass("alt");
});
/*-----------------------
	Free Shipping
-----------------------*/
function freeShipping() {
	if(price && parseFloat(jQuery(price).html().replace(/[$\s\r\n]/g,'')) > 99) {
		jQuery('#cart_continueshop').append('<img src="https://www.reactgear.com//v/vspfiles/templates/ReactGear/images/free.jpg" width="306" height="20" title="Free Shipping" alt="Free Shipping" style="float:right;" />');
	}
}
/*-----------------------
	Coupon
-----------------------*/
function coupons() {
	if(parseFloat(jQuery(price).html().replace(/[$\s\r\n]/g,'')) > 99 && (!jQuery('select[name=ShippingSpeedChoice]').get(0) || jQuery('select[name=ShippingSpeedChoice]').get(0).selectedIndex == 1)) {
		alerts('Please Note:<br /><br />'+couponCopy+'<br /><br />');
		jQuery('input[name=CouponCode]').val('');
	}
}
/*
function continueShopping () {
	
}
*/
/*-----------------------
	Alerts
-----------------------*/
function alerts(s) {
	if(document.getElementById('alerts_cn')) {
		//jQuery(document.body).append('<div style="display:none">'+jQuery(price).html().replace(/[$\s\r\n]/g,'')+'</div>');
		jQuery(document.body).elapsor({
			color : '#000',
			opacity : 80,
			func : function () {
				jQuery('#errors').html(s).css({ display : 'block' });
				//
				var d = [jQuery('#alerts_cn').innerWidth(),jQuery('#alerts_cn').innerHeight()];
				var w = [jQuery(document.body).innerWidth(),jQuery(window).height()];
				if(jQuery.browser.msie && jQuery.browser.version <= 6) {
					jQuery('#alerts_cn').css({ 'position' : 'absolute' });
					jQuery('select').hide();
				}
				else {
					jQuery('#alerts_cn').css({ 'position' : 'fixed'	});
				}
				jQuery('#alerts_cn').css({
					'top' : ((w[1]-d[1])/2),
					'left' : (w[0]-d[0])/2,
					display : 'block',
					'visibility' : 'visible'
				});
			}
		});
	}
}
function hideAlerts() {
	jQuery('#alerts_cn').css({
		display : 'none'
	});
	jQuery('select').show();
	jQuery('#errors').html('').css({ display : 'none' });
	jQuery('#alerts').html('').css({ display : 'none' });
	//
	jQuery(document.body).hideElapsor();
}
