// common2.js
//
// by Greg Saunders (saunders@socraticarts.com)
// Copyright 2003-2004
//
// Changes:
//   03/26/2003 - gms - created


// Debugging
function formatThing(obj){
  var prop, newLabel;
  var label = "s";
  var res = "";
  for (var prop in obj){
     newLabel = label + "." + prop;
	 res = res + newLabel + " = " + obj[prop] + "\r";
  }
  return(res);
}

function showThing(obj){
  alert(formatThing(obj));
}

// OBJECT INFO & SIZING

function getObjectInfo_UNUSED(obj){
   obj = findObject(obj);
   var res = new Array();
   res = new createStructure("left,top,width,height", 0, 0, 0, 0);
   var bi = browserInfo();
   if(bi.NS4){
   	  res.left = obj.left;
	  res.top = obj.top;
	  res.width = obj.style.pixelWidth;
	  res.height = obj.style.pixelHeight;
   }
   else if(bi.NS6plus){
      res.left = parseInt(obj.style.left);
	  res.top = parseInt(obj.style.top);
	  res.height = parseInt(obj.height);
	  res.width = parseInt(obj.width);
   }
   else{
   	   res.left = obj.style.pixelLeft;
	   res.top = obj.style.pixelTop;
	   res.width = obj.style.pixelWidth;
	   res.height = obj.style.pixelHeight;
	   res.height = obj.height;
   }
   if (String(res.left)=="NaN") res.left=0;     //NS
   if (String(res.top)=="NaN") res.top=0;       //NS
   if (String(res.height)=="NaN") res.height=0; //NS
   if (String(res.width)=="NaN") res.width=0;   //NS
   if(res.height == 0){
   	   res.height = obj.scrollHeight;
   }
   return(res);
}

function setObjectSize_UNUSED(obj, width, height){
  if(browserInfo().NS4){
  	throwError("Netscape 4 does not support setObjectSize");
  }
  obj = findObject(obj);
  // not sure if findObject works...may need this:
  // var obj = document.all['theDoc'];
  // obj.style.width = "500px";
  if (width != null) {obj.style.pixelWidth = width;}
  if (height != null) {obj.style.pixelHeight = height;}
}


// Convenient functions to hide & show groups of objects.
function showObjects(list){
  map("showObject", list);
}

function hideObjects(list){
  map("hideObject", list);
}







// List functions
function myToString(obj){
	//need to convert URL objects to strings so list methods work correctly
	//the toString() method returns something other than what we want.
	return(obj + "");
}

function listLen(list, delim){
	delim=getDefaultParam(delim, ",");
	list = myToString(list);
	return(mySplit(list, delim).length);
}

function listGetAt(list, position, delim){
	delim=getDefaultParam(delim, ",");
	if(position < 1 || position > listLen(list, delim)){
		throwError("listGetAt index out of range:  list=" + list + ", position=" + position);
	}
	list = myToString(list);
	var a = list.split(delim);
	return(a[position-1]);
}

function listLast(list, delim){
	return(listGetAt(list, listLen(list,delim), delim));
}

function listFirst(list, delim){
	return(listGetAt(list, 1, delim));
}

function listFindNoCase(list, value, delim){
	delim=getDefaultParam(delim, ",");
	list = myToString(list);
	var a = list.split(delim);
	var obj;
	value = (value+"").toLowerCase();
	for(var i=0; i<a.length; i++){
		obj = a[i]+"";
		if(obj.toLowerCase() == value){
			return(i+1);
		}
	}
	return(0);
}


//NOTE:  THIS SHOULD HANDLE ESCAPING OF REGEXP CHARS, BUT NOT IMPLEMENTED
function escapeRE(str){
	return(str);
}

function replaceNoCase(string, substring1, substring2, scope){
	string = string + "";
	scope = getDefaultParam(scope, "ALL");
	if(scope != "ALL"){
		throwError("invalid scope");
	}
	var re = new RegExp(escapeRE(substring1), "gi");
	return(string.replace(re, substring2));
}

function structKeyListUNTESTED(structure){
	var res = "";
	var delim = "";
	for (var prop in structure){
		res = res + delim + prop;
		delim=",";
	}
}

function struct2string(struct){
	var structDelim = "&";
	var encodedStructDelim = "theStructDelim";
	var delim = "";
	var res = "";
	for (var prop in struct){
		res = res + delim + prop + "=" + replaceNoCase(struct[prop], structDelim, encodedStructDelim);
		delim=structDelim;
	}
	return(res);
}

function listAppend(list, value, delim){
	delim=getDefaultParam(delim, ",");
	if(listLen(list, delim) == 0){
		return(value);
	}
	else{
		return(list + delim + value);
	}
}

function listDeleteAt(list, pos, delim){
	delim=getDefaultParam(delim, ",");
	var res = "";
	var n = listLen(list, delim);
	if(pos > n){
		throwError("listDeleteAt");
	}
	for(var i=1; i<=n; i++){
		if(i != pos){
			res = listAppend(res, listGetAt(list, i, delim), delim);
		}
	}
	return(res);
}

function listRest(list, delim){
	delim=getDefaultParam(delim, ",");
	return(listDeleteAt(list, 1, delim));	
}

function string2struct(string){
	var structDelim = "&";
	var encodedStructDelim = "theStructDelim";
	var pair, i;
	var res = structNew();
	for(i=1; i<=listLen(string, structDelim); i++){
		pair = listGetAt(string, i, structDelim);
		res=structInsert(res, listFirst(pair, "="), replaceNoCase(listRest(pair, "="), encodedStructDelim, structDelim));
	}
	return(res);
}


// Onload functions
gOnloadFunctions = "";
function pushOnloadFunction(name){
  gOnloadFunctions = listAppend(gOnloadFunctions, name);
}

function funcall(fnName){
	eval(fnName + "()");  //can't use apply on Mac IE 5.1
}	

function runOnloadFunctions(){
  if(gOnloadFunctions != ""){
	  map("funcall", gOnloadFunctions);
  }
}


// URL Params
function getURLparam(name){
	var params = parent.location.search;
	if(params == ""){
		return(null);
	}
	params = params.toLowerCase();
	params = params.substring(1);
	params = params.split("&");
	for(var i=0; i<params.length; i++){
		if(params[i].split("=")[0] == name.toLowerCase()){
			return(params[i].split("=")[1]);
		}
	}
	return(null);
}

// Cookies 

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    res = unescape(document.cookie.substring(len,end));
    res = res.replace(/\+/g, " ");
    return(res);
}
	
function setCookie(name, value, expires, path, domain, secure, escapeP) {
    if (escapeP == null) escapeP=true;
    if (escapeP) value = escape(value);
    if (path == null) path = "/";
	if (expires == 'NEVER')
	 var expires = new Date(2037,8,22);
    document.cookie = name + "=" + value +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    ((secure == null) ? "" : "; secure");
}

