/**
 * @author admin
 */

inviteForm = {

    formVals 			: null,
    responseMsg			: null,
    formId				: null,
    formBox				: null,
    errorBox			: null,

    senderNameField		: null,
    senderEmailField	: null,
    recipientNameField	: null,
    recipientEmailField : null,
    noteField			: null,
    submitButton		: null,
    
    formUrl				: '/message/form/process-tell-friend',

    process : function(e)
    {
    	this.formId 			 = dijit.byId('tellFriendForm');
//    	this.errorBox 			 = dojo.byId('message-box');
        this.senderNameField 	 = dijit.byId('senderName');
        this.senderEmailField 	 = dijit.byId('senderEmail');
        this.recipientNameField  = dijit.byId('recipientName');
        this.recipientEmailField = dijit.byId('recipientEmail');
        this.noteField			 = dijit.byId('note');

        if (!this.senderNameField.isValid()) {
            this.senderNameField.focus();
            return false;
        }
        if (!this.recipientNameField.isValid()) {
			this.recipientNameField.focus();
			return false;
		}
        if (!this.senderEmailField.isValid()) {
            this.senderEmailField.focus();
			return false;
		}
		if (!this.recipientEmailField.isValid()) {
			this.recipientEmailField.focus();
			return false;
		}
		
		if (this.formId.isValid()) { 
			
			// Hide form and display confirmation message. 
			// We may want to do this in the result handler after we know there's no errors
			var formEle = dojo.byId('tellFriendForm');
			var confirmEle = this.confirmBox(this.formId.attr('value'));
			var tellFriendEle = dojo.byId('tell-friend-form-box');
			
			dojo.style(confirmEle, 'opacity', '0'); // hide it 
			var animHideForm = dojo.fadeOut({ node: formEle, duration: 500}).play();
			dojo.connect(animHideForm, 'onEnd', function() {
				dojo.addClass(formEle, 'hide');
				dojo.removeClass(confirmEle, 'hide');
				dojo.fadeIn({ node: confirmEle, duration: 500}).play();
			});
			
			// Cache sender name and email if they resend
			var senderName = this.senderNameField.attr('value');
			var senderEmail = this.senderEmailField.attr('value');
			
			// Connect 'Yes | No' links
			var confirmLinks = dojo.query('#confirm-box a').connect('click', function(evt) {

				dojo.stopEvent(evt);
				evtId = evt.target.id;
				
				if (evtId == 'yes-email') { // show form and hide confirm
					
					dojo.addClass(confirmEle, 'hide');
					dojo.style(formEle, 'opacity', '0');
					dojo.removeClass(formEle, 'hide');
					var animShowForm = dojo.fadeIn({ node: formEle, duration: 500}).play();
					
					// Populate fields
					dijit.byId('senderName').attr('value', senderName);
					dijit.byId('senderEmail').attr('value', senderEmail);
					
				} else if (evtId == 'no-email') { // fade out tellFriendEle box then onEnd set default properties
					
					var animHideTellFriend = dojo.fadeOut({ node: tellFriendEle, duration: 500}).play();
					dojo.connect(animHideTellFriend, 'onEnd', function() {
						dojo.style(formEle, 'opacity', '1');
						dojo.removeClass(formEle, 'hide');
						dojo.addClass(confirmEle, 'hide');
						
						var tellFriendBoxId = dojo.byId('tell-friend-form-box'); // This is used in the message page -- A little messy... 
						if (tellFriendBoxId) {
							dojo.addClass(tellFriendBoxId, 'hide');
						}
					});
				}
			});
			
//			alert("Ready to submit data: " + dojo.toJson(this.formId.attr("value")));
//			this.submitButton.disabled = true;
			var subComments = this.noteField.attr('value').substring(0, 300);
		
			dojo.xhrPost({
				form: 'tellFriendForm',
				url: this.formUrl,
				handleAs: 'json-comment-optional',
				handle: this.resultHandler,
				content: { note: subComments}
			});
			
			// Reset
			dijit.byId('tellFriendForm').reset();
			
		} else {
			return false;
//			dojo.style(this.errorBox, {display: 'block'});
//			dojo.fadeIn({ node: this.errorBox, duration:500 }).play();
		}
		
		return false;
	},
	
	// Leave this here for reference, but not using presently
	resultHandler : function(result, ioArgs)
	{
		var error 		 = result instanceof Error;
		var responseText = error ? result.message : result.response;
		var validResult  = result.valid == '1' ? true : false;
		var confirmBox   = dojo.byId('confirm-box');
		
		var resultDiv = dojo.create('div', {innerHTML: responseText, style: "padding: 10px 0;"});
		var n = dojo.place(resultDiv, confirmBox, 'next');
		return;

		// Some code from another project, shows how to handle an array of errors
		if (result.errors) {
			var errors = eval(result.errors);
			var errorStr = '<br />';
			
			dojo.forEach(errors, function(err) {
				errorStr += "<div class='error_item'><b>" + err.field + "</b>: " + err.error + "</div>";
			});
			responseText += errorStr;
		}
		
		cform.responseMsg.innerHTML = responseText;
		
		// If login is good, remove form
		if (validResult) {
			cform.formBox.innerHTML = '';
		} else {
//			cform.submitButton.value = 'Submit';
			this.submitButton.disabled = false;
		}
		
		return false;
	},
	
	confirmBox : function(formValues) 
	{
		var confirmBox = dojo.byId('confirm-box');
		var confirmHTML = "An invitation has been sent to: " + formValues.recipientName + " &lt;" + formValues.recipientEmail + "&gt;"
						+ "<br />Would you like to send another invitation?<br />"
						+ "<a href='#' id='yes-email'>Sure</a>|<a href='#' id='no-email'>No Thanks</a>";
		var formId = dojo.byId('tellFriendForm');
		confirmBox.innerHTML = confirmHTML;
		return confirmBox;
	}

};

function processInvite(evt)
{
//	evt.preventDefault();
	dojo.stopEvent(evt);
	var fo = inviteForm;
	fo.process(evt);
	return false;
}


