﻿String.prototype.find=function(what){
	return (this.indexOf(what)>=0 ? true : false);
}
String.prototype.strip_tags = function(){
	return this.replace(/(<([^>]+)>)/ig,"");
}
String.prototype.htmlencode = function(bool){
	if(bool){
		return this.replace(/&amp;/gi,"&");
	}else{
		return this.replace(/&/gi,"&amp;");
	}
}
String.prototype.ltrim = function(){
	return this.replace(new RegExp("^[ ]+","gi"),"");
} 
String.prototype.rtrim = function(){
	return this.replace(new RegExp("[ ]+$","gi"),"");
}
String.prototype.trim = function(){
	return this.ltrim(this.rtrim());
}
if (!Array.prototype.push){
    Array.prototype.push = function(elem){
        this[this.length] = elem;
    }
}
Function.prototype.close = function(obj)
{
  if (!window.__objs){
    window.__objs = [];
    window.__funs = [];
  }
  var fun = this;
  var objId = obj.__objId;
  if (!objId) __objs[objId = obj.__objId = __objs.length] = obj;
  var funId = fun.__funId;
  if (!funId) __funs[funId = fun.__funId = __funs.length] = fun;
  if (!obj.__closes) obj.__closes = [];
  var close = obj.__closes[funId];
  if (close) return close;
  obj = null;
  fun = null;
  return __objs[objId].__closes[funId] = function ()
  {
    return __funs[funId].apply(__objs[objId], arguments);
  };
}
var em= function(){
	function load(func){
		var oldonload = window.onload;
		if(typeof window.onload != "function")
			window.onload = function(){
				attach(window,"unload",clean);
				func();
			}
		else{
			window.onload = function(){
				oldonload();
				func();
			}
		}
	}
	function clean(){
		window.__objs = null;
		window.__funs = null;
	}
	function attach(obj,type,func,useCapture){
		if(window.addEventListener)
			obj.addEventListener(type,func,useCapture);
		else if(window.attachEvent)
			obj.attachEvent("on"+type,func);
	}
	function detach(obj,type,func,useCapture){
		if(obj.removeEventListener)
			obj.removeEventListener(type,func,useCapture);
		else if(obj.detachEvent)
			obj.detachEvent("on"+type,func);
	}
	return {
		attach:attach,
		detach:detach,
		load:load
	}
}();
function $(str){
	var ts=str.replace(" ",""),objs=[],ds=[];
	if(ts.find(",")){
		ds=ts.split(",");
		var l=ds.length;
		for(var i=0;i<l;i++){
			objs[i]=document.getElemntById(ds[i]);
		}
		return (objs);
	}else{
		var obj=document.getElementById(ts);
		return (obj)?obj:false;
	}
}
function $$(str){
	var i,j,selid="",selclass="",tag=str,tag2="",v2,k,f,a,s=[],objs=[],c;
	if(str.find("#")){ 
	    if(str.find(" ")){
	        s=str.split(" ");
	        var fs=s[0].split("#");
	        f=$(fs[1]);
	        if(f){
	            v=f.getElementsByTagName(s[1]);
	            for(i=0;i<v.length;i++) objs.push(v[i]);
       	 	}
	        return(objs);
	    }else{
	        s=str.split("#");
	        selid=s[1];
	        if(selid!=""){
	            f=$(selid);
	            if(f) objs.push(f);
	            return(objs);
            }
        }
    }
	if(str.find(".")){     
		s=str.split(".");
	    tag=s[0];
	    selclass=s[1];
	    if(selclass.find(" ")){  
	        s=selclass.split(" ");
	        selclass=s[0];
	        tag2=s[1];
        }
    }
	var v=document.getElementsByTagName(tag);
	if(selclass==""){
	    for(i=0;i<v.length;i++) objs.push(v[i]);
	    return(objs);
    }
	for(i=0;i<v.length;i++){
	    c=v[i].className.split(" ");
	    for(j=0;j<c.length;j++){
	        if(c[j]==selclass){
	            if(tag2==""){
	            	objs.push(v[i]);
	            }else{
	                v2=v[i].getElementsByTagName(tag2);
	                for(k=0;k<v2.length;k++) objs.push(v2[k]);
                }
            }
        }
    }
	return(objs);	
}
var g= {
	ns:function(obj){
		var target = obj.nextSibling;
		while(target.nodeType != 1){
			target = target.nextSibling;
		}
		return target;
	},
	fc:function(obj){
		var target = obj.firstChild;
		while(target.nodeType != 1)
			target = target.nextSibling;
		return target;
	},
	ps:function(obj){
		var target = obj.previousSibling;
		while(target.nodeType != 1){
			target = obj.previousSibling;
		}
		return target;
	},
	cs:function(obj){
		var target = obj.childNodes,l=target.length,matches = [];
		for(var i=0; i<l; i++){
			if(target[i].nodeType == 1)
				matches[matches.length] = target[i];
		}
		return (matches.length>0)?matches:false;
	},
	lc:function(obj){
		var target=obj.lastChild;
		while(target.nodeType !=1){
			target = target.previousSibling;
		}
		return target;
	},
	is:function(obj,prop){
		if(obj.currentStyle)
		    return(obj.currentStyle[prop]);
		if(document.defaultView.getComputedStyle)
		    return(document.defaultView.getComputedStyle(obj,'')[prop]);
		return(null);
	},	
	pi:function(img){
		var image = new Image();
		image.src = img;
	}
};
if(typeof cgAjax == "undefined") var cgAjax = {};
var  Nversion = navigator.userAgent;
cgAjax.IE = Nversion.find("MSIE");
if(cgAjax.IE){
	var versionN = navigator.appVersion;
	cgAjax.IE7 = versionN.find("7");
	cgAjax.IE6 = versionN.find("6");
	cgAjax.IE8 = versionN.find("8");
}
cgAjax.FF = Nversion.find("Firefox");
cgAjax.SF = Nversion.find("Apple");
cgAjax.OP = Nversion.find("Opera");
cgAjax.dump = function(obj){
	var debug = $('jsDebug');
	if(!debug){
		var div = document.createElement("DIV");
		div.setAttribute("id","jsDebug");
		var p = document.getElementsByTagName("BODY")[0];
		var t = g.fc(p);
		p.insertBefore(div,t);
		debug = div;
	}
	if(obj instanceof Object){
		for(var i in obj){
			debug.innerHTML +="<p><span>"+i+"</span>"+":";
			debug.innerHTML +=eval("obj."+i)+"</p>";
		}
	}else{
		debug.innerHTML +="<p>"+obj+"</p>";
	}
}
cgAjax.XHRFactory = {
	createXHR: function(){
		try {
			if(cgAjax.IE6){
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}else if(cgAjax.IE7 || cgAjax.FF || cgAjax.SF || cgAjax.OP || cgAjax.IE8){
				var xhr = new XMLHttpRequest();
			}else {
				throw("HttpRequest is not support in your browser!");
			}
		}catch(e){
			alert(e.message);	
		}
		
		return xhr;
	}
}
cgAjax.HttpRequest = function(type){
	this.handler = "";
	this.async = true;
	this.reponseType = type;
	this.httpObj = cgAjax.XHRFactory.createXHR();
	this.completefunc = null;
	this.pp = {};
	this.gp = "";
}
cgAjax.HttpRequest.prototype.post = function(sData){
	if(sData == null){
		var sData = "";
		for(var name in this.pp){
			sData += escape(name)+ "=" +escape(this.pp[name])+ "&";
		}
		sData = sData.substring(0,sData.length-1);
	}
	
	this.httpObj.open("POST",this.handler,this.async);
	this.httpObj.onreadystatechange = this.requestComplete.close(this);
	if(this.responseType == "xml")
		this.httpObj.setRequestHeader("Content-Type","text/xml");

	this.httpObj.send(sData);
}
cgAjax.HttpRequest.prototype.get = function() {
	this.httpObj.open("GET",this.handler+this.gp, this.async);
	this.httpObj.onreadystatechange = this.requestComplete.close(this);
	if(this.reponseType == "xml")
		this.httpObj.setRequestHeader("Content-Type","text/xml");
	
	this.httpObj.send(null);
}
cgAjax.HttpRequest.prototype.setGetParam = function(key,value){
	this.gp +="&"+key+"="+value;
}
cgAjax.HttpRequest.prototype.setPostParam = function(name,value){
	if(value == null){
		delete this.pp[name];
	}else{
		this.pp[name] = value;
	}
}
cgAjax.HttpRequest.prototype.requestComplete = function(){
	if(this.httpObj.readyState == 4){
		if(this.httpObj.status == 200){
			this.completefunc.call(this);
		}
	}
}
cgAjax.HttpRequest.prototype.abort = function(){
	this.httpObj.onreadystatechange = function(){};
	this.httpObj.abort();
}
cgAjax.extend = function(subClass,baseClass){
	function inheritance(){};
	inheritance.prototype = baseClass.prototype;
	subClass.prototype = new inheritance();
	subClass.base = baseClass;
	if(baseClass.c) baseClass.prototype.c = baseClass.c;
	subClass.c = baseClass.prototype;	
}
cgAjax.implement = function(sub,ibase){
	for(var property in ibase.prototype){
		sub[property] = ibase.prototype[property]; 
	}
}
cgAjax.Template = function(){
	this.root = '';
	this.head = '';
	this.trunk = '';
	this.foot = '';
	this.node = '';
	this.action = '';
}
cgAjax.Template.prototype.attach = function(headId,target){
	var parent = target.parentNode,obj = $(headId);
	if(!obj){
		var node = document.createElement(this.node);
		node.setAttribute("id",headId);
		node.innerHTML = this.root;
		parent.appendChild(node);
		obj = $(headId);
	}else{
		obj.innerHTML = this.root;
		obj.className = '';
	}
	this.action = new cgAjax.effect(obj);
}
cgAjax.autoCompleteTpl = function(){
	this.root = '';
	this.trunk = "<li>$value</li>";
	this.node = "UL";
	
	this.render = function(jsonObj){
		var length = jsonObj.length;
		i=0;
		while(jsonObj[i]){
			this.root += this.trunk;
			this.root = this.root.replace(/\$value/i,jsonObj[i]);
			i++;
		}
	}	
	this.colorize = function(value){
		var ex = new RegExp(value,'gi');
		this.root = this.root.replace(ex,"<span>"+value+"</span>");
	}
}
cgAjax.extend(cgAjax.autoCompleteTpl,cgAjax.Template);
cgAjax.Module = function(params){
	this.http = new cgAjax.HttpRequest();
	this.http.handler = params.url.htmlencode(true);
	this.headId = params.id;
	this.target = $(params.target);
	this.tpl = eval("new "+params.tpl+"();");
	this.json = null;
}
cgAjax.Module.prototype.json_decode = function(){
	var rs = this.http.httpObj.responseText;
	if(rs.length>2){
		this.json = eval(rs);
		return true;
	}else{
		return false;
	}
}
cgAjax.Module.prototype.display = function(bool){
	this.tpl.render(this.json);
	if(bool){
		this.tpl.colorize(this.target.value.replace(new RegExp("[^a-zA-Z0-9_]+","gi")," ").trim());
	}
	this.tpl.attach(this.headId,this.target);
	this.status = true;
}
cgAjax.Module.prototype.clear = function(bool){
	var obj = $(this.headId),parent = obj.parentNode;
	if(obj){
		if(!bool){
			parent.removeChild(obj);
		}else{
			var j=0;
			while(this.tpl.action.objs[j]){
				this.tpl.action.gui.removeChild(this.tpl.action.objs[j]);
				j++;
			}
			this.tpl.action = '';
		}
		this.tpl.root = '';
		this.http.gp = '';
	}
}
cgAjax.autoComplete = function(params){
	this.stop = true;
	this.focusClass = params.li;
	/*Constructing the object with base class constuct*/
	cgAjax.autoComplete.base.apply(this,arguments); 
	if(!this.target) return false;
	/*Binding Events To Target*/ 
	this.exec = function(){
		if(this.target.value.replace(" ","").length >=3 && this.stop){
			em.detach(this.target,"keydown",this.focus.close(this));
			this.http.completefunc = function(){
				cgAjax.autoComplete.c.clear.apply(this,[true]);
				if(cgAjax.autoComplete.c.json_decode.call(this)){
					cgAjax.autoComplete.c.display.apply(this,[true]);
					em.attach(this.target,"blur",this.clear.close(this));
					em.attach(this.target,"keydown",this.focus.close(this));	
					this.chgText();
				}else{
					cgAjax.autoComplete.c.clear.call(this);
				}
			}.close(this);
			this.http.setGetParam('keyword',this.target.value.replace(new RegExp("[^a-zA-Z0-9_]+","gi")," ").trim());
			this.http.get();
		}
	}
	
	this.clear = function(){
		em.detach(this.target,"keydown",this.focus.close(this));
		setTimeout(function(){var obj=$(this.headId);obj.className='hidden';}.close(this),100);
		this.stop = true;
	}
	this.effect = function(e){
		if(!e) e = window.event;
		if(!e.target) e.target = e.srcElement;
		var j=0;
		while(this.tpl.action.objs[j]){
			var obj = this.tpl.action.objs[j];
			if(obj.className.find(this.focusClass)){
				obj.className = '';
				obj.fstatus = false;	
				break;
			}
			j++;
		}
		if(e.target.nodeName != 'SPAN'){
			this.tpl.action.chgText(this.target,e.target);
			e.target.fstatus = true;
			e.target.className = this.focusClass;
		}else{
			var parent = e.target.parentNode;
			parent.fstatus = true;
			parent.className = this.focusClass;
		}
		if(cgAjax.IE6) e.target.style.cursor = 'pointer';	
	}
	this.focus = function(e){
		if(!e) e = window.event;	
		if(e.keyCode == 40 || e.keyCode == 38){
			var nextEl = this.tpl.action.keyControl(e,this.focusClass);
			this.tpl.action.chgText(this.target,nextEl);
			this.stop = false;
		}else{
			this.stop = true;
		}
	}
	this.chgText = function(){
		var j=0;
		while(this.tpl.action.objs[j]){
			this.tpl.action.objs[j].fstatus = false;
			em.attach(this.tpl.action.objs[j],"mouseover",this.effect.close(this));
			em.attach(this.tpl.action.objs[j],"click",function(){document.forms[0].submit();});
			j++;
		}
	}
	em.attach(this.target,"keyup",this.exec.close(this));	
}
cgAjax.extend(cgAjax.autoComplete,cgAjax.Module);
cgAjax.effect = function(guiObj){
	this.gui = guiObj;
	this.objs = g.cs(this.gui);	
}
cgAjax.effect.prototype.keyControl = function(e,className){
	var j=0,currentEl='',nextEl;
	while(this.objs[j]){
		if(this.objs[j].fstatus){
			currentEl = this.objs[j];
			currentEl.className = '';
			currentEl.fstatus = false;
			var currentIndex = j;
			break;
		}
		j++;
	}
	if(e.keyCode == 38 && currentEl != ''){
		if(currentIndex > 0){
			nextEl = this.objs[currentIndex-1];
		}else{
			nextEl = currentEl;
		}
	}else if(e.keyCode == 40 && currentEl != ''){
		if(currentIndex < this.objs.length-1){
			nextEl = this.objs[currentIndex+1];
		}else{
			nextEl = currentEl;
		}
	}else{
		nextEl = this.objs[0];
	}
	nextEl.className = className;
	nextEl.fstatus = true;
	return nextEl;
}
cgAjax.effect.prototype.chgText = function(dst,src){
	dst.value = src.innerHTML.strip_tags();
}
cgAjax.Controller = function(){
	var modules = $$("blockquote.ajax"),i=0;
	while(modules[i]){
		var params=new Object(),attrs=g.cs(modules[i]),j=0;
		while(attrs[j]){
			var attr = attrs[j];
			var key = attr.getAttribute('title');
			var value = attr.innerHTML;
			eval("params."+key+"='"+value+"';");
			j++;
		}
		eval("new "+params.module+"(params);");
		i++;
	}	
	return true;
}
em.load(cgAjax.Controller);



