//<!--
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)xmlhttp=new XMLHttpRequest();
// code for IE6, IE5
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 


var parseXML = function(text){
  if (window.DOMParser){
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text.trim(),"text/xml");
  }
  else{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(text.trim());
  }
  
  return xmlDoc;
}
  
     function right(e) {
      if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2))
		return false;
	  else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 || event.button == 3)) {
		return false;
	  }
		return true;
	 }

     //document.onmousedown=right;
     //document.onmouseup=right;
     //if (document.layers) window.captureEvents(Event.MOUSEDOWN);
     //if (document.layers) window.captureEvents(Event.MOUSEUP);
     //window.onmousedown=right;
     //window.onmouseup=right;

	

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}


function mouseXY(prod,e)
{
 var posx = 0;
 var posy = 0;
 if (!e) var e = window.event;
 if (e.pageX || e.pageY)
 {
 posx = e.pageX;
 posy = e.pageY;
 }
 else if (e.clientX || e.clientY)
 {
 posx = e.clientX + document.body.scrollLeft;
 posy = e.clientY + document.body.scrollTop;
 }
 // posx and posy contain the mouse position relative to the document
  var viewportwidth;
  var viewportheight;
 
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined'){
      viewportwidth = window.innerWidth;
      viewportheight = window.innerHeight;
  }
 
  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
       viewportwidth = top.document.documentElement.clientWidth;
       viewportheight = top.document.documentElement.clientHeight;
  }
  // older versions of IE
  else{
       viewportwidth = top.document.getElementsByTagName('body')[0].clientWidth;
       viewportheight = top.document.getElementsByTagName('body')[0].clientHeight;
  }
 
 divBoxHeight = parseInt(top.window.document.getElementById('divProductInfo').style.height);
 divBoxWidth = parseInt(top.window.document.getElementById('divProductInfo').style.width);
 top.window.parent.document.getElementById('divProductInfo').style.left=posx + 325;
 if(325 + posx + divBoxWidth > viewportwidth){
  top.window.document.getElementById('divProductInfo').style.left = viewportwidth - (90 + divBoxWidth);
 }

 if(300 + posy +  divBoxHeight > screen.height){
  top.window.document.getElementById('divProductInfo').style.top = screen.height - (200 + divBoxHeight);
 }
 else{
  top.window.document.getElementById('divProductInfo').style.top=posy + 125;
 }
 getProductSummary(prod);
 top.window.document.getElementById('divProductInfo').className = "";
}


// Error handler for the asynchronous functions.
var displayError = function(statusCode, statusMsg){
   alert('Status: ' + statusCode + ', ' + statusMsg);
}


var scrollAllergyFacts = function(f){
 ColdFusion.navigate('dspAllergyFacts.cfm?intFact='+f,'divAllergyFacts');
}

String.prototype.trim = function(){
 return this.replace(/^\s+|\s+$/g,"");
}

var isNumeric = function(sText){
   var validChars = "0123456789.";
   var isNumber=true;
   var chr;
   for (i=0; i<sText.length && isNumber==true; i++){ 
     chr = sText.charAt(i); 
     if (validChars.indexOf(chr) == -1){
      isNumber = false;
     }
   }
   return isNumber;
}


Array.prototype.findIdx = function(value){
 for (var i=0; i < this.length; i++){
  if (this[i] == value){
   return i;
  }
 }
}

//Popup Window
openWindow = function(content,w,h){
 window.name = "originalWindow";
 newWindow = window.open(content,'newWindow','scrollbars=1,resize=1,width='+w+',height='+h);
 newWindow.focus();
}

 var changeFontSize = function(div,font){
  document.getElementById(div).className = font;
 }

 
var displayIngredient = function(i){
 var strWndName = 'wdnIngredientDef';
 var bExists = true;
 
 try{
  ColdFusion.Window.getWindowObject(strWndName);
 }
 catch(err){
  bExists = false;
 }
 
 if(bExists){
  ColdFusion.navigate('/members/ingredient_definition.cfm?ingredient='+i,strWndName+'_body');
 }
 else{
  ColdFusion.Window.create(strWndName,'','/members/ingredient_definition.cfm?ingredient='+i,{center:true,draggable:false,height:400,width:600,modal:true});
 }
 
 if(document.getElementById('divFFHS')){
   document.getElementById('divFFHS').className = 'hidden';
   ColdFusion.Window.onHide(strWndName,showHealthScore);
 }
 
 ColdFusion.Window.getWindowObject(strWndName).show();
} 


var showHealthScore = function(){
  document.getElementById('divFFHS').className = '';
}


var openNonMemberTour = function(){
 var strWndName = 'wdnNonMemberTour';
 var bExists = true;
 
 try{
  ColdFusion.Window.getWindowObject(strWndName);
 }
 catch(err){
  bExists = false;
 }
 
 if(bExists){
  ColdFusion.navigate('/public/nonMemberTour.html',strWndName+'_body');
 }
 else{
  ColdFusion.Window.create(strWndName,'Quick Tour','/public/nonMemberTour.html',{x:200,y:0,draggable:false,height:400,width:470,modal:true});
 }
 
 ColdFusion.Window.getWindowObject(strWndName).show();
 
 ColdFusion.Window.onHide('wdnNonMemberTour',destroyNonMemberTour);
} 

var destroyNonMemberTour = function(){
 ColdFusion.Window.destroy('wdnNonMemberTour',true);
}


var openMemberTour = function(){
 var strWndName = 'wdnMemberTour';
 var bExists = true;
 
 try{
  ColdFusion.Window.getWindowObject(strWndName);
 }
 catch(err){
  bExists = false;
 }
 
 if(bExists){
  ColdFusion.navigate('/members/memberTour.html',strWndName+'_body');
 }
 else{
  ColdFusion.Window.create(strWndName,'Quick Tour','/members/memberTour.html',{x:200,y:0,draggable:false,height:400,width:470,modal:true});
 }
 
 ColdFusion.Window.getWindowObject(strWndName).show();
 
 ColdFusion.Window.onHide('wdnMemberTour',destroyMemberTour);
} 

var destroyNonMemberTour = function(){
 ColdFusion.Window.destroy('wdnNonMemberTour',true);
}


var destroyMemberTour = function(){
 ColdFusion.Window.destroy('wdnMemberTour',true);
}


var destroyWindow = function(win){
 ColdFusion.Window.destroy(win,true);
}


var openAJAXModal = function(name,loc,w,h,center,top,left){
 if(!w){
  w = 400;
 }
 if(!h){
  h = 300;
 }
 
 if(center == null){
  center = true;
 }
 
 var strWndName = name;
 var bExists = true;
 
 try{
  ColdFusion.Window.getWindowObject(strWndName);
 }
 catch(err){
  bExists = false;
 }
 
 if(bExists){
  ColdFusion.navigate(loc,strWndName+'_body');
 }
 else{
  ColdFusion.Window.create(strWndName,'',loc,{y:top,x:left,center:center,draggable:false,height:h,width:w,modal:true});
 }
 
 if(document.getElementById('divFFHS')){
   document.getElementById('divFFHS').className = 'hidden';
   ColdFusion.Window.onHide(strWndName,showHealthScore);
 }
 
 ColdFusion.Window.getWindowObject(strWndName).show();
}


var countChars = function(box,div){
 document.getElementById(div).innerHTML = document.getElementById(box).value.length;
} 
 
 
function urlValue(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];
} 


var adClick = function(id,ip){
  oGeneral = new GeneralFunctions();
  oGeneral.setAdClick(id,ip);
}



var getURLParam = function(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 

var addEmailInput = function(ct){
   var strEmail1 = "";
   var strEmail2 = "";
   var strEmail3 = "";
   var strEmail4 = "";
   var strEmail5 = "";
   
   if(document.getElementById('txtEmail1')){
     strEmail1 = escape(document.getElementById('txtEmail1').value);
   }
   if(document.getElementById('txtEmail2')){
     strEmail2 = escape(document.getElementById('txtEmail2').value);
   }
   if(document.getElementById('txtEmail3')){
     strEmail3 = escape(document.getElementById('txtEmail3').value);
   }
   if(document.getElementById('txtEmail4')){
     strEmail4 = escape(document.getElementById('txtEmail4').value);
   }
   if(document.getElementById('txtEmail5')){
     strEmail5 = escape(document.getElementById('txtEmail5').value);
   }
   ColdFusion.navigate('/members/dspFriendEmailInput.cfm?count='+ct+'&txtEmail1='+strEmail1+'&txtEmail2='+strEmail2+'&txtEmail3='+strEmail3+'&txtEmail4='+strEmail4+'&txtEmail5='+strEmail5,'divFriendEmail');

}

var tellAFriend = function(){
   ColdFusion.Ajax.submitForm('frmTellAFriend','/members/actTellAFriend.cfm',tellAFriendResponse);
}

var tellAFriendResponse = function(r){
  if(parseXML(r).getElementsByTagName("status")[0].childNodes[0].nodeValue == "Success"){
    destroyWindow('wndTellFriend');
	openAJAXModal('wndTellFriend','/members/dspTellAFriendResponse.cfm',200,200);
  }
  else{
    loopErrors(parseXML(r).getElementsByTagName("messages")[0].getElementsByTagName("message"));
  }
}


var loopErrors = function(xml){
 
  $(".error").each(function(){
   this.innerText = "";
   this.setAttribute("class","hidden");
  });


  for(i=0;i<xml.length;i++){
    document.getElementById(xml[i].getElementsByTagName("field")[0].childNodes[0].nodeValue).innerText = xml[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
    document.getElementById(xml[i].getElementsByTagName("field")[0].childNodes[0].nodeValue).setAttribute("class", "error");
  }
}


var validateEmail = function(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   return reg.test(address);
}

//-->


