/*************		BEGIN PRELOAD OF BACKGROUND IMAGES 	*************/
function doPreload()
{
   var the_images = new Array('images/search/search-dunes.jpg','images/search/search-historical.jpg','images/search/search-iwo-jima.jpg','images/search/search-nebula.jpg','images/search/search-penguins.jpg','images/search/search-pyramids.jpg','images/search/search-the-arts.jpg','images/search/search-tiger.jpg','images/search/search-volcano.jpg','images/search/search-windmill.jpg');
   preloadImages(the_images);
}

function preloadImages(the_images_array) {

   for(var loop = 0; loop < the_images_array.length; loop++)
   {
 	var an_image = new Image();
	an_image.src = the_images_array[loop];
   }
}
/*************		END PRELOAD OF BACKGROUND IMAGES	*************/


/*************		BEGIN FUNCTION TO CHANGE THE BACKGROUND IMAGE ON THE HOME PAGE 	*************/
function changeback(){
	//var images = new Array(10);
	var images = new Array(6);
	
	//var textArray = new Array(10);
	var articlesId = new Array(6);
	
	//var textArray = new Array(10);
	var articlesTitle = new Array(6);
	images[0] = 'img/search/beach.jpg';
        images[1] = 'img/search/bird.jpg';
        images[2] = 'img/search/caterpillar.jpg';
        images[3] = 'img/search/mural.jpg';
        images[4] = 'img/search/tut.jpg';
        images[5] = 'img/search/waterfall.jpg';
	
	articlesId[0] = 'ar833888';
	articlesId[1] = 'ar831761';
	articlesId[2] = 'ar830520';
	articlesId[3] = 'ar834558';
	articlesId[4] = 'ar832027';
	articlesId[5] = 'ar831960';

	articlesTitle[0] = 'Nayarit';
	articlesTitle[1] = 'Tlaxcala';
	articlesTitle[2] = 'Caterpillar';
	articlesTitle[3] = 'Photography';
	articlesTitle[4] = 'Seven Natural Wonders of the World';
	articlesTitle[5] = 'Tutankhamen';


	var objDiv = document.getElementById("home-center-top");
	
	var i = Math.floor(Math.random() * images.length);
	objDiv.style.backgroundImage = 'url(' + images[i] + ')';
	objDiv.innerHTML='<a href=article?id='+articlesId[i]+'><img src="'+images[i]+'"/></a>';
	setTimeout('changeback()',5000);
}
/*************		END FUNCTION TO CHANGE THE BACKGROUND IMAGE ON THE HOME PAGE 	*************/


/*************		BEGIN FUNCTION TO SET FOCUS ON A PARTICULAR INPUT FIELD 	*************/
function setFocusOfElement(which){
	var theInputField = document.getElementById(which);
	theInputField.focus();
}
/*************		END FUNCTION TO SET FOCUS ON A PARTICULAR INPUT FIELD 	*************/

/*************		BEGIN FUNCTION TO HIDE AWAY AN ERROR MESSAGE AFTER 5 SECONDS 	*************/
/*** USE THIS CODE TO REMOVE THE ERROR MESSAGE
hideawayerrorbox('error-classes');		-- THIS NEEDS TO BE PLACED IN THE ONLOAD EVENT WHEN THE ERROR MESSAGE APPEARS
hideawayerrorbox						-- THIS IS THE NAME OF THE FUNCTION
error-classes							-- THIS IS THE NAME OF THE DIV THAT IS TO HIDE AFTER 5 SECONDS
***/
var theerrordiv;
function hideawayerrorbox(which){
	theerrordiv = document.getElementById(which);
	setTimeout('hideerror()',5000);
}
function hideerror(){
	theerrordiv.style.display = 'none';
}
/*************		END FUNCTION TO HIDE AWAY AN ERROR MESSAGE AFTER 5 SECONDS 	*************/







/************* BEGIN FUNCTION TO EXPAND THE FOOTER DROP DOWN *************/
function setDivPosition(which){
    var divToPos = document.getElementById(which);
    divToPos.style.display = 'block';
}
function hideRollOverDiv(which){
    var divToHide = document.getElementById(which);
    divToHide.style.display = 'none';
}
/************* END FUNCTION TO EXPAND THE FOOTER DROP DOWN *************/


/************* BEGIN FUNCTION TO EXPAND TOOLS DROP DOWN *************/
function eaxp_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>To e-mail this article to someone, enter an e-mail address below, then click send.</b><br/>'+
				'    <div class="inputField">  '+
				'       <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'+							
				'		    <input type="text" class="long" name="email" width="270" size="40"/>'+
				'		</div>'+	
				'       <input type="submit" value="Send" class="send"/>'+
				'	    <input class="cancel" type="button" value="Cancel" onclick="javascript:exp_tool(\'none\');"/></div>'+
				'</form></div>';
		}
		else if(media == 'ta')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'ta\')" border="0">'+
				'<b>Pour envoyer cet article &aacute; un(e) ami(e), entrez son adresse de courriel dans la boite ci-dessous et cliquez sur Envoyer.</b><br/>'+
				'		<input type="text" name="email" width="270" size="40"/><br/>&nbsp;&nbsp;'+
				'		<input type="submit" value="Envoyer"/>&nbsp;&nbsp;'+
				'		<input type="button" value="Annuler" onclick="javascript:exp_tool(\'none\');"/>'+
				'</form>';
		}
		else if(media == 'activity')
		{
			t.innerHTML='<form name="emailForm" method="post" onSubmit="return validate(\'activity\')" border="0">'+
				'<b>Pour envoyer cet article &aacute; un(e) ami(e), entrez son adresse de courriel dans la boite ci-dessous et cliquez sur Envoyer.</b><br/>'+
				'		<input type="text" name="email" width="270" size="40"/><br/>&nbsp;&nbsp;'+
				'		<input type="submit" value="Envoyer"/>&nbsp;&nbsp;'+
				'		<input type="button" value="Annuler" 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">Choose a format:  '+
				' 	<input type="radio" name="ft" value="pdf" checked> PDF&nbsp;&nbsp;'+
				'	<input type="radio" name="ft" value="html"> HTML '+				
				'	<input type="text" class="long" />'+
				'	</div>'+
				'	<input type="submit" value="Save" class="save"/>'+
				'	<input class="cancel" type="button" value="Cancel" 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("wb-sde-tools");
		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 = 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="/kids/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;
		 }
	 }
	 else if( media == 'ta')
	 {
		var a2="/kids/Save?id="+mt2+"&ft=html";
		location.href =a2; 
		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("Please select one of the formats.");
			return false;
		}
	}
	
	var emailAddress = document.emailForm.email.value;
	emailAddress = normalizeQuery(emailAddress); 
	if (emailAddress == "") 
	{
		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(emailAddress))
	{
		alert("Please input a valid email address!");
		document.emailForm.email.focus();
		return false;
	}
	if ( (emailAddress.length <= 4) || (emailAddress.indexOf("@") == -1) || (emailAddress.indexOf(".") == -1)) 
	{
		alert("Please input a valid email address!");
		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 = 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="/kids/Email?id="+mtl+"&ft="+rad_val+"&email="+emailAddress;		
		  location.href =a;  
		  return false;
	 }
	 else if (media == 'ta')
	 {
		 var a="/kids/Email?id="+mtl+"&ft=html&email="+emailAddress;
		 location.href =a; 
		 return false;
	 }
	else if (media == 'activity')
	 {
		 var a="/kids/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 *************/
