/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2011 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var CheckoutChanges = {
	LeftNavElementId: 'content_left',
	ContentContainerId: 'content_area',
	INITIALIZED: false,
	IS_LOGGED_IN: false,
	
	checkLogin: function(){
		new Ajax.Request('/myaccount.asp',{
			method: 'get',
			encoding: 'ISO-8859-1',
			onSuccess: function(transport){
				if(transport.responseText.indexOf('Welcome to your account') != -1){
					CheckoutChanges.IS_LOGGED_IN = true;
				}	
				if(CheckoutChanges.INITIALIZED){
					CheckoutChanges.updateUrls();
				}
				else{
					document.observe('CheckoutChanges:initialized', CheckoutChanges.updateUrls);
				}
			}			
		});
		
	},
	
	updateUrls: function(){
		if(CheckoutChanges.IS_LOGGED_IN){//If they are already logged in just take them to the checkout page.
			$('support_member_checkout_link').writeAttribute('href', '/one-page-checkout.asp');
			$('returning_customer_checkout_link').writeAttribute('href', '/one-page-checkout.asp');
		}		
	},
	
	initialize: function () {
		var url = window.location.href;
		var onCheckoutPage = null;
		
		onCheckoutPage = CheckoutChanges.isOnCheckoutPage(url);
		//check if on checkout or shopping cart page
		if (onCheckoutPage) {			
			if	(url.toLowerCase().indexOf('one-page-checkout.asp') != -1) {
				var mySavedBillingSelectDefaultOption = $$('select[name="My_Saved_Billing"] > option:first-child');
				var mySavedShippingSelectDefaultOption = $$('select[name="My_Saved_Shipping"] > option:first-child');

				if (mySavedBillingSelectDefaultOption.first() != null) {
					mySavedBillingSelectDefaultOption.first().update('Choose Address...');
				}

				if (mySavedShippingSelectDefaultOption.first() != null) {
					mySavedShippingSelectDefaultOption.first().update('Choose Address...');
				}
					
			}
			
			if (CheckoutChanges.isOnCheckoutPage(window.location.href) || 1==1) {//TODO: Remove
				var cartForm = $$('form[name="form"]').first();
				var cartProceedAndLoginTable = $$('div#content_area table[width="420"]').first();
				var proceedToCheckoutButtons = new Element('div', {id: 'cartProceedAndLoginTable'});
				var paddedTablesCollection = $$('table[cellpadding="5"], table[cellpadding="1"], table[cellpadding="10"]');
				
				
				
				//get rid of shopping cart padded tables' cell padding
				paddedTablesCollection.each(function(paddedTable){
					paddedTable.writeAttribute({'cellpadding':'0'});
				});
				
				$(CheckoutChanges.ContentContainerId).setStyle({'width':'745px', 'padding':'0px 0px 0px 0px'});
									
				if (cartProceedAndLoginTable != undefined && cartProceedAndLoginTable != null) {
					cartProceedAndLoginTable.update(''); 
				}
				
				if (cartForm != undefined && cartForm != null) {
					cartForm.insert({after: proceedToCheckoutButtons});
					proceedToCheckoutButtons.update(''
						+'<div id="checkout_login">'
						+'	<div id="checkout_login_head"></div>'
						+'	<div id="checkout_login_body">'
						+'		<div class="checkout_login_col" id="checkout_login_col1">'
						+'			<div>'
						+'				<a id="support_member_checkout_link" href="/supporting_membership_discount_a/224.htm" title="Supporting Member Login" class="checkout_login_col_top"></a>'						
						+'			</div>'
						+'			<div class="checkout_login_col_btm">'
						+'				<img id="checkout_login_col1_card" src="//assets2.assets.redhatsocietystore.com/v/vspfiles/templates/RedHats2/images/template/checkout_login_col1_card.jpg" alt="Membership Card" />'
						+'				<div class="checkout_login_col_btm_text">If you are a current supporting member, please click the Checkout button below.</div>'
						+'				<a id="support_member_checkout_link" href="/supporting_membership_discount_a/224.htm" title="Supporting Member Login" class="checkout_login_btn_container">'
						+'					<img src="//assets2.assets.redhatsocietystore.com/v/vspfiles/templates/RedHats2/images/template/cart_checkout_btn.gif" alt="Supporting Member Login" title="Supporting Member Login" />'
						+'				</a>'
						+'			</div>'
						+'		</div>'
						+'		<div class="checkout_login_separator"></div>'
						+'		<div class="checkout_login_col" id="checkout_login_col2">'
						+'			<div class="checkout_login_col_top"></div>'
						+'			<div class="checkout_login_col_btm">'
						+'				<div class="checkout_login_col_btm_text">If you have never ordered with us, please click the Checkout button below to create an account.</div>'
						+'				<div class="checkout_login_btn_container">'
						+'				  <form name="Proceed_To_Checkout_Form" method="post" action="https://www.redhatsocietystore.com/login.asp">'
						+'					<input type="image" id="btn_checkout_guest_bottom" src="//assets2.assets.redhatsocietystore.com/v/vspfiles/templates/RedHats2/images/template/cart_checkout_btn.gif" name="btn_checkout_guest">'
						+'				  </form>'
						+'				</div>'
						+'			</div>'
						+'		</div>'
						+'		<div class="checkout_login_separator"></div>'
						+'		<div class="checkout_login_col" id="checkout_login_col3">'
						+'			<div class="checkout_login_col_top"></div>'
						+'			<div class="checkout_login_col_btm">'
						+'				<div class="checkout_login_col_btm_text">If you have placed an order with us before, please click the Checkout button below.</div>'
						+'				<a id="returning_customer_checkout_link" href="/-a/229.htm" title="Returning Customers" class="checkout_login_btn_container">'
						+'					<img src="//assets2.assets.redhatsocietystore.com/v/vspfiles/templates/RedHats2/images/template/cart_checkout_btn.gif" alt="Returning Customers" title="Returning Customers" />'
						+'				</a>'
						+'			</div>'
						+'		</div>'
						+'		<div class="clearfloat"></div>'
						+'	</div>'
						+'	<div id="checkout_login_foot">'
						+'		<input style="margin: 0px;" name="IsAGift" value="Y" onclick="javascript:IsAGiftClick(this);" type="checkbox" id="IsAGift">'
						+'		<label for="IsAGift">Show gift options during checkout</label>'
						+'	</div>'
						+'</div>'
					+ ''); 
				}
			}
		}
		CheckoutChanges.INITIALIZED = true;
		Event.fire(document, 'CheckoutChanges:initialized');
	},

	isOnCheckoutPage: function(url) {
		if (url == null) {
			return false;
		}
		
		return ((url.toLowerCase().indexOf('shoppingcart.asp') != -1) ||
				(url.toLowerCase().indexOf('articles.asp?id=224') != -1) ||
				(url.toLowerCase().indexOf('a/224.htm') != -1) ||
				(url.toLowerCase().indexOf('one-page-checkout.asp') != -1));
	}
}

/* Load once the DOM has loaded */
try {
	CheckoutChanges.checkLogin();
	document.observe('dom:loaded', CheckoutChanges.initialize);
} catch (error) {
	/*ignore*/
}
