var TEMPLATE_DIR = "oralblue";
var SampleProductClicked = "SAMPLE1";
var strLocation = ""+window.location+"";
var MinOrderValue = 70; // $70 
var _sample_product = new Array();

	_sample_product["Clen"] = "SAMPLE1";
	_sample_product["D-anabol 25"] = "SAMPLE2";
	_sample_product["Deca 200"] = "SAMPLE3";
	_sample_product["Winn 50"] = "SAMPLE4";
	_sample_product["Var 10"] = "SAMPLE5";
	_sample_product["Tren 75"] = "SAMPLE6";
	_sample_product["Pituitary Growth Hormone"] = "SAMPLE7";
	_sample_product["Test 600x"] = "SAMPLE8";


window._sample_product = _sample_product;
window.MinOrderValue = MinOrderValue;


var CheckoutCookie = {

	createCookie: function(name, value, days){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else 
			var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	},
	
	readCookie: function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') 
				c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) 
				return c.substring(nameEQ.length, c.length);
		}
		return null;
	},
	
	eraseCookie: function(name){
		CheckoutCookie.createCookie(name, "", -1);
	}
	
}


var CheckoutPromo = {
	status:null,
	PromoBody:null,
	CancelBtn:null,
	
	init:function(){
		//alert("Into Init");
		CheckoutPromo.status=CheckoutCookie.readCookie("PromoStatus");
		
		if (CheckoutPromo.status == null){
			CheckoutPromo.ShowPromo();
		}
		else if (CheckoutPromo.status == 1){
			
			if (CheckoutCookie.readCookie("CartID5") != CheckoutCookie.readCookie("lastCart")) {
				// user checked a product, but session expired
				if (CheckoutCookie.readCookie("CartID5")) {
					CheckoutPromo.ShowPromo();
				}
			}
			else {

				var samples = [];
				var sample_products_name = "";
				$(".carttext.colors_text").each(function(index, elem){
					sample_products_name = jQuery.trim($(elem).text());

					if (sample_products_name in _sample_product) {
						samples.push($(elem));
					}

				});
				
				
				if (samples.length > 1) {

					$.each(samples, function(i, sample){
						if (i < samples.length-1) {
							var removeUrl = $(sample).parents('table:eq(0)"').parent().prev().prev().prev().find('a').attr('href');
							alert("Sorry, but you can't have more than one free sample per checkout");
							window.location.href = "/"+removeUrl;
						}
					});

				}
				else {

					//$(samples[0]).parents('table:eq(0)"').parent().prev().prev().find('input').val("1").hide();
				}
				

				
			}
			
		}
		
	},
	
	ShowPromo:function(){
		
		$("body").append("<div id='chPromo'></div>")
		
		CheckoutPromo.PromoBody = $("body").children("#chPromo");
		
		$.get('/v/vspfiles/templates/oralblue/membership_promo.html', function(data) {
			  CheckoutPromo.PromoBody.html(data);
			  CheckoutPromo.PromoBody.prepend("<div class='blackCurtain' style='height:"+ CheckoutPromo.findHeight()+"px;'>&nbsp;</div>");
		});
		
	},
	
	SetCookie: function(){
		var CartID = CheckoutCookie.readCookie("CartID5");
		CheckoutCookie.createCookie("PromoStatus",1,1);
		CheckoutCookie.createCookie("lastCart",CartID,1);
	},

	AddtoCart: function(){
		CheckoutPromo.SetCookie();
		var addProduct = "/ShoppingCart.asp?ProductCode="+window.SampleProductClicked;
		window.location.href = addProduct;
	},


	
	CancelOffer:function(){
		CheckoutPromo.SetCookie();
		CheckoutPromo.PromoBody.hide("slow");			
	},

	findHeight: function(){
		var	scnHei = document.body.clientHeight;
		return(scnHei);
	}
	
	
}





$(document).ready(function(){

jQuery("b:contains('forum membership coupon')").parent().html("<b style='color:red;font-size:11px'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forums.steroid.com signup coupon redeem</b>");

jQuery("#v65-cart-coupon-entry-details-button").css("padding","3px");
jQuery("#v65-cart-coupon-entry-details-div").css("color","#C00");
jQuery("#v65-cart-coupon-entry-details-div").css("font-size","12px");
jQuery("#v65-cart-coupon-entry-details-div").css("font-weight","bold");
jQuery("#v65-cart-coupon-entry-details-input").css("padding","3px");
jQuery("#v65-cart-coupon-entry-details-input").css("font-size","15px");
jQuery("#v65-cart-coupon-entry-details-input").css("display","block");
jQuery("#v65-cart-coupon-entry-details-input").attr("size","17");

	//$.getJSON('/AjaxCart.asp?GetIndex=True', function(data) {	


	$.ajax({
			  url: "/AjaxCart.asp?GetIndex=True",
			  cache: false,
			  dataType: "json",
			  success: function(data) {
	window.data = data;

	

	$.each(data, function(key, val) {					 
						if(key == "Products"){
						 $.each(val, function(k, v) {
							if(v.ProductName in _sample_product){

								if(v.Quantity != 1) {
								// free products increased by user ?
//								alert("free products increased by user ?");
									
									_field_id_str = "#Quantity" + v.ProductIndex;
									jQuery(_field_id_str).val("1");	
									jQuery(_field_id_str).focus();

									if(strLocation.match(/ShoppingCart/i)){
									blockWindow();
									if (!$.browser.msie) {
									jQuery("form[name=form]").append('<input type="hidden" name="btnRecalculate.x" value="52" />');
									jQuery("form[name=form]").append('<input type="hidden" name="btnRecalculate.y" value="9" />');
									}
									document.forms['form'].submit();
									}

									if(strLocation.match(/one-page-checkout/i)){
									blockWindow();
									if (!$.browser.msie) {
									jQuery("form[name=OnePageCheckoutForm]").append('<input type="hidden" name="btnRecalculate.x" value="52" />');
									jQuery("form[name=OnePageCheckoutForm]").append('<input type="hidden" name="btnRecalculate.y" value="9" />');
									}
									document.forms['OnePageCheckoutForm'].submit();
									}
								

								} 


							}
						 });
						}
	});


if(strLocation.match(/ShoppingCart/i) || strLocation.match(/one-page-checkout/i)){
// free products there but price < $70 ?	
if(!order_value_bigger_than_70()){
	if (free_products_in_cart()) {
		// remove free products
		 $.each(data, function(key, val) {					 
					if(key == "Products"){
						$.each(val, function(k, v) {
							if(v.ProductName in _sample_product){ 
								//alert("free products there but price < $70 ?");
								
								removeProductCode = v.ProductIndex;
								//alert("Removing product " + removeProductCode);
								blockWindow();
								window.location.href = "/ShoppingCart.asp?remove=" + removeProductCode;
							}
						});
					}
		});
	}
} else if (free_products_in_cart()) {
	if(!forum_membership_coupon_in_cart()){
		// remove free products 
		$.each(data, function(key, val) {					 
						if(key == "Products"){
							$.each(val, function(k, v) {
								if(v.ProductName in _sample_product){ 
									removeProductCode = v.ProductIndex;
									//alert("free products there but membership coupon gone");
									blockWindow();
									window.location.href = "/ShoppingCart.asp?remove=" + removeProductCode;
								}
							});
						}
		});

	}
}
}

// free products there but membership coupon gone?



// check if multiple free product added?

/*
var free_products_in_cart_array = new Array();

$.each(data, function(key, val) {					 
						if(key == "Products"){
						 $.each(val, function(k, v) {
							if(v.ProductName in _sample_product){
							free_products_in_cart_array.push(v.ProductIndex);	
							}
						 });
						}
});

if (free_products_in_cart_array.length > 1)
{
	blockWindow();
	alert("Removing more than 1 extra free product from cart");
	removeProductCode  = free_products_in_cart_array[0];
	blockWindow();
	window.location.href = "/ShoppingCart.asp?remove=" + removeProductCode;
}

*/

// free products increased by user ?
$.each(data, function(key, val) {					 
						if(key == "Products"){
						 $.each(val, function(k, v) {
							if(v.ProductName in _sample_product){

								if(v.Quantity != 1) {								
									
									_field_id_str = "#Quantity" + v.ProductIndex;
									jQuery(_field_id_str).val("1");	
									jQuery(_field_id_str).focus();

									if(strLocation.match(/ShoppingCart/i)){
									blockWindow();
									jQuery("form[name=form]").append('<input type="hidden" name="btnRecalculate.x" value="52" />');
									jQuery("form[name=form]").append('<input type="hidden" name="btnRecalculate.y" value="9" />');
									document.forms['form'].submit();
									}

									if(strLocation.match(/one-page-checkout/i)){
									blockWindow();
									jQuery("form[name=OnePageCheckoutForm]").append('<input type="hidden" name="btnRecalculate.x" value="52" />');
									jQuery("form[name=OnePageCheckoutForm]").append('<input type="hidden" name="btnRecalculate.y" value="9" />');
									document.forms['OnePageCheckoutForm'].submit();
									}
								

								} 


							}
						 });
						}
	});


		if (window.location.href.match(/shoppingcart.asp/i)) {

				if (forum_membership_coupon_in_cart()) {
					//alert("Before Show Promo");
					if (order_value_bigger_than_70()) {
						//alert("Show Promo");
						CheckoutPromo.init();
					}	
				}
		}

}});

	//});	


	




	
});



function free_products_in_cart() {
var ret = false;
 data = window.data;
 _sample_product = window._sample_product;
	 $.each(data, function(key, val) {					 
					if(key == "Products"){
						$.each(val, function(k, v) {
							if(v.ProductName in _sample_product){ 
								ret =true;
							}
						});
					}
	});
	return ret;
}

function order_value_bigger_than_70(){
data = window.data;
var cartTotal = 0;
_sample_product = window._sample_product;
MinOrderValue = window.MinOrderValue;
var ret = false;

		 $.each(data, function(key, val) {					 
					if(key == "Totals"){
					 $.each(val, function(k, v) {
						cartTotal = parseInt(v.CartTotal.replace("$","").replace(".00",""));
						if (cartTotal >= MinOrderValue)
						{
							//alert("cartTotal >= MinOrderValue");
							ret =true;
						}
					 });
					}
				 });

	return ret;
	
}

function forum_membership_coupon_in_cart() {
	//FORUMREF2011
var ret = false;
 data = window.data;
 _sample_product = window._sample_product;
	 $.each(data, function(key, val) {					 
					if(key == "Products"){
					 $.each(val, function(k, v) {
						if(v.ProductName.toString().match(/forum\ membership\ coupon/g))
						{
							ret =true;
						}
			});
		}
	});
	return ret;
}





function blockWindow()
{
	jQuery("body").css("overflow","hidden");
	jQuery("html").css("overflow","hidden");
	jQuery('<div id="termsAndConditionsOverlay" style="display:none;"><div id="divLoading1" style="text-align:center;"><img src="/v/vspfiles/templates/'+TEMPLATE_DIR+'/ajax-loader.gif"><br>Please wait... We are processing your order. It may take few seconds...</div><div id="loader_msg" style="color:#FFF;text-align:center;font-weight:bold;font-size:13px;"></div></div>').appendTo('body');
	jQuery('#termsAndConditionsOverlay').css("position","absolute");
	jQuery('#termsAndConditionsOverlay').css("background-color","#000");
	jQuery('#termsAndConditionsOverlay').css("top",'0px');
	jQuery('#termsAndConditionsOverlay').css("left",'0px');
	jQuery('#termsAndConditionsOverlay').css("height",jQuery("body").height()+'px');
	jQuery('#termsAndConditionsOverlay').css("width",jQuery("body").width()+'px');
	jQuery('#termsAndConditionsOverlay').css("opacity","0.5");
	jQuery('#termsAndConditionsOverlay').css("z-index","999");
	jQuery('#divLoading').css("position","absolute");
	jQuery('#divLoading').center();
	jQuery('#divLoading').css("opacity","1");
	jQuery('#termsAndConditionsOverlay').show();
}


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
}
