function exp_tool(toolname,media) 
{	
    var createElement="false";
    t = document.getElementById("wb-sde-tools-content");
    if(!t)
    {
		t = document.createElement("div");
		t.id="wb-sde-tools-content";
		createElement="true";
    } 
	if(toolname == 'email')
	{
		if(media == 'ar')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'ar\')" border="0">'+
				'<b>To e-mail this article to someone, enter an e-mail address below, then click send.</b><br/>'+
				'    <div class="inputField headRadio"><div class="text">Choose a format: '+
				'	<input type="radio" name="ft" value="pdf" checked>PDF&nbsp;&nbsp;'+
				'		<input type="radio" name="ft" value="html">HTML'+
				'		</div>'+
				'		<input type="text" class="long" name="emailAddress" width="270" size="40"/> <br /></div>'+
				'               <div> ' +
				'               <input type="submit" value="Send" class="send"/>' +
				'		<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'</form>';
		}
		else if(media == 'ta')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'ta\')" border="0">'+
				'<b>To e-mail this table to someone, enter an e-mail address below, then click Send.</b><br/>'+
				'    <div class="inputField headRadio"><div class="text">Choose a format: '+
				'	<input type="radio" name="ft" value="pdf" checked>PDF&nbsp;&nbsp;'+
				'		<input type="radio" name="ft" value="html">HTML'+
				'		</div>'+
				'		<input type="text" name="emailAddress" width="270" size="40"/><br /></div>'+
				'               <div>' +
				'               <input type="submit" value="Send"  class="send"/>' +
				'		<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>' +
				'</form>';
		}
		//added for ENH -43
		else if(media == 'sr')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'sr\')" border="0">'+
				'<b>To e-mail this article to someone, enter an e-mail address below, then click send.</b><br/>'+
				'		<input type="text" class="long" name="emailAddress" width="270" size="40"/> <br /><br /><input type="submit" value="Send" class="send"/>'+
				'		<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/>'+
				'</form>';
		}
		else if(media == 'myresearchemail')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'mr\')" border="0">'+
				'<b>To e-mail this article to someone, enter an e-mail address below, then click send.</b><br/>'+
				'    <div class="inputField headRadio"> '+
				'		<input type="text" class="long" name="email" width="270" size="40"/> <input type="submit" value="Send" class="send"/></div>'+
				'		<div class="cancel"><input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'</form>';
		}
		
		active('email');	
		inactive('save');
	}
	else if(toolname == 'save')
	{
		if(media == 'ar')
		{
			t.innerHTML='<form name="saveForm" method="post" onSubmit="return validateSave(\'ar\')" border="0">'+
				'<div class="headRadio" style="margin:0px 0px 10px;"><div class="text">Choose a format:  '+
				' 	<input type="radio" name="ft" value="pdf" checked> PDF&nbsp;&nbsp;'+
				'	<input type="radio" name="ft" value="html"> HTML '+
				'	</div>'+
				'	<input type="submit" value="Save" class="save"/>'+
				'	<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'   </form>';
		}
		else if(media == 'activity')
		{
			t.innerHTML='<form name="saveForm" method="post" onClick="return validateSave(\'activity\')" border="0">'+
				'  yea </form>';
		}
		else if(media == 'myresearchemail')
		{			
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validateUserEmail();" border="0">'+
				'<b>To e-mail this article to someone, enter an e-mail address below, then click Send.</b><br/>'+
				'		<input type="text" class="long" name="email" width="270" size="40"/><br/>&nbsp;&nbsp;'+
				'		<input type="submit" class="send" value="Send"/>&nbsp;&nbsp;'+
				'		<input type="button" value="Cancel" class="cancel" onclick="javascript:exp_tool(\'none\',\'ar\');"/>'+
				'</form>';
		}
		if(media == 'ta')
		{
			t.innerHTML='<form name="saveForm" method="post" onSubmit="return validateSave(\'ta\')" border="0">'+
				'<div class="headRadio" style="margin:0px 0px 10px;"><div class="text">Choose a format:  '+
				' 	<input type="radio" name="ft" value="pdf" checked> PDF&nbsp;&nbsp;'+
				'	<input type="radio" name="ft" value="html"> HTML '+
				'	</div>'+
				'	<input type="submit" value="Save" class="save"/>'+
				'	<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'   </form>';
		}
		//added for ENH -43
		if(media == 'sr')
		{
			t.innerHTML='<form name="saveForm" method="post" onSubmit="return validateSave(\'sr\')" border="0">'+
				'<div class="headRadio" style="margin:0px 0px 10px;"><div class="text">Choose a format:  '+
				' 	<input type="radio" name="ft" value="html"> HTML '+
				'	</div>'+
				'	<input type="submit" value="Save" class="save"/>'+
				'	<input type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'   </form>';
		}
       	 active(toolname);	
		inactive('email');
	}
	else
	{
		prnt = document.getElementById("wb-sde-tools");
		t.innerHTML='';
		//prnt.removeChild(t);
		inactive('email');
		inactive('save');
	}
	if (createElement == "true")
	{
      prnt = document.getElementById("wb-sde-tools");
		  prnt.appendChild(t);
	}
			
}

function active(tname)
{
    getObject(tname+"Head").className=tname+"_active";
}

function inactive(tname)
{	
   getObject(tname+"Head").className=tname+"_inactive";
}

function getObject(objectId) {
	if (document.all && !document.getElementById) {
		return document.all(objectId);
	} else {
		return document.getElementById(objectId);
	}
}

function validateSave(media)
{
	
	var mt2="";
	
	if(location.search.substring(13,15) == 'em')  // Get query string. 
	{
		mt2 = location.search.substring(16,25);		
	}
	else
	{
		mt2 = getArgs("id"); 
	}	 
	 var rad_val2 = false;
	 if( media == 'ar' || media == 'ta')
	 {
		  for (var i=0; i < document.saveForm.ft.length; i++)
		  {
			if (document.saveForm.ft[i].checked)
			 {
				rad_val2 = true;				
				var a2 = "save?id="+mt2+"&ft="+document.saveForm.ft[i].value;				
				location.href =a2; 
				return false;
			 }
		 }
		 if (!rad_val2)
		 {
			alert("Please select one of the formats.");
			return false;
		 }
	 }
}

function validate(media)
{	
	if(media == 'ar' || media == 'ta' )
	{
		var radioSelected = false;
		for (i = 0;  i < document.emailForm.ft.length;  i++)
		{
			if (document.emailForm.ft[i].checked)
			radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("Please select one of the formats.");
			return false;
		}
	}
	if(media!='mr')
	var emailAdd = document.emailForm.emailAddress.value;
	if(media=='mr')
	var emailAdd = document.emailForm.email.value;
	emailAdd = normalizeQuery(emailAdd); 
	if (emailAdd == "") 
	{
		alert ('Please enter an email address.');
		return false;
	}

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(emailAdd))
	{
		alert("Please input a valid email address!");
		document.emailForm.emailAddress.focus();
		if(media=='mr')
		document.emailForm.email.focus();
		return false;
	}
	if ( (emailAdd.length <= 4) || (emailAdd.indexOf("@") == -1) || (emailAdd.indexOf(".") == -1)) 
	{
		alert("Please input a valid email address!");
		document.emailForm.emailAddress.focus();
		if(media=='mr')
		document.emailForm.email.focus();
		return false;
	}
////////////////// 
	// document.getElementById("wb-sde-tools-content").innerHTML = "<form class=\"confirm\"><strong>Your e-mail has been sent.</strong> <input type=\"submit\" value=\"OK\"/></form>";
/////////////////
	var mtl="";
	var extmtl="";
	if(location.search.substring(13,15) == 'em')  // Get query string. 
	{
		
		extmtl = location.search.substring(16,25);
		mtl = location.search.substring(4,12);
	}
	else
	{
		mtl = getArgs("id"); 
	}
	
	 var rad_val = false;
	 if( media == 'ar' || media == 'ta')
	 {
		
		 for (var i=0; i < document.emailForm.ft.length; i++)
		 {
			if (document.emailForm.ft[i].checked)
			{
			  rad_val = true;
			  var a = "";
			  if(extmtl == ''){
			  	a="email?id="+mtl+"&ft="+document.emailForm.ft[i].value+"&email="+emailAdd;
			  }else{
			  	a="email?id="+extmtl+"&em="+mtl+"&ft="+document.emailForm.ft[i].value+"&email="+emailAdd;
			  }
		    	  location.href =a;
		    	  return false;
			}
		 }
		  //document.emailForm.emailAddress.value=emailAdd;
		 if (!rad_val)
		 {
			alert("Please select one of the formats.");
			return false;
		 }
	 }
	 //added for ENH -43
	 else if (media == 'sr' || media == 'bt')
	 {
		 var a="emailmedia?id="+mtl+"&emailAddress="+emailAdd;
		 location.href =a;
		 return false;
	 }
	else if(media=='mr')
	{
		var a="myresearchemail?email="+emailAdd;
		location.href=a;
		return false;
	}
}

function normalizeQuery(inputStr) {
  var newString = inputStr;

  if (!inputStr.length) return "";

  // Remove leading spaces
  for (i = 0; i < newString.length; i++) {
    if (newString.charAt(i) !=' ') {
      newString = newString.substring(i,newString.length);
      break;
    }
  }

  // Remove trailing spaces
  for (i = newString.length-1; i >= 0 ; i--) {
    if (newString.charAt(i) !=' ')  { 
      newString = newString.substring(0, i+1);
      break;
    }
  }

  // Replace multiple spaces with single spaces
  for (i = 0; i< newString.length-1; i++) {
    if (newString.charAt(i) ==' ') {
      if (newString.charAt(i+1) ==' ') {
        newString= newString.substring(0, i+1) + newString.substring(i+2, newString.length);
        i--;
      }
    }
  }

  if (newString == " ") newString="";

  return newString;  
}

function getArgs(strInput) {
    var args = new Object();
    var query = location.search.substring(1);  // Get query string.
    var pairs = query.split(",");              // Break at comma.
    var value = "";
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');       // Look for "name=value".
	if (pos == -1) continue;               // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
        if(strInput == argname) {
            value = pairs[i].substring(pos+1); // Extract the value.
            value = unescape(value);          // Store as a property.
        }    
    }
    return value;                               // Return the object.
}
function validateUserEmail(){
	
	var emailAdd=document.emailForm.emailAddress.value;
	if (emailAdd == "") 
	{
		alert ('Please enter an email address.');
		return false;
	}
	else if ( (emailAdd.length <= 4) || (emailAdd.indexOf("@") == -1) || (emailAdd.indexOf(".") == -1)) 
	{
		alert("Please input a valid email address!");
		document.emailForm.emailAddress.focus();
		return false;
	}
	else{

		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (!filter.test(emailAdd))
		{
			alert("Please input a valid email address!");
			document.emailForm.emailAddress.focus();
			return false;
		}
		else{
		  	document.emailForm.action="myresearchemail";
		  	document.emailForm.submit;
		}
	}
	
}

//ENH-141: For Read Aloud
window.onload = function()
{	
    $('*', 'span.more').unbind();
    $('span.more').unbind().hover(function(e){
        $(this).append('<div class="elab"><div class="body">'+ $(this).attr("rel") +'</div><div class="bottom"></div></div>');
        $(".elab").css({'left' : e.pageX - 116, 'top' : e.pageY - $('.elab').height()+2});
        $(".elab .body").html($(this).attr("rel"));
        $('.elab').show();
        $('.elab').fadeIn(150);
    }, function(){
        $(".elab").fadeOut(100, function(){
            $(".elab").remove();
        });
    });
};
