
setTimeout("storeHistory();",500);
function storeHistory(){
	var lwcHistory = location.href;
	if ((lwcHistory.indexOf('login_main_page') == -1) 
		&& (lwcHistory.indexOf('register_main_page') == -1)
		&& (lwcHistory.indexOf('user_message_main_page') == -1)
		&& (lwcHistory.indexOf('sitedown_main_page') == -1)
		&& (lwcHistory.indexOf('wc_login_main_page') == -1)
		&& (lwcHistory.indexOf('wc_register_main_page') == -1)
		&& (lwcHistory.indexOf('wc_user_message_main_page') == -1)
		&& (lwcHistory.indexOf('article_comment_main_page') == -1)
		&& (lwcHistory.indexOf('emailfriend_main_page') == -1)){
		setCookie("lwcHist",lwcHistory,1);
	}
}
var exitCountdown = 5;
var exitURL = "";
var expireDate = new Date();
var curDate = new Date();
var runCheckCart = false;
expireDate.setFullYear(2008,05,25);

if (expireDate > curDate){
	document.observe('dom:loaded', function(e){
		$$('a').invoke('observe', 'click', checkCart);
	});
}
function checkCart(e){
	if (runCheckCart){
		var aTag = Event.element(e);
		var notOurs = false;
		var doCheck2 = true;
		var checkValue = aTag.href;
		//alert("Cart Items = " + cartItems);
		if (cartItems > 0){
			if ((checkValue.match("/common/clickthru/") != null) && (checkValue.match("X=") != null)){
				doCheck2 = false;
				var xValue = checkValue.substring(checkValue.indexOf("X=") + 2);
				if (xValue.match("http:") != null || xValue.match("https:") != null){
					doCheck2 = true;
				}
			}
			if (doCheck2){
				if (
					(checkValue.match(location.protocol + "//" + location.host) == null)
					&& (checkValue.match("https://lifeway.s3.amazonaws.com") == null)
					&& (checkValue.match("http://lifeway.edgeboss.net") == null)
					&& (checkValue.match("http://support.lifeway.com") == null)
					&& (checkValue.match("http://www.lifeway.com/") == null)
					&& (checkValue.match("http://lifeway.com/") == null)
					&& (checkValue.match("https://www.lifeway.com/") == null)
					&& (checkValue.match("https://lifeway.com/") == null)
					&& (checkValue.match("javascript:") == null)
					&& (checkValue.match("mailto:") == null)
					&& (checkValue != "#")
					){
						notOurs = true;
				}
			}
			if (notOurs){
				//alert("leaving lifeway\ncurrent domain:" + document.domain + "\nurl:" + aTag.href);
				Event.stop(e);

				exitURL = aTag.href;
				//show window
				var content = "/e/exit_check";
				var options = {
					title: 'Shopping Pop-up'
					,width: 506
					,params: 'url=' + aTag.href
				};
				Modalbox.show(content,options);
			}
		}
	}
}
function checkExitForm(){
	var email = $('txtEmail').value;
	var reason = $('selReason')[$('selReason').selectedIndex].value;
	var comments = $('txtComments').value;
	
	//alert("email:" + email + "\nreason:" + reason + "\ncomments:" + comments);
	
	var validEmail = true;
	var validReason = true;
	var validComments = true;
	var message = "";
	
	if (email.match(/\w/) == null){
		validEmail = false;
		message += "\nPlease enter an email address";
	}else if (email.match(/^.+@[^\.].*\.[a-z]{2,}$/) == null){
		validEmail = false;
		message += "\nPlease enter a valid email address";
	}
	if (reason == "0"){
		validReason = false;
		message += "\nPlease choose a Reason for Leaving";
	}
	
	if (!(validEmail && validReason && validComments)){
		alert("The following item(s) need your attention:\n" + message);
	}else{
		//submit info to server
		var frmOptions = {
			params: Form.serialize('frmExitCheck')
			, afterLoad: function(){EmailComplete();}
		};
		Modalbox.show('/e/exit_check', frmOptions);
		
	}
	return false;
}
var exitIntervalId = 0;
function EmailComplete(){
	Modalbox.show("<div>your message has been sent and you will be redirected in ... " + exitCountdown + "</div>", {height:50, transitions:false, resizeDuration:0});
	interval = setInterval('doExitCountdown()',1000);
}
function doExitCountdown(){
	exitCountdown--;
	if (exitCountdown < 0){
		//do nothing
	}else if (exitCountdown == 0){
		clearInterval(exitIntervalId);
		exitCheckContinue();
	}else{
		Modalbox.show("<div>your message has been sent and you will be redirected in ... " + exitCountdown + "</div>", {height:50, transitions:false, resizeDuration:0});
	}
}
function exitCheckContinue(){
	//alert("exitURL:'" + exitURL);
	if (exitURL.length > 0){
		window.location = exitURL;
	}
}
/*************************************************************
**************************************************************/
var arrMyInfo;
function getInfo(){
	if (typeof($j) != "undefined"){
		$j.post("/solo/getInfo.asp",{p:pr,m:me,a:ar,e:ev}, function(data){
			if (data.length > 0){
				setMyInfo(data);
			}
		});
	}
}
var ip = "";
function setMyInfo(data){
	var uh = "";
	var doSniff = "N";
	arrMyInfo = data.split("|");
	ip = arrMyInfo[0];
	uh = arrMyInfo[1];
	doSniff = arrMyInfo[2];
	
	if (uh == "OK"){
		updateHeader();
	}
}
function updateHeader(){
	var lnkRegister = $('lnkRegister');
	var lnkLogin = $('lnkLogin');
	if (lnkRegister){
		lnkRegister.innerHTML = "profile";
	}
	if (lnkLogin){
		lnkLogin.innerHTML = "logout";
	}	
}
getInfo()
/*************************************************************
**************************************************************/
/*************************************************************
	Load Shopping Cart
**************************************************************/
function getCart(){
	if (typeof($j) != "undefined"){
		$j.post("/cart/xml/summary/", buildCart, "xml");
	}
}
var cartInfo = "";
function buildCart(xml){
	cartInfo = xml;
	var count = $j("Props > Count",xml).text();
	if (count > 0){
		viewcart = $j("Props > View",xml).text();
		mini = '<span class="text_space">';
		mini += '<a href="'+viewcart+'">'+ count +' item</a>';
		mini += '</span>';
		mini += '<a href="'+viewcart+'"><img src="/lwc/images/cart_top.gif" alt="view cart" border="0"/></a>';
		mini += '<a href="'+viewcart+'">view cart</a>';
	}else{
		mini = '<span class="text_space">0 item</span>';
		mini += '<img src="/lwc/images/cart_top.gif" alt="view cart" border="0"/>';
		mini += '<span class="HeaderHideViewCart">view cart</span>';
	}
	$j('#HeaderTop').html(mini);
}
getCart();
/*************************************************************
**************************************************************/

if (typeof(adInfoJS) != "undefined"){
	if (adInfoJS.length > 0){
		if (typeof(arrAdInfo) != "undefined"){
			if (arrAdInfo.length > 0){
				document.write(adInfoJS);
			}
		}
	}
}

/*************************************************************
load polls once the page is ready
**************************************************************/
$j(document).ready(loadPolls);
function loadPolls() {
	if (typeof(pollsURL) != "undefined") {
		if (pollsURL.length > 0 ) {
			$j("#poll_content").load(pollsURL);
		}
	}
}
$j(document).ready(function(){
	loadTypeAhead();
});
function loadTypeAhead(){
	var service_url = "http://www2.lifeway.model/ahead/";
	if (location.host.match(/\.com/) != null){
		if (ip.indexOf("172.16") >= 0 || ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("192.168") >= 0 || ip == "209.136.112.131" || ip == "216.0.60.131"){
			service_url = "http://www2.lifeway.com/ahead/";
		}else{
			service_url = "http://a729.g.akamai.net/f/729/16507/1d/www2.lifeway.com/ahead/";
		}
	}
	
	//var service_url = "http://www.lifeway.test/test.json";
	if ($j('#SearchForm #Ntk').length > 0){
		$j('#inputbox').typeahead({
			field:'Ntt'
			,url:service_url
			,dataType:'jsonp'
			,onDefaultEnter: function(e){
				if (search(document.SearchForm)){
					$j('#SearchForm').submit();
				}
			}
			,onSelectedItem: function(index, char_count, chars_typed, val, e){
				var e = e || window.event;
				var key = e.charCode || e.keyCode;
				if (key != 39){ // Do not sumbit if right arrow is used
					$j('#inputbox').typeahead_clear();
					if (search(document.SearchForm)){
						var tahType = 'keyword';
						switch ($j('#SearchForm select#Ntk option:selected').val()){
							case "All":
								tahType = 'keyword';
								break;
							case "Title":
								tahType = 'title';
								break;
							case "Product Number":
								tahType = 'isbn';
								break;
							case "Author/Contributor":
								tahType = 'author';
								break;
						}
						objOmni.typeaheadEvent(this, tahType, char_count, index + 1, chars_typed, val);
						$j('#SearchForm').submit();
					}
				}
			}
		});
		setupConfig();
		$j('#SearchForm #Ntk').bind('change', function(){
			setupConfig();
			$j('#inputbox').typeahead_load();
		});
	}else{
		$j('#inputbox').typeahead({
			field:'sp'
			,url:service_url
			,postData:{
				'type':'keyword'
				,'format':'jsonp'
				,'ref':'com'
			}
			,dataType:'jsonp'
			,onDefaultEnter: function(e){
				if (checkForm(document.SearchForm)){
					$j('#SearchForm').submit();
				}
			}
			,onSelectedItem: function(index, char_count, chars_typed, val, e){
				var e = e || window.event;
				var key = e.charCode || e.keyCode;
				if (key != 39){ // Do not sumbit if right arrow is used
					$j('#inputbox').typeahead_clear();
					if (checkForm(document.SearchForm)){
						objOmni.typeaheadEvent(this, 'keyword', char_count, index + 1, chars_typed, val);
						$j('#SearchForm').submit();
					}
				}
			}
		});
	}
	$j(document).bind('click', function(){
		$j('#inputbox').typeahead_clear();
	});
}
function setupConfig(){
	switch ($j('#SearchForm select#Ntk option:selected').val()){
		case "All":
			$j('#inputbox').typeahead_configure({
				postData:{'type':'keyword','format':'jsonp','ref':'com'}
				,onLoadList: function(filter){return true;}
			});
			break;
		case "Title":
			$j('#inputbox').typeahead_configure({
				postData:{'type':'title','format':'jsonp','ref':'com'}
				,onLoadList: function(filter){return true;}
			});
			break;
		case "Product Number":
			$j('#inputbox').typeahead_configure({
				postData:{'type':'isbn','format':'jsonp','ref':'com'}
				,onLoadList: function(filter){return false;}
			});
			break;
		case "Author/Contributor":
			$j('#inputbox').typeahead_configure({
				postData:{'type':'author','format':'jsonp','ref':'com'}
				,onLoadList: function(filter){return true;}
			});
			break;
	};
}