﻿// JScript File
var dhtml = {
	aEv: function(elm, evType, fn, useCapture) {
			if (elm.attachEvent) {
			elm['e' + evType + fn] = fn;
			elm[evType + fn] = function() { 
				elm['e'+ evType + fn]( window.event );
			}
			elm.attachEvent('on'+ evType, elm[evType + fn]);
			} else
			  elm.addEventListener(evType, fn, false );
		},
	gB: function(id) {
		return document.getElementById(id);
	},
	gT: function(obj, tag) {
		return obj.getElementsByTagName(tag);
	},
	stripCharacters: function(s) {
		s = s.replace(' ', '_');
		s = s.replace('(', '');
		s = s.replace(')', '');
		return s;
	},
	replace: function(string,text,by) {
	  var stringLength = string.length
	  var textLength = text.length;
	  if ((stringLength == 0) || (textLength == 0)) return string;

	  var i = string.indexOf(text);
	  if ((!i) && (text != string.substring(0,textLength))) return string;
	  if (i == -1) return string;

	  var newstr = string.substring(0,i) + by;

	  if ( (i+textLength) < stringLength)
		newstr += dhtml.replace(string.substring(i + textLength, stringLength),
			text, by);
	  return newstr;
	},
	addElm: function(container, tag, attributes) {
		var obj = document.createElement(tag);
		var _a = attributes.split('|');
		if (_a != null) {
			for (var i = 0; i < _a.length; i++) {
				var attr = _a[i].split('=')[0];
				var value = _a[i].split('=')[1];
				switch (attr) {
					case 'class' :
						obj.className = value;
						break;
					case 'style' :
						var styles = value.split(';');
						var key = styles[0].split(':')[0];
						if (key.indexOf('-') != -1) {
							var a = key.substring(key.indexOf('-') + 1, key.indexOf('-') + 2).toUpperCase();
							var b = key.replace(key.substring(key.indexOf('-'), key.indexOf('-') + 2), a);
							key = b;
						}
						obj.style[key] = styles[0].split(':')[1]
						break;
					default :
						obj.setAttribute(attr, value);	
						break;				
				}
			}
		}
		if (container != null) {
			container.appendChild(obj);
		}else{
			document.appendChild(obj);
		}
		return obj;
	},
	cElm: function(container, tag, attributes) {
		var obj = document.createElement(tag);
		if (attributes != null) {
			var _a = attributes.split('|');
			for (var i = 0; i < _a.length; i++) {
				var attr = _a[i].split('=')[0];
				var value = _a[i].split('=')[1];
				switch (attr) {
					case 'src' :
						obj.src = value;
						break;
					case 'class' :
						obj.className = value;
						break;
					case 'style' :
						var styles = value.split(';');
						var key = styles[0].split(':')[0];
						if (key.indexOf('-') != -1) {
							var a = key.substring(key.indexOf('-') + 1, key.indexOf('-') + 2).toUpperCase();
							var b = key.replace(key.substring(key.indexOf('-'), key.indexOf('-') + 2), a);
							key = b;
						}
						obj.style[key] = styles[0].split(':')[1]
						break;
					default :
						obj.setAttribute(attr, value);	
						break;				
				}
			}
		}
		if (container != null) {
			container.appendChild(obj);
		}else{
			document.appendChild(obj);
		}
		return obj;
	},	
	gX: function(method, url, params) {
		if (!Sarissa) return;
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open(method, url, true);				
		if (method == 'POST') {
			xmlhttp.setRequestHeader("Host", "localhost");
			xmlhttp.setRequestHeader("Content-Type", 
				"application/x-www-form-urlencoded");		
		}
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				alert(xmlhttp.responseText);
				return xmlhttp.responseText;
			}
		};
		xmlhttp.send(params);
	},
	stop: function(e) {
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
	},
	setOpacity: function(elm, value) {
		elm.style.opacity = value / 10;
		elm.style.filter = 'alpha(opacity=' + value * 10 + ')';				
	},
	formatCurrency: function(amount) {
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	},
	sN: function(dom, name) {
		if (dom.selectNodes(name).length > 0) {
			return dom.selectNodes(name)[0].childNodes[0].nodeValue;
		}else{
			return '';
		}
	}	
}