﻿var getElement = function (id) { return (document.all ? document.all[id] : document.getElementById(id)); }

function createDiv(props, onclick){
	var div = document.createElement('DIV');
	if(props.id != null){ div.id = props.id; }
	if(props.css != null){ div.className = props.css; }
	if(props.html != null){ div.innerHTML = props.html; }
	if(onclick != null){ div.onclick = (typeof(onclick) == 'function' ? onclick : new Function(onclick)); }
	return div;
}

function createAnchor(props, onclick){
	var anchor = document.createElement('A');
	if(props.href != null){ anchor.href = props.href; }
	if(props.id != null){ anchor.id = props.id; }
	if(props.css != null){ anchor.className = props.css; }
	if(props.target != null){ anchor.target = props.target; }
	if(props.html != null){ anchor.innerHTML = props.html; }
	if(onclick != null){ anchor.onclick = (typeof(onclick) == 'function' ? onclick : new Function(onclick)); }
	return anchor;
}

function createElem(tag, props, onclick){
	var elem = document.createElement(tag);
	if(props.id != null){ elem.id = props.id; }
	if(props.css != null){ elem.className = props.css; }
	if(props.html != null){ elem.innerHTML = props.html; }
	if(onclick != null){ elem.onclick = (typeof(onclick) == 'function' ? onclick : new Function(onclick)); }
	return elem;
}

function createInput(props, onclick){
	var elem = document.createElement('INPUT');
	if(props.type != null){ elem.type = props.type; }
	if(props.id != null){ elem.id = props.id; }
	if(props.css != null){ elem.className = props.css; }
	if(props.value != null){ elem.value = props.value; }
	if(onclick != null){ elem.onclick = (typeof(onclick) == 'function' ? onclick : new Function(onclick)); }
	return elem;
}

function createTable(props){
	var table = document.createElement('TABLE');
	if(props.id != null){ table.id = props.id; }
	if(props.css != null){ table.className = props.css; }
	if(props.cellPadding != null){ table.cellPadding = props.cellPadding; }
	if(props.cellSpacing != null){ table.cellSpacing = props.cellSpacing; }
	if(props.border != null){ table.border = props.border; }
	return table;
}

function appendTableRow(table){
	return table.insertRow(table.rows.length);
}

function appendTableCell(row, props){
	var cell = row.insertCell(row.cells.length);
	if(props.css != null){ cell.className = props.css; }
	if(props.align != null){ cell.align = props.align; }
	if(props.vAlign != null){ cell.vAlign = props.vAlign; }
	if(props.width != null){ cell.width = props.width; }
	if(props.html != null){ cell.innerHTML = props.html; }
	if(props.colSpan != null){ cell.colSpan = props.colSpan; }
	if(props.rowSpan != null){ cell.rowSpan = props.rowSpan; }
	if(props.title != null){ cell.title = props.title; }
	return cell;
}

function setOpacity(elem, val){
    try{
	    var filterOpacity = val*100;
	    elem.style.filter = 'alpha(opacity:' + filterOpacity + ')';
	    elem.style.opacity = val.toString();
	    elem.style.KHTMLOpacity = val.toString();
	    elem.style.MozOpacity = val.toString();
	}
	catch(e){}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if(obj.offsetParent){
	    do{
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }while(obj = obj.offsetParent);
    }
    return {left:curleft,top:curtop};
}

function addEvent(obj, type, fn){ 
    if(obj.attachEvent){ obj.attachEvent('on' + type, fn); }
    else if(obj.addEventListener){ obj.addEventListener(type, fn, false); }
} 

function removeEvent(obj, type, fn){ 
    if(obj.detachEvent){ obj.detachEvent('on' + type, fn); }
    else if(obj.addEventListener){ obj.removeEventListener(type, fn, false); }
}

function cancelEventBubble(e){
	if (!e) var e = window.event;
	if(e){
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();
	}
}

function replace(str, find, replaceString){
	while(str.indexOf(find) != -1){
	    str = str.replace(find, replaceString);
    }
    return str;
}
