//
//
//	'system.js' written by Peter Liu
//
//



//
//	Class XmlDoc Defination
//
function XmlDoc(url, handler)
{
	this.doc = null;
	this.url = url;
	if (handler) 
		this.init(handler);
}

XmlDoc.prototype.init = function (handler)
{
	var doc = null;

	if (Util.isEmpty(this.url)) return;

	if (window.ActiveXObject) {
		try {
			doc = new ActiveXObject('Microsoft.XMLDOM');
			if (handler) {
				doc.onreadystatechange = function ()
				{ if (doc.readyState == 4) handler(doc); }
			}
		} catch (ex) {
			return;
		}
	} else if (document.implementation && document.implementation.createDocument) {
		try {
			doc = document.implementation.createDocument('', '', null);
			if (handler) {
				doc.onload = function ()
				{ handler(doc); }
			}
		} catch (ex) {
			return;
		}
	}

	if (!handler)
		doc.async = false;

	try {
		doc.load(this.url);
	} catch (ex) {
		return;
	}

	this.doc = doc;
}

XmlDoc.prototype.getDoc = function ()
{
	if (this.doc)
		return this.doc;
	else {
		this.init();
		return this.doc;
	}	
}



//
//	Class Locale Defination
//
Locale.DEFAULT_LANG = 'zh_CN';
Locale.DEFAULT_PATH = '/js/locale/';
Locale.DEFAULT_SUFFIX = '.xml';

function Locale(lang, path)
{
	this.doc = null;
	this.setLang(lang, path);
}

Locale.prototype.getLang = function ()
{
	return this.lang;
}

Locale.prototype.setLang = function (lang, path)
{
	if (Util.isEmpty(lang)) return;

	var pth = Locale.DEFAULT_PATH;
	if (!Util.isEmpty(path))
		pth = path;

	var xml = new XmlDoc(pth + lang + Locale.DEFAULT_SUFFIX);
	this.doc = xml.getDoc();
	if (this.doc)
		this.lang = lang;
}

Locale.prototype.getMsg = function (id)
{
	if (!this.doc)
		this.setLang(Locale.DEFAULT_LANG);

	if (!this.doc) 
		return id;

	var lists = this.doc.getElementsByTagName('id');	
	for (var i = 0, n = lists.length; i < n; i++) {
		if (Util.getNodeText(lists[i]) == id) {
			var msgs = lists[i].parentNode.getElementsByTagName('msg');
			if (!msgs.length) return id;

			var txt = Util.getNodeText(msgs[0]);
			if (Util.isEmpty(txt))
				return id;
			else
				return txt;
		}
	}

	return id;
}



//
//	Class HttpReq Defination
//
HttpReq.DEFAULT_METHOD = 'GET';

function HttpReq()
{
	this.http = null;
	this.url = '';
	this.free = true;
	this.init();
}

HttpReq.prototype.init = function ()
{
	var http = null;

	if (window.ActiveXObject) {
		try {
			http = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (ex) {
			return;
		}
	} else if (window.XMLHttpRequest) {
		try {
			http = new XMLHttpRequest();
		} catch (ex) {
			return;
		}
	}

	this.http = http;
}

HttpReq.prototype.connect = function (url, handler, interrupt, method, data)
{
	if (!this.http) this.init();
	if (!this.http) return false;

	if (Util.isEmpty(url) || !handler) return false;

	if (!this.free) {
		if (interrupt) {
			try {
				this.http.about();		
			} catch (ex) {
				this.http.about;
			}
		} else
			return false;
	}

	var m = HttpReq.DEFAULT_METHOD;
	var d = null;
	if (method && method.toLowerCase() == 'post') {
		m = 'POST';
		if (data) d = data;
	}

	try {
		this.http.open(m, url);
		var req = this;
		this.http.onreadystatechange = function ()
		{
			if (req.http.readyState == 4) {
				if (typeof(handler) == typeof(Util.TYPEOF_OBJ) && 
						handler.httpReqHandler)
					handler.httpReqHandler(req.http);
				else if (typeof(handler) == typeof(Util.TYPEOF_FUN))
					handler(req.http); 
				req.free = true; 
			}
		}
		this.free = false;
		this.http.send(d);
	} catch (ex) {
		return false;
	}

	return true;
}



//
//	Class Util Defination
//
Util.RE_VALID_EMAIL = /^([\w-\.]+)@([\w-]+)(\.(\w+))*$/;
Util.TYPEOF_STR = 'string';
Util.TYPEOF_FUN = function () { };
Util.TYPEOF_OBJ = new Object();

function Util()
{
}

Util.isEmpty = function (obj)
{
	if (!obj) return true;

	if (typeof(obj) == typeof(Util.TYPEOF_STR))
		return (obj.search(/[\S]/) == -1);

	return false;
}

Util.getNodeText = function (node)
{
	var text = '';
	if (!node) return text;

	if (node.nodeType == 3 && !Util.isEmpty(node.nodeValue))
		text += node.nodeValue;

	if (node.nodeType == 1) {
		for (var i = 0, n = node.childNodes.length; i < n; i++) 
			text += Util.getNodeText(node.childNodes[i]);
	}
		
	return text;
}

Util.trimStr = function (str)
{
	return str.replace(/\s/g, '');
}



//
//	Class Dict Defination
//
function Dict()
{
	this.ks = new Array();
	this.vs = new Array();
	this.len = 0;
	if (arguments.length)
		this.init(arguments);
}

Dict.prototype.init = function (args)
{
	for (var i = 0, n = Math.round(args.length / 2); i < n; i++) {
		var k = args[i * 2];
		var v = args[i * 2 + 1];
		if (!Util.isEmpty(k)) {
			if (Util.isEmpty(v))
				this.add(k, null);
			else
				this.add(k, v);
		}
	}
}

Dict.prototype.length = function ()
{
	return this.len;
}

Dict.prototype.getId = function (k)
{
	for (var i = 0; i < this.len; i++) 
		if (this.ks[i] == k)
			return i;
	return -1;
}

Dict.prototype.has = function (k)
{
	if (this.getId(k) == -1)
		return false;
	else
		return true;
}

Dict.prototype.get = function (k)
{
	var i = this.getId(k);
	if (i == -1)
		return null;
	else
		return this.vs[i];
}

Dict.prototype.add = function (k, v)
{
	this.ks[this.len] = k;
	this.vs[this.len] = v;
	this.len++;
}

Dict.prototype.del = function (k)
{
	var i = this.getId(k);
	if (i == -1) return;
	this.ks.splice(i, 1);
	this.vs.splice(i, 1);
	this.len--;
}

Dict.prototype.keys = function ()
{
	return this.ks;
}

Dict.prototype.values = function ()
{
	return this.vs;
}



//
// Class Validator Defination
//
function Validator(form)
{
	this.form = form;
	this.err = '';
}

Validator.prototype.validate = function (funcs)
{
	for (var i = 0, n = this.form.elements.length; i < n; i++) {
		var elem = this.form.elements[i];
		if (Util.isEmpty(elem.name))
			continue;

		if (!this.valid(elem, funcs.get(elem.name))) {
			elem.focus();
			return false;
		}
	}

	return true;
}

Validator.prototype.valid = function (elem, func)
{
	if (elem.getAttribute('optional') != '') {
		if (!this.checkEmpty(elem))
			return false;
		if (func != null)
			return func(this, elem);
		else
			return true;
	} else {
		if (this.checkEmpty(elem) && func != null)
			return func(this, elem);
		else
			return true;
	}
}

Validator.prototype.checkEmpty = function (elem)
{
	if (elem.type.toLowerCase() == 'radio')// || elem.type.toLowerCase().indexOf('select') == 0)
		return true;

	if ((elem.type.toLowerCase() == 'checkbox' && !elem.checked) || 
			 Util.isEmpty(elem.value)) {
		this.setError(elem.name, 'is empty');
		return false;
	}

	return true;
}

Validator.prototype.setError = function (name, msg)
{
	this.err = name.toUpperCase() + '_' + 
						 msg.toUpperCase().replace(/\s/g, '_');
}



//
// Class Cookie Defination
//
function Cookie()
{
	this.dict = new Dict();
	this.init();
}

Cookie.prototype.init = function ()
{
	if (Util.isEmpty(document.cookie))
		return;
	var items = document.cookie.split(';');
	for (var i = 0, n = items.length; i < n; i++) {
		var pair = items[i].split('=');
		this.dict.add(Util.trimStr(pair[0]), unescape(pair[1]));
	}
}

Cookie.prototype.has = function (k)
{
	return this.dict.has(k);
}

Cookie.prototype.get = function (k)
{
	return this.dict.get(k);
}



//
//	Global Variables Defination
//
var LC = new Locale();
