
function newXMLHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
    		// Try ActiveX
		try { 
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) { 
			// first method failed 
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				 // both methods failed 
			} 
		}
 	}
   	return xmlreq;
} 

function JsonRequest() {
}

JsonRequest.prototype.send = function (url, userCallback) {
	var request = newXMLHttpRequest();
	if(request.readyState == 4 || request.readyState == 0) {
	
		request.open("GET", url, true);
		request.onreadystatechange = function () {
			if (request.readyState == 4) try {
				var obj = eval("(" + request.responseText + ")");
				try {
				userCallback(obj);
				} catch (e) {if(console != null) console.log(e);}
			} catch(e) {
			}
		}; 
		
		request.send(null);
	}
}

JsonRequest.prototype.send_post = function(url, userCallback, parameters) {
	var request = newXMLHttpRequest();
	if(request.readyState == 4 || request.readyState == 0) {
		request.open("POST", url, true);
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", parameters.length);
		request.setRequestHeader("Connection", "close");
		request.onreadystatechange = function () {
			if (request.readyState == 4) try {
				var obj = eval("(" + request.responseText + ")");
				try {userCallback(obj);
				} catch (e) {console.log(e);}
			} catch(e) {}
		}; 
		request.send(parameters);
	}
}

function removeChildNodes(elem) {
	while(elem.lastChild) {
		elem.removeChild(elem.lastChild);
	}
}

function popupWindow(x,y,w,h,url,title) {
  var attrs="location=no,menubar=no,toolbar=no,status=no,resizable=no,scrollbars=no,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y;
  subwindow=window.open(url,title,attrs);
}

// ---------------------------------------------------

function DataBinder() {
}

DataBinder.prototype.bindAttr = function(attrName, prop) {
	this[prop] = new AttrBinding(attrName);
}

DataBinder.prototype.bindData = function(root,data) {
	for (var i = 0; i < root.childNodes.length; i++) {
		var node = root.childNodes[i];
		var value = data[node.id];
		if(value != null) {
			if(this[node.id] != null) {
				this[node.id].bind(node,value);
				this.bindData(node,data);
			} else {
				removeChildNodes(node);
				node.appendChild(document.createTextNode(value));
			}
		} else {
			this.bindData(node,data);
		}
	}
}

DataBinder.prototype.bindDataToElem = function(elemId,data) {
	this.bindData(document.getElementById(elemId),data);
}

// ---------------------------------------------------

function RadioButtonRenderer(onIcon,offIcon) {
	this.onIcon = onIcon;
	this.offIcon = offIcon;
}

RadioButtonRenderer.prototype.bind = function(elem,value) {
	removeChildNodes(elem);
	var img = document.createElement('img');
	if(value) {
		img.src = this.onIcon;
	} else {
		img.src = this.offIcon;
	}
	elem.appendChild(img);
}

// ---------------------------------------------------

function ImageSrcRenderer(path,def,post) {
	this.path = path;
	this.def = def;
	this.post = post;
}

ImageSrcRenderer.prototype.bind = function(elem,value) {
	if(value != null && value != '')
		elem.src = this.path + '/' + value + this.post;
	else
		elem.src = this.path + '/' + this.def + this.post;
}

// ---------------------------------------------------

function IconListRenderer(onIcon,offIcon,count,intVal) {
	this.onIcon = onIcon;
	this.offIcon = offIcon;
	this.count = count;
	this.intVal = intVal;
}

IconListRenderer.prototype.bind = function(elem,value) {
	removeChildNodes(elem);
	if(this.intVal) {
		for(var i=0;i<this.count;i++) {
			var img = document.createElement('img');
			if(i <= value) {
				img.src = this.onIcon;
			} else {
				img.src = this.offIcon;
			}
			elem.appendChild(img);
		}
	} else {
		var max = (this.count*value + 0.5);
		for(var i=0;i<this.count;i++) {
			var img = document.createElement('img');
			if(i < max) {
				img.src = this.onIcon;
			} else {
				img.src = this.offIcon;
			}
			elem.appendChild(img);
		}
	}
}
// ---------------------------------------------------

function QuestDetailLinkRenderer(path) {
	this.path = path;

}

QuestDetailLinkRenderer.prototype.bind = function(elem,value) {
	if(value != null && value != '')
		elem.href = this.path + 'mission/' + value;
}

// ---------------------------------------------------
function EnumRenderer(options)
{
	this.options = options;
}

EnumRenderer.prototype.bind = function(elem, value)
{
	for (var i = 0; i < this.options.length; i++)
	{
		if(value == this.options[i].name)
		{
			elem.innerHTML = this.options[i].html;
		}	
	}

}

// ---------------------------------------------------

function BooleanRenderer(trueValue, falseValue, trueStyle, falseStyle) {
	this.trueValue = trueValue;
	this.falseValue = falseValue
	this.trueStyle = trueStyle;
	this.falseStyle = falseStyle
}

BooleanRenderer.prototype.bind = function(elem,value) {
	removeChildNodes(elem);
	var span = document.createElement('span');
	if(value) {
		span.appendChild(document.createTextNode(this.trueValue));
		span.style.fontWeight = "bold";
		span.style.color = "green";
	} else {
		span.appendChild(document.createTextNode(this.falseValue));
		span.style.fontWeight = "bold";
		span.style.color = "red";
	}
	elem.appendChild(span);
}

// ---------------------------------------------------

function DefaultRenderer(prefix,postfix) {
	this.prefix = prefix;
	this.postfix = postfix;
}

DefaultRenderer.prototype.bind = function(elem,value) {
	removeChildNodes(elem);
	elem.appendChild(document.createTextNode(this.prefix + value + this.postfix));
}

// ---------------------------------------------------

function AttrBinding(attrName) {
	this.attrName = attrName;
}


AttrBinding.prototype.bind = function(elem,value) {
	if(this.attrName.toLowerCase() == "onclick") {
		//value = "function;data;data;..."
		if(value == null || value == "") return;
		if(value.indexOf(";") <= 0) {
			AddEvent(elem,"click",window[value]);
			return;
		}
		AddEvent(elem,"click",function() {
			window[value.substring(0,value.indexOf(";"))](value.substring(value.indexOf(";")+1));
			});
		return;
	}
	if(this.attrName.toLowerCase() == "class") {
		if(jQuery.browser.msie) elem.setAttribute("class",value);
		else elem.className = value;
		return;
	}
	elem[this.attrName] = value;
}
// ----------------------------------------------------
function LinkRenderer(path,post)
{
	this.path = path;
	this.post = post;
}

LinkRenderer.prototype.bind = function(elem, value)
{
//	if(value != null && value != '')
//	{
		removeChildNodes(elem);
		elem.href = this.path+value+this.post;
		elem.appendChild(document.createTextNode(value));
//	}
}

// ----------------------------------------------------
function LinkReseter(path)
{
	this.path = path;
}

LinkReseter.prototype.bind = function(elem, value)
{
	elem.href = this.path+value;
}
