/* -----------------------------------------------------------------------------------------------
	DanUts.js
	Revision History
	----------------

	05-Nov-04 v07
		Created

	20-May-05
		Added primitive set functions
		
	09-Aug-05
		Edits for cross-browser compatibility
		
	03-Sep-05
		Added Get/SetAtt(ribute) - replace all instances in this unit with reference to new functions
		Previous in (local) DanUts01.js.old

------------------------------------------------------------------------------------------------- */

function BrowserCheck() {
var b = navigator.appName;
if (b == "Netscape") this.b = "NS";
else if (b == "Microsoft Internet Explorer") this.b = "IE";
else this.b = b;
this.v = parseInt(navigator.appVersion);
this.NS = (this.b == "NS" && this.v>=4);
this.NS4 = (this.b == "NS" && this.v == 4);
this.NS5 = (this.b == "NS" && this.v == 5);
this.IE = (this.b == "IE" && this.v>=4);
this.IE4 = (navigator.userAgent.indexOf('MSIE 4')>0);
this.IE5 = (navigator.userAgent.indexOf('MSIE 5')>0);
if (this.IE5 || this.NS5) this.VER5 = true;
if (this.IE4 || this.NS4) this.VER4 = true;
this.OLD = (! this.VER5 && ! this.VER4) ? true : false;
this.min = (this.NS||this.IE);
}
var _browserObj = new BrowserCheck();
var isIE = _browserObj.IE;
var isNS = _browserObj.NS;
var browserId = (isIE ? "IE" : (isNS ? "NS" : "??"));

// -----------------------------------------------------------------------------------------------------  sameText()
function sameText(s1, s2) {
	// need to add prototpye method to String type!!!
	if (s1 && s2) {
		return (s1.toLowerCase() == s2.toLowerCase())
	}
	else
	  return false;
} //sameText

// -------------------------------------------------------------------------------------------------  containsText()
function containsText(AText, ASubText) {
	// need to add prototpye method to String type!!!
	if (AText && ASubText) {
		AText = AText.toLowerCase();
		return (AText.indexOf(ASubText.toLowerCase()) >= 0)
	}
	else
	  return false;
} // containsText

// -----------------------------------------------------------------------------------------------------  padZero()
function padZero(s, w) {
	s = String(s);
	while (s.length < w)
		s = '0' + s;
	return s;
}

function getScriptName() {
	// returns name of current script
	var s = document.location.pathname;
	var p = Math.max(s.lastIndexOf('/'),s.lastIndexOf('\\'));
	if (s != '/')
		s = s.slice(p+1);
	return s;
}

// return rule style for style sheet with selectorText rNam from style sheet id ssNam
// we can then do, e.g., getStyleSheetRule ("style1", ".menItem").width = "150px"
function getStyleSheetRule (ssNam, rNam) {
	var ss = document.styleSheets[ssNam];
	if (ss) {
		for (i = 0; i < ss.rules.length; i++) {
			if (ss.rules[i].selectorText == rNam)
				return  ss.rules[i].style
		}
	}
}

// --------------------------------------------------------------------------------------------------------  getEl()
function getEl (elName, parent) {
	// getElementById within parent, or document if null
	// return ((parent) ? parent : document).getElementById(elName);
	if (isIE)
		return ((parent) ? parent : document).getElementById(elName);
	else if (isNS)
		return ((parent) ? parent : document).getElementById(elName);
	else
		return ((parent) ? parent : document).getElementById(elName);
} // getEl

// --------------------------------------------------------------------------------------------------------  getAtt()
function getAtt (el, attName) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		return el[attName];
	else
		return null;
} // getAtt

// --------------------------------------------------------------------------------------------------------  setAtt()
function setAtt (el, attName, attVal) {
	if (typeof(el)=="string")
		el = getEl(el);
	if (el)
		el[attName] = attVal;
} // setAtt

// --------------------------------------------------------------------------------------------------------  getEl()
function getElVal (el) {
	var retVal = '';
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		retVal = el.innerText; // NOT value
	return retVal;
} // getElVal

// --------------------------------------------------------------------------------------------------------  setEl()
// deprecated
function setEl (elName, AinnerHTML) {
	var el = getEl(elName);
	if (el)
		el.innerHTML = AinnerHTML;
} // setEl

// ----------------------------------------------------------------------------------------------------  getElHTML()
function getElHTML (el ) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		return el.innerHTML;
	else
		return "el is not an object";
} // getElHTML

// ----------------------------------------------------------------------------------------------------  setElHTML()
function setElHTML (el, AinnerHTML) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		el.innerHTML = AinnerHTML;
} // setElHTML

// -----------------------------------------------------------------------------------------------------  createEl()
function createEl (elType) {
	// createElement of type elType
	return document.createElement(elType);
}

// ---------------------------------------------------------------------------------------------------------- dispEl
// display element el if exp==1, else hide
function dispEl (el, exp) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		if (exp==1)
		  el.style.display = "block"
		 else
		  el.style.display = "none"
}

// ---------------------------------------------------------------------------------------------------------- setFormVal
// set form value element el to value val
function setFormVal (el, val) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		el.value = val
} // setFormVal

// ---------------------------------------------------------------------------------------------------------- getFormVal
// get form value element el from
function getFormVal (el) {
	var retVal = '';
	if (typeof(el)=="string")
		el = getEl (el);
	if (el)
		retVal = el.value; // NOT value
	return retVal;
} // getFormVal

// ---------------------------------------------------------------------------------------------  getPageEventCoords
function getPageEventCoords(evt) {
	var coords = {left:0, top:0};
	if (evt.pageX) {
  	coords.left = evt.pageX;
  	coords.top = evt.pageY;
	} else if (evt.clientX) {
  	coords.left = evt.clientX + document.body.scrollLeft - document.body.clientLeft;
   	coords.top = evt.clientY + document.body.scrollTop - document.body.clientTop;
  	// include html element space, if applicable
   	if (document.body.parentElement && document.body.parentElement.clientLeft) {
     	var bodParent = document.body.parentElement;
     	coords.left += bodParent.scrollLeft - bodParent.clientLeft;
     	coords.top += bodParent.scrollTop - bodParent.clientTop;
   	}
 	}
    	return coords;
} // getPageEventCoords

// -------------------------------------------------------------------------------------------------------  addDebug
function addDebug(S, N, el) {
  if (!el)
  	el = addDebug.defEl;
  if (el==null)
  	return;
  if (N && (debugNos > '') && (debugNos.indexOf(','+N+',')==-1))
  	return;
	var o = createEl("div");
	o.innerText = S;
	el.appendChild(o);
}

// Returns argument with 'px' appended ------------------------------------------------------------------------  pix
function pix(N) {
	return N + 'px';
}
// Returns unique identifier qn( q1, q2 etc), starting with q1 -----------------------------------------------  unId
unId.N = 0;  // Initialise with 0
function unId() {
	return 'q' + ++unId.N
}
// Returns TRUE if el contains style nam ------------------------------------------------------------  containsStyle
function containsStyle(el, nam) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el) {
		var s = getAtt(el, "className");
		if (s==nam)
			return true
		else if (s.indexOf(nam+" ") >= 0)
			return true
		else if (s.indexOf(" "+nam) >= 0)
			return true
		else
			return false
	}
	else
		return false
}
// Add style to item class name --------------------------------------------------------------------------  addStyle
function addStyle(el, nam) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('addStyle: "' + nam + '" to "' + el+'"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (el) {
		addDebug ('---- addStyle: "' + nam + '" to "' + el + ' class: ' + getAtt(el,'className')+'" in "' + el + '"', 6);
		if (!containsStyle(el, nam)) {
			setAtt(el,"className", getAtt(el,"className") + " " + nam);
			addDebug ('---- Style now "' + getAtt(el,'className')+'"', 6);
		}
		else
			addDebug ('---- Style already there', 6);
	}
}
// Remove style from item class name ------------------------------------------------------------------  removeStyle
function removeStyle(el, nam) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('removeStyle: "' + nam + '" from "' + el+'"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (!el)
		return;
	addDebug ('---- removeStyle: "' + nam + '" from "' + getAtt(el,'className')+'" in "' + el + '"', 6);
	var s = getAtt(el,"className");
	var sa = s.split(" ");
	s = '';
	for (var i = 0; i < sa.length; i++) {
		if ((sa[i] != "") && (sa[i] != nam)) 
			s += sa[i] + " ";
	}
	s = s.substr (0, s.length-1);
	setAtt(el,"className", s);
	addDebug ('---- Style now "' + getAtt(el,'className')+'"', 6);
}
// Replace style in item class name ------------------------------------------------------------------  replaceStyle
function replaceStyle(el, namFrom, namTo) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('ReplaceStyle "' + namFrom + '" with "' + namTo + '" in "' + el + '"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (!el)
		return;
	if (containsStyle(el, namFrom) && !containsStyle(el, namTo)) {
		var s = getAtt(el,"className");
		addDebug ('---- removeStyle: "' + namFrom + '" from "' + getAtt(el,'className')+'"', 6);
		var sa = s.split(" ");
		s = '';
		for (var i = 0; i < sa.length; i++) {
			if ((sa[i] != "") && (sa[i] != namFrom)) 
				s += sa[i] + " ";
		}
		s += namTo;
		addDebug ('---- addStyle: "' + namTo + '"', 6);
		setAtt(el,"className", s);
		addDebug ('---- Style now "' + getAtt(el,'className')+'"', 6);
	}
	else 
		addDebug ('---- Style already exists');
}

// Returns TRUE if el contains Id nam ------------------------------------------------------------  containsId
function containsId(el, nam) {
	if (typeof(el)=="string")
		el = getEl (el);
	if (el) {
		var s = getAtt(el,"className");
		if (s==nam)
			return true
		else if (s.indexOf(nam+" ") >= 0)
			return true
		else if (s.indexOf(" "+nam) >= 0)
			return true
		else
			return false
	}
	else
		return false
}
// Add Id to item class name --------------------------------------------------------------------------  addId
function addId(el, nam) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('addId: "' + nam + '" to "' + el+'"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (el) {
		addDebug ('---- addId: "' + nam + '" to "' + el + ' class: ' + getAtt(el,'className')+'" in "' + el + '"', 6);
		if (!containsId(el, nam)) {
			setAtt(el,"className", getAtt(el,"className") + " " + nam);
			addDebug ('---- Id now "' + getAtt(el,'className')+'"', 6);
		}
		else
			addDebug ('---- Id already there', 6);
	}
}
// Remove Id from item class name ------------------------------------------------------------------  removeId
function removeId(el, nam) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('removeId: "' + nam + '" from "' + el+'"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (!el)
		return;
	addDebug ('---- removeId: "' + nam + '" from "' + getAtt(el,'className')+'" in "' + el + '"', 6);
	var s = getAtt(el,"className");
	var sa = s.split(" ");
	s = '';
	for (var i = 0; i < sa.length; i++) {
		if ((sa[i] != "") && (sa[i] != nam)) 
			s += sa[i] + " ";
	}
	s = s.substr (0, s.length-1);
	setAtt(el,"className", s);
	addDebug ('---- Id now "' + getAtt(el,'className')+'"', 6);
}
// Replace Id in item class name ------------------------------------------------------------------  replaceId
function replaceId(el, namFrom, namTo) {
	addDebug ('++++++++++++++++++', 6);
	addDebug ('ReplaceId "' + namFrom + '" with "' + namTo + '" in "' + el + '"', 6);
	if (typeof(el)=="string")
		el = getEl (el);
	if (!el)
		return;
	if (containsId(el, namFrom) && !containsId(el, namTo)) {
		var s = getAtt(el,"className");
		addDebug ('---- removeId: "' + namFrom + '" from "' + getAtt(el,'className')+'"', 6);
		var sa = s.split(" ");
		s = '';
		for (var i = 0; i < sa.length; i++) {
			if ((sa[i] != "") && (sa[i] != namFrom)) 
				s += sa[i] + " ";
		}
		s += namTo;
		addDebug ('---- addId: "' + namTo + '"', 6);
		setAtt(el,"className", s);
		addDebug ('---- Id now "' + getAtt(el,'className')+'"', 6);
	}
	else 
		addDebug ('---- Id already exists');
}

/* primitive set functions */
	function newSet() {
		return ',';
	}	// newSet
	
	function inSet (S, N) {
		// returns true if N in set
		return (S.indexOf(','+String(N)+',') >= 0)
	} // inSet
	
	function inSetP (S, N) {
		// returns position of N in set, inc leading comma, else 0
		return S.indexOf(','+String(N)+',')
	} // inSetP
	
	function addSet (S, N) {
		// Adds N to set S - returns new
		if (inSet (S, N))
			return S										// already there - return original
		else
			return S + String(N) + ',';
	} // addSet
	
	function delSet (S, N) {
		// removes N from if there set - returns new
		var S1 = ','+String(N)+',';
		var P = S.indexOf(S1);
		if (P >= 0)
			return S.substr(0, P+1) + S.substr(P+S1.length)
		else
			return S
	} // delSet
	
	function nInSet (S) {
		var N = -1;
		for (var i = 0; i < S.length; i++)
			if (S.charAt(i)==',')
				N++;
		return N
	} // nInSet
	
