// <script>

/*
  i-MARK, Inc. SmartCAT API 1.0 Copyright 2000
  Author: Jon Scott-Sheldon
  Date:   July 12, 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 CFG = "cfg";
var DOC_URI = "http://jamesbury.smartcats.com/sc_app/sc_api_usage.asp";
var HELP_WIND_ATTRIB = "resizable, scrollbars, height=600, width=900";

// 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, 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;
}

function GetConfig(famID, targetFrame)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < 1)
  {
    Help(CFG);
    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_configurator.asp?famID=" + famID;
}

function GetFilter(famID, targetFrame, uom)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < 3)
  {
    Help(FILT);
    return;
  }

  var theParams = "";
  
  if (arguments.length > 3)
  {
    for (var i = 3, 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;
  if (!isEmpty(uom))
    QueryString += "&uom=" + uom;
  QueryString += "&StateVar=" + theParams;
  
  targetFrame.location.href = catalog_url + "sc_app/sc_filter.asp?" + QueryString;
}

function GetSpec(famID, targetFrame, prtNo, uom)
{
  if (isEmpty(catalog_url))
  {
    alert("Specify the SmartCAT catalog URL.");
    Help();
    return;
  }
  
  if (arguments.length < 4)
  {
    Help(SPEC);
    return;
  }

  var theParams = "";
  
  if (arguments.length > 4)
  {
    for (var i = 4, 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;
  if (!isEmpty(uom))
    QueryString += "&uom=" + uom;
  QueryString += "&KeyVar=" + theParams;
    
  targetFrame.location.href = catalog_url + "sc_app/sc_spec.asp?" + QueryString;
}

function GetRFQ(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";
}

function GetSearch(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";
}

function Help(theFunction)
{
  var theHelpWindow;
  
  switch (theFunction)
  {
    case "" + DESC:
      theHelpWindow = window.open(DOC_URI + "?u=" + DESC, "usage", HELP_WIND_ATTRIB + "")
      break;
    case "" + CFG:
      theHelpWindow = window.open(DOC_URI + "?u=" + CFG, "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 + "")
  }
}