Header.prototype.constructor = Header;

function Header(){
}

Header.getPageNewsletterSubmit = function(lang){

    var httpRequest = Utils.getXMLHttpRequest();
    httpRequest.open('GET', Config.appConfig.urlBase + lang + '/newsletter?returnFormat=json', true);
    httpRequest.send(null);

    httpRequest.onreadystatechange = function(){
        Header.constructNewsletterSubmit(httpRequest, lang);
    };
}

Header.constructNewsletterSubmit = function(httpRequest, lang){
    if (httpRequest.readyState == 4) {

        if (httpRequest.status == 200) {
            result = JSON.parse(httpRequest.responseText);

            var form = document.createElement("form");
            form.id = "newsletterSubmit";
            form.onsubmit = function() {
            					Header.validateNewsletterSubmit(result.translations.extraWarning);
            					return false;
            				};
            form.method = "post";
            form.action = Config.appConfig.urlBase + '?returnFormat=json';

            var submitedAction = document.createElement("input");
            submitedAction.type = "hidden";
            submitedAction.name = "__submited_action";
            submitedAction.id = "__submited_action";
            submitedAction.value = "Entity:SubscribeNewsletter";
            form.appendChild(submitedAction);

            var newsletterSubmitText = document.createElement("div");
            newsletterSubmitText.innerHTML = result.content;
            newsletterSubmitText.id = "popupIntroText";
            form.appendChild(newsletterSubmitText);

            var formInputs = {"subscriberName": "text","subscriberEmail": "text"};

            for (var i in formInputs) {

                var inputText = document.createElement("div");
				inputText.className="labelDiv";
                inputText.appendChild(document.createTextNode('* ' + result.translations.NewsletterSubmit[i] + ': '));
                var input = document.createElement("input");
                input.type = formInputs[i];
                input.name = i;
                input.id = i;
                var iContainer = document.createElement('div');
                iContainer.className = 'inputText';
                iContainer.appendChild(input);
                var tmpp = document.createElement('div');
                tmpp.className = 'dinput';
                tmpp.appendChild(inputText);
                tmpp.appendChild(iContainer);
                form.appendChild(tmpp);
            }

            var mandatoryText = document.createElement("div");
            mandatoryText.setAttribute("id", "mandatoryText");

			var mandatoryTextField = document.createElement("span");
            mandatoryTextField.appendChild(document.createTextNode(Utils.htmlEntitiesDecode(result.translations.mandatoryFields)));


            var submitBt = document.createElement("input");
            submitBt.type = "image";
            submitBt.src = "images/" + lang + "/bt_send.gif";

            mandatoryText.appendChild(submitBt);
			mandatoryText.appendChild(mandatoryTextField);
            form.appendChild(mandatoryText);

			var tmpDIV = document.createElement('div');
            var imgTitle = document.createElement('img');
            imgTitle.src = Config.appConfig.urlContents + result.pageTitleImage;
            imgTitle.title = result.pageTitle;
            tmpDIV.appendChild(imgTitle);
            var tmpHR = document.createElement('hr');
            var tmpDIV2 = document.createElement('div');
            tmpDIV2.innerHTML = '<hr/>';
            tmpDIV2.className = 'hr';
            tmpDIV.appendChild(tmpDIV2);
            var tmpDIV3 = document.createElement('div');
            tmpDIV3.id = 'newsLetterContent';
            tmpDIV3.className = 'contentBlock';
            tmpDIV3.appendChild(form);
            tmpDIV.appendChild(tmpDIV3);
            Utils.popup(tmpDIV);
        }
    }
}

var inputNewsletterSubmitColor = '#60797D';
var inputNewsletterSubmitErrorColor = '#D6400B';

Header.validateNewsletterSubmit = function(warning){

    var nameField = document.getElementById('subscriberName');
    var emailField = document.getElementById('subscriberEmail');
    var introText = document.getElementById('popupIntroText');
    var submitedActionField = document.getElementById('__submited_action');

    var validatorMessage = document.getElementById('mandatoryText');

    var toReturn = true;

    if (Utils.trim(nameField.value) == '') {
    	introText.innerHTML = warning;
        toReturn = false;
    }

    if (!Utils.isEmailAddr(Utils.trim(emailField.value))) {
        emailField.style.color = inputNewsletterSubmitErrorColor;
        introText.innerHTML = warning;
        toReturn = false;
    }
    else {
        Header.validateNewsletterSubmitResetColor(emailField);
    }

    if (toReturn == false) {
        validatorMessage.style.fontWeight = 'bold';
    }
    else {
        Utils.submitAJAXForm('newsletterSubmit', Header.NewsletterSubmit);
    }
    return false;
}

Header.NewsletterSubmit = function(){
    if (this.readyState == 4) {
        if (this.status == 200) {
            var result = JSON.parse(this.responseText);

            var newsletterContent = document.getElementById('newsLetterContent');
            newsletterContent.innerHTML = "";

            var ErrorDiv = document.createElement('div');
            ErrorDiv.style.marginTop = '25px';
            ErrorDiv.style.marginBottom = '25px';

            var xclamationIcon = document.createElement("div");
            xclamationIcon.className = result.errorCode == 1?'done':'failed';
            ErrorDiv.appendChild(xclamationIcon);

            var messageError = document.createElement("div");
            messageError.className = 'xclamationText';
            messageError.innerHTML = result.message;
            ErrorDiv.appendChild(messageError);

            newsletterContent.appendChild(ErrorDiv);

            document.getElementById('popcontainer_content').style.minHeight = '0';
        }
    }
}

Header.validateNewsletterSubmitResetColor = function(element){
    element.style.color = inputNewsletterSubmitColor;
}

