// <script>

/******************************************************************************
* sc_FindFrame
*
* This utility function takes two arguments, the name of a frame and an optional
* reference to a window level (like top or self).  If a level is not sent it gets
* a reference to the sc_header frame and, making that assumption that sc_header's parent
* is the top most level, then recurses down the frame heirarchy to find the frame requested.
*
* 
* Returns - a reference to the frame if found, null if not found or if sc_header
* is not found
*******************************************************************************
*/

// constants for this library

var str_headerFrame = "sc_header";

function sc_FindFrame(frameName, level)
{
  var theFrame = null;
 
  if (!level)
    level = sc_FindHeader(self);
 
  if (level == null)  
    return null;
  else
    // We actually want the parent frameset of the header frame
    // in this case
    level = level.parent;
    
  for(var i = 0; i < level.frames.length; i++)
  {
    if(level.frames[i].name == frameName)
      theFrame = level.frames[i];
    else
      theFrame = imkFindFrame(frameName, level.frames[i]);
    if(theFrame != null)
      break;
  }
  
  return theFrame
}

/******************************************************************************
* sc_CheckForHeader
*
* This utility function accepts a reference to the currentWindow (self) or any window
* for that matter.  It attempts to find the frame sc_header.  It recurses its way up
* the window heirarchy, making the assumption that sc_header is either a sibling frame
* or part of a parents frame structure.
*
* It is intended to be used as a check to make sure a user is browsing a catalog
* within the appropriate frame structure.
* 
* Returns - true if sc_header is located, false otherwise
*******************************************************************************
*/

function sc_CheckForHeader(currentWindow)
{
  var isFound = false;
  
  for (var i = 0; i < currentWindow.frames.length; i++)
	    if (currentWindow.frames[i].name == str_headerFrame)
      isFound = true;
  
  if (!isFound)
    if (currentWindow.parent.frames.length && (currentWindow != top))
      isFound = sc_CheckForHeader(currentWindow.parent);
  
  return isFound;
}

/******************************************************************************
* sc_FindHeader
*
* This utility function accepts a reference to the currentWindow (self) or any window
* for that matter.  It attempts to find the frame sc_header.  It recurses its way up
* the window heirarchy, making the assumption that sc_header is either a sibling frame
* or part of a parents frame structure.
*
* It is intended to be used to find a reference to the sc_header frame
* 
* Returns - a reference to sc_header if located, null otherwise
*******************************************************************************
*/

function sc_FindHeader(currentWindow)
{
  var theWindow = null;
  
    for (var i = 0; i < currentWindow.frames.length; i++)
	{
		try
		{
		  if (currentWindow.frames[i].name == str_headerFrame)
			theWindow = currentWindow.frames[i];
		}
		catch(err)
		{
		}
	}
    if (theWindow == null)
      if (currentWindow.parent.frames.length && (currentWindow != top))
        theWindow = sc_FindHeader(currentWindow.parent);

  return theWindow;
}

/******************************************************************************
* sc_GetViewID
*
* This utility function gets a reference to the sc_header frame and then
* checks for the existance of a viewID variable.
*
* Returns - a the view code for the current state or null if a view code is not found
*******************************************************************************
*/
function sc_GetViewID()
{
  var frmHeader = sc_FindFrame("sc_header")
  if (frmHeader)
    if (frmHeader.view_code)
      if (!imkIsEmpty(frmHeader.view_code))
        return frmHeader.view_code;
  return null;
}

/******************************************************************************
* sc_OpenLocation
*
* Opens a url in the specified target frame within a smartCAT server context
* 
* if a the querystring parameter "vid" is not found then
* the view code is automatically appended to the querystring 
*
* when no_view_code is true the view is not appended to the querystring
*
*******************************************************************************
*/
function sc_OpenLocation(loc, target, no_view_code)
{
  // default target if none specified
  if(!target)
    target = "_self";

  if (no_view_code != true)
  {
    if (loc.search(/vid/ig) == -1)
    {
      var vid = sc_GetViewID();
      if (vid)
        loc += (loc.search(/\?/g) == -1) ? "?vid=" + vid : "&vid=" + vid ;
    }
  }

  switch(target)
  {
    case "_blank":
      open(loc);
      break;
      
    case "_parent":
      window.parent.location = loc;
      break;
      
    case "_self":
      self.location = loc;
      break;
      
    case "_top":
      top.location = loc;
      break;
      
    default:
      var frm = sc_FindFrame(target);
      if(frm != null)
		    frm.location = loc;
  }
}

function sc_ReplaceLocation(loc, target, no_view_code)
{
  // default target if none specified
  if(!target)
    target = "_self";
    
  if (no_view_code != true)
  {
    if (loc.search(/vid/ig) == -1)
    {
      var vid = sc_GetViewID();
      if (vid)
        loc += (loc.search(/\?/g) == -1) ? "?vid=" + vid : "&vid=" + vid ;
    }
  }

  switch(target)
  {   
    case "_parent":
      window.parent.location.replace(loc);
      break;
      
    case "_self":
      self.location.replace(loc);
      break;
      
    case "_top":
      top.location.replace(loc);
      break;
      
    default:
      var frm = sc_FindFrame(target);
      if(frm != null)
		    frm.location.replace(loc);
  }
}

function LoadSelFrame(famID)
{
  var menuFrm = sc_FindFrame("sc_header");
  var vid;
  
  if (menuFrm)
    if (menuFrm.viewID)
      vid = menuFrm.viewID;
  
  sc_OpenLocation("../sc_app/sc_selframe.asp?famID=" + famID + "&vid=" + vid, "sc_main");
}

function sc_SaveStateVar(str_varName)
{
  var hdr = sc_FindFrame("sc_header");
  if(hdr != null)
  {
    if(typeof(hdr.StateVars) == "undefined")
      hdr.StateVars = new Array();
      
    var len = hdr.StateVars.length;
    for(var i = 0; i < len; i++)
      if(hdr.StateVars[i] == str_varName)
        return;
    
    hdr.StateVars[len] = str_varName;
  }
}

function sc_GetStateVars()
{
  var str_stateXML = "";
  var hdr = sc_FindFrame("sc_header");

  if(hdr != null)
  {
    if(typeof(hdr.StateVars) != "undefined")
    {
      var name;
      var val;
      var len = hdr.StateVars.length;
      for(var i = 0; i < len ; i++)
      {
        name = hdr.StateVars[i];
        if (typeof(hdr[name]) != "undefined")
					val  = hdr[name].toString();
        
        if(val)
		{
//alert(name)
//alert(val)
          str_stateXML += "<var n=\"" + imkXMLEscape(name) + "\" v=\"" + imkXMLEscape(val) + "\" />";
		}
      }      
      str_stateXML = "<scState>" + str_stateXML + "</scState>";
    }
  }
  return str_stateXML;
}

function scMain(loc, isSSL)
{
	var hdr = sc_FindFrame("sc_header");
	hdr.SelectedIndex = 0;
	sc_OpenMain(loc, isSSL);
}


function sc_OpenMain(loc, isSSL)
{  
  var str_url;
  var re; //bad protocol
  var str_protocol; //good protocol
  if(isSSL)
  {
    re = /^http:/i; 
    str_protocol = "https:";
  }
  else
  {
    re = /^https:/i; 
    str_protocol = "http:";
  }
  //finds sc_header frame - must exist to continue
  var hdr = sc_FindFrame("sc_header");
  if(hdr != null)
  {
    var str_hdrLoc = hdr.location;
    if(re.test(str_hdrLoc)) // found bad protocol - switch needed
    {
      var parent = hdr.parent;
      if(parent != null)
      {
        str_parentLoc = parent.location.toString();
        str_parentLoc = str_parentLoc.replace(re, str_protocol);
        var pos = str_parentLoc.indexOf("sc_app"); 
        if(pos > 0)
        {
          str_url = str_parentLoc.substring(0, pos + 7) + "default.asp?MainLoc=" + escape(loc);
            
          var vid = sc_GetViewID();
          if (vid)
            str_url += "&vid=" + escape(vid);
						 
          // get state, make certain defaults are inclluded first
          sc_SaveStateVar("SelectedIndex");
          sc_SaveStateVar("strSelectionState");
          sc_SaveStateVar("MainLoc");
          var str_state = sc_GetStateVars();
   
          if(str_state != "")
            str_url += "&CatState=" + escape(str_state);
           
          parent.location = str_url;
        }
        else
        {
        sc_OpenLocation(loc, "sc_main");
        }
      }
    }
    else // no switch needed
    {

      sc_OpenLocation(loc, "sc_main");
    }
  }
}


function sc_OpenSSL(path,isSSL,target)
{
	var str_url;
  var re;
  var str_protocol;
  if(isSSL)
  {
    re = /^http:/i;
    str_protocol = "https:";
  }
  else
  {
    re = /^https:/i;
    str_protocol = "http:";
  }	
str_url = sc_resolveURL(path).replace(re, str_protocol);
sc_OpenLocation(str_url, target)
}

function sc_resolveURL(relPath)
{
  var path_info = document.location.toString(); 
  var absPath = path_info.substring(0, path_info.lastIndexOf("/")  +1);
    
  absPath += relPath;
  
  // Beautify the path by removing all instances of ./
  // and <some dir>/../ except the case of http://<server>/../
  var parts = absPath.replace(/\/\.\//g, "/").split("/");
    for (var i=4; i<parts.length; i++)
  {
    if (parts[i] == "..")
    {
      for (var j=i+1; j<parts.length; j++)
      {
        parts[j-2] = parts[j];
      }
      parts.length -= 2;
      i -= 2;
    }
  }
  absPath = parts.join("/");
  return absPath;
}
