// Generally useful JavaScript used by nuWiki

var POPUP_WINDOW_WIDTH = 500;
var POPUP_WINDOW_HEIGHT = 480;

/*var nuWiki_PREF_COOKIE_NAME = "nuWikiPREF";
var COOKIE_PREF_SEPARATOR = "|"; // separates key-value pairs
var COOKIE_PREF_VALUE_SEPARATOR = "="; // separates key from value
var COOKIE_EXPIRY_TIME = 365 * 24 * 60 * 60 * 1000; // one year from now*/

// Constants for the browser type
var ns4 = (document.layers) ? true : false;
var ie4 = (document.all) ? true : false;
var dom = (document.getElementById) ? true : false;

// Chain a new load handler onto the existing handler chain
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// if prepend is true, adds the function to the head of the handler list
// otherwise it will be added to the end (executed last)
function addLoadEvent(func, prepend) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = function() {
			func();
		};
	} else {
		var prependFunc = function() {
			func(); oldonload();
		};
		var appendFunc = function() {
			oldonload(); func();
		};
		window.onload = prepend ? prependFunc : appendFunc;
	}
}

// Determine the first index of a string in an array.
// Return -1 if the string is not found.
function indexOf(element, array) {
	if (!array || array.length == undefined) return -1;
	var i = array.length;
	while (i--)	{
		if (array[i] == element) return i;
	}
	return -1;
}

// Applies the given function to all elements in the document of
// the given tag type
function applyToAllElements(fn, type) {
    var c = document.getElementsByTagName(type);
    for (var j = 0; j < c.length; j++) {
        fn(c[j]);
    }
}

//integrated from common.js used by alpha of Nuwiki
/* toggle an element's display */
function toggle(obj) {
  var el = document.getElementById(obj);
  if ( el.style.display != 'none' ) {
    el.style.display = 'none';
  }
  else {
    el.style.display = '';
  }
}

/* grab Elements from the DOM by /* insert an element after a particular node */
function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}

/* Array prototype, matches value in array: returns bool */
Array.prototype.inArray = function (value) {
  var i;
  for (i=0; i < this.length; i++) {
    if (this[i] === value) {
      return true;
    }
  }
  return false;
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

/* From http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/ */
function insertAtCursor(myField, myValue) {
  var position = myField.scrollTop;

  //IE support
  if (document.selection) {
    myField.focus();

    sel = document.selection.createRange();
    sel.text = myValue;

  } else if (myField.selectionStart || myField.selectionStart == '0') {
    //MOZILLA/NETSCAPE support

    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    myField.selectionStart = startPos;
    myField.selectionEnd = startPos + myValue.length;
    myField.focus();

    myField.scrollTop = position;
  } else {
    myField.value += myValue;
  }

  myField.scrollTop = position;
}

function headTailAtCursor(myField, myHead,myTail) {
  var position = myField.scrollTop;

  //IE support
  if (document.selection) {
    myField.focus();

    var selectedRange = document.selection.createRange();
    if (selectedRange.parentElement() == myField) {
      var strSelection = document.selection.createRange().text ;
      document.selection.createRange().text = myHead + strSelection + myTail;
    }

    
  } else if (myField.selectionStart || myField.selectionStart == '0') {
    //MOZILLA/NETSCAPE support

    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myValue = myField.value.substring(startPos,endPos);
    myField.value = myField.value.substring(0, startPos) + myHead + myValue + myTail + myField.value.substring(endPos, myField.value.length);
    myField.selectionStart = startPos + myHead.length;
    myField.selectionEnd = startPos + myValue.length + myHead.length;
    myField.focus();

  } else {
    myField.value += myHead + myTail;
  }

  myField.scrollTop = position;
}

function convertAngleBrackets(frmObj) {
	var index;
	var tmpStr;
	var newStr;
	var tmpChar;
	var preString;
	var postString;
	var strLen;
	newStr = "";
	tmpStr = frmObj.value;
	strLen = tmpStr.length;

	if (strLen > 0)  {
		for (index = 0; index < strLen; index++)  {
			if (index == 0)  {
				tmpChar = tmpStr.substring(0,1);
				if (tmpChar == "<") {
				   tmpChar = "&lst;";
				}
				else {
				   if (tmpChar == ">") {
				     tmpChar = "&gst;";
				   }
				   else {
						if(tmpChar == "+") {
							tmpChar = "&plus;"
						} else {
							if(tmpChar == "=") {
							  tmpChar = "&eq;"
							} else {
							  if(tmpChar == "%") {
								tmpChar = "&pct;"
							  }
							}
						}				   
				   }
				}
				postString = tmpStr.substring(1,strLen);
				newStr = tmpChar
			}
			else {
				tmpChar = tmpStr.substring(index, index+1);
				if (tmpChar == "<") {
				   tmpChar = "&lst;";
				}
				else {
				   if (tmpChar == ">") {
				     tmpChar = "&gst;";
				   }
				   else {
						if(tmpChar == "+") {
							tmpChar = "&plus;"
						} else {
							if(tmpChar == "=") {
								tmpChar = "&eq;"
							} else {
							  if(tmpChar == "%") {
								tmpChar = "&pct;"
							  }
							}
						}		   
				   }
				}
				newStr = newStr + tmpChar
		  	}
  		}
	}
	frmObj.value = newStr;
}

function clearDefaultandCSS(el) {
	if (!el.defaultValue) {
		el.defaultValue = el.value;
	}
	if (el.defaultValue == el.value) {
		el.value = "";
	}

}

function setDefaultText(el) {
	if (el.value == "" && el.defaultValue) el.value = el.defaultValue;
}

