/* 
Title:   cpn.js
Author:  Shadanan Sharma
Site:    http://www.shadanan.com/
Version: 1.0
License: This script is released under the terms specified by
         the LGPL (GNU Lesser General Public License). Under this
         license, the user may use, distribute and link with
         this code in closed source development. Any modifications
         to this code must be released under the LGPL or other
         stricter copy-left licenses. This notice must be included
         in any derivative work.
*/

function navigate(dest) {
	if (document.getElementById("maincontent") == null) this.location.href = dest;
	else document.getElementById("maincontent").src = dest;
}

function post(url, params, callback) {
	var http = getXmlHttp();
	http.open("POST", url, true);
	
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			callback(http.responseXML.documentElement, http);
		}
	}
	
	http.send(params);
}

function get(url, callback) {
	var http = getXmlHttp();
	
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			callback(http.responseXML.documentElement, http);
		}
	}
	
	http.open("GET", url, true);
	http.send(null);
}

function getXmlHttp() {
	try {
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { 
			try { 
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("You must enable JavaScript to use this page.");
			}
		}
	}
}

function MaxSizeDiv(div, height) {
	//Properties
	this.div = div;
	this.maxheight = height;
	
	//Methods
	this.resize = function() {
		this.div.style.height = "auto";
		if (this.div.offsetHeight > this.maxheight) {
			this.div.style.height = (this.maxheight + 'px');
		}
	}
	
	this.alertHeight = function() {
		this.div.style.height = "auto";
		alert("Height: " + this.div.style.height + ", OffsetHeight: " + this.div.offsetHeight + ", Max Height: " + this.maxheight);
	}
	
	//Init
	this.div.style.overflow = "auto";
	this.resize();
}

function getQueryVariable(key) {
	var query = window.location.search.substring(1);
	var keys = query.split("&");
	for (var i = 0; i < keys.length; i++) {
		var keyval = keys[i].split("=");
		if (keyval[0] == key) return keyval[1];
	}
}

function MessageBox(div) {
	//Properties
	this.div = div;
	
	//Init
	this.div.style.display = 'none';

	//Methods
	this.show = function() {
		this.div.style.display = 'block';
	}
	
	this.hide = function() {
		this.div.style.display = 'none';
	}
	
	this.showMessage = function(msg) {
		this.div.innerHTML = msg;
		this.div.style.display = 'block';
	}
	
	this.setMessage = function(msg) {
		this.div.innerHTML = msg;
	}
}
