// <script>

/*
  i-MARK, Inc. SmartCAT API 4.0 Copyright 2000
  Author:  Jon Scott-Sheldon
  Date:    July 12, 2000
  Revised: Sept 05, 2000
  see /sc_app/sc_api_usage.asp for documentation
*/

var DESC = "desc";
var FILT = "filt";
var SPEC = "spec";
var SRCH = "srch";
var RFQ = "rfq";
var DOC_URI = "../sc_app/sc_api_usage.asp";
var HELP_WIND_ATTRIB = "resizable, scrollbars, height=600, width=900";
var NUM_FILT_ARGS = 4;
var NUM_SPEC_ARGS = 5;

// supporting functions internal to API only
// if javascript supported the concept of a private function the below six functions would be so
// due to Netscape's inferior browser these functions are declared first
function isValidFamID(theID){return (isEmpty(theID) || isNaN(theID) || theID < 0) ? false : true;}
function isValidParamStr(theParam){var arr_Param = theParam.split("="); return (!isNaN(arr_Param[0]) && (parseInt(arr_Param[0]) > 0) && !isEmpty(arr_Param[1])) ? true : false;}
function isEmpty(value){return (value == "" || value == null || value == "undefined" || value.search(/^\s*$/) != -1) ? true : false;}
function imkLTrim(theString){return (!isEmpty(theString)) ? theString.substr(theString.search(/\S/)) : theString;}
function imkRTrim(theString){return (!isEmpty(theString)) ? theString.substr(0, theString.search(/\S\s*$/) + 1) : theString;}
function imkTrim(theString){return imkRTrim(imkLTrim(theString));}

// start of API functions
function InitAPI()
{
  var isURL = false;
  eval("if (window.catalog_url && !window.isEmpty(catalog_url)) isURL = true;");
  if (!isURL)
  {
    alert("The SmartCAT catalog url must be defined. View the API documentation.");
    Help();
  }
}

function GetDescription(famID, distID, targetFrame)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < 1)
  {
    Help(DESC);
    return;
  }

  targetFrame = (targetFrame == "self") ? self : top;
  famID = imkTrim(famID+"");

  if (!isValidFamID(famID+""))
  {
    alert("The famID argument has not been set or is not valid.");
    return;
  }
  
  targetFrame.location.href = catalog_url + "sc_app/sc_description.asp?famID=" + famID + "&code=" + distID;
}

function GetFilter(famID, distID, targetFrame, uom)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < NUM_FILT_ARGS)
  {
    Help(FILT);
    return;
  }

  var theParams = "";
  
  if (arguments.length > NUM_FILT_ARGS)
  {
    for (var i = NUM_FILT_ARGS, argLen = arguments.length; i < argLen; i++)
    {
      if (!isValidParamStr(arguments[i]))
      {
        alert("Malformed filter parameter argument.");
        return;
      }
      theParams += arguments[i] + "|";
    }
  }
  
  targetFrame = (targetFrame == "self") ? self : top;
  famID = imkTrim(famID+"");

  if (!isValidFamID(famID+""))
  {
    alert("The famID argument has not been set or is not valid.");
    return;
  }
  
  var QueryString = "famID=" + famID + "&code=" + distID;
  if (!isEmpty(uom))
    QueryString += "&uom=" + uom;
  QueryString += "&StateVar=" + escape("" + theParams);
  
  targetFrame.location.href = catalog_url + "sc_app/sc_filter.asp?" + QueryString;
}

function GetSpec(famID, distID, targetFrame, prtNo, uom)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < NUM_SPEC_ARGS)
  {
    Help(SPEC);
    return;
  }

  var theParams = "";
  
  if (arguments.length > NUM_SPEC_ARGS)
  {
    for (var i = NUM_SPEC_ARGS, argLen = arguments.length; i < argLen; i++)
    {
      if (!isValidParamStr(arguments[i]))
      {
        alert("Malformed spec parameter argument.");
        return;
      }
      theParams += imkTrim(arguments[i]) + "|";
    }
  }
  
  targetFrame = (targetFrame == "self") ? self : top;
  famID = imkTrim(famID+"");
  prtNo = imkTrim(prtNo+"");
  uom = imkTrim(uom);
  
  if (!isValidFamID(famID+""))
  {
    alert("The famID argument has not been set or is not valid.");
    return;
  }

  if (isEmpty(uom))
  {
    alert("The uom argument has not been set.");
    return;
  }
  
  if (isEmpty(prtNo))
  {
    alert("The prtNo argument has not been set.");
    return;
  }
  
  var QueryString = "famID=" + famID + "&prtNo=" + prtNo + "&code=" + distID;
  if (!isEmpty(uom))
    QueryString += "&uom=" + uom;
  QueryString += "&KeyVar=" + escape( "" + theParams);
    
  targetFrame.location.href = catalog_url + "sc_app/sc_spec.asp?" + QueryString;
}

function GetRFQ(distID, targetFrame)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  targetFrame = (targetFrame == "self") ? self : top;
  targetFrame.location.href = catalog_url + "sc_app/sc_RFQ.asp?code=" + distID;
}

function GetSearch(distID, strSearch, targetFrame)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  targetFrame = (targetFrame == "self") ? self : top;
  targetFrame.location.href = catalog_url + "sc_app/sc_search.asp?code=" + distID;
}

function Help(theFunction)
{
  var theHelpWindow;
  
  switch (theFunction)
  {
    case "" + DESC:
      theHelpWindow = window.open(DOC_URI + "?u=" + DESC, "usage", HELP_WIND_ATTRIB + "")
      break;
    case "" + FILT:
      theHelpWindow = window.open(DOC_URI + "?u=" + FILT, "usage", HELP_WIND_ATTRIB + "")
      break;
    case "" + SPEC:
      theHelpWindow = window.open(DOC_URI + "?u=" + SPEC, "usage", HELP_WIND_ATTRIB + "")
      break;
    case "" + SRCH:
      theHelpWindow = window.open(DOC_URI + "?u=" + SRCH, "usage", HELP_WIND_ATTRIB + "")
      break;
    case "" + RFQ:
      theHelpWindow = window.open(DOC_URI + "?u=" + RFQ, "usage", HELP_WIND_ATTRIB + "")
      break;
    default:
      theHelpWindow = window.open(DOC_URI + "", HELP_WIND_ATTRIB + "")
  }
}

function GetRFQEx(distID, prtNo, GetDesc, targetFrame)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  targetFrame = (targetFrame == "self") ? self : top;
  
  var partNo = prtNo + "~~1~~" + GetDesc;
  //var partNo = prtNo + " - " + selVoltage + "~~1~~" + GetDesc;
  targetFrame.location.href = catalog_url + "sc_app/sc_RFQEx.asp?Items=" + escape(partNo) + "&vid=" + escape(distID);
}

//function GetRFQExAsco(distID, famID, prtNo, strUOM, targetFrame)
//{
 // if (isEmpty(catalog_url))
 // {
  //  alert("Specify the SmartCAT catalog URL.");
   // Help();
   // return;
  //}
  
  //targetFrame = (targetFrame == "self") ? self : top;

 // targetFrame.location.href = catalog_url + "sc_app/sc_RFQAsco.asp?famID=" + famID + "&vid=" + escape(distID) + "&prtNo=" + escape(prtNo);
//}
