
	var dayOfWeek = ['SUN','MON','TUE','WED','THU','FRI','SAT'];

	function isReturnKey(evt) {
		if (evt != null) {
			var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
			return (charCode == 13);
		}
		return false;
	}

	function unhideMe(evt) {
		var node = (evt.target) ? evt.target : evt.srcElement;
		dojo.html.removeClass(node, 'hide');
	}

	function byId(id) {
		return dojo.byId(id);
	}

	function setClassName(id, newClass) {
		dojo.html.setClass(id, (newClass==null)?'':newClass);
	}

	function showIf(id, bool, className) {
		var node = byId(id);
		if(node == null) return;
		if (bool) {
			dojo.html.removeClass(node, 'hide');
			if (className != null) dojo.html.addClass(node, className);
		} else {
			if (className != null) dojo.html.removeClass(node, className);
			dojo.html.addClass(node, 'hide');
		}
	}

	function hideIf(id, bool, className) {
		showIf(id, !bool, className);
	}

	function setTextContent(id, text) {
		var element = byId(id);
		byId(id).innerHTML=text;
		//dojo.html.textContent(byId(id), text);
	}

	function getNodeAndDescendants(node) {
		var ret = new Array();
		ret.push(node);
		getDescendants(node, ret);
		return ret;
	}

	function getDescendants(node, ary) {
		var ret = (ary != null) ? ary : new Array();
		var children = node.childNodes;
		for (var count = 0; count < children.length; count++) {
			var child = children[count];
			if (child.nodeType == 1) { /*document.ELEMENT_NODE == 1 -- not supported in IE*/
				ret.push(child);
				getDescendants(child, ret);
			}
		}
		return ret;
	}

	function createElement(name, map, text) {
		var ret = document.createElement(name);
		for (var attName in map) {
			var val = map[attName]+'';
			ret.setAttribute(attName, val);
			if ('className'==attName) ret.setAttribute('class', val);
		}
		if (text != null) ret.appendChild(document.createTextNode(text));
		return ret;
	}

	function createElements(map) {
		var tag = map.tagName;
		var text = map.textContent;
		var children = map.children;

		if ((text != null) && (children != null)) window.alert('you can not specify both textContent and children. Add a string to the children array instead');
		if ((tag == null) && (text != null)) return text;

		var ret = document.createElement(tag);

		for (var key in map) {
			var attName = toAttName(key);
			if (attName != null) {
				var val = map[key]+'';
				ret.setAttribute(attName, val);
			}
		}

		if (children != null) {
			for (var count = 0; count < children.length; count++) {
				var val = children[count];
				if ((typeof val) == 'object') {
					ret.appendChild(createElements(val));
				} else {
					ret.appendChild(document.createTextNode(val));
				}
			}
		}

		if (text != null) ret.appendChild(document.createTextNode(text));

		return ret;
	}

	function createHTML(map) {
		var tag = map.tagName;
		var text = map.textContent;
		var children = map.children;

		if ((text != null) && (children != null)) window.alert('you can not specify both textContent and children. Add a string to the children array instead');
		if ((tag == null) && (text != null)) return text;

		var str = '<' + tag;

		for (var key in map) {
			var attName = toAttName(key);
			if (attName != null) {
				var val = map[key]+'';
				str += ' ' + attName + '="' + val + '"';
			}
		}

		str += '>';

		if (children != null) {
			for (var count = 0; count < children.length; count++) {
				var val = children[count];
				if ((typeof val) == 'object') {
					str += createHTML(val);
				} else {
					str += val;
				}
			}
		}

		if (text != null) str += text;

		str += '</' + tag + '>';

		return str;
	}

	function toAttName(attributeName) {
		if (attributeName == 'className') return 'class';
		if (attributeName == 'typeName') return 'type';
		if (attributeName == 'children') return null;
		if (attributeName == 'tagName') return null;
		if (attributeName == 'textContent') return null;
		return attributeName;
	}

	function prepareRepeatedNodes(idTemplate, number) {
		var count = 0;
		var done = false;
		var lastNode = null;
		while (!done) {
			var node = byId(numberId(idTemplate, count+''));
			var enough = count >= number;
			if ((node == null) && enough) {
				done = true;
			} else {
				if ((node == null) && (lastNode != null)) {
					node = lastNode.cloneNode(true);
					dojo.lang.forEach(getNodeAndDescendants(node), function(n){
						var oldId = n.id;
						var newId = numberId(oldId,count+'');
//						window.alert('replaceId :' + oldId + ': with :' + newId + ':');
						n.id=newId;
					});
					dojo.dom.insertAfter(node, lastNode);
				}
				showIf(node, !enough);
			}
			count++;
			lastNode = node;
		}
	}

	function replaceImage(id, src) {
		var oldNode = byId(id);
		if (oldNode.src != src) {
			var parent = oldNode.parentNode;
			if (parent != null) {
				var newNode = oldNode.cloneNode(true);
				newNode.src = src;
				parent.replaceChild(newNode, oldNode);
			} else {
				oldNode.src = src;
			}
		}
	}

	function removeById(id){
		var child = byId(id)
 		var parent = child.parentNode;
		parent.removeChild(child);
	}

	function numberId(oldId, newSuffix) {
		var idx = oldId.lastIndexOf('-');
		var ret = (idx == -1) ? oldId : (oldId.substring(0,idx+1)+newSuffix);
		return ret;
	}

	function invokeRemoteMethod(methodName, args, loadFunction, errorFunction) {
		//var eFunc = (errorFunction != null) ? errorFunction : function(type, error) { window.alert('ERROR:\n' + methodName + '\n' + error.type + ':' + error.number + '\n' + error.message); };
		var eFunc = (errorFunction != null) ? errorFunction : function(type, error) { window.alert('Your browser is having difficulty communicating with the CozmoTV server.\nPlease try again later'); };
		var content = new Object();
		content.methodName = methodName;
		content.argCount = args.length;
		content.timestamp = new Date().getTime();
		for (var count = 0; count < args.length; count++) {
			content['arg'+count] = args[count];
		}

		return dojo.io.bind({
			transport: 'XMLHTTPTransport',
			method: 'GET',
			url: 'client.json',
			content: content,
			mimetype: 'text/json',
			load: loadFunction,
			error: eFunc
		});
	}

	function invokeRemoteMethodOnForm(formId, loadFunction, errorFunction) {
		var form = byId(formId);
		//var eFunc = (errorFunction != null) ? errorFunction : function(type, error) { window.alert('ERROR:\n' + error.type + ':' + error.number + '\n' + error.message); };
		var eFunc = (errorFunction != null) ? errorFunction : function(type, error) { window.alert('Your browser is having difficulty communicating with the CozmoTV server.\nPlease try again later'); };

		dojo.require("dojo.io.IframeIO");

		return dojo.io.bind({
			transport: 'IframeTransport',
			formNode: form,
			mimetype: 'text/json',
			load: loadFunction,
			error: eFunc
		});
	}

	function scrollToTop() {
		window.scrollTo(0,0);
	}

	function truncateAtSpace(str, len) {
		if (str.length <= len) return null;
		var ret = str.substring(0, len);
		var idx = ret.lastIndexOf(' ');
		if (idx != -1) ret = ret.substring(0, idx);
		return ret;
	}

	function setSelectValue(id, val) {
		var sel = byId(id);
		for (var count = 0; count < sel.options.length; count++) {
			var option = sel.options[count];
			if (option.value == "" + val) option.selected = true;
		}
	}

	function cssHexFormat(str){
		str = str.toLowerCase();
		return str.replace('0x', '#');
	}

	function colorPickerHexFormat(str){
		str = str.toLowerCase();
		str = str.replace('0x', '');
		return str.replace('#', '');
	}

	function setBackgroundColor(id, colorString){
		if(colorString != null && colorString != 'null'){
			byId(id).style.backgroundColor = cssHexFormat(colorString);
		}
	}

