
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

function validateFields()
{
	var shareFriend = document.getElementById("shareFriendForm");
	var friendsName = shareFriend.friendsname.value.trim();
	var friendsEmail = shareFriend.friendsemail.value.trim();
	var friendsName2 = shareFriend.friendsname2.value.trim();
	var friendsEmail2 = shareFriend.friendsemail2.value.trim();
	var friendsName3 = shareFriend.friendsname3.value.trim();
	var friendsEmail3 = shareFriend.friendsemail3.value.trim();
	var yourName = shareFriend.yourname.value.trim();	
	var yourEmail = shareFriend.youremail.value.trim();
	var securityCode = shareFriend.securitycode.value;
	var permission = shareFriend.permission.checked;
	var errorFlag = false;
	var friendNameFlag = false;
	var friendEmailFlag = false;
	
	//Check the First Name and Email		
	if (friendsName.length==0)
	{
		friendNameFlag = true;												
	}
		
	if (friendsEmail.length==0)
	{
		friendEmailFlag = true;			
	}
		
	//Check the second set of Name and Email to make sure if one is filled, so is the other
	if (friendsName2.length != 0 || friendsEmail2.length != 0)
	{
		if (friendsName2.length != 0 && friendsEmail2.length == 0)
		{
			friendEmailFlag = true;	
		}
				
		if (friendsName2.length == 0 && friendsEmail2.length != 0)
		{
			friendNameFlag = true;	
		}
		
	}
	
	//Check the third set of Name and Email to make sure if one is filled, so is the other
	if (friendsName3.length != 0 || friendsEmail3.length != 0)
	{
		if (friendsName3.length != 0 && friendsEmail3.length == 0)
		{
			friendEmailFlag = true;	
		}
				
		if (friendsName3.length == 0 && friendsEmail3.length != 0)
		{
			friendNameFlag = true;	
		}		
	}
	
	//Check the Your name/email fields
	if (yourName.length==0)
	{
		document.getElementById("yourNameErrorText").style.display = "block";
		errorFlag = true;								
	}
	else
	{
		document.getElementById("yourNameErrorText").style.display = "none";
	}
	
	if (yourEmail.length==0)
	{
		document.getElementById("yourEmailErrorText").style.display = "block";
		errorFlag = true;			
	}
	else
	{
		document.getElementById("yourEmailErrorText").style.display = "none";
	}
   	
	//Make sure they entered a security code
	if (securityCode.length==0)
	{
		document.getElementById("securityKeyErrorText").style.display = "block";
		errorFlag = true;	
	}
	else
	{
		if (checkCaptchaCode(securityCode) == "true")  //validate the code entered
		{
			document.getElementById("securityKeyErrorText").style.display = "none";			
		}
		else
		{
			document.getElementById("securityKeyErrorText").style.display = "block";
			//reloadCaptcha();
			errorFlag = true;			
		}
	}
	
	
	if (!permission)
	{
		document.getElementById("permissionErrorText").style.display = "block";
		errorFlag = true;	
	}
	else
	{
		document.getElementById("permissionErrorText").style.display = "none";
	}
	
	//THIS SECTION IS FOR MAKING SURE THE DATA ENTERED IS VALID FOR EMAIL/NAME
	
	if (friendsName.length != 0)	
	{		
		var NameValid = checkName(friendsName);
		if (!NameValid)
		{
			friendNameFlag = true;
		}								
	}
	
	if (friendsEmail.length != 0)
	{
		var EmailValid = checkMail(friendsEmail);
		if (!EmailValid)
		{
			friendEmailFlag = true;
		}		
	}
	
	if (friendsName2.length != 0)	
	{		
		var NameValid = checkName(friendsName2);
		if (!NameValid)
		{
			friendNameFlag = true;
		}								
	}
	
	if (friendsEmail2.length != 0)
	{
		var EmailValid = checkMail(friendsEmail2);
		if (!EmailValid)
		{
			friendEmailFlag = true;
		}		
	}
	
	if (friendsName3.length != 0)	
	{		
		var NameValid = checkName(friendsName3);
		if (!NameValid)
		{
			friendNameFlag = true;
		}								
	}
	
	if (friendsEmail3.length != 0)
	{
		var EmailValid = checkMail(friendsEmail3);
		if (!EmailValid)
		{
			friendEmailFlag = true;
		}		
	}
	
	if (yourName.length != 0)	
	{		
		var NameValid = checkName(yourName);
		if (!NameValid)
		{
			document.getElementById("yourNameErrorText").style.display = "block";
			errorFlag = true;
		}
		else
		{
			document.getElementById("yourNameErrorText").style.display = "none";
		}						
	}
	
	if (yourEmail.length != 0)	
	{		
		var EmailValid = checkMail(yourEmail);
		if (!EmailValid)
		{
			document.getElementById("yourEmailErrorText").style.display = "block";
			errorFlag = true;
		}
		else
		{
			document.getElementById("yourEmailErrorText").style.display = "none";
		}						
	}
	
	//checks all the friends names for errors
	if (friendNameFlag)
	{
		document.getElementById("yourFriendsNameErrorText").style.display = "block";
		errorFlag = true;												
	}
	else
	{
		document.getElementById("yourFriendsNameErrorText").style.display = "none";
	}
	
	//checks all the friends emails for errors
	if (friendEmailFlag)
	{
		document.getElementById("yourFriendsEmailErrorText").style.display = "block";
		errorFlag = true;			
	}
	else
	{
		document.getElementById("yourFriendsEmailErrorText").style.display = "none";
	}
	
	//checks all the fields for errors	
	if(errorFlag) {
		document.getElementById("errorMessageDiv").style.display = "block";
		document.getElementById("rightImageDiv").style.padding= "38px 0 0 0";
		return false;  //this means there was an error, so return false to halt processing
	}  
	else {
		document.getElementById("errorMessageDiv").style.display = "none";
		document.getElementById("rightImageDiv").style.padding= "0";
		return true;
	}	 
}

function checkCaptchaCode(text)
{
	var returnCode = getFile("/includes/tellafriend/inc/check-captcha.php?capcheck=" + text);
	return returnCode;
}

function reloadCaptcha()
{
	document.getElementById("sc_captcha").src = "/includes/tellafriend/inc/captchasecurityimages.php";
}

function getFile(url) 
{
	if (window.XMLHttpRequest) {
		AJAX=new XMLHttpRequest();
	} else {
		AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (AJAX) {
		AJAX.open("GET", url, false);
		AJAX.send(null);
		return AJAX.responseText;
	} else {
		return false;  
	}
}

function checkMail(email)
{		
	var emailFlag = true;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) emailFlag = false;
	
	return emailFlag;
}

function checkName(name)
{	
	//@"^[-.a-zA-Z''-'\s]{1,40}$";
	var nameFlag = true;
	var filter  = /^[-.a-zA-Z-\s]{1,40}$/;
	if (!filter.test(name)) nameFlag = false;
	
	return nameFlag;
}