Footer.prototype.constructor = Footer;

function Footer(){
}

Footer.getPageTermsConditions = function(lang){

    var httpRequest = Utils.getXMLHttpRequest();
    httpRequest.open('GET', lang + '/termos-e-condicoes?returnFormat=json', true);
    httpRequest.send(null);

    httpRequest.onreadystatechange = function(){
        Footer.constructPageTermsConditions(httpRequest);
    };
}

Footer.constructPageTermsConditions = function(httpRequest){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {

            result = JSON.parse(httpRequest.responseText);

            var tmpDIV = document.createElement('div');
            tmpDIV.style.width = '100%';
            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 divContent = document.createElement('div');
			divContent.appendChild(tmpDIV);
			var text = document.createElement('div');
			text.className = 'contentBlock';
            text.innerHTML = result.content;
			divContent.appendChild(text);

            Utils.popup(divContent);
        }
    }
}

Footer.getPageInformationRequest = function(lang){

    var httpRequest = Utils.getXMLHttpRequest();
    httpRequest.open('GET', lang + '/pedido-de-informacao?returnFormat=json', true);
    httpRequest.send(null);

    httpRequest.onreadystatechange = function(){
        Footer.constructPageInformationRequest(httpRequest, lang);
    };
}

Footer.constructPageInformationRequest = function(httpRequest, lang){
    if (httpRequest.readyState == 4) {

        if (httpRequest.status == 200) {
            result = JSON.parse(httpRequest.responseText);

            var form = document.createElement("form");
            form.id = "informationRequest"
            form.onsubmit = function (){
            					Footer.validateInformationRequest(result.translations.InfoRequest.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 = "Contact:Submit";
            form.appendChild(submitedAction);

            var informationRequestText = document.createElement("div");
            informationRequestText.innerHTML = result.content;
            informationRequestText.id = "popupIntroText";
            form.appendChild(informationRequestText);

            var formInputs = {
                "InfoRequestName": "text",
                "InfoRequestEmail": "text",
                "InfoRequestSubject": "text",
                "InfoRequestMessage": "textarea"
            };
            /*
             for (var ttt in result.translations){
             alert(ttt + "=" +result.translations[ttt]);
             }
             */
            var inClass = '';
            for (var i in formInputs) {
                var inputText = document.createElement("div");
				inputText.className="labelDiv";
                inputText.appendChild(document.createTextNode(result.translations.InfoRequest[i]));
                if (formInputs[i] == "textarea") {
                    var input = document.createElement("TEXTAREA");
                    inClass = 'textArea';
                }
                else {
                    var input = document.createElement("input");
                    input.type = formInputs[i];
                    inClass = 'inputText';
                }
                input.name = i;
                input.id = i;
                var iContainer = document.createElement('div');
                iContainer.className = inClass;
                iContainer.appendChild(input);
                var tmpP = document.createElement('div');
                tmpP.className = 'dinput';
                tmpP.appendChild(inputText);
                tmpP.appendChild(iContainer);
                form.appendChild(tmpP);
                //form.appendChild(input);
            }

            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 tmpDIV2 = document.createElement('div');
            tmpDIV2.innerHTML = '<hr/>';
            tmpDIV2.className = 'hr';
            tmpDIV.appendChild(tmpDIV2);
            var tmpDIV3 = document.createElement('div');
            tmpDIV3.id = 'pagerequestContent';
            tmpDIV3.appendChild(form);
            tmpDIV3.className = 'contentBlock';
            tmpDIV.appendChild(tmpDIV3);

            Utils.popup(tmpDIV);
        }
    }
}

var originalColor = '#FFFFFF';
var colorToPaint = '#eecccc';

Footer.validateInformationRequest = function(warning){

    var nameField = document.getElementById('InfoRequestName');
    var emailField = document.getElementById('InfoRequestEmail');
    var subjectField = document.getElementById('InfoRequestSubject');
    var messageField = document.getElementById('InfoRequestMessage');
    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;
       // nameField.style.background = colorToPaint;
        toReturn = false;
    }

    if (!Utils.isEmailAddr(Utils.trim(emailField.value))) {
    	introText.innerHTML = warning;
        //emailField.style.background = colorToPaint;
        toReturn = false;
    }

    if (Utils.trim(subjectField.value) == '') {
    	introText.innerHTML = warning;
        //subjectField.style.background = colorToPaint;
        toReturn = false;
    }

    if (Utils.trim(messageField.value) == '') {
    	introText.innerHTML = warning;
        //messageField.style.background = colorToPaint;
        toReturn = false;
    }

    if (toReturn == false) {
        validatorMessage.style.fontWeight = 'bold';
    }
    else {
        Utils.submitAJAXForm('informationRequest', Footer.submitInformationRequest);
    }
    return false;
}

Footer.submitInformationRequest = function(){

    if (this.readyState == 4) {
        if (this.status == 200) {
            result = JSON.parse(this.responseText);

            var InformationRequestContent = document.getElementById('pagerequestContent');
            InformationRequestContent.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);

            InformationRequestContent.appendChild(ErrorDiv);

            document.getElementById('popcontainer_content').style.minHeight = '0';
        }
    }
}

Footer.validateInformationRequestResetColor = function(element){
    element.style.background = originalColor;
}
