// JavaScript Document
var script_url = "/cgi/scstore.pl";

var targetFrame;
var containerFrame;
var recurseDepth;
function findFrames(target)
{
        targetFrame = null;
        containerFrame = null;
        recurseDepth=0;
        recurseFindFrames(window.top.frames, target);
}

function recurseFindFrames(aFrames, target)
{
	for(i=0;i<aFrames.length;i++){
	  var frame = aFrames[i];
	  if ( frame.name == target ){ targetFrame=frame; }
	  if ( frame.name == 'containerframe' ){ containerFrame=frame; }
	  if ( frame.frames.length > 0 && recurseDepth < 10 ){
	    recurseDepth++;
	    recurseFindFrames(frame.frames);
	  }
	}
}

function loadFrame(url, target)
{
	// If there is no 'target' then do a link to
	// 'containterframe' with function=Enter, then let the link happen...
	findFrames(target);
	if ( containerFrame == null ){
		alert('No Container Frame!');
	   return;
	}
  //alert(url);
	if ( targetFrame == null )
	{
	  containerFrame.window.location=script_url+"?function=Enter&mainurl="+makeUrlParameter(url); // Should create mainframe and direct it to url
	}
	else
	{
	  targetFrame.window.location=url;
	}
}

function searchFrame(url, target)
{
        // Have to reteive the keyword from the searchform
        var keyword = document.forms.searchForm.elements.keyword.value;
        url += "&keyword="+keyword;
        loadFrame(url, target);
}

function submitenter(myfield,e, url, target)
{
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

        if (keycode == 13)
           {
           searchFrame(url, target);
           return false;
           }
        else
           return true;
}

// Change any 'naughly chars' in a url, so that it can
// be used in Get or Post data
function makeUrlParameter(url)
{
	var parm = "";
	for (var i=0; i<url.length;i++){
		var c = url.charAt(i);
		if ( c == '=' || c == '&' || c == '%' ){
			c = "%"+url.charCodeAt(i).toString(16);

		}
		parm += c;
	}
	//alert(parm);
	return parm;
}
