/* 	Order Page Scripts 

	(c) Cequal Products 2008
	
*/

// Function: setSwatch
// Desc: 	Call this to select a swatch on the page
// 			and to deselect all other swatches. It will
// 			load the image, store the product id, and set
//			the price
function selectSwatch(pid) {
	
	//Add by John Malone from FirstScribe
	//This code tells Omniture that a color swatch has been selected.
	swatchOmniture(pid);
	
	// unselect all the swatches
	//var cells = $ES("img","swatch_grid");
	var cells = $$("table.swatch_grid img");
	cells.each(function(cur) {
		cur.removeClass("selected");
		cur.addClass("unselected");
	});
	
	// select the appropriate swatch
	
	var selector = 'img#' + pid;
	var sel = $E(selector);
	if(!$defined(sel)) { // a problem selecting the item, so try the default on the page
		pid = document.forms.data.default_xcartid.value;
		selector = 'img#' + pid;
		sel = $E(selector);
		if(!$defined(sel)) { // still no go
			sel = cells[0]; 
		}
		
	}
	sel.addClass("selected");
	sel.removeClass("unselected");
	
	// save the product id for return vists
	Cookie.set(getCookieId(),pid);
	
	// replace the big product image
	var indexstring = pid;
	var newimg = imgs[indexstring];
	$E('td#large_image img').replaceWith($(newimg));
	
	// replace the fabric name
	var fabricname = product_names[indexstring];
	$E('span#fabric','large_image').setHTML(fabricname);
	
	// load the coupon code possibly stored in a cookie
	var couponCode = Cookie.get("coupon");
	
	// update the checkout button code
	var checkoutBtns = $$('table.finalorderpage a#checkout_btn');
	checkoutBtns.each(function(cur) {
		//Add by John Malone from FirstScribe
		//We need to specify the source from all Cequel sites for the purposes of reporting in the cart
		cur.href = 'http://store.cequal.com/cart.php?mode=add&productid=' + pid + '&amount=1&source=bedlounge';
		if(couponCode != null && couponCode != "") {
			cur.href += "&coupon=" + couponCode;
		}
		//cur.onclick = 'cartOmniture(pid);';
		cur.removeEvent('click');
		cur.addEvent('click',function() { cartOmniture(pid); });
		
		//cur.href = 'http://store.cequal.com/cart.php?mode=add&productid=' + pid + '&amount=1';
		//cur.onclick = 'pageTracker._link(this.href); return false;';
		//cur.removeEvents('click');
		//cur.addEvent('click',function(){pageTracker._link(this.href); return false;})
	});
}

//Add by John Malone from FirstScribe
//This method us used to notify Omniture that a product has been placed in the shopping cart.
function cartOmniture(pid)
{
	s = s_gi('fscrcequalbedlounge');
	s.linkTrackVars = 'products,events';
	s.linkTrackEvents = 'scAdd';
	s.products = ";" + pid;
	s.events = 'scAdd';
	s.tl(this,'o','Add to Cart');
}

//Add by John Malone from FirstScribe
//This method us used to notify Omniture that a color swatch has been selected..
function swatchOmniture(pid)
{
	s = s_gi('fscrcequalbedlounge');
	
	// replaced previous code on 9/18/08 by Ken
	s.linkTrackVars = 'prop3,products,events';
    s.linkTrackEvents = 'prodView';
    s.prop3= pid;
    s.products = ";" + pid;
    s.events = 'prodView';
	
    //s.linkTrackVars = 'prop3';
	//s.prop3= pid;
	
	s.tl(this,'o','Color Swatch View');
}

function getCookieId() {
	var prodSpan = $E('div#infoblock_title span.order_choice');
	if($defined(prodSpan) && prodSpan != false) {
		
		var cookieId = 'lastpid_' + prodSpan.getText();
		if($defined(cookieId)) {
			cookieId = cookieId.replace(' ','_','gm').replace('+','','gm');
			
			return cookieId;
		}
	}
	return 'lastpid';
}

function resizeSwatchGrids() {
	var tables = $ES("table.swatch_grid");
	tables.each(function(cur) {
		var cells = $ES('td',cur);
		var width = cells.length * 57;
		if(width > 420) {
			width = 420/cells.length;
		}
		cur.setStyle('width',width);
	});
}

function selectLastSwatch() {
	
	var cookieID = Cookie.get(getCookieId());
	var urlID = gup('pid');
	
	var pid = "";
	
	if(urlID != null && urlID != false && urlID != "")
		pid = urlID;
	else if(cookieID != false && $("img#" + cookieID) != false)
		pid = cookieID;
	else
		pid = document.forms.data.default_xcartid.value;
	
	selectSwatch(pid);
		
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

