/*
* Version 0.0.1
*/
if ( typeof ANY == "undefined" ) {
	var ANY = {};
}

/* 
* init 
*
* @param void
* @return void
*/
ANY.init = function() {}

ANY.init();

/**
* util functions
*
*/
ANY.util = {};
function _ANY_util__boolean2JsonString (s) {
	return String(s);
}
ANY.util._boolean2JsonString = _ANY_util__boolean2JsonString;
function _ANY_util__string2JsonString (s) {
	return '"' + s + '"';
};
ANY.util._string2JsonString = _ANY_util__string2JsonString;

function _ANY_util__array2JsonString (s) {
	var ar = ['['];
	var b = false;
	for(p in s) {
		if (b) {
			ar.push(',');
		}
		ar.push(ANY.util.toJsonString(s[p]));
		b = true;
	}
	ar.push(']');
	return ar.join('');
};
ANY.util._array2JsonString = _ANY_util__array2JsonString;

function _ANY_util__object2JsonString(s) {
		var ar = ['{'];
		var b = false;
		for(p in s) {
			if (b) {
				ar.push(',');
			}
			ar.push(ANY.util._string2JsonString(p) + ':' + ANY.util.toJsonString(s[p]));
			b = true;
		}
		ar.push('}');
		return ar.join('');		
	};
ANY.util._object2JsonString = _ANY_util__object2JsonString;

function _ANY_util_toJsonString(s){
		var b ='';
		if (typeof s == "string") {
			b += ANY.util._string2JsonString(s);
		} else if (typeof s == "number") {
			b += s;
		} else if (typeof s == "boolean") {
			b += ANY.util._boolean2JsonString(s);
		} else if (s instanceof Array) {
			b += ANY.util._array2JsonString(s);
		} else if (typeof s == "object") {
			b += ANY.util._object2JsonString(s);
		}
		return b;
	};
ANY.util.toJsonString = _ANY_util_toJsonString;

function _ANY_util_checkDate(y, m, d) {
	var date = new Date(y, m-1, d);
	if ( (parseInt(y, 10) == date.getFullYear()) &&
			(parseInt(m, 10) == (date.getMonth()+1)) &&
				(parseInt(d, 10) == date.getDate())) {
		return true;
	} else {
		return false;
	}
}
ANY.util.checkDate = _ANY_util_checkDate;

function _ANY_util_getHost() {
	var e = new RegExp('http://(.+):([0-9]+)/', 'ig');
	var url = document.URL;
	var host = '';
	if (url.match(e)) {
		host += RegExp.$1 + ':' + RegExp.$2 + '/';
	}
	return host;
}
ANY.util.getHost = _ANY_util_getHost;

/*
* network
*/
ANY.net = {
	_createXhrRequest: function(){
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					return null;
				}
			}
		} else {
			return null;
		}
	},
	
	asyncRequest: function(url, callback){
		var req = ANY.net._createXhrRequest();
		if (req == null) {
			return null;
		}
		req.open("GET", url, true);
		req.send(null);
		req.onreadystatechange = function()
		{
			//alert(req.readyState);
			if (req.readyState==4 && req.status==200 && callback) {
				callback(req);
			}
		}
	},
	
	responseFilter: function(src) {
		if(navigator.appVersion.indexOf( "KHTML" ) > -1){
	        var esc = escape(src);
	        return(esc.indexOf("%u") < 0 && esc.indexOf("%") > -1) ? decodeURIComponent(esc) : src;
	    } else {
	    	return src;
	    }
	}
};

ANY.json ={};
ANY.rss = {};
ANY.session = {
	getId : function() {
		var sessionId;
		for (var i = 0; i < document.cookie.split('; ').length; i++) {
			var crumb = document.cookie.split('; ')[i].split('=');
			if (crumb[0] == "ANY_B" && crumb[1] != null) {
				sessionId = crumb[1];
				break;
			}
		}
		if (!sessionId) {
			return null;
		}
		return sessionId;
	}
};
ANY.dom = {
	get: function (nodeId) {
		return document.getElementById(nodeId);
	},

	getVal: function (nodeId) {
		var node = ANY.dom.get(nodeId);
		if (!node) {
			return null;
		}
		return node.value;
	},
	
	createNode: function(tagName, nodeValue, attrs){
		var el = document.createElement(tagName);
		for(var attr in attrs) {
			var styleNode = document.createAttribute(attr);
			styleNode.nodeValue = attrs[attr];
			el.attributes.setNamedItem(styleNode);
		}
		if (nodeValue != null && nodeValue != 'undefined') {
			if (typeof nodeValue == 'string') {
				var textNode = document.createTextNode(nodeValue);
				el.appendChild(textNode);
			} else if (typeof nodeValue == 'object'){
				el.appendChild(nodeValue);
			}
		}
		return el;
	},
	
	addNode: function(tagName, childNodes, attrs) {
		var el = document.createElement(tagName);
		for(var attr in attrs) {
			var styleNode = document.createAttribute(attr);
			styleNode.nodeValue = attrs[attr];
			el.attributes.setNamedItem(styleNode);
		}
		for(var i=0;i<childNodes.length;i++) {
			el.appendChild(childNodes[i]);
		}
		return el;
	},
	
	addEvent: function(el, type, func, useCapture) {
		if (!el) {
			return false;
		}
		if (!useCapture) {
			useCapture = false;
		}
		if (el.addEventListener) {
			el.addEventListener(type, func, false);
		} else if (el.attachEvent) {
			el.attachEvent('on'+type, func);
		} else {
			return false;
		}
		return true;
	},
	
	removeEvent: function(el, type, func, useCapture){
		if (!el) {
			return false;
		}
		if (!useCapture) {
			useCapture = false;
		}
		if (el.removeEventListener) {
			el.removeEventListener(type, func, useCapture);
		} else if (el.detachEvent) {
			el.detachEvent('on'+type, func);
		} else {
			return false;
		}
		return true;
	},
	
	getSelectedVal: function(nodeId) {
		var el = ANY.dom.get(nodeId);
		if (!el) {
			alert('cannot get element');
			return null;
		}
		var options = el.options;
		var index = el.selectedIndex;
		return options[index].value;
	}
};