if ( !window.HSN ) { window.HSN = {}; }

/*
 * set up the dom feedback form
 */
HSN.setUpFeedback = function(){
	var feedback = document.getElementById('feedback');
	var feedbackLink = document.getElementById('feedbackLink');
	var closeButton = document.getElementById('closeButton');

	// close icon
	var closeLink = document.createElement('a');
	closeLink.setAttribute('id', 'closeIcon');
	closeLink.setAttribute('href', '#');
	var closeIcon = document.createElement('img');
	closeIcon.setAttribute('src', 'cmn/img/feedback/icn_close.png');
	closeIcon.setAttribute('id', 'closeIcon');
	closeIcon.setAttribute('alt', 'close');
	closeLink.appendChild(closeIcon);
	feedback.appendChild(closeLink);
	YAHOO.util.Event.addListener(closeLink, 'click', function() { feedback.style.visibility = 'hidden'; });

	if (feedback) {
		YAHOO.util.Event.addListener(feedbackLink, 'click', function() { feedback.style.visibility = 'visible'; });
	}
	if (closeButton) {
		YAHOO.util.Event.addListener(closeButton, 'click', function() { feedback.style.visibility = 'hidden'; });
	}

	HSN.feedback = new HSN.FeedbackForm();
}

HSN.FeedbackForm = function(e) {
	this.request = {};

	// set up ajax request
	try{
		this.request = new XMLHttpRequest(); // Opera 8.0+, Firefox, Safari
	} catch (e){
		try{
			this.request = new ActiveXObject("Msxml2.XMLHTTP"); // IE
		} catch (e) {
			try{
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				return false;
			}
		}
	}

	this.getElements();
	this.initButtons();
}
HSN.FeedbackForm.prototype = {
    getElements: function() {
        this.feedback = document.getElementById('feedback');
        this.form = document.getElementById('feedbackForm');
        this.nameLabel = document.getElementById('nLabel');
        this.myName = document.getElementById('name');
        this.emailLabel = document.getElementById('eLabel');
        this.emailAddress = document.getElementById('emailAddress');
        this.commentLabel = document.getElementById('cLabel');
        this.comment = document.getElementById('comment');
        this.error = document.getElementById('feedbackError');
        this.success = document.getElementById('success');
        this.cancelButton = document.getElementById('cancelFeedbackButton');
        this.submitButton = document.getElementById('submitFeedbackButton');
        this.closeButton = document.getElementById('closeButton');
    },

    show: function() {
        this.feedback.style.visiblity = 'visible';
    },

    hide: function() {
        this.feedback.style.visibility = 'hidden';
    },

    initButtons: function() {
        YAHOO.util.Event.addListener(this.cancelButton, 'click', this.cancel.bind(this));
        YAHOO.util.Event.addListener(this.submitButton, 'click', this.submit.bind(this));
        YAHOO.util.Event.addListener(this.closeButton, 'click', this.close.bind(this));
    },

    cancel: function(e) {
        this.clearError();
        this.form.reset();
        this.hide();
    },

    submit: function(e) {
        var query = this.makeNewQuery();

        this.request.open("GET", "feedback.ashx" + query, true);
        this.request.send(null);

        this.request.onreadystatechange = function() {
            this.clearError();
            if (this.request.readyState == 4) {
                if (this.request.responseText.match("Thank")) {
                    this.showSuccess();
                    this.form.reset();
                } else {
                    this.error.innerHTML = this.request.responseText; // display error message
                    this.detailError(this.request.responseText.toLowerCase()); // highlight fields that need correcting
                }
            };
        } .bind(this);

        YAHOO.util.Event.stopEvent(e);
    },

    close: function(e) {
        this.hide();
    },

    clearError: function() {
        this.nameLabel.className = '';
        this.myName.className = 'text';
        this.emailLabel.className = '';
        this.emailAddress.className = 'text';
        this.commentLabel.className = '';
        this.comment.className = '';
        this.error.innerHTML = '';
    },

    detailError: function(text) {
        if (text.match('name')) {
            this.nameLabel.className = 'formError';
            this.myName.className = 'text missingField';
        }
        if (text.match('your email address')) {
            this.emailLabel.className = 'formError';
            this.emailAddress.className = 'text missingField';
        }
        if (text.match('comment')) {
            this.commentLabel.className = 'formError';
            this.comment.className = 'missingField';
        }
    },

    /* concat data to be sent via ajax 
    */
    makeNewQuery: function() {
        var myname = escape(document.getElementById('name').value);
        var emailAddress = escape(document.getElementById('emailAddress').value);
        var comment = escape(document.getElementById('comment').value);

        var query = '?name=' + myname + '&email=' + emailAddress + '&comment=' + comment;
        return query;
    },

    showSuccess: function() {
        this.form.style.display = 'none';
        this.success.style.display = 'block';
    }
}
