function ReadCookie(aName){
  var allCookie, cookieVal, length, start, end;
  var devlText=false;
  if (aName=='CheckDevl') {
    aName='DEVL';
    devlText=true;
  }
  cookieVal="";
  name=aName+"=";
  allCookie=document.cookie;
  length=allCookie.length;
  if (length>0){
    start=allCookie.indexOf(name, 0);
    if (start!=-1){
      start+=name.length;
      end=allCookie.indexOf(";",start);
      if (end==-1) {
        end=length;
      }
      cookieVal=unescape(allCookie.substring(start,end));
    }
  }
  len=cookieVal.length;
  if (aName=="FirstName") {
    if (len>0) {
      if (cookieVal!='DEFAULT') {
        document.write('<div class="welcome_back_text" style="margin-top: 3px;" align="left">Welcome Back, ' + cookieVal); 
        document.write('!&nbsp;&nbsp;');
        document.write('<A Class="welcome_back_link" HREF="javascript:ChangeURL(\'FirstForm=NewReg\');">Sign Out</A></div>');
      }
    }
    else {
        document.write('<div class="welcome_back_text" style="margin-top: 3px;" align="left">'); 
        document.write('<A Class="welcome_back_link" HREF="https://ecom.lynnmed.com/?firstform=CUSBUY">');
        document.write('Click here</A> to sign in or register.</div>');
    }
  }
  else if (aName=="DEVL") {
    if (len>0) {
      if ((cookieVal=='ON') && (devlText)) {
        document.write('<CENTER>');
        document.write('<TABLE width=95%><tr>');
        document.write('<td height=25 valign=middle align=center>');
        document.write('<B><font size=2 color=red face=arial>');
        document.write('WARNING - Development Database');
        document.write('</font></B>');
        document.write('</td>');
        document.write('</tr></table>');
        document.write('</CENTER>');
      }
    }
  }
  else if (aName=="InCartNum") {
    if (len>0) {
		if (cookieVal=='1') {
		document.write(cookieVal + ' Item'); 
		}
		else {
	     document.write(cookieVal + ' Items'); 
		}
     }
    else {
      document.write('0 Items'); 
    }
  }

  return(cookieVal);
}

function cleanBadCookies(){
  var the_date = new Date("December 31, 2000"); 
  var the_cookie_date = the_date.toGMTString(); 
  var the_cookie = "SessionKey=000000;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
  document.cookie = the_cookie; 

  the_cookie = "InCartNum=0;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
  document.cookie = the_cookie; 

  var the_cookie = "Login=guest;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
  document.cookie = the_cookie; 

  var the_cookie = "USECOOKIE=TRUE;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
  document.cookie = the_cookie;

  var the_cookie = "FirstName=GUEST;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
  document.cookie = the_cookie; 
}

function init(calledBy){
  detectOS();
  if (calledBy == "index") {
    document.cookie = "test=ok";
    if (document.cookie.indexOf("test=ok")!=-1) openPopWinOnce()
    else alert("Lynn Medical has determined that your internet browser contains settings (Cookies are disabled) that may prevent our system from completing online orders.   These settings will not affect your ability to view all of the pages in our website so please continue to browse our site.  \n\n If you would like to place an order, please call us at 800-860-9389 and our customer service representatives will be happy to help you.");
    
    if(navigator.appName.substring(0,8)=="Netscape"){
      if (parseInt(navigator.appVersion)<5) alert("Lynn Medical has determined that you are running an old internet browser that we no longer support (Netscape 4.x or older) \n\nThese settings will not affect your ability to view many of the pages in our website (such as recipies) but will prevent you from creating online orders (shopping carts), using our Find a Store option, etc.  \n\nIf you like, you can update your browser by going to www.netscape.com and clicking the Downloads link, or by going directly to http://channels.netscape.com/ns/browsers/download.jsp \n\nIf you would rather not update your browser but would still like to place an order with Lynn Medical, please call us at 800-860-9389 and our customer service representatives will be happy to help you.");  
    } 

    cleanBadCookies();
  }
  else if (document.formName == "home") {
    cleanBadCookies();
  } 

  detectParameters();
}

function detectOS(){
var OSName="Unknown OS";

  if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
  if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
  if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; 
  if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; 

 // if (OSName=="MacOS") {
 //   alert("Lynn Medical has determined that you are accessing our site from a Mac computer.   We are currently in the process of updating our software and Macintosh users will not be able to use some of our online functionality (such as Online Shopping, the Store Locator, etc). \n\nIf you would like to place an order with us, please call us at 800-860-9389 and our customer service representatives will be happy to help you. \n\nThis system functionality will be back online by mid-January so please check back with us often.");
 // }

  return(OSName);
}

function detectParameters(){ 
  var urlParamStr=location.href.split("?");
  var urlParamItems, urlParamID;
  var i;

  if (urlParamStr[1]){
    urlParamItems=urlParamStr[1].split("&");
    i=0;
    while (urlParamItems[i]) {
      urlParamID=urlParamItems[i].split("=");
      if((urlParamID[0]=='SenderID') && (urlParamID[1])) {
        WriteCookie(urlParamID[0],urlParamID[1],'lynnmed.com','/', null ,0);
      }
      i++;
    }
  }
}

function WriteCookie(name,value,domain,path,expires,secure){ 
	var CookieVal, CookError;
	CookieVal=CookError="";
      expires = new Date("December 31, 2023");
	if (name){
		CookieVal=CookieVal+escape(name)+"=";
	}
	if (value){
            if (name='RedirectURL') CookieVal=CookieVal+value
            else CookieVal=CookieVal+escape(value);
	}
	if (domain){
		CookieVal=CookieVal+"; domain="+domain;
	}
	if (path){
		CookieVal=CookieVal+"; path="+path;
	}
	if (expires){
		CookieVal=CookieVal+"; expires="+expires.toGMTString();
	}
	if (secure){
		CookieVal=CookieVal+"; secure="+secure;
	}
	else{
		CookError=CookError+"Write failure";
	}
	document.cookie=CookieVal;  // sets the cookie
}

function previousPage(){
  history.go(-1);
}

function closeThisOpenThat(page){
  window.opener.ChangeURL(page);
  window.close();
}

function RedirectURL(URL) {
  WriteCookie('RedirectURL', URL ,'lynnmed.com','/', null ,0); 
  javascript:void(parent.SubmitClick('SearchLink','')); 
}  

function ChangeURL(firstForm) {
  var URL, ffStr;
  var ds = ReadCookie('DEVL');
  var dsStr = 'datasource=lynnprod';
//  var baseURL = 'https://www.lynnmed.com/?';
  var baseURL = 'http://ecom.lynnmed.com/?';

  if ((firstForm == "FirstForm=Account") || 
      (firstForm == "FirstForm=NewReg")) {
    baseURL = 'https://ecom.lynnmed.com/?';
  }

  if (firstForm == "FirstForm=NewReg") {
    var the_date = new Date("December 31, 2000"); 
    var the_cookie_date = the_date.toGMTString(); 
    var the_cookie = "SessionKey=000000;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
    document.cookie = the_cookie; 

    var the_cookie = "Login=guest;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
    document.cookie = the_cookie; 

    var the_cookie = "B2B=FALSE;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
    document.cookie = the_cookie;

    var the_cookie = "FirstName=GUEST;path=/;domain=lynnmed.com;expires=" + the_cookie_date; 
    document.cookie = the_cookie; 
  }

 // if (detectOS()=="MacOS") {
 //   exit;
 // }
  
  if ((ds.length>0) && (ds!='OFF')) {  
    dsStr = 'datasource=devl'; 
  }
 
  if (firstForm.length>0) ffStr = '&' + firstForm;

  URL = baseURL + dsStr + ffStr;

  if(navigator.appName.substring(0,8)=="Netscape"){
    if (parseInt(navigator.appVersion)<5) {
      alert("Lynn Medical has determined that you are running an old internet browser that we no longer support (Netscape 4.x or older) \n\nThese settings will not affect your ability to view many of the pages in our website (such as recipies) but will prevent you from creating online orders (shopping carts), using our Find a Store option, etc.  \n\nIf you like, you can update your browser by going to www.netscape.com and clicking the Downloads link, or by going directly to http://channels.netscape.com/ns/browsers/download.jsp \n\nIf you would rather not update your browser but would still like to place an order with Lynn Medical, please call us at 800-860-9389 and our customer service representatives will be happy to help you.");  
      exit;
    }
  }

  document.body.style.cursor='wait';
  location.href = URL;
  document.body.style.cursor='wait';
}

function openWindow(name, page) {
  if (name=="popup") {
    OpenWin = this.open(page, "PopupWindow", "width=350,height=250,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
  }
  else if (name=="tellafriend") {
    OpenWin = this.open(page, "TellWindow",   "width=400,height=465,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
  }
  else if (name=="nutrition") {
    OpenWin = this.open(page, "CtrlWindow",   "width=475,height=250,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
  }
}


function gotosite(pageName)
{
	window.top.location.href = "http://www.lynnmed.com" + pageName;
}

function NS4resize() {
	if (NS4sIW != window.innerWidth || NS4sIH != window.innerHeight) window.location.reload();
}
function getExplorerVersion() {
	var ieVers = parseFloat(navigator.appVersion);
	if( navigator.appName != 'Microsoft Internet Explorer' ) return ieVers;
	var tempVers = navigator.appVersion;
	var i = tempVers.indexOf( 'MSIE ' );
	if( i >= 0 ) {
		tempVers = tempVers.substring( i+5 );
		ieVers = parseFloat( tempVers ); 
	}
	return ieVers;
}

function handleOnClick(formName, defaultVar)
{
    if (formName.KEYWORDSFO.value == defaultVar)
    {
        formName.KEYWORDSFO.value = "";
    }
}


function handleOnBlur(formName, defaultVar)
{
   if ((formName.KEYWORDSFO.value.length==0) || (formName.KEYWORDSFO.value==null)) {
        formName.KEYWORDSFO.value = defaultVar;
    }
}

function handleEnterKeyPress(formName, defaultVar)
{
  var userEnteredValue = formName.KEYWORDSFO.value;
      userEnteredValue = userEnteredValue.replace(/ /g,"%20");

      if (userEnteredValue == defaultVar)
      {
          userEnteredValue = "";
      }

	if (userEnteredValue == "")
	{
		alert("Please specify a string to search");
	}
	else
	{
          ChangeURL('FirstForm=SEARCH&Keyword=' + userEnteredValue);
	}
}

