//TODO: add general comments here



function createAjaxRequestObject() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
    try {
      xmlHttpReq.overrideMimeType("text/xml"); //DEBUG: is this necessary?
    } catch (f) {}
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (g) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (h) {
        alert("Your browser does not support AJAX!"); //DEBUG
        return false;
      }
    }
  }
  if (!xmlHttp) {
    alert("Your browser does not support AJAX!"); //DEBUG
    return false;
  }
  return xmlHttp;
}



function ApplyForSN(SNForm) {

  strSubmit="";  okay=true;
  if (SNForm) {
    for (i = 0; i < SNForm.elements.length; i++) {
      formElem = SNForm.elements[i];
      switch (formElem.type) {
        case "text":  case "hidden":
          if (formElem.name == 'email' && (formElem.value.length < 1 || !formElem.value.match(/.+@.+\..+/))) {
            alert("Please enter a valid e-mail address before submitting.");
            document.getElementById('SNEmail').focus();
            okay = false;
          }
          strSubmit += formElem.name + '=' + escape(formElem.value.replace(/'/g,"")) + (i<SNForm.elements.length-1 ? '&' : '');
          break;
      }
    }
  }

  if (okay) {
    // make XML HTTP Request object
    var xmlHttp = createAjaxRequestObject();

    // create XML HTTP Request Handler
    xmlHttp.onreadystatechange = function() {
      if(xmlHttp.readyState == 4) {
        // parse out the strange and seemingly inconsequential error message
        document.getElementById("stocknotification").innerHTML = xmlHttp.responseText.replace(/<p>Session Replace.+?<\/p>/,"");
      }
    }

    xmlHttp.open("POST","oxid.php",true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(strSubmit);

    document.getElementById("stocknotification").innerHTML = "Hold on...";
  }

}

function ApplyForStockNotification(stockNotificationFormName) {
  SNForm = document.forms[stockNotificationFormName];
  ApplyForSN(SNForm);
  return false;
}



function StockNotificationStart() {
  SNDiv = document.getElementById('stocknotification');
  if (SNDiv) {

    // attach event handler to the form dynamically
    SNForm = document.forms['stocknotificationform'];
    if (SNForm) {
      try {
        SNForm.attachEvent('onsubmit', function(event) { event.returnValue=false; ApplyForStockNotification('stocknotificationform'); });  // IE
      } catch (e) {
        SNForm.setAttribute('onSubmit', "return ApplyForStockNotification('stocknotificationform');");  // good browsers
      }
    }

    // make XML HTTP Request object
    var xmlHttp = createAjaxRequestObject();

    // create XML HTTP Request Handler
    xmlHttp.onreadystatechange = function() {
      if(xmlHttp.readyState == 4) {
        // parse out the strange and seemingly inconsequential error message
        document.getElementById("stocknotification").innerHTML = xmlHttp.responseText.replace(/<p>Session Replace.+?<\/p>/,"");
      }
    }

    strSubmit="";
    if (SNForm) {
      for (i = 0; i < SNForm.elements.length; i++) {
        formElem = SNForm.elements[i];
        switch (formElem.type) {
          case "text":  case "hidden":
            if (formElem.name == 'cl')
              strSubmit += 'cl=stocknotificationstatus' + (i<SNForm.elements.length-1 ? '&' : '');
            else if (formElem.name == 'fnc')
              strSubmit += '';
            else
              strSubmit += formElem.name + '=' + escape(formElem.value) + (i<SNForm.elements.length-1 ? '&' : '');
            break;
        }
      }
    }

    xmlHttp.open("POST","oxid.php",true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(strSubmit);
  }
}





// create the button dynamically, so that it doesn't appear if there is a JavaScript problem
/*
SNDiv = document.getElementById('stocknotification');
if (SNDiv) {
  SNDiv.innerHTML = '<input type="button" value="Testing a new MenEssentials.com feature." id="StartSN" />';
}
// attach event handlers to the button dynamically, if it exists
StartSNButton = document.getElementById('StartSN');
if (StartSNButton) {
  try {
    StartSNButton.attachEvent('onclick', StockNotificationStart);  // IE
  } catch (e) {
    StartSNButton.setAttribute('onClick', "StockNotificationStart();");  // good browsers
  }
}
*/

/* copied code from something else, use as an example if we decide to add functionality in bulk on category pages
for (i=0 ; i<document.forms.length ; i++) {
  var formNameRegExp = /basket.+/;
  if (document.forms[i].name.match(formNameRegExp)) {
    //DEBUG: alert("Form name matched basket criterion: "+document.forms[i].name);
    try {
      document.forms[i].attachEvent('onsubmit', function(event) { event.returnValue=false; ajaxAddToCartFromCategoryPage(event.srcElement.name); });  // IE
      //DEBUG: alert("Attached event handler to form '"+document.forms[i].name+"'.");
    } catch (e) {
      document.forms[i].setAttribute('onSubmit', "return ajaxAddToCartFromCategoryPage('"+document.forms[i].name+"');");  // good browsers
      //DEBUG: alert("Attached event handler to form '"+document.forms[i].name+"':\n\n"+document.forms[i].getAttribute('onSubmit'));
    }
  }
}
*/

