
   var redirect_link;

function setLoading(mymsg) {
   setLoadingCursor();
   setLoadingBackground();
   if (mymsg) {
      setLoadingBox(mymsg);
   }
   else {
      setLoadingBox();
   }
}

function unSetLoading() {
   unsetLoadingCursor();
   unsetLoadingBackground();
   unsetLoadingBox();
}

function setLoadingCursor() {
   document.documentElement.className = 'waitCursor';
}

function unsetLoadingCursor() {
   document.documentElement.className = '';
}

function setLoadingBackground() {
   offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
/*
   coverDIV = document.getElementById("cover");
   coverDIV.style.top = offset + 'px';
   coverDIV.style.visibility = "visible";
*/
}

function unsetLoadingBackground() {
/*
   coverDIV = document.getElementById("cover");
   coverDIV.style.visibility = "hidden";
*/
}

function setLoadingBox(mymsg) {
   if (mymsg) {
      interactive_popup("Loading", mymsg, 400, 215);
   }
   else {
      interactive_popup("Loading", "<H3>Loading...</H3>", 400, 215);
   }
}

function unsetLoadingBox() {
   interactive_popup_close();
}

function getWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return(myWidth);
}

function reportissue(width, boxTop) {
      if (screen.width) {
//         boxLeft = (screen.width / 2) - (width / 2);
         boxLeft = (getWidth() / 2) - (width / 2);
      }
      else {
         boxLeft = 215;
      }
   popup_DIV = document.getElementById("reportissue");
   popup_DIV.style.width = width + 'px';
   popup_DIV.style.top = boxTop + offset + 'px';
   popup_DIV.style.left = boxLeft + 'px';
   popup_DIV.style.visibility = "visible";
   popup_DIV.style.overflow = "auto";
}

function email_deal(id, page, product) {
   setLoadingBackground();
   product = unescape(product);
   capindex = get_captcha();
   document.getElementById('interactive_popup_cancel').onclick = function () {javascript:email_deal_cancel(); return false;};
   var headerHTML = "Email Deal";
   var bodyHTML = "<H3>Tell a Friend</H3>"
   + "Please fill out the following info:"
   + "<P>Product Name: <B>" + product + "</B>"
   + "<P>To Email Address: <input name='email_deal.email' type='text' size='45' maxlength='100' VALUE='' id='email_deal.to_email'>"
   + "<P>Comments:"
   + "<P><textarea name='email_deal.comments' cols='40' rows='6' wrap='VIRTUAL' id='email_deal.comments'></textarea><P>"
   + "<P>Your Name: <input name='email_deal.email' type='text' size='45' maxlength='100' VALUE='' id='email_deal.from_name'>"
   + "<P>Your Email (optional): <input name='email_deal.email' type='text' size='45' maxlength='100' VALUE='' id='email_deal.from_email'>"
   + "<P>" + captcha[capindex][0] + " <INPUT TYPE='TEXT' NAME='email_deal.captcha' VALUE='' LENGTH=2 MAXLENGTH=2 SIZE=2 id='email_deal.captcha'>"
   + "<DIV style='color: red; text-decoration: bold;' id='email_deal.captcha_error'></DIV>"
   + "<INPUT TYPE='hidden' NAME='email_deal.id' VALUE=" + id + " id='email_deal.id'>"
   + "<INPUT TYPE='hidden' NAME='email_deal.page' VALUE=" + page + " id='email_deal.page'>"
   + "<P><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Email Deal' OnClick='javascript:email_deal_ok(capindex); return false;'>"
   + "<INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Cancel' OnClick='javascript:email_deal_cancel(); return false;'>";
   interactive_popup(headerHTML, bodyHTML, 650, 215);
}

function email_deal_cancel(id, type) {
   unsetLoadingBackground();
   popup_DIV = document.getElementById("interactive_popup");
   popup_DIV.style.visibility = "hidden";
}

function email_deal_ok(capindex) {
   if (document.getElementById("email_deal.captcha").value == captcha[capindex][1]) {
      page = document.getElementById("email_deal.page").value;
      id = document.getElementById("email_deal.id").value;
      to_email = document.getElementById("email_deal.to_email").value;
      comments = document.getElementById("email_deal.comments").value;
      from_name = document.getElementById("email_deal.from_name").value;
      from_email = document.getElementById("email_deal.from_email").value;
      if (!(to_email.match(/\S+\@\S+\.\S+/i))) {
         document.getElementById("email_deal.captcha_error").innerHTML = "Error: Email address of recipient must be in the form user@host";
      }
      else if ((!(from_email.match(/\S+\@\S+\.\S+/i))) && (!(from_email.match(/^\s*$/i)))) {
         document.getElementById("email_deal.captcha_error").innerHTML = "Error: Email address of sender must be in the form user@host";
      }
      else if (from_name.match(/^\s*$/i)) {
         document.getElementById("email_deal.captcha_error").innerHTML = "Error: Your name must be specified";
      }
      else {
         popup_DIV = document.getElementById("interactive_popup");
         popup_DIV.style.visibility = "hidden";
         setLoadingCursor();
         url = "/email_deal.php";
         if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
         }
         else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
         }
         req.open("POST", url, true);
         req.onreadystatechange = email_deal_finished_prompt;
         req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         req.send("type=" + escape("email_deal") + "&page=" + escape(page) + "&id=" + escape(id) + "&to_email=" + escape(to_email) + "&comments=" + escape(comments) + "&from_name=" + escape(from_name) + "&from_email=" + escape(from_email));
      }
   }
   else {
      document.getElementById("email_deal.captcha_error").innerHTML = "* Incorrect Code";
   }
}

function email_deal_finished_prompt() {
   unsetLoadingCursor();
   if (req.readyState == 4) {
      if (req.status == 200) {
         xmlDoc = req.responseXML.documentElement;
         status = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
         var bodyHTMLButton = "<P><INPUT TYPE='SUBMIT' NAME='OK' VALUE='OK' onClick='javascript:interactive_popup_close(); unsetLoadingBackground(); return false;'>";
         if (status == 0) {
            interactive_popup("Email Deal", "Email sent!" + bodyHTMLButton, 650, 215);
         }
         else {
            interactive_popup("Email Deal", "There was a problem sending your email.  Email not sent." + bodyHTMLButton, 650, 215);
         }
      }
   }
}

function interactive_popup_close() {
   popup_DIV = document.getElementById("interactive_popup");
   popup_DIV.style.visibility = "hidden";
}

function report_error(id, page, product) {
   setLoadingBackground();
   product = unescape(product);
   reportissue(545, 275, 250);
   document.getElementById("report_error.id").value=id;
   document.getElementById("report_error.product").innerHTML="<H2>" + product + "</H2>";
}

function interactive_popup(headerHTML, bodyHTML, width, boxTop, boxLeft) {
   if (!boxLeft) {
      if (screen.width) {
//         boxLeft = (screen.width / 2) - (width / 2);
         boxLeft = (getWidth() / 2) - (width / 2);
      }
      else {
         boxLeft = 215;
      }
   }
   popup_DIV = document.getElementById("interactive_popup");
   popup_DIV.style.width = width;
   popup_DIV_body = document.getElementById("interactive_popup_body");
   document.getElementById("interactive_popup_header_text").innerHTML = "<H2>" + headerHTML + "</H2>";
   popup_DIV.style.width = width + 'px';
   popup_DIV.style.top = boxTop + offset + 'px';
   popup_DIV.style.left = boxLeft + 'px';
   popup_DIV.style.visibility = "visible";
   popup_DIV.style.overflow = "auto";
   popup_DIV_body.innerHTML = bodyHTML;
}

function report_error_cancel(id, mytype) {
   unsetLoadingBackground();
   report_errorDIV = document.getElementById("reportissue");
   report_errorDIV.style.visibility = "hidden";
}

function report_error_ok() {
   if (document.getElementById("report_error.captcha").value == captcha[capindex][1]) {
      if (document.getElementById("report_error.email").value != '') {
      report_errorDIV = document.getElementById("reportissue");
      report_errorDIV.style.visibility = "hidden";
      id = document.getElementById("report_error.id").value;
      mytype = document.getElementById("report_error.issuetype").value;
      comments = document.getElementById("report_error.comments").value;
      if (document.getElementById("report_error.email").value) {
         var email = document.getElementById("report_error.email").value;
      }
      else {
         var email = "";
      }
      url = "/report_problem.php";
      if (window.XMLHttpRequest) {
         req = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
         req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      req.open("POST", url, true);
      req.onreadystatechange = report_error_finished_prompt;
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      setLoadingCursor();
      req.send("id=" + escape(id) + "&type=" + escape(mytype) + "&email=" + escape(email) + "&comments=" + escape(comments));
      }
      else {
         document.getElementById("reportissue.captcha_error").innerHTML = "Email address required for reporting errors.";
      }
   }
   else {
      document.getElementById("reportissue.captcha_error").innerHTML = "* Incorrect Code";
   }
}

function report_error_finished_prompt() {
   if (req.readyState == 4) {
      if (req.status == 200) {
         xmlDoc = req.responseXML.documentElement;
         status = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
         var bodyHTMLButton = "<P><INPUT TYPE='SUBMIT' NAME='OK' VALUE='OK' onClick='javascript:interactive_popup_close(); unsetLoadingBackground(); return false;'>";
         unsetLoadingCursor();
         if (status == 0) {
            interactive_popup("Report Error", "Thank you, your issue has been received and will be addressed." + bodyHTMLButton, 650, 215);
         }
         else {
            interactive_popup("Report Error", "Sorry, there was a problem receiving your request.  Please try again.  If the problem persists, please email jack@bargainjack.com." + bodyHTMLButton, 650, 215);
         }
      }
   }
}

function get_captcha(capindex) {
   if (capindex) {
      return captcha[capindex]['answer'];
   }
   else {
      return rand(captcha.length) - 1;
   }
}

function populate_search_box() {
   document.getElementById('query').value = getParameter("query");
   document.getElementById('daysAgo').value = getParameter("daysAgo");
}

function dealPopUp(id, link, instructions) {
   instructions = unescape(instructions);
   redirect_link = link;
   if (instructions) {
   if (readCookie('disable_dealinfo') != 1) {
   var winHandle = window.open("/dealinfo.php?id=" + id, 'dealinfo', 'noresize,dependent,titlebar=0,screenX=0,screenY=0,left=0,top=0,scrollbars=0,width=500,height=220');
   if (winHandle) {
      popUpsBlocked = false
      window.location.href=redirect_link;
   }
   else {
      if (readCookie('ignore_dealinfo') != 1) {
         popUpPrompt();
         popUpsBlocked = true
      }
      else {
         window.location.href=redirect_link;
      }
   }
   }
   else {
      window.location.href=redirect_link;
   }
   return false;
   }
   else {
      window.location.href=redirect_link;
   }
}

function popUpPrompt() {
   setLoadingBackground();
   document.getElementById('interactive_popup_cancel').onclick = function () {javascript:popup_prompt_cancel(); return false;};
   var headerHTML = "Error: Deal Instructions";
   var bodyHTML = "<H3>Error: Deal Instructions Blocked!</H3>"
   + "Error message: It looks like your popup blocker is preventing a BargainJack shopping instructions pop-up from opening.  "
   + "This pop-up window explains how to get the best price possible for the product that you just clicked on.  It has some "
   + "special instructions that you need to follow for optimum deal sweetness, and stays open while you're at the vendor's site as a convenient reminder."
   + "<P>"
   + "Please add bargainjack.com to your allowed popup URL list inside your browser's pop-up window settings to take advantage of this handy feature."
   + "<P>"
   + "<P><INPUT TYPE='checkbox' NAME='ignore' id='ignore'> Never show me this warning again"
   + "<P><INPUT TYPE='checkbox' NAME='ignore' id='disable'> I never want deal instructions in a pop-up window"
   + "<P><INPUT TYPE='SUBMIT' NAME='OK' VALUE='OK' onClick='javascript:PopUpPromptOK(); return false;'>";
   interactive_popup(headerHTML, bodyHTML, 650, 215);
}

function popup_prompt_cancel() {
   interactive_popup_close();
   unsetLoadingBackground();
   window.location.href=redirect_link;
}

function PopUpPromptOK() {
   var ignore = document.getElementById('ignore').checked;
   var disable = document.getElementById('disable').checked;
   interactive_popup_close();
   unsetLoadingBackground();
   if (ignore) {
      createCookie("ignore_dealinfo", "1", 1000);
   }
   else {
      createCookie("ignore_dealinfo", "0", 1000);
   }
   if (disable) {
      createCookie("disable_dealinfo", "1", 1000);
   }
   else {
      createCookie("disable_dealinfo", "0", 1000);
   }
   window.location.href=redirect_link;
}

function createCookie(name,value,days) {
   if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}

function eraseCookie(name) {
   createCookie(name,"",-1);
}

var subscribeArray;
var subscribeEmail;
var subscribeCount;
var subscribeStatus;

function subscribe_to_newsletter() {
   document.getElementById('interactive_popup_cancel').onclick = function () {javascript:_newsletter_cancel(); return false;};
   var arraycount = 0;
   subscribeCount = 0;
   subscribeStatus = 0;
   subscribeArray = new Array();
   if (document.getElementById(urgent_newsletter_name).checked) {
      subscribeArray[subscribeArray.length] = urgent_newsletter_name;
   }
   if (document.getElementById(daily_newsletter_name).checked) {
      subscribeArray[subscribeArray.length] = daily_newsletter_name;
   }
   subscribeEmail = document.getElementById("email").value;
   if (subscribeArray.length < 1) {
      alert("ERROR: you did not select any newsletters.  Please try again.");
      return(0);
   }
   else if (subscribeEmail.match(/\S+\@\S+\.\S+/)) {
      setLoadingBackground();
      interactive_popup("Newsletter Signup", "<DIV style='text-align: left;'>Please wait (this may take a few seconds)...", 400, 215);
      _newsletter_start();
   }
   else {
      alert("ERROR: the email address you entered is not a valid email address.  Please try again.");
      return(0);
   }
}

function _newsletter_start() {
   if (subscribeCount < subscribeArray.length) {
      _newsletter_submit(subscribeArray[subscribeCount], subscribeEmail);
      subscribeCount++;
   }
   else {
      setLoadingBackground();
      if (subscribeStatus == 0) {
         interactive_popup("Newsletter Signup Complete", "<DIV style='text-align: left;'><B>Signup complete!</B><P>NOTE: You will receive a separate email confirmation for each subscription.  If you have already signed up using this address, you may not receive another signup confirmation.<P>If you do not receive your email confirmation within three hours, please email admin@bargainjill.com with your email address and the list of newsletter subscriptions you selected."
         + "<P><BR><CENTER><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Back to Main' OnClick='javascript:_newsletter_okay(); return false;'></CENTER></DIV>", 400, 215);
      }
      else if (subscribeStatus == 1) {
         interactive_popup("Newsletter Signup Warning ", "<DIV style='text-align: left;'><B>Warning: Email address already subscribed!</B><P>You have already subscribed to one or more newsletters using the email address " + subscribeEmail + ".  If you have not received an email confirmation, we will resend the confirmation.  Otherwise, if you are still having problems, please make sure your spam filters are allowing email from the address admin@bargainjack.com."
         + "<P><BR><CENTER><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Back to Main' OnClick='javascript:_newsletter_okay(); return false;'></CENTER></DIV>", 400, 215);
      }
      else if (subscribeStatus == 2) {
         interactive_popup("Newsletter Signup Error", "<DIV style='text-align: left;'><B>Error adding subscription!</B><P>There was an error when adding your subscription.  Please try again.<P>NOTE: If you continue to get this error, please email admin@bargainjack.com with the following information: (1) the newsletters you've selected, (2) the email address you're using for signup, and (3) the type of browser/operating system you're using."
         + "<P><BR><CENTER><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='OK' OnClick='javascript:_newsletter_cancel(); return false;'></CENTER></DIV>", 400, 215);
      }
   }
}

function _newsletter_submit(name, email) {
   url = "/newsletter/mail.cgi";
   if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("POST", url, true);
   req.onreadystatechange = _newsletter_return;
   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   var request_text = "f=subscribe&list=" + escape(name) + "&email=" + escape(email);
   req.send("f=subscribe&list=" + escape(name) + "&email=" + escape(email));
}

function _newsletter_return() {
   if (req.readyState == 4) {
      if (req.status == 200) {
         xmlDoc = req.responseText;
         if (xmlDoc.match(/has already been subscribed to/)) {
            if (subscribeStatus < 1) {
               subscribeStatus = 1;
            }
         }
         else if (xmlDoc.match(/Please confirm/)) {
         }
         else {
            subscribeStatus = 2;
         }
         _newsletter_start();
      }
      else {
         subscribeStatus = 2;
      }
   }
/*
   if (req.readyState == 4) {
      if (req.status == 200) {
         _newsletter_start();
      }
      else {
         interactive_popup("Newsletter Signup Error", "<DIV style='text-align: left;'>Error: newsletter submission failed.  Please email jack@bargainjack.com with your email address for help."
+ "<P><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='OK' OnClick='javascript:_newsletter_cancel(); return false;'></DIV>", 400, 215);
         unsetLoadingBackground();
         return(1);
      }
   }
   else {
         interactive_popup("Newsletter Signup Error", "<DIV style='text-align: left;'>Error: newsletter submission failed.  Please email jack@bargainjack.com with your email address for help."
+ "<P><INPUT TYPE='SUBMIT' NAME='Submit' VALUE='OK' OnClick='javascript:_newsletter_cancel(); return false;'></DIV>", 400, 215);
         unsetLoadingBackground();
         return(1);
   }
*/
}

function _newsletter_cancel() {
   unsetLoadingBackground();
   popup_DIV = document.getElementById("interactive_popup");
   popup_DIV.style.visibility = "hidden";
}

function _newsletter_okay() {
   unsetLoadingBackground();
   popup_DIV = document.getElementById("interactive_popup");
   popup_DIV.style.visibility = "hidden";
   window.location.href="/";
}

function create_deal_thread(id) {
   var answer = confirm ("This will start a new thread in the forum\n for you to add comments.  Continue?");
   if (answer) {
      setLoading("<H2>Loading...</H2><FONT SIZE=-1>(NOTE: this may take several seconds<BR>to create the new thread, please wait.)</A>");
      var url = "/forums/insert_post.php";
      if (window.XMLHttpRequest) {
         req = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
         req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      req.open("POST", url, true);
      req.onreadystatechange = redirect_or_error;
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send("id=" + escape(id) + "&type=1");
   }
}

function create_one_day_deal_thread(id) {
   var answer = confirm ("This will start a new thread in the forum\n for you to add comments.  Continue?");
   if (answer) {
      setLoading("<H2>Loading...</H2><FONT SIZE=-1>(NOTE: this may take several seconds<BR>to create the new thread, please wait.)</A>");
      var url = "/forums/insert_post.php";
      if (window.XMLHttpRequest) {
         req = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
         req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      req.open("POST", url, true);
      req.onreadystatechange = redirect_or_error;
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send("id=" + escape(id) + "&type=2");
//      req.send("type=2&name=" + escape(name) + "&title=" + escape(title));
   }
}

function redirect_or_error() {
   if (req.readyState == 4) {
      if (req.status == 200) {
         xmlDoc = req.responseXML.documentElement;
         link = xmlDoc.getElementsByTagName("link")[0].childNodes[0].nodeValue;
         unSetLoading();
         location.href = link;
      }
   }
}

