	var $j = jQuery.noConflict();

	getSession = function() {
		// read the session cookie
		var sessionCookie = $j.cookie('sess');

		// if the cookie is present, then return it
		if (sessionCookie) {
			return sessionCookie;	
		}
		else {
			// set a random number
			var rand = Math.round(Math.random() * 1000000000);
		
			// set the cookie
			$j.cookie('sess', rand, {path: '/', domain: '.virginmobileusa.com'});
			
			// return number
			return rand;
		}
	}
	
	getUserStatus = function() {
		var userStatus = "logged out";
		
		if ($j.cookie('u_log')) {
			userStatus = "logged in";
		}
		
		return userStatus;
	}
	
	scrollPage = function(pageSection) {
		// console.log('scrollPage');
		// $j.scrollTo({ top:9999999, left:0 });
	}
	
	resizeContainer = function(newWidth, newHeight) {
		if (newWidth > -1) {
			$j('#flashContainer').width(newWidth);
		}
		if (newHeight > -1) {
			$j('#flashContainer').height(newHeight);
		}

		setMainContentHeight();

		if (typeof(self.parent.resizeIFrame) != 'undefined') {
			if (newWidth > -1) { newWidth = newWidth + 15; }
			
			if ($j('#page_wrapper').length) {
				var iframeContentHeight = $j('#page_wrapper').height();
				if (iframeContentHeight > newHeight) {
					newHeight = iframeContentHeight;	
				}
			}
			
			self.parent.resizeIFrame(newWidth,newHeight+212);
		}
	}

	resizeIFrame = function(newWidth, newHeight) {
		if (newWidth > -1) {
			$j('#TB_iframeContent').width(newWidth);
			$j('#TB_window').width(newWidth).css('marginLeft',(newWidth/2)*-1);
		}
		if (newHeight > -1) {
			$j('#TB_iframeContent').height(newHeight);
			setMainContentHeight();
		}
	}
	

	openPlanDetail = function(planID,fromLayer) {

		if (!fromLayer) {
			tb_show(null,'/plan-detail-flash.html?planid='+planID+'&height=500&width=907&TB_iframe=true',false);
		} 
		else {
			window.location.href = '/plan-detail-flash.html?planid='+planID;
		}		
	}
		
	openPhoneDetail = function(phoneID,fromLayer) {
		if (!fromLayer) {
			tb_show(null,'/phone-detail-flash.html?phoneid='+phoneID+'&height=500&width=907&TB_iframe=true',false);
		} 
		else {
			window.location.href = '/phone-detail-flash.html?phoneid='+phoneID;
		}		
	}
	
	
	openPlanCompare = function(fromLayer) { 

		if (!fromLayer) {
			tb_show(null,'/compare-plan-flash.html?height=500&width=907&TB_iframe=true',false);
		} 
		else {
			window.location.href = '/compare-plan-flash.html';
		}
	}
		
	openCompare = function(fromLayer) { 

		if (!fromLayer) {
			tb_show(null,'/compare-phone-flash.html?height=500&width=907&TB_iframe=true',false);
		} 
		else {
			window.location.href = '/compare-phone-flash.html';
		}
	}


	changePhone = function(phoneSKU, legalHTML) {
		// console.log('phoneSKU = ',phoneSKU,' | legalHTML = ',legalHTML);		
		$j('#BVQAFrame').attr('src','http://answers.virginmobileusa.com/answers/5166/product/'+phoneSKU+'/questionshome.htm?format=embedded');
		$j('#footnotes').html(legalHTML);
		setTimeout(resize, 0);
		setTimeout("BVcheckQALoadState()", 15000);
	}

	changePlan = function(planSKU, legalHTML) {
		// console.log('planSKU = ',planSKU,' | legalHTML = ',legalHTML);
		$j('#BVQAFrame').attr('src','http://answers.virginmobileusa.com/answers/5166/product/'+planSKU+'/questionshome.htm?format=embedded');
		$j('#footnotes').html(legalHTML);
		setTimeout(resize, 0);
		setTimeout("BVcheckQALoadState()", 15000);
	}




	cart=new Object();
		contractPhones = [];
		noContractPhones = [];	
		contractPlans = [];
		noContractPlans = [];		
		accessories = [];

		cart.noContractPhones = noContractPhones.join();
		cart.contractPhones = contractPhones.join();
		cart.contractPlans = contractPlans.join();
		cart.noContractPlans = noContractPlans.join();
		cart.accessories = accessories.join();
	

	goToCart = function(callingFromLayer) {
		var myCart = getCart();
		
		if (myCart.contractPhones.length) {
			// https://selfcare.helio.com/onlineshop/OrdServPort.jsp?ctc=24&dpn=HDW-AP820GM2K&plid=PALLIN500
			if (callingFromLayer) {
				top.location = 'https://www2.virginmobileusa.com/onlineshop/OrdServPort.jsp?ctc=24&dpn='+myCart.contractPhones+'&plid='+myCart.contractPlans;
			}
			else {
				window.location.href = 'https://www2.virginmobileusa.com/onlineshop/OrdServPort.jsp?ctc=24&dpn='+myCart.contractPhones+'&plid='+myCart.contractPlans;	
			}
			
		}
		else if (myCart.noContractPhones.length) {
			if (callingFromLayer) {
				top.location = 'http://www1.virginmobileusa.com/order/prepareShoppingcart.do';	
			}
			else {
				window.location.href = 'http://www1.virginmobileusa.com/order/prepareShoppingcart.do';	
			}
		}
	}

	
	addToCart = function(contractType,itemType,SKU,callingFromLayer)
	{
		myCart = getCart();
		
		if(contractType == 'prepaid' && itemType == 'phone')
		{
			noContractPhones.push(SKU)
			cart.noContractPhones = noContractPhones.join();
			
			//empty contract phones
			contractPhones = [];
			cart.contractPhones = contractPhones.join();
			
			//empty contract plans
			contractPlans = [];
			cart.contractPlans = contractPlans.join();			
		}
		else if(contractType == 'postpaid' && itemType == 'phone')
		{
			contractPhones.push(SKU)
			cart.contractPhones = contractPhones.join();
			
			//empty no contract phones
			noContractPhones = [];
			cart.noContractPhones = noContractPhones.join();
		}
		else if(contractType == 'postpaid' && itemType == 'plan')
		{
			contractPlans = [SKU];
			cart.contractPlans = contractPlans.join();
			
			//empty no contract phones
			noContractPhones = [];
			cart.noContractPhones = noContractPhones.join();
		}
		
		// set the cart cookie
		var cartCookieVal = $j.compactJSON(cart);
		$j.cookie('u_crt', cartCookieVal, {path: '/', domain: '.virginmobileusa.com'});
		
		// open the cart
		openCart(SKU,itemType,callingFromLayer);
	}
	
	openCart = function(sku,itemType,fromLayer) {
		$j.scrollTo({ top:0, left:0 });

		if (!fromLayer) {
			tb_show(null,'/cart.html?sku='+sku+'&itemType='+itemType+'&height=500&width=907&TB_iframe=true',false);
		} 
		else {
			window.location.href = '/cart.html?sku='+sku+'&itemType='+itemType+'&height=500&width=907&TB_iframe=true';
		}		
	}	
		
	removeFromCart = function(contractType,itemType,SKU)
	{
		myCart = getCart();
		
		if(contractType == 'prepaid' && itemType == 'phone')
		{
			noContractPhones.removeItems(SKU);
			cart.noContractPhones = noContractPhones.join();
		}
		else if(contractType == 'postpaid' && itemType == 'phone')
		{
			contractPhones.removeItems(SKU);
			cart.contractPhones = contractPhones.join();
		}
		else if(contractType == 'postpaid' && itemType == 'plan')
		{
			contractPlans.removeItems(SKU);
			cart.contractPlans = contractPlans.join();
		}
		
		// set cart cookie
		var cartCookieVal = $j.compactJSON(cart);
		$j.cookie('u_crt', cartCookieVal, {path: '/', domain: '.virginmobileusa.com'});
	}
	
	removeAllFromCart = function(contractType,itemType)
	{
		myCart = getCart();
		
		if(contractType == 'prepaid' && itemType == 'phone')
		{
			noContractPhones = [];
			cart.noContractPhones = noContractPhones.join();
		}
		else if(contractType == 'postpaid' && itemType == 'phone')
		{
			contractPhones = [];
			cart.contractPhones = contractPhones.join();
		}

		// set cart cookie
		var cartCookieVal = $j.compactJSON(cart);
		$j.cookie('u_crt', cartCookieVal, {path: '/', domain: '.virginmobileusa.com'});
	}	
	
	
	getCart = function() { 
		// get the cookie
		if ($j.cookie('u_crt')) {
			var cartCookie = $j.cookie('u_crt');
			cartCookie = $j.secureEvalJSON(cartCookie);

			// set the cookie values to local vars
			if (cartCookie.contractPhones) { contractPhones = cartCookie.contractPhones.split(','); }
			if (cartCookie.noContractPhones) { noContractPhones = cartCookie.noContractPhones.split(','); }
			if (cartCookie.contractPlans) { contractPlans = cartCookie.contractPlans.split(','); }
			if (cartCookie.noContractPlans) { noContractPlans = cartCookie.noContractPlans.split(',');	}
			accessories = [];
		}

		// convert to object params
		cart.noContractPhones = noContractPhones.join();
		cart.contractPhones = contractPhones.join();
		cart.contractPlans = contractPlans.join();
		cart.noContractPlans = noContractPlans.join();
		cart.accessories = accessories.join();

		// return the cart
		return cart;
	}
	
	closeLayer = function(section) { 
        self.parent.tb_remove(section);
	}		
	
	
	Array.prototype.removeItems = function(itemsToRemove) {

		if (!/Array/.test(itemsToRemove.constructor)) {
			itemsToRemove = [ itemsToRemove ];
		}
		var j;
		for (var i = 0; i < itemsToRemove.length; i++) {
			j = 0;
			while (j < this.length) {
				if (this[j] == itemsToRemove[i]) {
					this.splice(j, 1);
				} else {
					j++;
				}
			}
		}
	}


	// SHARETHIS
	function shareThisPage(url, title) {
		if (!url) {
			url = window.location;	
		}
		if (!title) {
			title = document.title;	
		}
		
		// console.log(url,' | ',title);

		var s = SHARETHIS.addEntry({
			url: url,
			title: title
		}, {button:false,popup:true});
		s.popup()
		return false;
	}	