/******************************************************************************

	Copyright (C) 2006-2007  John Ha

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

******************************************************************************/

function isDefined(a) { return typeof(a) != 'undefined'; }

function isObject(a) { return typeof(a) == 'object'; }

function isString(a) { return typeof(a) == 'string'; }

function isArray(a) { return isObject(a) && a.constructor == Array; }

var isIE = document.all;
var isNS6 = document.getElementById && !document.all;
var isOpera = window.opera;
var isSafari = navigator.userAgent.match('AppleWebKit');
var ieTrueBody =
	document.compatMode && document.compatMode != 'BackCompat' ?
		document.documentElement
	:	document.body;

if (!khanh) var khanh = {};
if (!khanh.core) {
	khanh.core = {
		push: function(arr, elm) {
		  arr[arr.length] = elm;
		},

		find: function(arr, s) {
			for(var i = 0; i < arr.length; i++)
				if(arr[i] == s) return i;
			return false;
		},

		// msie, firefox, opera
		browser: function(s) {
			if (s) s = s.toLowerCase();
			else return false;
			var agentStr = navigator.userAgent.toLowerCase();
			if(agentStr.indexOf(s) != -1) {
				if (s == 'msie' && (agentStr.indexOf('opera') != -1)) return false;
				return true;
			} else return false;
		},

		setCookie: function(cookieName, cookieValue, nDays) {
			var today = new Date();
			var expire = new Date();
			if (nDays == null || nDays == 0) nDays = 1;
			expire.setTime(today.getTime() + 3600000 * 24 * nDays);
			document.cookie = cookieName + '=' + escape(cookieValue) + ';expires=' + expire.toGMTString();
		},

		getCookie: function(cookieName) {
			var theCookie = '' + document.cookie;
			var ind = theCookie.indexOf(cookieName);
			if (ind == -1 || cookieName == '') return '';
			var ind1 = theCookie.indexOf(';', ind);
			if (ind1 == -1) ind1 = theCookie.length;
			if (ind + cookieName.length == theCookie.indexOf(';', ind)) return '';
			else return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
		},

		randomString: function(strLen, chars) {
			if (!strLen) strLen = 8;
			if (!chars) chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
			var str = '';
			for (i = 0; i < strLen; i++) {
				rnum = Math.floor(Math.random() * chars.length);
				str += chars.substring(rnum, rnum + 1);
			}
			return str;
		},
		
		// milliseconds
		delay: function(t) {
			date = new Date();
			var curDate = null;
			do curDate = new Date();
			while(curDate - date < t);
		}
	};
}
