/*********************************************************************************/
/*  KASANOVA JavaScript Framework, Version 1.5.00                                */
/*  (c) 2007 MasterPrimes                                                        */
/*                                                                               */
/*   Licensed under the Apache License, Version 2.0 (the "License");             */
/*   you may not use this file except in compliance with the License.            */
/*   You may obtain a copy of the License at                                     */
/*                                                                               */
/*       http://www.apache.org/licenses/LICENSE-2.0                              */
/*                                                                               */
/*   Unless required by applicable law or agreed to in writing, software         */
/*   distributed under the License is distributed on an "AS IS" BASIS,           */
/*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    */
/*   See the License for the specific language governing permissions and         */
/*   limitations under the License.                                              */
/*                                                                               */
/*  For details, see the MasterPrimes web site: http://www.masterprimes.com/     */
/*                                                                               */
/*********************************************************************************/
//Kasanova Parameter
if(typeof kasanova_define=="undefined"){
	var kasanova_define="kasanova.xml";
}
if(typeof kasanova_http_count=="undefined") {
	var kasanova_http_count=3;
}
if(typeof kasanova_doc_cache=="undefined") {
	var kasanova_doc_cache=5;
}
if(typeof kasanova_xmlload_wait=="undefined") {
	var kasanova_xmlload_wait=2000;
}
if(typeof kasanova_xmlwait_interval=="undefined") {
	var kasanova_xmlwait_interval=200;
}
if(typeof kasanova_json_count=="undefined") {
	var kasanova_json_count=3;
}

//XML Parameter
var NODE_ELEMENT=1;

//Kasanova Error View
function showErrorMessage(stMsg) {
	if(typeof kasanova_error_area!="undefined") {
		var el = document.getElementById(kasanova_error_area);
		if (el!=null) {
			if (el.innerHTML!="") el.innerHTML += "<br/>";
			el.innerHTML += stMsg;
		}
	}
}
function showExceptionMessage(e, methodName) {
	if(typeof kasanova_error_area!="undefined") {
		var el = document.getElementById(kasanova_error_area);
		var stMessage;
		if (el!=null) {
			if (el.innerHTML!="") el.innerHTML += "<br/>";
			el.innerHTML += _getExceptionMessage(e, methodName);
		}
	}
}
function _getExceptionMessage(e, methodName) {
	var msg;
	if (e instanceof Error) {
		msg = "Cause Exception: (MethodName: " + methodName + ", ";
		msg += "description: " + e.description + ", errorNumber: " + e.number + ")";
	} else {
		msg = "Cause Exception: (" + e.toString() + ")";
	}
	return msg;
}

function _errorEventAndMsg(errfunc, obj, errcode, errMsg) {
	if (errfunc!=null) {
		if (obj!=null) {
			errfunc(obj, [errcode, errMsg]);
		} else {
			errfunc([errcode, errMsg]);
		}
	}
	showErrorMessage(errMsg);
}

function _errorExceptionAndMsg(errfunc, obj, errcode, e, methodName) {
	var errMsg = _getExceptionMessage(e, methodName);
	if (errfunc!=null) {
		if (obj!=null) {
			errfunc(obj, [errcode, errMsg]);
		} else {
			errfunc([errcode, errMsg]);
		}
	}
	showErrorMessage(errMsg);
}

var ksvHttp = new Array(kasanova_http_count);
function KSVHttpInit() {
	for (var i=0; i<kasanova_http_count; i++) {
		ksvHttp[i] = new KSVHttp();
	}
}

function loadContent(obj, doc, func, errfunc, mode) {
	return loadContentExt(obj, doc, func, errfunc, mode, "get", "");
}
function loadContentExt(obj, doc, func, errfunc, mode, method, param, charset) {
	var min = kasanova_doc_cache;
	var mi = -1;
	for (var i=0; i<kasanova_http_count; i++) {
		if (ksvHttp[i].httpObj!=null && min>ksvHttp[i].docCount
			&& ksvHttp[i].docCount<kasanova_doc_cache) {
			mi = i;
			min = ksvHttp[i].docCount;
		}
	}
	if (mi!=-1) {
		ksvHttp[mi].cache(obj, doc, func, errfunc, mode, method, param, charset);
		return true;
	} else {
		_errorEventAndMsg(errfunc, obj, -10001, "Error: Not Used Kasanova Http Object");
		return false;
	}
}

//Kasanova HTTP Info Object
KSVHttpInfo=function() {
	this.obj = null;
	this.doc = null;
	this.func = null;
	this.errfunc = null;
	this.method = null;
	this.param = null;
	this.mode = 0;
	this.charset = null;
};
//Kasanova HTTP Object
KSVHttp=function() {
	this.httpObj = null;
	try {
		if (window.XMLHttpRequest) {
			this.httpObj = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			try {
				this.httpObj = new ActiveXObject("Msxml2.XMLHTTP") ;
			} catch (e1) {
				this.httpObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	} catch(e) {
		showExceptionMessage(e, "KSVHttp()");
		this.httpObj=null;
	}
	this.docIndex = 0;
	this.docPos = 0;
	this.docCount = 0;
	this.isLoad = false;
	this.docCache = null;
	if (this.httpObj!=null) {
		this.docCache = new Array(kasanova_doc_cache);
		for (var i=0; i<kasanova_doc_cache; i++) {
			this.docCache[i] = null;
		}
	}
};

KSVHttp.prototype={
	load:function(hInfo) {
		var self = this;
		var contentType = "";
		var charset="";
		if (self.httpObj==null) return;
		try {
			self.isLoad = true;
			if (hInfo.param=="" || hInfo.param==null) hInfo.method = "get";
			self.httpObj.open(hInfo.method, hInfo.doc, true);
			if (hInfo.charset!=null && hInfo.charset.length>0){
				charset = "charset=" + hInfo.charset;
			}
			if (hInfo.method=="post") {
				contentType = "application/x-www-form-urlencoded";
				if (charset.length>0) contentType+="; " + charset;
			} else if (hInfo.charset!=null && hInfo.charset.length>0){
				switch (hInfo.mode) {
					case 0: // Type text/html
						contentType = "text/html; " + charset;
						break;
					case 1: // Type xml
						contentType = "application/xml; " + charset;
						break;
				}
			}
			if (contentType.length>0) {
				self.httpObj.setRequestHeader("Content-Type", contentType);
			}
			if (typeof kasanova_http_nocache!="undefined" && kasanova_http_nocache){
				self.httpObj.setRequestHeader("'Cache-Control", "no-cache");
				self.httpObj.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
			}
			self.httpObj.onreadystatechange=function(){
				if (self.httpObj.readyState==4) {
					if (self.httpObj.status==200) {
						if (typeof(hInfo.func)=="function") {
							switch (hInfo.mode) {
							case 0:
								if (hInfo.obj!=null){
									hInfo.func(hInfo.obj, self.httpObj.responseText);
								} else {
									hInfo.func(self.httpObj.responseText);
								}
								break;
							case 1:
								if (hInfo.obj!=null){
									hInfo.func(hInfo.obj, self.httpObj.responseXML);
								} else {
									hInfo.func(self.httpObj.responseXML);
								}
								break;
							default:
								_errorEventAndMsg(hInfo.errfunc, hInfo.obj, -10002, 
													"Error: Invalid Mode Parameter");
								break;
							}
						}
					} else {
						_errorEventAndMsg(hInfo.errfunc, hInfo.obj, -10003, 
										"Error: Document Load Failed (Name: " + hInfo.doc + 
										", HttpStatus:" + self.httpObj.status + ")");
					}
					self.setHttpInfo(hInfo,null,null,null,0,null,null,null);
					self.docCount--;
					if (self.docPos < kasanova_doc_cache-1) {
						self.docPos++;
					} else {
						self.docPos=0;
					}
					if (self.docCount>0) {
						self.load(self.docCache[self.docPos]);
					} else self.isLoad = false;
				}
			}
			self.httpObj.send(hInfo.param);
		} catch(e) {
			_errorExceptionAndMsg(hInfo.errfunc, hInfo.obj, -10004, e, "KSVHttp.load()")
		}
	},
	cache:function(obj,doc,func,errfunc,mode,method,param,charset) {
		var hInfo = this.docCache[this.docIndex];
		if (hInfo==null) {
			hInfo = new KSVHttpInfo();
			this.docCache[this.docIndex] = hInfo;
		}
		this.setHttpInfo(hInfo,obj,doc,func,errfunc,mode,method,param,charset);
		this.docCount++;
		if (this.docIndex < kasanova_doc_cache-1) {
			this.docIndex++;
		} else {
			this.docIndex=0;
		}
		if (this.docCount>0) {
			hInfo = this.docCache[this.docPos];
			if (!this.isLoad) this.load(hInfo);
		}
	},
	setHttpInfo:function(hInfo,obj,doc,func,errfunc,mode,method,param,charset) {
		hInfo.obj = obj;
		hInfo.doc = doc;
		hInfo.func = func;
		hInfo.errfunc = errfunc;
		hInfo.mode = mode;
		hInfo.method = method;
		hInfo.param = param;
		hInfo.charset = charset;
	}
};

//JSON Request
KSVJsonRequest=function(jsonCount) {
	this._jsonCount = jsonCount;
	this._pos = 0;
	this._id = new Array(jsonCount);
	var d = new Date();
	for (i=0; i<jsonCount; i++) {
		this._id[i] = "ksv_json_" + d.getTime() + "_" + i;
	}
}

KSVJsonRequest.prototype = {
	load: function(url, charset) {
		try {
			var self = this;
			var id = self._id[self._pos]
			var scr = document.getElementById(id);
			var head = document.getElementsByTagName("head")[0];
			if (scr!=null) head.removeChild(scr);
			scr = document.createElement("script");
			scr.setAttribute("type", "text/javascript");
			if (charset!=null && charset.length>0) {
				scr.setAttribute("charset", charset);
			}
			scr.setAttribute("src", url);
			scr.setAttribute("id", id);
			head.appendChild(scr);
			if (self._pos < self._jsonCount-1) {
				this._pos++;
			} else {
				this._pos=0;
			}
		} catch(e) {
			showExceptionMessage(e, "KSVJsonRequest.load");
		}
	}
};

var ksvJsonRequest = new KSVJsonRequest(kasanova_json_count);

//KASANOVA Timer Object
KSVTimer=function(ksv, ival, bval, view, scr) {
	this.ksv = ksv;
	this.interval = ival;
	this.disabled = bval;
	this.view = view;
	this.script = scr;
}

KSVTimer.prototype={
	run:function() {
		var self = this;
		if (self.disabled) return;
		setTimeout(function(){self._call(self)}, self.interval);
	},
	_call:function(self) {
		if (self.disabled) return;
		if (self.view!=null) {
			var viewObj = self.ksv.views[self.view];
			if (viewObj!=null && viewObj!=undefined) {
				viewObj.run();
				self.run();
			} else {
				showErrorMessage("Error: Not View Object(Name: " + self.view + ")");
			}
		} else if (self.script!=null) {
			if(eval("typeof(" + self.script +")")=="function") {
				try {
					eval(self.script + "(self)");
					self.run();
				} catch(e) {
					showExceptionMessage(e, "KSVTimer.run");
				}
			} else {
				showErrorMessage("Error: Invalid function name(Name: " + self.script + ")");
			}
		}
	},
	setDisabled:function(bval) {
		this.disabled = bval;
		if (!bval) this.run();
	}
};

//Kasanova Attr Object
KSVAttr=function(id, nm, v) {
	this.id = id;
	this.name = nm;
	this.value = v;
}
//Kasanova Event Object
KSVEvent=function(id, oname, ev, v, sc) {
	this.id = id;
	this.oname = oname;
	this.event = ev;
	this.view = v;
	this.script = sc;
	this.eventElement = null;
	this.func = null;
}
//Kasanova Custom Object
KSVCustom=function(name, func) {
	this.name = name;
	this.func = func;
};
//Kasanova ErrorPage Object
KSVError=function(nm, cnt, tg) {
	this.name = nm;
	this.content = cnt;
	this.target = tg;
};

//Kasanova View Object
KSVView=function(ksv, nm, cnt, id, sc, md, ch, ev1, ev2, ev3) {
	this.ksv = ksv;
	this.name = nm;
	this.content = cnt;
	this.target = id;
	this.script = sc;
	this.method = md;
	this.charset = ch;
	this.attrs = null;
	this.params = null;
	this.events = null;
	this.vars = null;
	this.customObjects = null;
	this.onbeforeload = ev1;
	this.onload = ev2;
	this.onerror = ev3;
};

KSVView.prototype={
	load:function(viewObj) {
		var id, nm, v, ev,tp,o,name,obj;
		var self = this;
		for (var i=0; i < viewObj.childNodes.length; i++) {
			if (viewObj.childNodes[i].nodeType!=NODE_ELEMENT) continue;
			if (viewObj.childNodes[i].nodeName=="attr") {
				id = viewObj.childNodes[i].getAttribute("id");
				nm = viewObj.childNodes[i].getAttribute("name");
				v = viewObj.childNodes[i].getAttribute("value");
				if (id!=null && nm!=null) {
					obj = new KSVAttr(id, nm, v);
					if (self.attrs==null) self.attrs = new Array();
					self.attrs.push(obj);
				} else {
					showErrorMessage("Error: Invalid Element(Name: " + viewObj.childNodes[i].nodeName + ")");
				}
			} else if (viewObj.childNodes[i].nodeName=="event") {
				id = viewObj.childNodes[i].getAttribute("id");
				o = viewObj.childNodes[i].getAttribute("object");
				ev = viewObj.childNodes[i].getAttribute("event");
				v = viewObj.childNodes[i].getAttribute("view");
				nm = viewObj.childNodes[i].getAttribute("script");
				if (id!=null && o!=null) {
					showErrorMessage("Error: Invalid Element(Name: " + viewObj.childNodes[i].nodeName + ")");
					continue;
				}
				if (id!=null && ev!=null && (v!=null || nm!=null)) {
					obj = new KSVEvent(id, null, ev, v, nm);
					if (self.events==null) self.events = new Array();
					self.events.push(obj);
				} else if (o!=null && ev!=null && (v!=null || nm!=null)) {
					obj = new KSVEvent(null, o, ev, v, nm);
					if (self.events==null) self.events = new Array();
					self.events.push(obj);
				} else {
					showErrorMessage("Error: Invalid Element(Name: " + viewObj.childNodes[i].nodeName + ")");
				}
			} else if (viewObj.childNodes[i].nodeName=="var") {
				nm = viewObj.childNodes[i].getAttribute("name");
				v = viewObj.childNodes[i].getAttribute("value");
				tp = viewObj.childNodes[i].getAttribute("type");
				if (nm!=null && nm!="") {
					if (self.vars==null) self.vars = new Array();
					try {
						if (tp=="string") {
							self.vars[nm] = new String(v);
						} else if (tp=="int") {
							self.vars[nm] = parseInt(v,10);
						} else if (tp=="float") {
							self.vars[nm] = parseFloat(v);
						} else if (tp=="boolean") {
							if (v=="true") self.vars[nm] = true; else self.vars[nm] = false;
						} else {
							self.vars[nm] = v;
						}
					} catch(e) {
						showExceptionMessage(e, "KSVView.load");
					}
				}
			} else if (viewObj.childNodes[i].nodeName=="param") {
				nm = viewObj.childNodes[i].getAttribute("name");
				v = viewObj.childNodes[i].getAttribute("value");
				if (nm!=null && nm!="") {
					if (self.params==null) self.params = new Array();
					self.params[nm] = v;
				}
			} else {
				self.ksv._loadCustomTag(self, self.ksv, viewObj.childNodes[i]);
			}
		}
		return true;
	},
	setKSVEvent:function(self,el,ev) {
		if (ev.event=="ksv_submit") {
			el.setAttribute("action" , "javascript:" + self.ksv.name + ".submitView('" + 
							ev.id + "', '" + ev.view + "')");
		} else {
			showErrorMessage("Invalid KASANOVA Event Name: " + ev.event);
		}
	},
	setEvent:function(self) {
		var el = new Array();
		var evalCode = "";
		for (var i=0; self.events!=null && i<self.events.length; i++) {
			if (self.events[i].id!=null) {
				el[i] = document.getElementById(self.events[i].id);
			} else if (self.events[i].oname!=null) {
				el[i] = eval(self.events[i].oname);
			}
			if (el[i]==null) {
				showErrorMessage("Error: HTML Element not Found(Id: " + self.events[i].id + ")");
				continue;
			}
			//kasanova event
			if (self.events[i].event.indexOf("ksv_")>=0){
				self.setKSVEvent(self,el[i],self.events[i]);
				continue;
			}
			if (self.events[i].eventElement!=null && self.events[i].func != null) {
				self.removeEventListener(self, self.events[i], false);
			}
			if (self.events[i].view!=null && self.events[i].view!="") {
				evalCode += ("self.addEventListener(self, el[" + i + "], self.events[" + i + "], " +
						"function() {self.ksv.viewEvent(self.ksv,'" + self.events[i].view + "');}, " +
						"false);");
			} else if (self.events[i].script!=null && self.events[i].script!="") {
				evalCode+=("self.addEventListener(self, el[" + i + "], self.events[" + i + "]," +
						"function() {" + self.events[i].script + "(self);}, false);");
			}
		}
		if (evalCode!="") {
			try {
				eval(evalCode);
			} catch (e) {
				showExceptionMessage(e, "KSVView.setEvent");
			}
		}
		el = null;
	},
	addEventListener: function(self, el, ev, func, cap) {
		if (self.ksv.addListener(el,ev.event,func,cap)) {
			ev.eventElement = el;
			ev.func = func;
		}
	},
	removeEventListener: function(self, ev, cap) {
		if (self.ksv.removeListener(ev.eventElement,ev.event,ev.func,cap)) {
			ev.eventElement = null;
			ev.func = null;
		}
	},
	eventClear: function() {
		var self = this;
		var ev;
		for (var i=0; self.events!=null && i<self.events.length; i++) {
			ev = self.events[i];
			if (ev.eventElement!=null && ev.func != null) {
				self.removeEventListener(self, ev, false)
			}
		}
	},
	setAttribute:function(self){
		var attr,el;
		for (var i=0; self.attrs!=null && i<self.attrs.length; i++) {
			attr = self.attrs[i];
			if (attr==null) continue;
			el = document.getElementById(attr.id);
			if (el==null) {
				showErrorMessage("Error: HTML Element not Found(Id: " + attr.id + ")");
				continue;
			}
			try {
				el.setAttribute(attr.name,attr.value);
			} catch (e) {
				showExceptionMessage(e, "KSVView.setAttribute");
			}
		}
	},
	run:function() {
		var self = this;
		var urldata,url;
		if (self.content!=null) {
			urldata = self.getParams();
			url = self.content;
			if (self.method!="post" && urldata.length>0) {
				url += "?" + urldata;
				urldata = "";
			}
			if (self.onbeforeload!=null) self.onbeforeload(self);
			loadContentExt(self, url, self.setContent, self.onerror, 0, self.method, urldata, self.charset);
		} else {
			self.setEvent(self);
			self.setAttribute(self);
			self.runScript(self);
		}
	},
	getParams:function() {
		return getParams(this.params);
	},
	inputParameter:function(el) {
		switch (el.type.toLowerCase()) {
			case 'submit':
			case 'hidden':
			case 'password':
			case 'text':
				if (el.value!=null) {
					return el.name + "=" + encodeURIComponent(el.value);
				} else {
					return el.name + "=";
				};
			case 'radio':
			case 'checkbox':
				if (el.checked) {
					return el.name + "=" + encodeURIComponent(el.value);
				}
		}
	},
	selectParameter:function(el) {
		var n,opt,ary;
		var value = "";
		if (el.type == 'select-one') {
			n = el.selectedIndex;
			if (n>0) {
				opt = el.options[n];
				if (opt.value!=null) {
					value = el.name + "=" + encodeURIComponent(opt.value);
				} else {
					value = el.name + "=";
				}
			}
		} else {
			ary = new Array();
			for (var i = 0; i < el.length; i++) {
				opt = el.options[i];
				if (opt.selected) {
					if (opt.value!=null) {
						value = el.name + "=" + encodeURIComponent(opt.value);
					} else {
						value = el.name + "=";
					}
					ary.push(value);
				}
			}
			value = ary.join("&");
		}
		return value;
	},
	textAreaParameter:function(el) {
		if (el.value!=null) {
			return el.name + "=" + encodeURIComponent(el.value);
		} else {
			return el.name + "=";
		};
	},
	submit:function(formid) {
		var self = this;
		var params = new Array();
		var param,el,url;
		var paramdata = self.getParams();
		var form = document.getElementById(formid);
		if (form!=null && form.tagName.toLowerCase()=="form") {
			for (var i=0; i<form.elements.length; i++) {
				param = "";
				el = form.elements[i];
				switch (el.tagName.toLowerCase()) {
					case "input":
						if (!el.disabled) param = self.inputParameter(el);
						break;
					case "select":
						if (!el.disabled) param = self.selectParameter(el);
						break;
					case "textarea":
						if (!el.disabled) param = self.textAreaParameter(el);
						break;
				}
				if (param!="") params.push(param);
			}
			param = params.join("&");
			if (param!=null && param.length>0) {
				if (paramdata.length>0) param = paramdata + "&" + param;
			} else {
				if (paramdata.length>0) param = paramdata;
			}
			url = self.content;
			if (self.method!="post" && param!=null && param.length>0) {
				url += "?" + param;
				param = "";
			}
			if (self.onbeforeload!=null) self.onbeforeload(self);
			loadContentExt(self, url, self.setContent, self.onerror, 0, self.method, param);
		} else {
			showErrorMessage("Error: Invalid Form HTML Element (Id: " + formid + ")");
		}
	},
	setContent:function(self, httpText) {
		var el = document.getElementById(self.target);
		if (el!=null) {
			el.innerHTML = httpText;
		} else {
			_errorEventAndMsg(self.errfunc, self, -10005, 
							"Error: HTML Element not Found(Id: " + self.target + ")");
		}
		self.setEvent(self);
		self.setAttribute(self);
		self.runScript(self);
		if (self.onload!=null) self.onload(self);
	},
	runScript: function(self) {
		if (self.script==null) return;
		if(eval("typeof(" + self.script +")")=="function") {
			try {
				eval(self.script + "(self)");
			} catch(e) {
				showExceptionMessage(e, "KSVView.runScript");
			}
		}
	},
	$c:function(objName) {
		if (this.customObjects!=null) return this.customObjects[objName]; else null;
	}
};

function getParams(params) {
	var retVal = "";
	if (params!=null) {
		for (var nm in params) {
			if (retVal!="") retVal+="&";
			retVal+=(nm+"="+encodeURIComponent(params[nm]));
		}
	}
	return retVal;
}

//Kasanova Json Object
KSVJson=function(ksv, name, content, charset) {
	this.name = name;
	this.ksv = ksv;
	this.content = content;
	this.charset = charset;
	this.params = null;
}

KSVJson.prototype = {
	load:function(jsonObj) {
		var nm, v;
		var self = this;
		for (var i=0; i < jsonObj.childNodes.length; i++) {
			if (jsonObj.childNodes[i].nodeType!=NODE_ELEMENT) continue;
			if (jsonObj.childNodes[i].nodeName=="param") {
				nm = jsonObj.childNodes[i].getAttribute("name");
				v = jsonObj.childNodes[i].getAttribute("value");
				if (nm!=null && nm!="") {
					if (self.params==null) self.params = new Array();
					self.params[nm] = v;
				}
			} else {
				showErrorMessage("Error: Invalid Element(Name: " + jsonObj.childNodes[i].nodeName + ")");
			}
		}
		return true;
	},
	run:function() {
		var self = this;
		var urldata,url;
		if (self.content!=null) {
			urldata = getParams(self.params);
			url = self.content;
			if (urldata.length>0) {
				url += "?" + urldata;
				urldata = "";
			}
			ksvJsonRequest.load(url, self.charset);
		}
	}
};
//Kasanova Object
KSVObject=function(name){
	this.xmlError = false;
	this.name = name;
	this.version = 1;
	this.defaultView = null;
	this.customs = null;
	this.views = null;
	this.error = null;
	this.customObjects = null;
	this.timers = null;
	this.isload = false;
	this.loadwait = kasanova_xmlload_wait;
	this.onbeforeload = null;
	this.onload = null;
	this.jsons = null;
};

//Kasanova Property and Method
KSVObject.prototype={
	init:function() {
		var self = this;
		if (loadContent(self, kasanova_define, self.loadXml, null, 1)==false) {
			if(typeof kasanova_error_page=="undefined"){
				window.location.href = kasanova_error_page;
			} else {
				alert('This Browser not Supported');
			}
			return;
		}
	},
	$v:function(viewName) {
		if (this.views!=null) return this.views[viewName]; else null;
	},
	$c:function(objName) {
		if (this.customObjects!=null) return this.customObjects[objName]; else null;
	},
	$t:function(objName) {
		if (this.timers!=null) return this.timers[objName]; else null;
	},
	$j:function(jsonName) {
		if (this.jsons!=null) return this.jsons[jsonName]; else null;
	},
	loadCustom:function(self, customObj) {
		var nm, f, c;
		var flg = true;
		for (var i=0; i < customObj.childNodes.length; i++) {
			if (customObj.childNodes[i].nodeType!=NODE_ELEMENT) continue;
			if (customObj.childNodes[i].nodeName=="custom") {
				nm = customObj.childNodes[i].getAttribute("name");
				f = customObj.childNodes[i].getAttribute("func");
				if (f!=null) {
					c = new KSVCustom(nm, f);
					if (self.customs==null) self.customs = new Array();
					self.customs[nm] = c;
				} else {
					flg = false;
					showErrorMessage("Error: Invalid custom Element(Parent Element Name: kasanova)");
				}
			}
		}
		return flg;
	},
	loadView:function(self, viewObj) {
		var nm = viewObj.getAttribute("name");
		var v = null;
		if (nm!=null) {
			var cnt = viewObj.getAttribute("content");
			var id = viewObj.getAttribute("target");
			var sc = viewObj.getAttribute("script");
			var md = viewObj.getAttribute("method");
			var cs = viewObj.getAttribute("charset");
			var val = viewObj.getAttribute("onbeforeload");
			var ev1 = _getFunction(val);
			val = viewObj.getAttribute("onload");
			var ev2 = _getFunction(val);
			val = viewObj.getAttribute("onerror");
			var ev3 = _getFunction(val);
			if (cnt!=null && id==null) {
				showErrorMessage("Error:Invalid view Element(Name: "+ nm + ")");
				return false;
			}
			if (md!="post") md="get";
			if (md=="post" && cnt==null) {
				showErrorMessage("Error:Invalid view Element(Name: "+ nm + ")");
				return false;
			}
			md = md.toLowerCase();
			v = new KSVView(self, nm, cnt, id, sc, md, cs, ev1, ev2, ev3);
			if (v.load(viewObj)) {
				if (self.views==null) self.views = new Array();
				self.views.push(v);
				self.views[nm] = v;
				return true;
			} else {
				showErrorMessage("Error:Invalid view Element(Name: "+ nm + ")");
			}
		} else {
			showErrorMessage("Error: Invalid view Element(Parent Element Name: kasanova)");
		}
		return false;
	},
	loadTimer:function(self, timerObj) {
		var name, value, view, num, bval, scr, obj;
		name = timerObj.getAttribute("name");
		if (name==null || name=="") {
			showErrorMessage("Error: Invalid Element(Name: " + timerObj.nodeName + ")");
			return false;
		}
		value = timerObj.getAttribute("interval");
		try {
			num = parseInt(value,10);
		} catch(e) {
			showErrorMessage("Error: Invalid Attribute(Name: interval)");
			return false;
		}
		value = timerObj.getAttribute("default");
		if (value=="on") {
			bval = false;
		} else if (value=="off"){
			bval = true;
		} else {
			showErrorMessage("Error: Invalid Element(Name: " + timerObj.nodeName + ")");
			return false;
		}
		view = timerObj.getAttribute("view");
		scr = timerObj.getAttribute("script");
		if (view==null && scr==null) {
			showErrorMessage("Error: Invalid Element(Name: " + timerObj.nodeName + ")");
			return false;
		}
		obj = new KSVTimer(self, num, bval, view, scr);
		if (self.timers==null) self.timers=new Array();
		self.timers[name] = obj;
		return true;
	},
	loadJson: function(self, JsonObj) {
		var nm = JsonObj.getAttribute("name");
		var v = null;
		if (nm!=null) {
			var cnt = JsonObj.getAttribute("content");
			var cs = JsonObj.getAttribute("charset");
			if (cnt==null) {
				showErrorMessage("Error:Invalid json Element(Name: "+ nm + ")");
				return false;
			}
			v = new KSVJson(self, nm, cnt, cs);
			if (v.load(JsonObj)) {
				if (self.jsons==null) self.jsons = new Array();
				self.jsons.push(v);
				self.jsons[nm] = v;
				return true;
			} else {
				showErrorMessage("Error:Invalid json Element(Name: "+ nm + ")");
			}
		} else {
			showErrorMessage("Error: Invalid json Element(Parent Element Name: kasanova)");
		}
		return false;
	},
	loadXml:function(self, xDoc) {
		//Analyzed kasanova.xml
		var childs,value,name,content,obj;

		self.xmlError = false;
		if (xDoc.documentElement==null) {
			self.xmlError = true;
			showErrorMessage("Error: Invalid XML File");
			return;
		}
		if (xDoc.documentElement.nodeName!="kasanova") {
			self.xmlError = true;
			showErrorMessage("Error: Invalid Top Element Name");
			return;
		}
		self.version = xDoc.documentElement.getAttribute("version");
		value = xDoc.documentElement.getAttribute("onbeforeload");
		self.onbeforeload = _getFunction(value);
		value = xDoc.documentElement.getAttribute("onload");
		self.onload = _getFunction(value);
		childs = xDoc.documentElement.childNodes;
		for (var i=0; i < childs.length; i++) {
			if (childs[i].nodeType!=NODE_ELEMENT) continue;
			if (childs[i].nodeName=="customs") {
				self.loadCustom(self, childs[i]);
			} else if (childs[i].nodeName=="errorpage") {
				name = childs[i].getAttribute("name");
				content = childs[i].getAttribute("content");
				value = childs[i].getAttribute("target");
				self.error = new KSVError(name, content, value);
			} else if (childs[i].nodeName=="default") {
				value = childs[i].getAttribute("view");
				if (value!=null) self.defaultView = value.split(",");
			} else if (childs[i].nodeName=="timer") {
				self.loadTimer(self, childs[i]);
			} else if (childs[i].nodeName=="view") {
				self.loadView(self, childs[i]);
			} else if (childs[i].nodeName=="json") {
				self.loadJson(self, childs[i]);
			} else {
				self._loadCustomTag(self ,self, childs[i]);
			}
		}
		self.isload = true;
	},
	_loadCustomTag: function(parentObj,self,element) {
		var name,value,nm;
		if (self.customs!=null) {
			name = element.nodeName;
			nm = element.getAttribute("name");
			if (self.customs[name]!=undefined && self.customs[name]!=null) {
				value = self.customs[name].func;
				if(eval("typeof(" + value +")")=="function") {
					try{
						obj = eval(value + "(element)");
						if (obj!=null) {
							if (parentObj.customObjects==null) parentObj.customObjects = new Array();
							if (nm!=null && nm!="") {
								parentObj.customObjects[name+"."+nm]=obj;
							} else {
								parentObj.customObjects[name]=obj;
							}
						} else {
							showErrorMessage("Error: No return Object(FuncName: " + value + ")");
						}
					} catch (e) {
						showExceptionMessage(e, "KSVObject._loadCustomTag");
					}
				} else {
					showErrorMessage("Error: Function not Found(Name: " + value + ")");
				}
			} else {
				showErrorMessage("Error: Invalid Element(Name: " + element.nodeName + ")");
			}
		} else {
			showErrorMessage("Error: Invalid Element(Name: " + element.nodeName + ")");
		}
	},
	addListener:function(element, eventType, func, cap) {
		try {
			if (element.addEventListener) {
				element.addEventListener(eventType, func, cap);
			} else if (element.attachEvent) {
				element.attachEvent('on' + eventType, func);
			} else {
				showErrorMessage("Error: This Browser not Support");
				return false;
			}
			return true;
		} catch (e) {
			showExceptionMessage(e, "KSVObject.addListener");
			return false;
		}
	},
	removeListener:function(element, eventType, func, cap) {
		try {
			if (element.removeEventListener) {
				element.removeEventListener(eventType, func, cap);
			} else if (element.detachEvent) {
				element.detachEvent('on' + eventType, func);
			} else {
				showErrorMessage("Error: This Browser not Support");
				return false;
			}
			return true;
		} catch (e) {
			showExceptionMessage(e, "KSVObject.removeListener");
			return false;
		}
	},
	viewEvent:function(self, viewPage) {
		var views = viewPage.split(",");
		var viewObj = null;
		for (var i=0; views!=null && i<views.length; i++) {
			viewObj = self.views[views[i]];
			if (viewObj!=null && viewObj!=undefined) {
				viewObj.run();
			} else {
				showErrorMessage("Error: View not Found(ViewName: " + views[i] + ")");
			}
		}
	},
	submitView:function(formid, viewName) {
		var views = viewName.split(",");
		var viewObj = null;
		var self = this;
		for (var i=0; views!=null && i<views.length; i++) {
			viewObj = self.views[views[i]];
			if (viewObj!=null && viewObj!=undefined) {
				viewObj.submit(formid);
			} else {
				showErrorMessage("Error: View not Found(ViewName: " + views[i] + ")");
			}
		}
	}
};

//Create Kasanova Object
var ksvObject = new KSVObject("ksvObject");
var $k = ksvObject;
KSVHttpInit();
ksvObject.init();
$k.addListener(window, 'load', pageInit, false);

function _getFunction(funcname) {
	if (funcname==null || funcname=="") return null;
	if(eval("typeof(" + funcname +")")=="function") {
		try {
			return eval(funcname);
		} catch(e) {
			showExceptionMessage(e, "_getFunction");
		}
	} else {
		showErrorMessage("Error: Invalid Function Name(Name: " + funcname + ")");
	}
	return null;
}
function pageInit(e){
	var view = null;
	if (!$k.isload && !$k.xmlError && $k.loadwait>0) {
		setTimeout(function(){pageInit(e);}, kasanova_xmlwait_interval);
		$k.loadwait-=kasanova_xmlwait_interval;
		return;
	}
	if ($k.onbeforeload!=null) $k.onbeforeload($k);
	if ($k.defaultView!=null && $k.views!=null) {
		for (var i=0; i<$k.defaultView.length; i++) {
			view = $k.$v(ksvObject.defaultView[i]);
			if (view!=null && view!=undefined) {
				view.run();
			} else {
				showErrorMessage("Error: View not Found(ViewName: " + $k.defaultView[i] + ")");
			}
		}
	}
	if ($k.timers!=null) {
		for (var nm in $k.timers) {
			if (!$k.$t(nm).disabled) $k.$t(nm).run();
		}
	}
	$k.removeListener(window, 'load', pageInit, false);
	$k.addListener(window, 'unload', pageUnload, false);
}

function pageUnload(e) {
	var view=null;
	if ($k.timers!=null) {
		for (var nm in $k.timers) {
			$k.$t(nm).setDisabled(true);
		}
	}
	for (var i=0; $k.views!=null && i<$k.views.length; i++) {
		view = $k.$v(i);
		if (view!=null) {
			view.eventClear();
			$k.views[i]=null;
		}
	}
	$k.removeListener(window, 'unload', pageUnload, false);
	for (var i=0; i<kasanova_http_count; i++) {
		ksvHttp[i].httpObj = null;
		ksvHttp[i] = null;
	}
	ksvObject = null;
}

