function systemCompatible() {
	if (compatInfoAvail) {

		if (isCompat) {
			var confirm_string=null;
			if (reason=="ie_shell" || reason=="browser") {
				confirm_string = "IMPORTANT NOTICE - PLEASE READ\n\n";

				if (value!="None")
					confirm_string += "The Compete Toolbar is not currently compatible with "+value+".  ";
				else
					confirm_string += "A version of the Compete Toolbar is not currently available for this browser.  ";

				confirm_string += "Although you may continue this download now, please note that the toolbar will be installed within Internet Explorer.";
			}

			if (confirm_string!=null)
				return confirm(confirm_string+"\n\nContinue downloading the Compete Toolbar for Internet Explorer?\n");
			else
				return true;
		}
		else {
			var alert_string = "We're sorry - the toolbar is currently only available for ";
			if (reason=="None") {
				alert_string += "Internet Explorer version 6 on Microsoft Windows XP.";
			}
			else if (reason=="windows") {
				alert_string += "Microsoft Windows XP.";
				if (value!="None")
					alert_string += "  You have Windows "+value+".";
			}
			else if (reason=="version") {
				alert_string += "Microsoft Internet Explorer version 6.";
				if (value!="None")
					alert_string += "  You have version "+value+".";
			}
			alert(alert_string);
			return false;
		}
	}
	alert("We're sorry - the toolbar is currently only available for Internet Explorer version 6 on Microsoft Windows XP.");
	return false;
}


function dealPopup(domain, dealId, obscured, subdomain) {
	var obsString = obscured? "?obs=1" : "";
	var popupUrl = "/dealpopup/"+domain+"/"+dealId+"/"+obsString;
	if (subdomain) {
		popupUrl = subdomain+popupUrl;
	}

	var cideal = window.open(popupUrl, "_blank", "width=300,height=300,screenX=10,screenY=10,left=10,top=10");
	if (cideal==null || typeof(cideal)=="undefined") {
		if (!obscured)
			popupUrl += "?obs=1";

		var allowUrl = '/allowpopups?domain='+escape(domain)+'&popup='+escape(popupUrl);
		if (subdomain) {
			allowUrl = subdomain+allowUrl;
		}
		window.location=allowUrl;
		return false;
	}
	else {
		cideal.focus();
		return true;
	}
}

function openBlockedPopup(url) {
	var newwin = window.open(url, '_blank', 'width=300,height=300,screenX=10,screenY=10,left=10,top=10');
	if (newwin!=null && typeof(newwin)!="undefined")
		history.go(-1);
}

function truncateChildText(obj, tagName, width) {
	var childNodes = obj.getElementsByTagName(tagName);
	var node;
	var trunc;

	for (var i=0; i < childNodes.length; i++) {
		node = childNodes[i];
		truncateString(node, width);
	}
}

function truncateString(obj, pixelWidth) {
	if (!obj.innerText) return;

	var str		  = obj.innerText;
	var currLen	  = str.length;
	var truncated = false;
	var strWidth  = getPixelWidth(obj, truncated);

	while (strWidth > pixelWidth) {
		if (!truncated) {
			truncated = true;
			str = str.substr(0, str.length-1)+"...";
		}
		else {
			str = str.substr(0, str.length-4)+"...";
		}
		obj.innerText = str;
		strWidth = getPixelWidth(obj);
	}
}

function getPixelWidth(obj) {
	return obj.offsetWidth;
}

function setItemContents(anchorObj, text) {
	anchorObj.innerHTML = text;
}

function doMenuAction(anchorObj, domain, dealId) {
	var parent = anchorObj.parentNode;
	var allLinks = parent.getElementsByTagName("A");
	for (var i=0; i < allLinks.length; i++) {
		if (allLinks[i]==anchorObj)
			allLinks[i].innerText='Loading coupon...';
		else
			allLinks[i].disabled=true;
	}

	dealPopup(domain, dealId, true);
	return false;
}
function onloadEvent(method) {
	if (window.attachEvent) {
		return window.attachEvent("onload", method);
	}
	else if (window.addEventListener) {
		window.addEventListener("load", method, false);
		return true;
	}
	else {
		var oldonload = window.onload;
		if (typeof oldonload != 'function') {
			window.onload = method;
		}
		else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				method();
			}
		}
		return true;
	}
}

function addHandler(object, eventName, method) {
	if (object.attachEvent) {
		return object.attachEvent("on"+eventName, method);
	}
	else if (object.addEventListener) {
		object.addEventListener(eventName, method, false);
		return true;
	}
	else {
		return true;
	}
}

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

function trimInputFields() {
	var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];

        if (theInput && theInput.type == 'text') {
            theInput.value = trim(theInput.value);
        }
    }
    var formText = document.getElementsByTagName('textarea');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formText[i];
        if (theInput) {
        	theInput.value = trim(theInput.value);
        }
    }
}
function addOnSubmitTrim() {
	for (var i=0;i<document.forms.length;i++) {
		addHandler(document.forms[i], "submit", trimInputFields);
	}
}
onloadEvent(addOnSubmitTrim);

function validUrl(url) {
	var urlRegex = /^(https?:\/\/)?([a-zA-Z0-9]([\.\-_]?[a-zA-Z0-9])*(\.[a-zA-Z]{2,})+)(\/\S*)?$/;
	return url.match(urlRegex);
}

function hostname(url) {
	var urlRegex = /^(https?:\/\/)?([a-zA-Z0-9]([\.\-_]?[a-zA-Z0-9])*(\.[a-zA-Z]{2,})+)(\/\S*)?$/;
	var m = urlRegex.exec(url);
	if (m && m.length > 2) {
		return m[2];
	}
	else {
		return url;
	}
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

	// CODE RELATED TO "ENTER A SITE" BOXES
	function setSnapShot(snapshotSite) {
		clearSiteError();
		var maxlength = 255;
		var domainList = document.getElementById('snap_input').value;
		reg = /\s+/g;
		domainList=domainList.replace(reg,"");
		if (domainList.length==0) {
			setSnapshotFocus();
			document.getElementById('snap_input').value = "";
			return false;
		}

		var domain=domainList.split(",");
		var domains = "";
		var newDomainList;
		for (var i=0; i < domain.length; i++) {
			if (domain[i].length == 0) continue;
			if (domain[i].indexOf('.')==-1) {
				domain[i] += ".com";
			}
			domain[i] = domain[i].replace("https://","");
			domain[i] = domain[i].replace("http://","");

			domain[i] = hostname(domain[i]);
			if (domain[i].length==0) continue;

			newDomainList = domains+domain[i];
			if (i!=domain.length-1){
				newDomainList = newDomainList+"+";
			}
			if (newDomainList.length <= maxlength) {
				domains = newDomainList;
			}
			else {

				if (confirm("Sorry! The combined length of the domains you entered ("+newDomainList.length+" characters) is too long. The maximum length is "+maxlength+".\n\nWould you like to load SnapShot for just the first "+i+" domains?")) {
					alert("error");
					break;
				}
				else {
					return;
				}
			}
		}

		// redirect page
		window.location = snapshotSite+"/"+escape(domains);

		return false;
	}

	function clearSiteError() {
        // $j('#siteError').html('');
	}
	function displaySiteError(strn) {
        // $j('#siteError').html(strn);
	}
	function getElem(id) {
		return document.getElementById(id);
	}
	function setSnapshotFocus(){
		document.getElementById('snap_input').focus();
	}
	function setSearchFocus() {
		document.getElementById('search_input').focus();
	}
	function checkSearch(){
		var terms = trim(document.getElementById('search_input').value);
		if (terms.length==0) {
			setSearchFocus();
			document.getElementById('search_input').value = "";
			return false;
		}
		else {
			return true;
		}
	}
	function flip(fromTab, toTab, imgDir) {
		// hide fromTab stuff
		document.getElementById(fromTab+'_tab').innerHTML = '<a href="javascript:flip(\''+toTab+'\',\''+fromTab+'\',\''+imgDir+'\');"></a>';
		document.getElementById(fromTab).style.display = "none";

		// show and focus toTab stuff
		document.getElementById(toTab+'_tab').innerHTML = "<img src='"+imgDir+"/home-"+toTab+"-active.gif'/>";
		document.getElementById(toTab).style.display = "inline";
		document.getElementById(toTab+'_input').value = document.getElementById(fromTab+'_input').value;
		document.getElementById(toTab+'_input').focus();
	}

	function search(){
		document.getElementById('search').style.display = "inline";
		document.getElementById('snap').style.display = "none";
		document.getElementById('search_input').value = document.getElementById('snap_input').value;
	}
	function snapshot(){
		document.getElementById('snap').style.display = "inline";
		document.getElementById('search').style.display = "none";
		document.getElementById('snap_input').value = document.getElementById('search_input').value;
	}

function getText(el){
	if (typeof el.textContent == 'string') return el.textContent;
	if (typeof el.innerText == 'string') return el.innerText;
	return el.innerHTML.replace(/<[^>]*>/g,'');
}
function escapeUTF8(string) {
	if (encodeURIComponent) {
	    return encodeURIComponent(string);
	} else {
	    return escape(string);
	}
}
String.prototype.commafy = function () {
	return this.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
		return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
	});
}

Number.prototype.commafy = function () {
	return String(this).commafy();
}
