var tools = {};
tools.forEach = function(items, func) {
	for (var i = 0; items && i < items.length; i++)
		func(items[i]);
}
function Setcookie(name, value) {
	var argc = arguments.length;
	var argv = arguments;
	var path = (argc >= 3) ? argv[2] : null;
	var domain = (argc >= 4) ? argv[3] : null;
	var secure = (argc >= 5) ? argv[4] : false;
	var expire = new Date("December 31, 2020");
	var str = name + "=" + value + ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "")
			+ ((expire == null) ? "" : (";expires=" + expire.toGMTString()));
	document.cookie = str;
}
function Deletecookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = GetCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function Clearcookie() {
	var temp = document.cookie.split(";");
	var loop3;
	var ts;
	for (loop3 = 0; loop3 < temp.length; loop3++) {
		ts = temp[loop3].split("=")[0];
		if (ts.indexOf('mycat') != -1)
			DeleteCookie(ts);
	}
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}
tools.cookie = function(cookieName, cookieValue) {
	if (cookieName && cookieValue) {
		Setcookie(cookieName, cookieValue, "/");
	}
	if (cookieName)
		return GetCookie(cookieName);
}
tools.debug = function() {
}
tools.getDateStr = function() {
	var d = new Date();
	var s1 = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "["
			+ d.getMilliseconds() + "ms]"
	return s1;
}
tools.getTimeStampStr = function(start) {
	var unit = 1000;
	var d = new Date();
	var now = Math.round(d.getTime() / unit);
	var intervalTime = now - start;
	if (intervalTime < 0)
		return "less than 1 second ago";
	var tt;
	tt = Math.floor(intervalTime / (60 * 60 * 24 * 365));
	if (tt >= 1) {
		if (tt == 1)
			return " 1 year ago";
		else
			return " " + tt + " years ago"
	}
	tt = Math.floor(intervalTime / (60 * 60 * 24 * 30));
	if (tt >= 1) {
		if (tt == 1)
			return " 1 month ago"
		else
			return " " + tt + " months ago"
	}
	tt = Math.floor(intervalTime / (60 * 60 * 24 * 7));
	if (tt >= 1) {
		if (tt == 1)
			return " 1 week ago"
		else
			return " " + tt + " weeks ago"
	}
	tt = Math.floor(intervalTime / (60 * 60 * 24));
	if (tt >= 1) {
		if (tt == 1)
			return " 1 day ago";
		else
			return " " + tt + " days ago";
	}
	tt = Math.floor(intervalTime / (60 * 60));
	if (tt >= 1) {
		if (tt == 1)
			return " 1 hour ago";
		else
			return " " + tt + " hours ago";
	}
	tt = Math.floor(intervalTime / 60);
	if (tt >= 1) {
		if (tt == 1)
			return " 1 minute ago"
		else
			return " " + tt + " minutes ago";
	}
	tt = Math.floor(intervalTime)
	if (tt >= 1)
		return " 1 second ago";
	else
		return " " + tt + " seconds ago";
	return "";
}
function getUserId() {
	UserId = tools.cookie('UserId');
	if (!UserId)
		UserId = '';
	if (_Debug)
		UserId = "guest10203040";
	return UserId;
}
Array.prototype.indexOf = function(item) {
	for (var i = 0; i < this.length; i++) {
		if (item == this[i]) {
			return i;
		}
	}
	return -1;
}
Array.prototype.insertAt = function(index, obj) {
	this.splice(index, 0, obj);
}
Array.prototype.removeAt = function(index) {
	this.splice(index, 1);
}
Array.prototype.type = "Array";
String.prototype.type = "String";
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
Array.prototype.equal = function(arr) {
	if (arr.type == "Array") {
		for (var i = 0; i < this.length; i++) {
			var index = arr.indexOf(this[i]);
			if (index == -1)
				return false;
		}
		for (var i = 0; i < arr.length; i++) {
			var index = this.indexOf(arr[i]);
			if (index == -1)
				return false;
		}
		return true;
	}
	return false;
}
function removeQuotes(str) {
	var re = /\"/g;
	return str.replace(re, "");
}
function str2Obj(str) {
	var obj;
	try {
		obj = eval('(' + str + ')');
	} catch (e) {
		obj = {};
	}
	return obj;
}
function obj2Str(obj) {
	return JSON.stringify(obj);
}
function getRadioValue(name) {
	var nodes = document.getElementsByName(name);
	for (var i = 0; i < nodes.length; i++) {
		if (nodes[i].checked) {
			return nodes[i].value;
		}
	}
}
function isIE() {
	if (navigator.userAgent.indexOf("MSIE") >= 0)
		return true;
	else
		return false;
}
function addEmail() {
	var email = document.getElementById('email').value;
	var re = /^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/;
	var value = re.test(email);
	if (value) {
		var cmdPath = getServerUrl('/cgi-bin/strm/betatest.py');
		var req = new tickreelRequest(cmdPath, {
					c : 'add',
					email : email
				}, addEmailFunc, {});
		requestHandler.handleRequest(req);
	} else {
		alert('format error! ');
	}
}
function addEmailFunc(obj) {
	if (obj.stat == '0') {
		var msg = document.getElementById('msg');
		msg.innerHTML = 'Thank you for your interest. We will send you the invitation code soon.';
	}
}

