var xmlhttpNF;

// **********************************
// * saveNoteFollowup()
// **********************************

function saveNoteFollowup(strAccID, intBoughtID, intNoteID, strNote, dtFollowUp, strURL,
          strCorpID, intAccessID, strRecID, strSessionID)
{
  var strParams;
  var aryURL, strBaseURL;

  xmlhttpNF = GetxmlhttpNFObject();
  if (intBoughtID == "") { intBouthtID = "0"; }
  if (intNoteID == "") { intNoteID = "0"; }
  if (dtFollowUp == "") { dtFollowUp = "Nothing"; }

  strParams = '<?xml version="1.0" encoding="utf-8"?>\n'
    + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n'
    + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
    + 'xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">\n'
    + ' <soap12:Body>\n'
    + '  <saveNoteFollowUp xmlns="http://www.referenceUSA.com/">\n'
    + '   <strAccID>' + strAccID + '</strAccID>\n'
    + '   <intBoughtID>' + intBoughtID + '</intBoughtID>\n'
    + '   <intNoteID>' + intNoteID + '</intNoteID>\n'
    + '   <strNote>' + strNote + '</strNote>\n'
    + '   <strFollowUp>' + dtFollowUp + '</strFollowUp>\n'
    + '   <strCorpID>' + strCorpID + '</strCorpID>\n'
    + '   <strProdID>BUS</strProdID>\n'
    + '   <intAccessID>' + intAccessID + '</intAccessID>\n'
    + '   <strRecID>' + strRecID + '</strRecID>\n'
    + '   <strSessionID>' + strSessionID + '</strSessionID>\n'
    + '  </saveNoteFollowUp>\n'
    + ' </soap12:Body>\n'
    + '</soap12:Envelope>\n';

  strParams = strParams.replace("&", "%26");
  aryURL = window.location.href.split("/");
  strBaseURL = aryURL[0] + "//" + aryURL[2];
  xmlhttpNF.open("POST", strBaseURL + strURL, false);
  xmlhttpNF.setRequestHeader("Content-Type", "application/soap+xml; charset=utf-8");

  xmlhttpNF.send(strParams);
  var responseXML = xmlhttpNF.responseXML;
//alert("strParams=" + strParams);
//alert("text=" + xmlhttpNF.responseText);

  if (window.ActiveXObject)
  {
    // IE code
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(xmlhttpNF.responseText);
  }
  else
  {
    // code for Mozilla, Firefox, Opera, etc.
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xmlhttpNF.responseText,"text/xml");
  }
  responseXML = xmlDoc.documentElement;
  strRet = responseXML.getElementsByTagName("saveNoteFollowUpResult")[0].firstChild.nodeValue;

  return(strRet);
}

// **********************************
// * saveBoughtRecord()
// **********************************

function saveBoughtRecord(strAccID, intBoughtID, strURL,
          strCorpID, intAccessID, strRecID, strSessionID)
{
  var strParams;
  var aryURL, strBaseURL;

  xmlhttpNF = GetxmlhttpNFObject();
  strParams = '<?xml version="1.0" encoding="utf-8"?>\n'
    + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n'
    + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
    + 'xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">\n'
    + ' <soap12:Body>\n'
    + '  <saveBoughtRecord xmlns="http://www.referenceUSA.com/">\n'
    + '   <intBoughtID>' + intBoughtID + '</intBoughtID>\n'
    + '   <strAccID>' + strAccID + '</strAccID>\n'
    + '   <strCorpID>' + strCorpID + '</strCorpID>\n'
    + '   <strProdID>BUS</strProdID>\n'
    + '   <intAccessID>' + intAccessID + '</intAccessID>\n'
    + '   <strRecID>' + strRecID + '</strRecID>\n'
    + '   <strSessionID>' + strSessionID + '</strSessionID>\n'
    + '   <strUser></strUser>\n'
    + '   <strPass></strPass>\n'
    + '  </saveBoughtRecord>\n'
    + ' </soap12:Body>\n'
    + '</soap12:Envelope>\n';

  strParams = strParams.replace("&", "%26");
  aryURL = window.location.href.split("/");
  strBaseURL = aryURL[0] + "//" + aryURL[2];
  xmlhttpNF.open("POST", strBaseURL + strURL, false);
  xmlhttpNF.setRequestHeader("Content-Type", "application/soap+xml; charset=utf-8");

  xmlhttpNF.send(strParams);
  var responseXML = xmlhttpNF.responseXML;

  if (window.ActiveXObject)
  {
    // IE code
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(xmlhttpNF.responseText);
  }
  else
  {
    // code for Mozilla, Firefox, Opera, etc.
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xmlhttpNF.responseText,"text/xml");
  }
  responseXML = xmlDoc.documentElement;
  strRet = responseXML.getElementsByTagName("saveBoughtRecordResult")[0].firstChild.nodeValue;

  return(strRet);
}

// ******************************************
// * GetxmlhttpNFObject()
// ******************************************

function GetxmlhttpNFObject()
{
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlhttpNF = new xmlhttpNFRequest();
  }
  catch(e)
  {
      // Internet Explorer
    try
    {
      xmlhttpNF = new ActiveXObject("Msxml2.xmlhttp");
    }
    catch (e)
    {
      xmlhttpNF = new ActiveXObject("Microsoft.xmlhttp");
    }
  }
  return xmlhttpNF;
}

// **********************************
// * addNoteSelect()
// **********************************

function addNoteSelect(objSelect, intNoteID, strText)
{
  var intI;

      // add new one with the current last one's data
  if (objSelect.options.length > 0) {
    objSelect[objSelect.options.length] =
        new Option(objSelect.options[objSelect.options.length - 1].text,
        objSelect.options[objSelect.options.length - 1].value, false, false);
    for (intI = (objSelect.options.length - 1); intI > 0; intI--)
    {
      objSelect[intI].text = objSelect[intI - 1].text;
      objSelect[intI].value = objSelect[intI - 1].value;
    }
    objSelect[0].text = strText;
    objSelect[0].value = intNoteID;
  }
  else {
    objSelect[0] = new Option(strText, intNoteID, false, false);
  }
}

// **********************************
// * addNoteTR()
// **********************************

function addNoteTR(strTableID, intNoteID, strModDate, strText, strFollowup, intBoughtID)
{
  var intI;

  var tblBody = document.getElementById(strTableID);
  var newRow = tblBody.insertRow(-1);
  var newCell = newRow.insertCell(0);
  newCell.innerHTML = strModDate;
  newCell.className = "myNotesOff";
  newCell.id = "noteList_" + intNoteID;
  newCell.onClick = function() { changeListClass(eval(document), intNoteID,
    eval("document.all.allNotes.value" + intBoughtID), true); };
/*
  newCell.all.attachEvent("onclick", function(){
    changeListClass(document, intNoteID, document.all.allNotes.value, true);
  });
*/
/*
  newCell.setAttribute("onClick", changeListClass(document, intNoteID, document.all.allNotes.value, true));
*/
  newRow.appendChild(newCell);

  newRow = tblBody.insertRow(-1);
  newCell = newRow.insertCell(0);
  newCell.innerHTML = strText;
  newCell.className = "myNotesOff";
  newCell.id = "noteList_" + intNoteID;
  newCell.setAttribute("onClick", "changeListClass(document, " + intNoteID 
      + ", document.all.allNotes.value, true);");

  if (strFollowup.length > 10) {
    strFollowup = "-- Follow Up on " + strFollowup;
  }
  else {
    strFollowup = "";
  }

  newRow = tblBody.insertRow(-1);
  newCell = newRow.insertCell(0);
  newCell.innerHTML = strFollowup;
  newCell.className = "myNotesOff";
  newCell.id = "noteList_" + intNoteID;
  newCell.setAttribute("onclick", "changeListClass(document, " + intNoteID
        + ", document.all.allNotes.value, true);");
}

// **********************************
// * changeNoteSelect()
// **********************************

function changeNoteSelect(objSelect, intNoteID, strText)
{
  var intI;

  for (intI = 0; intI < objSelect.options.length; intI++)
  {
    if (objSelect[intI].value == intNoteID)
    {
      objSelect[intI].text = strText;
      break;
    }
  }
}

// **********************************
// * changeNoteTR()
// **********************************

function changeNoteTR(strTableID, intNoteID, strModDate, strText, strFollowup)
{
  var intI;

  var tblBody = document.getElementById(strTableID);
  for (intI = 0; intI < tblBody.rows.length; intI++) {
    if (tblBody.rows[intI].innerHTML.indexOf("noteList_" + intNoteID + "_1") > 0) {
      tblBody.rows[intI].firstChild.innerHTML = strModDate;
    }
    else if (tblBody.rows[intI].innerHTML.indexOf("noteList_" + intNoteID + "_2") > 0) {
      tblBody.rows[intI].firstChild.innerHTML = strText;
    }
    else if (tblBody.rows[intI].innerHTML.indexOf("noteList_" + intNoteID + "_3") > 0) {
      if (strFollowup.length > 10) {
        strFollowup = "-- Follow Up on " + strFollowup;
      }
      else {
        strFollowup = "";
      }
      tblBody.rows[intI].firstChild.innerHTML = strFollowup;
    }
  }
}

// **********************************
// * addHidden()
// **********************************

function addHidden(intForm, strID, strVal)
{
  var objNH = document.createElement("input");
  objNH.setAttribute("type", "hidden");
  objNH.setAttribute("name", "hiddenName");
  objNH.setAttribute("id", "hiddenName");
  objNH.setAttribute("value", "14041978");
  document.forms[intForm].appendChild(objNH);
}

// **********************************
// * nowFormatPrint()
// **********************************

function nowFormatPrint()
{
	var date = new Date();
	var d = date.getDate();
	var day = (d < 10) ? '0' + d : d;
	var m = date.getMonth() + 1;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;

	var hh = date.getHours();
  if (hh > 11) {
    strAMPM = "PM";
  }
  else {
    strAMPM = "AM";
  }
  if (hh > 12) {
    hh -= 12;
  }
	m = date.getMinutes();
	var mm = (m < 10) ? '0' + m : m;
	var ss = date.getSeconds();
	ss = (ss < 10) ? '0' + ss : ss;

  var strRet = month + "/" + day + "/" + year + " " + hh + ":" + mm + ":" + ss + " " + strAMPM;
  return(strRet);
}

// **********************************
// * changeListClass()
// **********************************

function changeListClass(objDoc, strNoteID, strAllNotes, bOn)
{
  var ob, strNewNoteID;

  strAllNotes = strAllNotes.replace("~" + strNoteID + "~", "~");
  if (strAllNotes.length > 2) {
    strNewNoteID = strAllNotes.substr(1, strAllNotes.substr(1).indexOf("~"));
  }
  else {
    strNewNoteID = "";
  }

  if (bOn == true) {
    objDoc.all.selectedNote.value = strNoteID;
    ob = objDoc.getElementById("noteList_" + strNoteID + "_1");
    if (ob != null) {
      ob.className = "myNotesOn";
    }
    ob = objDoc.getElementById("noteList_" + strNoteID + "_2");
    if (ob != null) {
      ob.className = "myNotesOn";
    }
    ob = objDoc.getElementById("noteList_" + strNoteID + "_3");
    if (ob != null) {
      ob.className = "myNotesOn";
    }
  }
  else {
    ob = objDoc.getElementById("noteList_" + strNoteID + "_1");
    if (ob != null) {
      ob.className = "myNotesOff";
    }
    ob = objDoc.getElementById("noteList_" + strNoteID + "_2");
    if (ob != null) {
      ob.className = "myNotesOff";
    }
    ob = objDoc.getElementById("noteList_" + strNoteID + "_3");
    if (ob != null) {
      ob.className = "myNotesOff";
    }
  }
  if (strNewNoteID != "") {
    changeListClass(objDoc, strNewNoteID, strAllNotes, false);
  }
}

