		
function js_email($name,$domain){
   if(!$domain){ $domain='cmg.org';}
   $email = $name + "@" + $domain;
   $link=$email;
   document.write("<a href=\"mailto:" + $email + "\">" + $link + "</a>");
}


function js_email_link($name,$domain,$link){
   if(!$domain){ $domain='cmg.org';}
   $email = $name + "@" + $domain;
   if(!$link){$link=$email;}
   document.write("<a href=\"mailto:" + $email + "\">" + $link + "</a>");
}

function js_email_link_subject($name,$domain,$link,$subject){
   if(!$domain){ $domain='cmg.org';}
   $email = $name + "@" + $domain;
   if(!$link){$link=$email;}
   $subject=escape($subject);
   document.write("<a href=\"mailto:" + $email + "?subject=" + $subject + "\">" + $link + "</a>");
}

function js_email_subject($name,$domain,$subject){
   if(!$domain){ $domain='cmg.org';}
   $email = $name + "@" + $domain;
   $link=$email;
   $subject=escape($subject);
   document.write("<a href=\"mailto:" + $email + "?subject=" + $subject + "\">" + $link + "</a>");
}

function js_email_link_subject_body($name,$domain,$link,$subject,$body){
   if(!$domain){ $domain='cmg.org';}
   $email = $name + "@" + $domain;
   if(!$link){$link=$email;}
   $subject=escape($subject);
   $body=escape($body);
   document.write("<a href=\"mailto:" + $email + "?subject=" + $subject + "&body=" + $body + "\">" + $link + "</a>");
}

function toggleMenu(currMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currMenu).style
		if (thisMenu.display == "block") {
			thisMenu.display = "none"
		}
		else {
			thisMenu.display = "block"
		}
		return false
	}
	else {
		return true
	}
}

//Used by Measure IT pages
function cookieVal(cookieName) {
	thisCookie = document.cookie.split("; ")
	for (i=0; i<thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1]
		}
	}
	return ""
}


function cookieTest(){
	/* Set a cookie to be sure that one exists.*/
	/*
		expireDate = new Date
		expireDate.setMonth(expireDate.getMonth()+1)
		document.cookie = "test="+0+";expires=" + expireDate.toGMTString()
	*/
	document.cookie = "test="+0;
	if(document.cookie == ""){
		return false;
	}else{
		return true;
	}
}


function isValid(){


	bypass=0;
	
	if(window.location.href.match(/test\.cmg\.org/)){
		bypass=1; // Set bypass to 1 on test server
		//alert('auth disabled');
	}
	
	//loginCookie='valid';
	// used to get set = 1
	// Set by masterpassword library, however still using valid cookie, to let MeasureIT users stay logged in for 3 year
	// As of 2007, now using masterpassword library exclusively, so we can track readership.
	loginCookie='CMGUSERID'; 
	
	if (bypass || !cookieTest()) {
	    /* if a cookie is not found - let them pass  */
    		//alert("COOKIES need to be enabled to proceed!");
    		
    	}else{
	   valid=cookieVal(loginCookie);
	    if(valid==""){ 
	    	if(isFreeRide()){
	    	   // Do nothing 
	    	   //alert("Checking for free ride");
	    	}else{

	    	   // Redirect to login page.
		   alert("Thanks for your interest in MeasureIT, CMG's free monthly online newsletter. You have exceeded the maximum number of unregistered page views. Please login or register to continue viewing articles.");
		   //Remember document they were trying to access
		   where_next=window.location
		   document.cookie = "where_next="+where_next+";path=/;expires=" + expireDate.toGMTString()
		   //Redirect to login page.
		   window.location='/measureit/login.html';
		}
	    }else{
		// else they have a login cookie set
		//clear where next
		document.cookie = "where_next="+''+";path=/;expires=" + expireDate.toGMTString()
	    }

	}
}

function logout(){
	oldLoginCookie='valid'	
	loginCookie='CMGUSERID'
	expireDate = new Date
	expireDate.setMonth(expireDate.getMonth()-1)
	document.cookie = loginCookie +"="+0+";path=/;expires=" + expireDate.toGMTString()
	document.cookie = oldLoginCookie +"="+0+";path=/measureit/;expires=" + expireDate.toGMTString()
	alert("Logout Successful!");

}


function isFreeRide(){   
	threshold=2; // number of free article views/month
	digg_bump=10; // allow 10 free article view to digg referred users
	view_count=cookieVal('view_count');

	// Set date to First of next month, that way view_count is reset to 0 every month.
	expireDate = new Date
	expireDate.setDate(1)
	expireDate.setHours(0)
	expireDate.setMinutes(0)
	expireDate.setSeconds(0)
	expireDate.setMonth(expireDate.getMonth()+1)

	//alert(expireDate.toString());

	
	if(view_count==""){
		view_count=0;

	}
	
	// Check if they came from digg, if so bump down the view count (only once)
	ref=document.referrer;
	if(ref.indexOf("digg.com")!=-1 && view_count >= 0){ //true if came from digg.com
		view_count=Number(view_count)-Number(digg_bump);
	}
	// else they have a login cookie set or under threshold
	var file=window.location.pathname;
	if(file.indexOf("/issues/")!=-1){ // i.e. artcicle view
		//alert("Incrementing view_count");
		view_count=Number(view_count)+Number(1);
		document.cookie = "view_count=" + view_count + ";path=/;expires=" + expireDate.toGMTString();
	}
	
	if(view_count!="" && view_count<=threshold){ 
		//alert("Under threshold");
		return 1;
	}else{
		//alert("Inside free ride else");
		return 0;
	}

}

function localize(str){
	
	var d = new Date(str)
	document.write(d.toString())
}

function popup(path,width,height){
	dimens='width=' + width + ',height=' + height; 
	XW = window.open(path,'Xwin',dimens);
	XW.focus();
}


function mit_loginout(){
	valid=cookieVal('CMGUSERID');
	if(valid==''){
		document.write('<a href="/measureit/login.html">Login & Help</a>');
	}else{
		document.write('<a href="javascript:logout()">Logout</a>');
	}
}

function isLoggedIn(){
	valid=cookieVal('CMGUSERID');
	if(valid==''){
		return(false);
	}else{
		return(true);
	}
}

function getWindowHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return ( myHeight );
}
function getWindowWidth() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return ( myWidth );

}

function sizeCol(){
	var win_ht     = getWindowHeight();
	var content_ht = document.getElementById('context').offsetHeight;
	var fudge      = 250 /* other elements plus padding and fudge factor */
	var col_ht     = win_ht - content_ht - fudge
	if(col_ht < 0){
		col_ht = 1
	}
	document.getElementById('column').style.height = col_ht + "px";

}


function gtrack(url){
	var pageTracker = _gat._getTracker("UA-4046573-2");
	pageTracker._trackPageview(url);
}