var cw_tellafriend = {
	init: function(){
		if ( jQuery('#cw_tellafriend_js') )
			jQuery('#cw_tellafriend_js').show();
		if ( jQuery('#cw_tellafriend_nojs') )
			jQuery('#cw_tellafriend_nojs').hide();
		if ( jQuery('#cw_addanother') )
			jQuery('#cw_addanother').click(function(){ cw_tellafriend.addAnother(); return false; });
		if ( jQuery('#cw_tellafriendform') )
			jQuery('#cw_tellafriendform').submit(function(){ cw_tellafriend.send() });
		if ( jQuery('#cw_tellafriendclear') )
			jQuery('#cw_tellafriendclear').click(function(){ cw_tellafriend.clear() });	
	},
	
	addAnother: function(){
		jQuery('#cw_tellafriend_js a').remove();
		var content = '\
			<div>\
			<input type="text" name="cw_friendemail" class="cw_breakinputs" \/>\
			<span class="cw_error"><\/span>\
			<a id="cw_addanother" href="#" style="display:block">Add Another<\/a>\
			<\/div>\
		';
		jQuery('#cw_tellafriend_js').append(content);
		jQuery('#cw_addanother').click(function(){ cw_tellafriend.addAnother(); return false; });
	},
	
	send: function(){
		params = {
			useremail : jQuery('input[name=cw_useremail]').val(),
			friendemail : cw_app.collectInputs('cw_friendemail'),
			subject : jQuery('input[name=cw_subject]').val(),
			message : jQuery('textarea[name=cw_message]').val(),
			code : jQuery('input[name=cw_tellafriendcode]').val(),
			encryption : jQuery('input[name=cw_tellafriendcrypt]').val(),
			sendcopy : jQuery('input[name=cw_sendcopy]:checked').val()||'',
			lettergroupid : jQuery('input[name=lettergroupid]').val()||''
		};
		
		if ( cw_tellafriend.validate(params) ) {
			jQuery.post( cw_rootPath + '\
				/index.cfm?siteid=' + cw_siteid + '&action=TellAFriend.Send&requesttype=update&js=true',
				params,
				function(response){
					cw_tellafriend.sendresponse(response);	
				},
				'json');
		}
		else
			$('#cw_tellafriendsend').attr("disabled", false);
	},
	
	sendresponse: function(response){
		if (response.CONTENT) {
			var content = '\
				Your message was sent. Click \
				<a href="#" id="cw_tellafriendshow">here<\/a> \
				if you would like to send another.';
			$('#cw_tellafriendsend').attr("disabled", false);
		}
		else {
			var content = '\
				There was an error while sending your message. Please click \
				<a href="#" id="cw_tellafriendshow">here<\/a> \
				to try again.';
		} 
		jQuery('#cw_tellafriendresponse').html(content);
		jQuery('#cw_tellafriendform').hide();
		jQuery('#cw_tellafriendresponse').show();
		jQuery('#cw_tellafriendshow').click(function(){ cw_tellafriend.showform(); return false });
	},
	
	showform: function(){
		cw_tellafriend.clear();
		jQuery('#cw_tellafriendform').show();
		jQuery('#cw_tellafriendresponse').hide();
	},
	
	validate: function(params){
		var valid = true;
		var tempArray = new Array();
		
		jQuery('#cw_useremail_error').html('');
		jQuery('#cw_friendemail_error').html('');
		jQuery('#cw_subject_error').html('');
		jQuery('#cw_message_error').html('');
		jQuery('#cw_tellafriendcode_error').html('');
		
		if (!params.useremail.length){
			valid = false;
			jQuery('#cw_useremail_error').html('Email is required');
		}
		else if (!cw_validation.isEmail(params.useremail)) {
			valid = false;
			jQuery('#cw_useremail_error').html('Invalid email');
		}
		
		if (!params.friendemail.length){
			valid = false;
			jQuery('#cw_friendemail_error').html('Friend email is required');
		}
		else {
			jQuery('input[name=cw_friendemail]').each(function(){
				if ($(this).val().length && !cw_validation.isEmail($(this).val())){
					$(this).parent().children('.cw_error').html('Invalid address');
					valid = false;
				}
				else
					$(this).parent().children('.cw_error').html('');
			});
		}
		
		if (!params.subject.length){
			valid = false;
			jQuery('#cw_subject_error').html('<br />Subject is required');
		}
		if (!params.message.length){
			valid = false;
			jQuery('#cw_message_error').html('<br />Message is required');
		}
		if (!params.code.length){
			valid = false;
			jQuery('#cw_tellafriendcode_error').html('Code is required');
		}
		
		if (valid){
			jQuery.ajaxSetup({
				'async': false
			});
			
			jQuery.getJSON(cw_rootPath + '\
				/index.cfm?siteid=' + cw_siteid + '&action=TellAFriend.CheckCode&requesttype=update',
				params,
				function(response){
					if (!response.CONTENT) {
						valid = false;
						jQuery('#cw_tellafriendcode_error').html('Code is invalid');
					}	
				});
		}
		
		return valid;	
	},
	
	clear: function(){
		var content = '\
			<input type="text" name="cw_friendemail" class="cw_breakinputs" \/>\
			<span class="cw_error" id="cw_friendemail_error"><\/span>\
			<a id="cw_addanother" href="#" style="display:block">Add Another<\/a>\
		';
		jQuery('#cw_tellafriend_js').html(content);
		jQuery('#cw_addanother').click(function(){ cw_tellafriend.addAnother(); return false; });
		jQuery('input[name=cw_useremail]').val('');
		jQuery('input[name=cw_friendemail]').val('');
		jQuery('input[name=cw_subject]').val('');
		jQuery('textarea[name=cw_message]').val('');
		jQuery('input[name=cw_tellafriendcode]').val('');
	}
}

