// ************************
// stuff for mockups.
// ************************

function getArgItems () {
	// ***********
	// mockup-only function
	//
	// common routine: parse out any passed values in the URL that led to this page
	// in real application, this will be handled by the application server code, eh?

	var paramArray = new Array();

	// split the query string into param=val pieces
	var qs = location.search.substring(location.search.indexOf("?")+1);
	qs = qs.split("&");
	// split param and value into individual pieces
	for (var i=0; i<qs.length; i++) {
		tmp = qs[i].split("=");
		paramArray[tmp[0]] = tmp[1];
	}

	return paramArray;
}



function handlebutton (d) {
	if (d == true)  {
		// if just a boolean true was passed, then just send it back in history one step.
		history.go (-1);
	}
	else {
		location = d;
	}
	return false;
}

function handlebutton_extended (d) {
	// MOCKUP ONLY
	// like handlebutton, but gets the url parameters and persists them.
	
	var p = location.search.substring(location.search.indexOf("?")+1);
	d = d.concat("?", p);
	if (d == true)  {
		history.go (-1);
	}
	else {
		location = d;
	}
	return false;
}

function parameters_persist () {
	// **********
	// mockup-only function
	
	// get any parameters.
	var p = location.search.substring(location.search.indexOf("?")+1);

	var links = document.links;
	
	// rewrite links to persist parameters.
	for ( var i=0; i < links.length; i++ ) {
		var link = links[i].href;
		if ( link != "javascript:void(0);" ) {
			links[i].href = link.concat("?", p);
		}
	}
}



function findObj(theObj, theDoc) {
	// handy bit to locate an object in the DOM
	// Example: obj = findObj("image1");

	var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

// * Dependencies * 
// this function requires the following function:
// findObj ()
//
// Accepts a variable number of arguments, in triplets as follows:
// arg 1: simple name of a layer object, such as "Layer1"
// arg 2: ignored (for backward compatibility)
// arg 3: 'hide' or 'show'
// repeat...
//
// Example: showHideLayers(Layer1,'','show',Layer2,'','hide');

function showHideLayersORIG()
{ 
  var i, visStr, obj, args = showHideLayersORIG.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 'show') visStr = 'visible';
        else if(visStr == 'hide') visStr = 'hidden';
      }
      obj.visibility = visStr;
    }
  }
}


function showHideLayers() { 
// Example: showHideLayers(Layer1,'show',Layer2,'hide');

  var i, disStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-1); i+=2)
  {
    if ((obj = findObj(args[i])) != null)
    {
      disStr = args[i+1];
      if (obj.style)
      {
        obj = obj.style;
        if(disStr == 'show') disStr = 'block';
        else if(disStr == 'hide') disStr = 'none';
      }
      obj.display = disStr;
    }
  }
}

var TimeToFade = 330.0;

function showHideLayers_fade () { 

  var i, disStr, obj, args = showHideLayers_fade.arguments;
  for (i=0; i<(args.length); i+=1) {
	if ((obj = findObj(args[i])) != null) {
		
		fade (args[i]);

	}
  }
}


function fade(eid) {
	//var element = document.getElementById(eid);
	var element = findObj (eid);
  
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }
  
}

function animateFade(lastTick, eid) {  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
	//var element = document.getElementById(eid);
	var element = findObj (eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;

    // get it off the DOM now that it has faded out.
    element.style.display = "none";
    return;
    
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}



function toggleLayer (whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var thingStyle = document.getElementById(whichLayer).style;
		thingStyle.display = thingStyle.display? '':'block';
	}
	else if (document.all) {
		// this is the way old msie versions work
		var thingStyle = document.all[whichLayer].style;
		thingStyle.display = thingStyle.display? '':'block';
	}
	//else if (document.layers) {
	//	// this is the way nn4 works
	//	var style2 = document.layers[whichLayer].style;
	//	style2.display = style2.display? "":"block";
	//}
}


function checkDisplay (whichLayer) {
	// find out if a layer is displayed or not now..
	if (document.getElementById) {
		// this is the way the standards work
		var s = document.getElementById(whichLayer).style;
		if (s.display == 'block') return true;
		// next case seems to take care of initial state
		// on page load, where display is not anything.
		else if (s.display == '') return true;
		// if neither, than isnt displayed
        else return false;
        
		//return (s.display? true:false);
	}
	else if (document.all) {
		// this is the way old msie versions work
		var s = document.all[whichLayer].style;
		return (s.display? true:false);
	}
}




// COOKIES
function createCookie(name,value,days) {

// When calling createCookie() you have to give it three bits of information: 
// the name and value of the cookie and the number of days it is to remain active.
// If you set the number of days to 0 the cookie is trashed when the user closes the browser. 
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {

// To read out a cookie, call this function and pass the name of the cookie. 
// Put the name in a variable. 

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




function addEvent (obj, evType, fn, useCapture ) {
	if (obj.addEventListener){
		//alert ("evType:"+evType);
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be attached");
	}
}

function removeEvent(obj, evType, fn, useCapture){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be removed");
}
}




// ******
// 

function booleanToggle (r, d) {
	//var i = r.value;
	var s = r.checked;

	switch ( s ) {
		case true:
			showHideLayers (d, "show");
			break;
		case false:
			showHideLayers (d, "hide");
			break;
		default:
			break;
	}
}

function showHideLayers_iterate (l, w) {
	
	var act = "show";
	if (!w) {
		act = "hide";
	}
	
	for (var i = 0; i < l.length; i++ ) {
		showHideLayers (l[i], act);
	}
}

