function Utils(){}

Utils.queryParamValueForKey = function (name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

Utils.enableField = function (field){
	field.value = "";
	field.className = "active";
}

Utils.trim = function (text, maxLength) {
	return (text.length > maxLength) ? text.substring(0, maxLength - 3) + "..." : text;
}

Utils.contentIdFromURL = function (url) {
	if (url === undefined) return "";
	var matches = url.match(/\d{4}-\d+(_\d)?-(\d+)/);
	// if we get a URL, return its match. otherwise, return the url var which should be the content Id itself
	return (matches) ? matches[2] : url;
}

Utils.updateEmailButton = function() {
	var mailTo = document.getElementById("mailTo");
	var emailSubject = escape(document.getElementById("mailToSubject").innerHTML);
	var emailURL = unescape(document.getElementById("mailToURL").innerHTML);
	var emailBody = escape(document.getElementById("mailToBody").innerHTML + emailURL);
	mailTo.href = "mailto:?subject=" + emailSubject + "&body=" + emailBody;	
}

Utils.adTimer = function() {
	if (document.getElementById('countdownTimer')) {
		var numSeconds = 5;
		var countdownLink = document.getElementById('countdownTimer');
		var loadAd = function() {
			countdownLink.innerHTML = "Your video will start in " + numSeconds + " seconds";
			numSeconds--;
			if (numSeconds < 0) {
				clearInterval(loadTimer);
				var target = new Object();
				target.href = countdownLink.href;
				target.title = "CNET Video";
				var ajax = new iphone.ajax(countdownLink.href, function(content) {
					countdownLink.style.display = "none";
					document.getElementById("sponsoredText").style.display = "block";
					document.getElementById("cnettvAd").style.display = "none";
					document.getElementById("cnettvClip").innerHTML = content;
				});
				ajax.run();
			}
		}
			
		var loadTimer = setInterval(loadAd, 1000);	
		setTimeout(loadAd, 0);        		
	}
}

Utils.submitForm = function(form) {
	var target = new Object();
	target.href = form.actionURL.value + "&query=" + form.query.value;
	target.title= form.title;
	ciUI.goToPage(target, false, true);
}

Utils.postFunct = function() {
	Utils.updateEmailButton();
	Utils.adTimer();
}

Utils.goToStandardSite = function() {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+30);
	document.cookie = "cnetiphone=0;domain=.cnet.com;expires="+exdate;
	location.href="http://www.cnet.com";
}

Utils.stayMobile = function() {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+30);
	document.cookie = "cnetiphone=1;domain=.cnet.com;expires="+exdate;
	document.getElementById("redirMessage").style.display = "none";
	location.href="?";
}

Utils.getCookie = function(cookieName) {
  var results = document.cookie.match ('(^|;) ?' + cookieName + '=([^;]*)(;|$)');
  if (results)
    return (unescape(results[2]));
  else
    return null;
}

addEventListener("load", function(event) {
	if (Utils.queryParamValueForKey("email") == 1) {
		var target = new Object();
		var nodeId = Utils.queryParamValueForKey("node");
		var assetId = Utils.queryParamValueForKey("asset");
		var pageId = Utils.queryParamValueForKey("pageId");
		target.href = "4250-" + nodeId + "-" + assetId + ".html?nomesh&page=" + pageId;
		target.title = unescape(Utils.queryParamValueForKey("title"));
		ciUI.goToPage(target);
	}
	ciUI.postFunct = Utils.postFunct;
}, false);