var $ = function(id) {
	if(id) {
		return document.getElementById(id);
	}
};

var $Form = {
	submitById: function(id, action) {
		var form = $(id);
		if(form) {
			if(action) {
				form.action = action;
			}
			form.submit();
		}
	}
};

//-------------------------------------------------------------

var canvera = new Object();
canvera.util = new Object();
canvera.http = new Object();
canvera.dhtml = new Object();

canvera.util.Map = function() {
    var map = new Object();
    var size = 0;
    var keys = new Array();
    var values = new Array();
    this.put = function(key, value) {
        if(typeof key == 'string' && typeof value != 'undefined') {
            map[key] = value;
            keys.push(key);
            values.push(value);
            size++;
        }
    }
    this.get = function(key) {
        var value = null;
        if(typeof key == 'string') {
            value = map[key];
        }
        return value;
    }
    this.has = function(key, value) {
        var has = false;
        if(typeof key == 'string' && typeof value != 'undefined') {
            if(typeof map[key] != 'undefined' && map[key] == value) {
                has = true;
            }
        }
        return has;
    }
    this.hasKey = function(key) {
        var has = false;
        if(typeof key == 'string') {
            for(eachKey in map) {
                if(eachKey == key) {
                    has = true;
                    break;
                }
            }
        }
        return has;
    }
    this.hasValue = function(value) {
        var has = false;
        if(typeof value == 'undefined') {
            for(eachKey in map) {
                if(map[eachKey] == value) {
                    has = true;
                    break;
                }
            }
        }
        return has;
    }
    this.getKeys = function() {
        return keys;
    }
    this.getValues = function() {
        return values;
    }
    this.getSize = function() {
        return size;
    }
}

canvera.http.Request = function(args) {
    var method = 'GET';
    var url = null;
    var async = true;
    var params = new canvera.util.Map();
    var callback = null;

    if(typeof args == 'object') {
        if(typeof args['method'] == 'string') {
            switch(args['method'].toUpperCase()) {
                case 'GET': method = 'GET'; break;
                case 'POST': method = 'POST'; break;
            }
        }
        if(typeof args['url'] == 'string') {
            url = args['url'];
        }
        if(typeof args['async'] == 'boolean') {
            async = args['async'];
        }
        if(typeof args['params'] == 'object') {
            for(name in args['params']) {
                params.put(name, args['params'][name]);
            }
        }
        if(typeof args['callback'] == 'function') {
            callback = args['callback'];
        }
    }
    
    this.getMethod = function() {
        return method;
    }
    this.isGet = function() {
        return method == 'GET';
    }
    this.useGet = function() {
        method = 'GET';
    }
    this.usePost = function() {
        method = 'POST';
    }
    this.isPost = function() {
        return method == 'POST';
    }
    this.isAsynchronous = function() {
        return async;
    }
    this.isSynchronous = function() {
        return !async;
    }
    this.setAsynchronous = function() {
        async = true;
    }
    this.setSynchronous = function() {
        async = false;
    }
    this.getUrl = function() {
        return url;
    }
    this.setUrl = function(arg) {
        if(typeof arg == 'string') {
            url = arg;
        }
    }
    this.getParameterMap = function() {
        return params;
    }
    this.setParameterMap = function(map) {
        if(typeof map == 'object' && map.constructor == canvera.util.Map) {
            params = map;
        }
    }
    this.getCallback = function() {
        return callback;
    }
    this.setCallback = function(arg) {
        if(typeof arg == 'function') {
            callback = arg;
        }
    }
    this.getQueryString = function() {
        var queryString = null;
        var keys = params.getKeys();
        if(keys.length > 0) {
            queryString = '';
            for(var keyIndex = 0; keyIndex < keys.length; keyIndex++) {
                var key = keys[keyIndex];
                var value = params.get(key);
                if(keyIndex > 0) {
                    queryString += '&';
                }
                if(value.constructor == Array) {
                    for(var valIndex = 0; valIndex < value.length; valIndex++) {
                        var val = value[valIndex];
                        var paramString = key + '=' + val;
                        if(valIndex > 0) {
                            queryString += '&';
                        }
                        queryString += paramString;
                    }
                }
                else {
                        var paramString = key + '=' + value;
                        queryString += paramString;
                }
            }
        }
        return queryString;
    }
    this.serialize = function() {
        var queryString = this.getQueryString();
        var fullUrl = url;
        if(typeof queryString == 'string' && queryString.length > 0) {
            if(fullUrl.indexOf('?') == -1) {
                fullUrl = fullUrl + '?' + queryString
            }
            else {
                if(fullUrl.indexOf('?') == fullUrl.length - 1 || fullUrl.indexOf('&') == fullUrl.length - 1) {
                    fullUrl = fullUrl + queryString
                }
                else {
                    fullUrl = fullUrl + '&' + queryString
                }
            }
        }
        return fullUrl;
    }
}

canvera.http.Response = function(args) {
    var statusCode = null;
    var statusMessage = null;
    var headerString = null;
    var headers = canvera.util.Map();
    var text = null;
    var xml = null;
    
    if(typeof args['status'] == 'number') {
        statusCode = args['status'];
    }
    if(typeof args['message'] == 'string') {
        statusMessage = args['message'];
    }
    if(typeof args['headers'] == 'string') {
        headerString = args['headers'];
    }
    if(typeof args['text'] == 'string') {
        text = args['text'];
    }
    if(typeof args['xml'] == 'object') {
        xml = args['xml'];
    }
    
    this.getStatusCode = function() {
        return statusCode;
    }
    this.getStatusMessage = function() {
        return statusMessage;
    }
    this.getHeader = function(name) {
        return headers.get(name);
    }
    this.getHeaderString = function() {
        return headerString;
    }
    this.getText = function() {
        return text;
    }
    this.getXml = function() {
        return xml;
    }
}

canvera.http.Processor = {
    process: function(request) {
        if(request) {
            var paramMap = request.getParameterMap();
            if(paramMap) {
                var time = '' + new Date().getTime();
                paramMap.put(time, time);
                request.setParameterMap(paramMap);
            }
        }
        var xmlHttpRequest = null;
        if(window.ActiveXObject) {
            xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
        }
        else {
            xmlHttpRequest = new XMLHttpRequest();
        }
        if(xmlHttpRequest) {
            if(request.isGet()) {
                xmlHttpRequest.open(request.getMethod(), request.serialize(), request.isAsynchronous());
            }
            else if(request.isPost()) {
                xmlHttpRequest.open(request.getMethod(), request.getUrl(), request.isAsynchronous());
            }
            var createResponse = function(ajaxRequest) {
                var responseArgs = {
                    status: ajaxRequest.status,
                    message: ajaxRequest.statusText,
                    headers: ajaxRequest.getAllResponseHeaders(),
                    text: ajaxRequest.responseText,
                    xml: ajaxRequest.responseXML
                }
                return new canvera.http.Response(responseArgs);
            }
            if(request.isAsynchronous()) {
                xmlHttpRequest.onreadystatechange = function() {
                    if(xmlHttpRequest.readyState == 4) {
                        var response = createResponse(xmlHttpRequest);
                        var callback = request.getCallback();
                        if(callback) {
                            callback(request, response);
                        }
                    }
                }
            }
            if(request.isGet()) {
                xmlHttpRequest.send(null);
            }
            else if(request.isPost()) {
                xmlHttpRequest.send(request.getQueryString());
            }
            if(request.isSynchronous()) {
                return createResponse(xmlHttpRequest);
            }
        }
    }
}


//Popup script

canvera.dhtml.Popup = function(defaultProperties) {
    if(typeof(defaultProperties) == 'object' && typeof(defaultProperties.name) == 'string' && defaultProperties.name.length > 0) {
        var name = defaultProperties.name;
        var src = defaultProperties.src;
        var scheme = defaultProperties.scheme;
        var title = defaultProperties.title;
        var left = defaultProperties.left;
        var top = defaultProperties.top;
        var width = defaultProperties.width;
        var height = defaultProperties.height;
        var overflow = defaultProperties.overflow;
        var center = defaultProperties.center;
        var modal = defaultProperties.modal;
        var draggable = defaultProperties.draggable;
        var parameters = null;
        var popupElement = $(defaultProperties.popupElement);
        var dragHandle = $(defaultProperties.dragHandle);
        var contentHolder = $(defaultProperties.contentHolder);
        var eventMask = null;
        var onLoad = defaultProperties.onLoad;
        var onOpen = defaultProperties.onOpen;
        var onClose = defaultProperties.onClose;
        var onUnload = defaultProperties.onUnload;
        var mouseIsDown = false;
        var startX;
        var startY;
        var endX;
        var endY;
	    this.open = function(customProperties) {
	        if(typeof(customProperties) == 'object') {
	           if(typeof(customProperties.src) == 'string' && customProperties.src.length > 0) {
	               src = customProperties.src;
	           }
               if(typeof(customProperties.scheme) == 'string' && customProperties.scheme.length > 0) {
                   scheme = customProperties.scheme;
               }
               if(typeof(customProperties.title) == 'string') {
                   title = customProperties.title;
               }
               if(typeof(customProperties.left) == 'number' && customProperties.left >= 0) {
                   left = customProperties.left;
               }
               if(typeof(customProperties.top) == 'number' && customProperties.top >= 0) {
                   top = customProperties.top;
               }
               if(typeof(customProperties.width) == 'number' && customProperties.width > 0) {
                   width = customProperties.width;
               }
               if(typeof(customProperties.height) == 'number' && customProperties.height > 0) {
                   height = customProperties.height;
               }
               if(typeof(customProperties.overflow) == 'string' && customProperties.overflow.length > 0) {
                   overflow = customProperties.overflow;
               }
               if(typeof(customProperties.center) != 'undefined') {
                   center = customProperties.center;
               }
               if(typeof(customProperties.modal) != 'undefined') {
                   modal = customProperties.modal;
               }
               if(typeof(customProperties.draggable) != 'undefined') {
                   draggable = customProperties.draggable;
               }
               if(typeof(customProperties.popupElement) == 'string' && customProperties.popupElement.length > 0) {
                   popupElement = $(customProperties.popupElement);
               }
               if(typeof(customProperties.dragHandle) == 'string' && customProperties.dragHandle.length > 0) {
                   dragHandle = $(customProperties.dragHandle);
               }
               if(typeof(customProperties.contentHolder) == 'string' && customProperties.contentHolder.length > 0) {
                   contentHolder = $(customProperties.contentHolder);
               }
               if(typeof(customProperties.onLoad) == 'function') {
                   onLoad = customProperties.onLoad;
               }
               if(typeof(customProperties.onOpen) == 'function') {
                   onOpen = customProperties.onOpen;
               }
               if(typeof(customProperties.onClose) == 'function') {
                   onClose = customProperties.onClose;
               }
               if(typeof(customProperties.onUnload) == 'function') {
                   onUnload = customProperties.onUnload;
               }
               parameters = customProperties.parameters;
	        }
            var thisPopup = this;
	        if(popupElement) {
	           if(popupElement.parentNode != document.body) {
		           popupElement.parentNode.removeChild(popupElement);
		           document.body.appendChild(popupElement);
	           }
	           popupElement.onkeypress = function(event) {
	               var esc;
	               if(typeof(event) == 'undefined') {
	                   esc = 27;
	                   event = window.event;
	               }
	               else {
	                   esc = event.DOM_VK_ESCAPE;
	               }
	               if(event.keyCode == esc) {
	                   thisPopup.close();
	               }
	           }
	        }
	        if(draggable && dragHandle && popupElement) {
	           dragHandle.onmousedown = function(event) {
	               dragHandle.style.cursor = 'move';
	               if(typeof(event) == 'undefined') {
	                   event = window.event;
	               }
	               startX = event.clientX;
	               startY = event.clientY;
	               mouseIsDown = true;
	           }
               popupElement.onmouseup = function(event) {
                   dragHandle.style.cursor = 'default';
                   if(typeof(event) == 'undefined') {
                       event = window.event;
                   }
                   mouseIsDown = false;
               }
               popupElement.onmousemove = function(event) {
                   if(typeof(event) == 'undefined') {
                       event = window.event;
                   }
                   if(mouseIsDown) {
                       endX = event.clientX;
                       endY = event.clientY;
                       thisPopup.drag();
                       startX = endX;
                       startY = endY;
                   }
               }
               popupElement.onmouseout = dragHandle.onmouseup;
	        }
	        if(modal) {
	            this.mask();
            }
	        this.resize(width, height);
	        this.overflow(overflow);
	        this.load(src, parameters);
	        if(center) {
	           this.center();
	        }
	        else {
                this.move(left, top);
            }
            this.show();
            this.onOpen();
	    }
	    this.drag = function() {
	       if(popupElement) {
	           var x = left + endX - startX;
	           var y = top + endY - startY;
	           var maxX = canvera.dhtml.Window.getViewportLeft() + canvera.dhtml.Window.getViewportWidth() - width;
               var maxY = canvera.dhtml.Window.getViewportTop() + canvera.dhtml.Window.getViewportHeight() - height;
	           if(x >= 0 && x <= maxX && y >= 0 && y <= maxY) {
	               this.move(x, y);
	           }
	       }
	    }
	    this.close = function() {
           this.onClose();
	       this.hide();
	       this.unload();
	       if(modal) {
	           this.unmask();
	       }
	    }
        this.show = function() {
           if(popupElement) {
               popupElement.style.display = 'block';
           }
           if(contentHolder) {
               contentHolder.style.display = 'block';
           }
        }
        this.hide = function() {
           if(contentHolder) {
               contentHolder.style.display = 'none';
           }
           if(popupElement) {
               popupElement.style.display = 'none';
           }
        }
        this.unload = function() {
           if(contentHolder) {
               this.onUnload();
               contentHolder.innerHTML = '';
           }
        }
        this.move = function(x, y) {
            if(popupElement) {
               if(typeof(x) == 'number' && x >= 0) {
                   left = x;
                   popupElement.style.left = x + 'px';
               }
               if(typeof(y) == 'number' && y >= 0) {
                   top = y;
                   popupElement.style.top = y + 'px';
               }
            }
        }
        this.resize = function(w, h) {
            if(contentHolder) {
               if(typeof(width) == 'number' && width >= 0) {
                   width = w;
                   contentHolder.style.width = width - 40 + 'px';
               }
               if(typeof(height) == 'number' && height >= 0) {
                   height = h;
                   contentHolder.style.height = height - 67 + 'px';
               }
           }
        }
        this.load = function(source, parameters) {
            if(contentHolder) {
               if(typeof(source) == 'string' && source.length > 0) {
                   switch(scheme) {
                       case 'ajax': {
	                       var request = new canvera.http.Request({url:source,params:parameters,async:false});
	                       var response = canvera.http.Processor.process(request);
	                       contentHolder.innerHTML = response.getText();
	                       break;
                       }
                       case 'dom': {
                           var srcElement = $(source);
                           if(srcElement) {
	                           contentHolder.innerHTML = srcElement.innerHTML;
                           }
                           break;
                       }
                   }
                   this.onLoad();
               }
            }
        }
        this.overflow = function(action) {
            if(contentHolder) {
	            contentHolder.style.overflow = overflow;
            }
        }
        this.mask = function() {
            if(eventMask == null || typeof(eventMask) == 'undefined') {
                eventMask = document.createElement('div');
                document.body.appendChild(eventMask);
	            eventMask.className = 'Mask';
	            eventMask.style.left = '0px';
                eventMask.style.top = '0px';
	            eventMask.style.width = canvera.dhtml.Window.getPageWidth() + 'px';
                eventMask.style.height = canvera.dhtml.Window.getPageHeight() + 'px';
	            eventMask.style.display = 'block';
            }
        }
        this.unmask = function() {
            if(eventMask) {
                document.body.removeChild(eventMask);
                eventMask = null;
            }
        }
        this.onLoad = function() {
            if(typeof(onLoad) == 'function') {
                onLoad();
            }
        }
        this.onOpen = function() {
            if(typeof(onOpen) == 'function') {
                onOpen();
            }
        }
        this.onClose = function() {
            if(typeof(onClose) == 'function') {
                onClose();
            }
        }
        this.onUnload = function() {
            if(typeof(onUnload) == 'function') {
                onUnload();
            }
        }
        this.center = function() {
            var x = canvera.dhtml.Window.getViewportLeft() + (canvera.dhtml.Window.getViewportWidth() - width)/2;
            var y = canvera.dhtml.Window.getViewportTop() + (canvera.dhtml.Window.getViewportHeight() - height)/2;
            this.move(x, y);
        }
    }
};

canvera.dhtml.Window = {
	getViewportWidth: function() {
	    var viewportWidth;
	    if (typeof window.innerWidth != 'undefined')
	    {
	         viewportWidth = window.innerWidth;
	    }
	    else if (typeof document.documentElement != 'undefined'
	        && typeof document.documentElement.clientWidth !=
	        'undefined' && document.documentElement.clientWidth != 0)
	    {
	          viewportWidth = document.documentElement.clientWidth;
	    }
	    else
	    {
	          viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
	    }
	    return viewportWidth;
	},
	getViewportHeight: function() {
	    var viewportHeight;
	    if (typeof window.innerHeight != 'undefined')
	    {
	         viewportHeight = window.innerHeight;
	    }
	    else if (typeof document.documentElement != 'undefined'
	        && typeof document.documentElement.clientHeight !=
	        'undefined' && document.documentElement.clientHeight != 0)
	    {
	          viewportHeight = document.documentElement.clientHeight;
	    }
	    else
	    {
	          viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
	    }
	    return viewportHeight;
	},
	getViewportLeft: function() {
	    var viewportLeft;
	    if(window.pageXOffset) {
	        viewportLeft = window.pageXOffset;
	    }
	    else if(document.body.scrollLeft) {
	        viewportLeft = document.body.scrollLeft;
	    }
	    else {
            viewportLeft = document.documentElement.scrollLeft;
	    }
	    return viewportLeft;
	},
	getViewportTop: function() {
	    var viewportTop;
	    if(window.pageYOffset) {
	        viewportTop = window.pageYOffset;
	    }
	    else if(document.body.scrollTop) {
	        viewportTop = document.body.scrollTop;
	    }
	    else {
            viewportTop = document.documentElement.scrollTop;
	    }
	    return viewportTop;
	},
	getPageWidth: function() {
	   var pageWidth;
	   if(window.scrollMaxX) {
	       pageWidth = window.innerWidth + window.scrollMaxX;
	   }
	   else {
	       if(document.body.scrollWidth > document.documentElement.scrollWidth) {
	           pageWidth = document.body.scrollWidth;
	       }
	       else {
               pageWidth = document.documentElement.scrollWidth;
	       }
	   }
	   return pageWidth;
	},
	getPageHeight: function() {
       var pageHeight;
       if(window.scrollMaxY) {
           pageHeight = window.innerHeight + window.scrollMaxY;
       }
       else {
			if(document.body.scrollHeight > document.documentElement.scrollHeight) {
			   pageHeight = document.body.scrollHeight;
			}
			else {
               pageHeight = document.documentElement.scrollHeight;
			}
       }
       return pageHeight;
	}
};

canvera.dhtml.Browser = {
    isIE: function() {
        if(document.all) {
            return true;
        }
        else {
            return false;
        }
    }
};

canvera.dhtml.Selectable = function(properties) {
    this.id = properties.id;
    this.element = properties.element;
    this.onSelect = properties.onSelect;
    this.onDeselect = properties.onDeselect;
    this.name = properties.name;
    this.value = properties.value;
    
    var html = "<input id='" + properties.id + "' type='checkbox' name='" + properties.name + "' value='" + properties.value + "' style='display:none;'/>";
    document.write(html);
    properties.element.select = function() {
        properties.onSelect(properties.element);
        properties.element.onclick = properties.element.deselect;
        $(properties.id).checked = true;
    }
    properties.element.deselect = function() {
        properties.onDeselect(properties.element);
        properties.element.onclick = properties.element.select;
        $(properties.id).checked = false;
    }
    properties.element.onclick = properties.element.select;
    if(typeof canvera.dhtml.Selectable[properties.name] == 'undefined') {
        canvera.dhtml.Selectable[properties.name] = new Array();
    }
    canvera.dhtml.Selectable[properties.name].push(properties.element);
}

canvera.dhtml.Selectable.selectAll = function(identifier) {
    if(canvera.dhtml.Selectable[identifier] && canvera.dhtml.Selectable[identifier].length > 0) {
        for(var index = 0; index < canvera.dhtml.Selectable[identifier].length; index++) {
            canvera.dhtml.Selectable[identifier][index].select();
        }
    }
}

canvera.dhtml.Selectable.deselectAll = function(identifier) {
    if(canvera.dhtml.Selectable[identifier] && canvera.dhtml.Selectable[identifier].length > 0) {
        for(var index = 0; index < canvera.dhtml.Selectable[identifier].length; index++) {
            canvera.dhtml.Selectable[identifier][index].deselect();
        }
    }
}
