/* For Send to a Friend page */

// 1. Checks that the email is valid onsubmit.
// 2. Updates the preview in realtime.

function isEmailValid(email)
{
	var emailFilter=/^.+@.+\..{2,3,4,6}$/;
    if ((emailFilter.test(email))) 
	{
	    return true;
	}
	else
	{
	    return false;
	}
}


Event.observe(window, 'load', function(){
   Event.observe('name', 'keyup', updatePreview);
   Event.observe('email', 'keyup', updatePreview);
   updatePreview();
});

function htmlentities(dirtyString){
	var cleanString=dirtyString.replace(/\</g,"&lt;");
	cleanString=cleanString.replace(/\>/g,"&gt;");
	cleanString=cleanString.replace(/\&[^(lt;|gt;)]/g,"&amp;");

	return cleanString;	
}

function checkForm() {
    var errors = 0;
    
    // Check name
    if ($F('name').length == 0) { errors++; alert("Please enter a name."); }
    
    // Check email is present and correct
    if($F('email').length == 0) { errors++; alert("Please enter an email address."); }
    else if (isEmailValid($F('email'))) { errors++; alert("Please enter a valid email address"); }
    
    if (errors == 0) {
        return true; // Data entered appears to be alright.
    }
    else
    {
        return false; // Do not submit the form.
    };
}



function updatePreview() {
    name = "[name]";
    email = "[email]";
    from = $F('from');
    
    if ($F('name') != "") { name = htmlentities($F('name')); };
    if ($F('email') != "") { email = htmlentities($F('email')); };
    
    Element.update('fromto', "From: "+from+" &#60;donotreply@webbliworld.com&#62;<br />To: "+name+" &#60;"+email+"&#62;");
    Element.update('emailmessage', "Hi "+name+"<br />Check out <a href=\"http://www.webbliworld.com\">www.webbliworld.com</a> and meet the Webblis, the world's craziest gang in the world's biggest playground!<br /><br />Investigate Wij's top-secret WebbliLab, join roving reporter Wanda at the WebbiIceCaps and meet extreme explorer, Wez, in his pursuit of the endangered stripy-nosed elephant! Herb the Yeti will show you how to create and name your very own Webbli character. You can even build a personal WebbliStak, play games, enter competitions and earn Webbles.<br /><br />So, what are you waiting for?  You're just one click away from your first WebbliTastic adventure!");
}
    