/* Last updated: 10/08/09 */

var bn_searchEnabled = true;
var bn_guideEnabled = true;
var bn_policyLoaded = false;

function bn_isNotEmpty(name) {
	return (name != null) && (name != "");
}
function bn_setCookie(c_name,c_value,c_domain,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	if (c_domain != null && c_domain != "") {
		document.cookie = c_name+"="+escape(c_value)+";domain="+c_domain+";path=/"+((expiredays==null)?"":";expires="+exdate.toGMTString());
	} else {
		document.cookie = c_name+"="+escape(c_value)+";path=/"+((expiredays==null)?"":";expires="+exdate.toGMTString());
	}
}
function bn_getOrderInfo() {
	if (typeof(bnOrderId) != "undefined" && bn_isNotEmpty(bnOrderId))
		baynote_tag.attrs.purchaseId = bnOrderId;
	if (typeof(bnOrderTotal) != "undefined" && bn_isNotEmpty(bnOrderTotal))
		baynote_tag.attrs.totalPurchases = parseFloat(bnOrderTotal);
	if (typeof(bnOrderDetails) != "undefined" && bn_isNotEmpty(bnOrderDetails))
		baynote_tag.attrs.purchaseDetails = bnOrderDetails;
}
function bn_getMediaInfo() {
	if (typeof(bnMediaDuration) != "undefined" && bn_isNotEmpty(bnMediaDuration)) {
		baynote_tag.attrs.expectedDuration = bnMediaDuration;
	}	
}
function bn_setAttrs(){
	var divs = document.getElementsByTagName("div");
	if (!divs) return;
	
	for (var i = 0; i < divs.length; i++) {
		if (!divs[i]) return;
			if (divs[i].id == "bnProductID") {
				var url = document.getElementById("bnProductID").innerHTML;
				url = url + "";				
				if(bn_isNotEmpty(url)) {
					baynote_tag.url = url;
				}
			}
	}
	
}
function bn_showObserver() {
	/* 1. set customer id */
	bn_customerId = "vitacost";
	/* 2. set customer code */
	bn_code = "www";
	var bn_locHref = window.location.href;
	if (bn_locHref.indexOf("https://") == 0) {
		baynote_tag.server = "https://" + bn_customerId + "-" + bn_code + ".baynote.net";
	} else {
		baynote_tag.server = "http://" + bn_customerId + "-" + bn_code + ".baynote.net";
	}
	baynote_tag.customerId = bn_customerId;
	baynote_tag.code = bn_code;
	baynote_tag.type = "baynoteObserver";
	/* 3. set customer domain (optional) */
	baynote_globals.cookieDomain = "vitacost.com";
	/* 4. collect purchase info (optional) */ 
	bn_getOrderInfo();
	bn_getMediaInfo();
	bn_setAttrs();
	baynote_tag.show();
}
function bn_checkPolicy() {
	if (!bnPolicy.get("guide","ok")) {
	    bn_guideEnabled = false;
	    bn_setCookie('bn_guide','false',baynote_globals.cookieDomain,90);
	} else {
	    bn_setCookie('bn_guide','true',baynote_globals.cookieDomain,90);
	}
	if (!bnPolicy.get("search","ok")) {
	    bn_searchEnabled = false;
	    bn_setCookie('bn_search','false',baynote_globals.cookieDomain,90);
	} else {
	   bn_setCookie('bn_search','true',baynote_globals.cookieDomain,90);
	}
	   bn_policyLoaded = true;
}
function bn_waitForPolicy() {
	if(typeof(bnResourceManager) != "undefined" && bnResourceManager != null) {
		bnResourceManager.waitForResource("Policy", function() { bn_checkPolicy(); });
	}
}
if (typeof(baynote_tag)!="undefined") {
	bn_showObserver();
	bn_waitForPolicy();
}


