// common1.js
//
// by Greg Saunders (saunders@socraticarts.com)
// Copyright 2003-2004
//
// Changes:
//   03/01/2003 - gms - created


// BOOTSTRAPPING ///////////////////////////////////////////////////////

function throwError(msg){
   alert(msg);
   var dummy = sorryAnErrorHasOccurred; //halt processing
}

function len(str){
   return(str.length);
}

function getConstant(name, dummyParamDoNotDefine){
	switch(name){
		case "undefined":
			//return(undefined);  -- this breaks IE Mac 5.1!
			return(dummyParamDoNotDefine);
			break;
		default:  throwError("case fell through:  " + name);
	}
}

function isDefined(param){
    if(param === null){  //need this to distinguish null from undefined
		return(true);
	}
	return(param != getConstant("undefined"));
}

function getDefaultParam(param, defaultValue){
	if(isDefined(param)){
		return(param);
	}
	else{
		return(defaultValue);
	}
}

function arrayNew(dim){
	return(new Array);
}

function mySplit(str, delim){
	if(str == ""){
		return(arrayNew(1));
	}
	return(str.split(delim));  //requires JS 1.1
}

function createStructure(fields, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9){
  if(p9 != null) throwError("too many fields passed to createStructure()");
  var a = mySplit(fields, ",")  //fields.split(",");
  for(var i=0; i<a.length; i++){
     eval("this." + a[i] + " = p" + i);
  }
  return(this);
}

function structNew(){
	return(new Object);
}

function structInsert(structure, key, value){
	structure[key] = value;
	return(structure);
}

function structFind(structure, key){
	return(structure[key]);
}

function findAndSplit(string, theSubstring, guaranteeP){
   guaranteeP = getDefaultParam(guaranteeP, true);
   var pos = string.indexOf(theSubstring);
   if(pos<0 && guaranteeP){
   	  throwError('string "' + string + '" does not contain substring "' + theSubstring + '"');
   }
   var res = structNew();
   if(pos<0){
       //res = new createStructure("left,mid,right", string, "", "");
	   res = structInsert(res, "left", string);
	   res = structInsert(res, "mid", "");
	   res = structInsert(res, "right", "");
   }
   else{		
	   //res = new createStructure("left,mid,right", string.substr(0, pos), theSubstring, string.substr(pos+len(theSubstring), len(string)));
	   res = structInsert(res, "left", string.substr(0, pos));
	   res = structInsert(res, "mid", theSubstring);
	   res = structInsert(res, "right", string.substr(pos+len(theSubstring), len(string)));	   
   }
   return(res);
}



// OBJECTS ///////////////////////////////////////////////////////////

function MM_findObj(n, d) { //from Macromedia, v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function findObject(objectOrObjectName, guaranteeP){
   var res;
   if (guaranteeP==null) guaranteeP=true;
   if(typeof(objectOrObjectName)=="string"){
      res = MM_findObj(objectOrObjectName);
      if(res==null && guaranteeP) throwError("Object not found:  " + objectOrObjectName);
   }
   else
      res = objectOrObjectName;
   return(res);
}


// IMAGE MANIPULATION ///////////////////////////////////////////////////////////

function swapImage(img, oldkey, newkey){
  if (document.images){
	var imgObj = findObject(img, false);
	if(imgObj==null){
		return(null);  //failed...image not loaded yet
	}
	var delim = "_";
	if(imgObj.src.indexOf(delim) == -1){
		delim = "-";
	}
	var r = findAndSplit(imgObj.src, delim + oldkey + ".", false);
	if(len(r.mid)){
		//alert(r.left + delim + newkey + "." + r.right);
		imgObj.src = r.left + delim + newkey + "." + r.right;
	}
  }
}

function rollon(img, rolloverKeywordOn, rolloverKeywordOff) {
  rolloverKeywordOn = getDefaultParam(rolloverKeywordOn, "ro");
  rolloverKeywordOff = getDefaultParam(rolloverKeywordOff, "off");
  swapImage(img, rolloverKeywordOff, rolloverKeywordOn);
}

function rolloff(img, rolloverKeywordOn, rolloverKeywordOff) {
  rolloverKeywordOn = getDefaultParam(rolloverKeywordOn, "ro");
  rolloverKeywordOff = getDefaultParam(rolloverKeywordOff, "off");
  swapImage(img, rolloverKeywordOn, rolloverKeywordOff);
}


// Windows ///////////////////////////////////////////////////////////////////////

function windowOpenAndFocus(theURL, winName, params){
  if(params==null) params = 'height=600,width=700,toolbar,resizable,scrollbars,location,menubar,status,directories';
  var w = window.open(theURL, winName, params);
//  window.open(theURL, winName, params);
  w.focus();
  //w = null;  //attempt to stop strange IE bug
}

// Location wrappers
function setURL(theURL, replaceP){
  if(replaceP==null) replaceP=false;
  if(replaceP){
  	document.location.replace(theURL);
  }
  else{
  	document.location.href = theURL;     //document.URL = theURL;
  }
}

function openEC(theURL){
	windowOpenAndFocus(theURL, "ec", "height=600,width=900,scrollbars,resizable");
}

function openEmpireCity(theURL){
	windowOpenAndFocus(theURL, "empireCity", "height=600,width=900,scrollbars,resizable");
}

function openSBSG(theURL){
	windowOpenAndFocus(theURL, "ec", "height=650,width=900,scrollbars,resizable");
}

function openWin(theURL, width, height){
	windowOpenAndFocus(theURL, "ec", "width=" + width + ",height=" + height + ",scrollbars,resizable");
}

function openOffsiteLink(theURL){
	window.open(theURL);
}



// Menus /////////////////////////////////////////////////////////////////////

function selectedMenuItemName(menu){
	return(menu.options[menu.selectedIndex].name)
}

function selectedMenuItemValue(menu){
	return(menu.options[menu.selectedIndex].value)
}


// Selection /////////////////////////////////////

function mySelect(fieldName){
	findObject(fieldName).select();
}

var gSubmittedP = false;
function submitOnce(){
	if(gSubmittedP){
		alert("Your data is already being sent; please wait.");
		return(false);
	}
	else{
		gSubmittedP = true;
		return(true);
	}
}

// Windows ////////////////////////////////////

function parentWindow() {
   if(window.opener == null || window.opener.closed)
     return(null)
   else
     return(window.opener);
}

function parentExistsP() {
   if (parentWindow() == null)
   		return(false);
   else{
		if (false && typeof parentWindow().parentExistsP != 'object')//function
			return(false);
		else
			return(true);
	}
}

function refreshParent(closeSelfP, targetURL,openNewWindowIfNoParentP) {
    var win;
	
	if(openNewWindowIfNoParentP == null) openNewWindowIfNoParentP = false;
	
	if (parentExistsP()) {
	    win = parentWindow();
        if (closeSelfP) win.focus();
        if (targetURL==null) win.location.reload(); else win.location = targetURL;
    } else if (openNewWindowIfNoParentP && targetURL != null){
		windowOpenAndFocus(targetURL,"newwin");
	}
    if (closeSelfP) self.close();
}


// GLOBALS //////////////////////////////////////
var gGlobals = new Array();

function setGlobal(name, val){
  gGlobals[name] = val;
}

function getGlobal(name){
  return(gGlobals[name]);
}

function map(fn, list){
  var a = mySplit(list, ",");  
  for(var i=0;i<a.length;i++){
	eval(fn + "('" + a[i] + "')");
  }
}

// Browser info
function browserInfo(){
   var res = new Array();
   res.IE = document.all;
   res.IE4 = document.all && !document.getElementById;
   res.NS4 = document.layers;
   res.NS6plus = !res.IE&&document.getElementById;
   res.NS = res.NS4 || res.NS6plus;
   return(res);
}

function ensureBrowserOK(fn){
   if(fn==null) fn = "";
   if (browserInfo().NS4)
     throwError("Netscape 4 does not support function " + fn);
}


// BASIC DIV MANIPULATION
function showObject(objName){
    var obj = findObject(objName);
	var bi = browserInfo();
	if(bi.NS4)
		obj.visibility="show";
	else
	    obj.style.visibility = "visible";
    runAfterMethods("showObject", objName);
}

function hideObject(objName)
{
    var obj = findObject(objName);
	var bi = browserInfo();
	if(bi.NS4)
    	obj.visibility="hide";
	else
	    obj.style.visibility = "hidden";
    runAfterMethods("hideObject", objName);
}


// Object text 

function setObjectText(objName, text){
	ensureBrowserOK("setObjectText");
	var obj = findObject(objName);
	if(obj.type == 'text' || obj.type == 'textarea')
	  obj.value = text;
	else
	  //obj.innerHTML = unescape(text);
	  obj.innerHTML = text;  //gms 10/29/2003
}

function getObjectText(objName){
	var obj = findObject(objName);
	if(obj.type == 'text' || obj.type == 'textarea')
	  return(obj.value)
	else
	  return(obj.innerHTML);
}



// After methods
function runAfterMethods(functionName, objectID){
   var a = getGlobal("afterMethods");
   for(var i=0; i<a.length; i++){
      if(a[i].functionName == functionName && a[i].objectID == objectID) eval(a[i].code);
   }
}

function defineAfterMethod(functionName, objectID, code){
  var dummy = push(getGlobal("afterMethods"), new createStructure("functionName, objectID, code", functionName, objectID, code));
}

setGlobal("afterMethods", new Array(0));

