/************* BEGIN FUNCTION TO EXPAND TOOLS DROP DOWN *************/
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='<div class="wb-sde-tools-content"><form name="emailForm" method="post" onSubmit="return validate(\'ar\')" border="0">'+
				'<b>Para enviar este artículo a alguien, entrar una dirección de correo electrónico abajo, entonces clic en enviar.</b><br/>'+
				'    <div class="inputField">  '+
				'       <div class="text">Elegir un formato: '+
				'	        <input type="radio" name="ft" value="pdf" checked>PDF&nbsp;&nbsp;'+
				'		    <input type="radio" name="ft" value="html">HTML'+							
				'		    <input type="text" class="long" name="email" width="270" size="40"/>'+
				'		</div>'+	
				'       <input type="submit" value="Enviar" class="send"/>'+
				'	    <input class="cancel" type="button" value="Cancelar" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'</form></div>';
		}
		else if(media == 'ta')
		{
			t.innerHTML='<div class="wb-sde-tools-content"><form name="emailForm" method="post" onSubmit="return validate(\'ta\')" border="0">'+
				'<b>E-mail a esta mesa a alguien, escriba una dirección de correo electrónico a continuación, haga clic en Enviar.</b><br/>'+
				'    <div class="inputField">  '+
				'       <div class="text">Elegir un formato: '+
				'	        <input type="radio" name="ft" value="pdf" checked>PDF&nbsp;&nbsp;'+
				'		    <input type="radio" name="ft" value="html">HTML'+		
				'		<input type="text" name="email" width="270" size="40"/><br/>&nbsp;&nbsp;'+
				'		</div>' +
				'		<input type="submit" value="Enviar" class="send"/>&nbsp;&nbsp;'+
				'		<input type="button" value="Cancelar" class="cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'</form></div>';
		}
		else if(media == 'activity')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'activity\')" border="0">'+
				'<b>Para enviar este actividade a alguien, entrar una dirección de correo electrónico abajo, entonces clic en enviar.</b><br/>'+
				'		<input type="text" name="email" width="270" size="40"/><br/>&nbsp;&nbsp;'+
				'		<input type="submit" value="Enviar"/>&nbsp;&nbsp;'+
				'		<input type="button" value="Cancelar" onclick="javascript:exp_tool(\'none\');"/>'+
				'</form>';
		}
	
		
		
		active('email');	
		inactive('save');
	}
	else if(toolname == 'save')
	{
		if(media == 'ar')
		{
			t.innerHTML='<div class="wb-sde-tools-content"><form name="saveForm" method="post" onSubmit="return validateSave(\'ar\')" border="0">'+
				'<div class="headRadio" style="margin:0px 0px 10px;"><div class="text">Elegir un formato:   '+
				' 	<input type="radio" name="ft" value="pdf" checked> PDF&nbsp;&nbsp;'+
				'	<input type="radio" name="ft" value="html"> HTML '+				
				'	</div>'+
				'	<input type="submit" value="Guardar" class="save"/>'+
				'	<input class="cancel" type="button" value="Cancelar" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'   </form></div>';
		}else if(media == 'ta')
		{
			t.innerHTML='<div class="wb-sde-tools-content"><form name="saveForm" method="post" onSubmit="return validateSave(\'ta\')" border="0">'+
				'<div class="headRadio" style="margin:0px 0px 10px;"><div class="text">Elegir un formato:  '+
				' 	<input type="radio" name="ft" value="pdf" checked> PDF&nbsp;&nbsp;'+
				'	<input type="radio" name="ft" value="html"> HTML '+				
				'	</div>'+
				'	<input type="submit" value="Guardar" class="save"/>'+
				'	<input class="cancel" type="button" value="Cancelar" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'   </form></div>';
		}
		else if(media == 'activity')
		{
			t.innerHTML='<form name="saveForm" method="post" onClick="return validateSave(\'activity\')" border="0">'+
				'  yea </form>';
		}
       	 active(toolname);	
		inactive('email');
	}
	else
	{
		prnt = document.getElementById("toolsSm");
		prnt.removeChild(t);
		inactive('email');
		inactive('save');
	}
	if (createElement == "true")
	{
        prnt = document.getElementById("toolsSm");
		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 = getArgs("id"); 
	 var rad_val2 = false;
	 if( media == 'ar')
	 {
		  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("	Por favor, seleccione uno de los formatos.");
			return false;
		 }
	 }
	 else if( 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("	Por favor, seleccione uno de los formatos.");
			return false;
		 }
	 }
	else if( media == 'activity')
	{
		 
		var a2="/kids/ActSave?id="+mt2+"&ft=pdf";
		location.href =a2; 
		return false;
	}
	
}

function validate(media)
{	
	if(media == 'ar')
	{
		var radioSelected = false;
		for (i = 0;  i < document.emailForm.ft.length;  i++)
		{
			if (document.emailForm.ft[i].checked)
			radioSelected = true;
		}
		if (!radioSelected)
		{
			alert("	Por favor, seleccione uno de los formatos.");
			return false;
		}
	}
	
	var emailAddress = document.emailForm.email.value;
	emailAddress = normalizeQuery(emailAddress); 
	if (emailAddress == "") 
	{
		alert ('Por favor, entrar una dirección válida de correo electrónico.');
		return false;
	}

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(emailAddress))
	{
		alert("Por favor, ingrese una dirección de correo válida");
		document.emailForm.email.focus();
		return false;
	}
	if ( (emailAddress.length <= 4) || (emailAddress.indexOf("@") == -1) || (emailAddress.indexOf(".") == -1)) 
	{
		alert("Por favor, entrar una dirección válida de correo electrónico.");
		document.emailForm.email.focus();
		return false;
	}

	 var mtl = getArgs("id"); 
	 var rad_val;
	 if( media == 'ar')
	 {
		
		 for (var i=0; i < document.emailForm.ft.length; i++)
		 {
			if (document.emailForm.ft[i].checked)
			{
			 rad_val = document.emailForm.ft[i].value;
			}
		 }
		  var a="email?id="+mtl+"&ft="+rad_val+"&email="+emailAddress;		
		  location.href =a;  
		  return false;
	 }
	 else if (media == 'ta')
	 {
	 	 for (var i=0; i < document.emailForm.ft.length; i++)
		 {
			if (document.emailForm.ft[i].checked)
			{
			 rad_val = document.emailForm.ft[i].value;
			}
		 }
		  var a="email?id="+mtl+"&ft="+rad_val+"&email="+emailAddress;		
		  location.href =a;  
		  return false;
	 }
	else if (media == 'activity')
	 {
		 var a="/eeh/email?id="+mtl+"&ft=pdf&email="+emailAddress;
		 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.
}
 
   
/************* END FUNCTION TO EXPAND TOOLS DROP DOWN *************/


//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();
        });
    });
};










