function getUrlParameter( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results === null )
		return "";
	else
		return results[1];
}

function _br() {
	return document.createElement("br");
}

function _a(href, text) {
	var a = document.createElement("a");
	if (href != null) {
		a.setAttribute("href", href);
	}
	if (text != null) {
		a.appendChild(textNode(text));
	}
	
	return a;
}

function _div(id, clazz) {
	var d = document.createElement("div");

	if (clazz != null){
		d.className = clazz;
	}
	if (id != null) {
		d.setAttribute("id", id);
	}

	return d;
}

function _span(id, clazz) {
	var d = document.createElement("span");

	if (clazz != null){
		d.className = clazz;
	}
	if (id != null) {
		d.setAttribute("id", id);
	}

	return d;
}

function _em(text){
	var em = document.createElement("em");
	em.appendChild(textNode(text));
	return em;
}

function _b(text){
	var b = document.createElement("b");
	b.appendChild(textNode(text));
	return b;
}

function strTrim(s) {
	return s.replace(/^\s+|\s+$/, ''); 
}

function arrayClone(src) {
	var dest = new Array();
	for (var i = 0; i < src.length; ++i) {
		dest.push(src[i]);
	}
	return dest;
}

function clearMouse(image) {
	var i = __img(image);
	var f = function(){ 
		this.src = i; 
		nd();
	};
	return f;
}

function overMouse(image, over) {
	var i = __img(image);
	var f = function(){ 
		this.src = i; 
		overlib (over, DELAY, 0);
	};
	return f;
}


function defaultErrorHandler(errorString, exception) {
	if (exception == null) {
		//unrecognized error, don't display errorString
		alert("We are unable to process your request at this time.");
	} else {
		alert("We are unable to process your request at this time.");
		//alert(errorString);
	}
}

function getConsoleBody() {
	var root = null;
	if (window.__main) {
		root = __main
	}
	return getElement('consoleBody', root);
}

function getElement(id, root) {
	var doc = document;
	if (root != null) {
		doc = root.document;
	}
	return doc.getElementById(id);
}

function image(props) {
	if (isString(props)) {
		var p = { 
			src : props
		};
		props = p;
	}

	return __image(__img, props);
}

function cbThumbNail(cb) {
	var cbId = cb.id;
	var last = cb.lastModified;
	var p = { 
		src : cbId,
		mod : last
	};
	return __image(thumbNailUrl, p);
}

function getThumbImage(id, mod) {
	var props = { 
		src : id,
		clazz : "thumbView",
		mod : mod
	};

	return __image(thumbMunger, props);
}


function __image(munger, props) {
	var i = document.createElement("img");
	if(props.src != null){
		i.setAttribute("src", munger(props.src, props.mod));
	}
	else{
		i.setAttribute("src", munger());
	}
	if (props.id != null) {
		i.setAttribute("id", props.id);
	}
	if (props.clazz != null) {
		i.className = props.clazz;
	}	
	if (props.style != null) {
		i.setAttribute("style", props.style);
	}
	if (props.width != null) {
		alert("width");
		i.setAttribute("width", props.width);
	}
	if (props.height != null) {
		alert("height");
		i.setAttribute("height", props.height);
	}
	if (props.over != null) {
		i.onmouseover = buildMouser(props.over, munger);
		if (props.out == null) {
			i.onmouseout = mouser(munger(props.src));
		} else  {
			i.onmouseout = buildMouser(props.out, munger);
		}
	}
	if (props.down != null) {
		i.onmousedown = buildMouser(props.down, munger);
		i.onmouseup = buildMouser(props.up, munger);
	}

	return i;
}

function buildMouser(p, munger) {
	if (isString(p)) {
		return mouser(munger(p));
	} else if (isFunction(p)) {
		return p;
	} else {
		throw new Error ("Only strings and functions are permitted.");
	}
}

function mouser(s) {
	var f = function(){ 
		this.src = s; 
	};
	return f;
}

function __img(i) {
	return _AX + "/images/" + i;
}

function getMoniker(glip) {
	if (glip.getTypeDef() != null) {
		return glip.getTypeDef().moniker;
	}
	return "";
};

function isString(a) {
    return typeof a == 'string';
}

function isFunction(a) {
    return typeof a == 'function';
}

function thumbNailUrl(id, mod) {
	return _AX + "/web/gallery/getCellBlockThumbNail.htm?id="+id +"&mod="+mod;
}

function thumbMunger(id, mod) {
	var url = _AX  + "/web/gallery/getImage.htm?size=0&imageId=" + id;
	if (mod != null) {
		url = url + "&mod=" + mod;
	}
	return url;
}

function userPicMunger(id) {
	return _AX  + "/web/gallery/getImage.htm?imageId=" + id;
}

function initContext() {
	dynamicJS.init(
		{
			callback:function(data) {
				_AX = data;
			},
			async:false
		}
	);
}

function killChildren(e) {
	if ( e == null) {
		return;
	}
	while(e.hasChildNodes()){
		e.removeChild(e.firstChild);
	}
}

function textNode(txt) {
	return document.createTextNode(txt)
}


function isIE(){
	return isIt('msie');
}

function isOpera(){
	return isIt('opera');
}

function isIt(browser){
	var detect = navigator.userAgent.toLowerCase();	
	
	if (checkIt(browser.toLowerCase())) {
		return true;
	} else{
		return false;	
	}
	
	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		return place;
	}
}

function browserWarning() {
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;

	if (checkIt('safari')) {
		browser = "Safari";
		//browserError(browser);
	} else if (checkIt('opera')) {
		browser = "Opera";
		browserError(browser);
	} else if (checkIt('msie')) {
		browser = "Internet Explorer";
		if (navigator.appVersion.indexOf("Mac")!=-1) {
			browser = "Mac Internet Explorer"
			browserError(browser);
		}
	} else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
		//browserError(browser);
	} else {
		browser = "unknown";
		browserError(browser);
	}
	
	if (!version) version = detect.charAt(place + thestring.length);
	
	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}
	
	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}
}

function osTest() {
	var OSName="Unknown";
	if (navigator.appVersion.indexOf("Win")!=-1) {
		OSName="Windows";
	} else if (navigator.appVersion.indexOf("Mac")!=-1) {
		OSName="MacOS";
	}
	return OSName;
}

function browserError(browser) {
	var brsr = browser;
	if (brsr == "Opera") {
		alert("We do not currently support Opera.");
	//} else if (brsr == "Safari") {
		//alert("safari.");
	//} else if (brsr == "Internet Explorer") {
		//alert("IE.");
	} else if (brsr == "Mac Internet Explorer") {
		alert("We do not support Internet Explorer for Macintosh.\nPlease use Firefox or Safari.");
	//} else if (brsr == "Netscape Navigator") {
		//alert("Firefox.");
	} else if (brsr == "unknown") {
		var os = osTest();
		if (os == "Windows") {
			alert("We currently do not support your browser.\nPlease use Firefox or Internet Explorer.");
		} else if (os == "MacOS") {
			alert("We currently do not support your browser. Please use Firefox or Safari.");
		} else {
			alert("Gloto is only available on Windows or Macintosh operating systems at this time.");
		}
	}
}

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;

  if (ready==net.READY_STATE_COMPLETE){
	var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}

function suppressErrors(){
	DWREngine.setWarningHandler(function(msg){
				//alert("warningHandler");
			});
	DWREngine.setErrorHandler(function(msg){
				//alert("errorHandler");
			});	
}

function capString(string, len){
	if (string.length > len) {
		return string.slice(0,len) + "...";
	} else {
		return string;
	}
}

function smileyEscape(text){
	var txt = new String(text);
		txt = txt.replace(/&lt;/gi, "<");
		txt = txt.replace(/&gt;/gi, ">");
		txt = txt.replace(/&#40;/gi, "(");
		txt = txt.replace(/&#41;/gi, ")");
	return txt;
}

function li(label){
	return "<li>"+label+"</li>";	
}

function getApiContent(contentItem){
	if(contentItem == null){
		return "";
	}
	
	var path = "Cellblock/apiContent/" + contentItem;
	var content = "";
	cmController.getContent(path, {
		callback: function(data) {
		content = data;
		},
		async:false
									   });
	return content;
}

function getEmbedContent(contentItem){
	if(contentItem == null){
		return "";
	}
	
	var path = "Cellblock/embedContent/" + contentItem;
	var content = "";
	cmController.getContent(path, {
		callback: function(data) {
		content = data;
		},
		async:false
									   });
	return content;
}

function getHelpContent(contentItem){
	if(contentItem == null){
		return "";
	}
	
	var path = "Cellblock/helpContent/" + contentItem;
	var content = "";
	cmController.getContent(path, {
		callback: function(data) {
		content = data;
		},
		async:false
									   });
	return content;
}