// JavaScript Document

function validate_send_to_friend(theForm)
{
	var errors = ''; // error messages to alert the user with
	var args = ''; // arguments to send the email
	
	if(theForm.friends_name.value == "")
	{
		errors += "Please enter your friend's name.\n";	
	}
	if(!send_to_friend_valid_email(theForm.friends_email.value))
	{
		errors += "Please enter your friend's email address.\n";	
	}
	if(theForm.your_name.value == "")
	{
		errors += "Please enter your name.\n";	
	}
	if(!send_to_friend_valid_email(theForm.your_email.value))
	{
		errors += "Please enter your email address.\n";	
	}
	
	if(errors != "")
	{
		alert(errors);
	}
	else // build the query string and reload the div
	{
		args += "&email_subject="+theForm.email_subject.value;
		args += "&friends_name="+theForm.friends_name.value;
		args += "&friends_email="+theForm.friends_email.value;
		args += "&message="+theForm.message.value;
		args += "&your_name="+theForm.your_name.value;
		args += "&your_email="+theForm.your_email.value;
		args += "&opt_in="+theForm.opt_in.checked;
		//alert(theForm.opt_in.checked);
		args += "&pf_id="+theForm.pf_id.value;
		args += "&dept_id="+theForm.dept_id.value;
		args += "&product_name="+theForm.product_name.value;
		args += "&product_price="+theForm.product_price.value;
		
		callAHAH('/ajax/send_to_friend.asp?validate=yes'+args, 'product_page_send_to_friend', '<br><br><br><img src=/assets/Animations/ajax-loader.gif><br>Sending...<br><br><br>');
		
		//alert(args); // debug only
	}
	
	// we never actually submit the form
	return false;
}

function send_to_friend_valid_email(email) {
	email = trim(email);
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(email);
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
