// This JS library handles send to a friend function for JavaScript enables browsers
// function to handle send email to a friend link click
function _sendToAFriendGetForm(sDivName, sURL, sHashCode, nNewsID, sCommentsDiv){11
	$.get(sURL, { nNewsID: nNewsID },
	function(data){
		$("#"+sDivName).html(data);
	});
			
	// stop redirect to URL
	return false;
}

// handle send to a friend form submit
function _sendToAFriendSendMail(sDivName, sURL, sCommentsDiv, sHashCode){
	if(_sendToAFriendValidate()){
		pars						= 'sUserName=' + $("#sUserName").val() + '&sUserEmail=' + $("#sUserEmail").val() + '&sFriendName=' + $("#sFriendName").val() + '&sFriendEmail=' + $("#sFriendEmail").val() + '&sHashCode=' + sHashCode + '&nID=' + $("#nID").val();
		$.get(sURL, pars, function(data){
		    $("#"+sDivName).html(data);
		});
	}
}

function _sendToAFriendValidate() {
	var bValid = true;
	$(document).ready(function() {
		// validate sent to a friend form on keyup and submit
		$("#frmSendToAFriend").validate({
			onfocusout: 	false,
			onkeyup:		false,
			onclick:		false,
			
			// error messages will be displayed as paragraphs with class "error"
			errorElement: "p",
			
			// place the error labels within .formRow but before .frmElements
			errorPlacement: function(error, element) 
			{
				error.insertBefore(element.prev());
			},
			
			rules: {
				sUserName: "required",
				sUserEmail: {
					required: 		true,
					email: 			true
				},
				sFriendName: "required",
				sFriendEmail: {
					required: 		true,
					email: 			true
				},
				recaptcha_response_field:{
					recaptchaCheck:	$('#recaptcha_challenge_field')
				}
			},
			messages: {
				sUserName: "Please enter your name",
				sUserEmail: "Please enter a valid email address",
				sFriendName: "Please enter your friend's name",
				sFriendEmail: "Please enter a valid friend's email address",
				recaptcha_response_field:{recaptchaCheck: 	jQuery.format("Invalid captcha. Please try again")
			}
			}
		});
		
		bValid = $("#frmSendToAFriend").valid();
	});
	
	return bValid;
};

//this is the validate method which checks the Captcha
//this does an Ajax request which takes the two fields, performs the check, 
//then returns a 1 if it passed the check and a 0 if it fails
//also if it does fail the captcha refreshes with a new pair of words 

jQuery.validator.addMethod('recaptchaCheck', function(value) {
	var sURL 							= $('#sSendToAFriendURL').val();
	var thedata = "";

	$.ajax({
		async: false,
		url: sURL+'index.cfm?event=page.article.sendtoafriend.ajax',
		dataType: 'json',
		data: {
				recaptcha_challenge_field: 	$('#recaptcha_challenge_field').val(),
				recaptcha_response_field: 	$('#recaptcha_response_field').val() 
				},
		success: function(data) {
			thedata = data;
		}
	});

	if(thedata.success == 1){
		// reload captcha, so that if other fields fail, we have a new captcha to check on!
		Recaptcha.reload(); 
		return true;
	}else if(thedata.success == 0){
		Recaptcha.reload();
		return false;
	}
}, '' );
