/*
 * Code based on ZmLogin.js
 * Methods used to finish second phase of login
 */

var lastServer = "ls_last_server";

ZM_APP_URI  = location.port == "80" ? "/mail" : ":" + location.port + "/mail";

getMailUrl = function (mailServer) {
        var ms = mailServer || location.hostname;
        return (location.protocol + "//" + ms + ZM_APP_URI + window.location.search);
    };

getAuthUrl = function (mailServer) {
    var ms = mailServer? mailServer: location.hostname;
    return (location.protocol + "//" + ms + ((location.port == 80) ? "" : ":" + location.port) +"/auth/" + window.location.search);
};

getAuthUrlHTML = function (mailServer) {
    var ms = mailServer? mailServer: location.hostname;
    return (location.protocol + "//" + ms + ((location.port == 80) ? "" : ":" + location.port) +"/auth/?client=standard");
};

postAuthToServer = function(mailServer, authToken, tokenLifetime, pubComputer, app) {
    var form = document.createElement('form');
    document.body.appendChild(form);

    var html = new Array();
    var i = 0;

    html[i++] = "<input type='hidden' name='authToken' value='" + authToken + "'>";

    if (pubComputer)
        html[i++] = "<input type='hidden' name='publicComputer' value='" + pubComputer + "'>";

    if (tokenLifetime)
        html[i++] = "<input type='hidden' name='atl' value='" + tokenLifetime + "'>";

    // Do not use app, not working
    //if (app)
    //    html[i++] = "<input type='hidden' name='app' value='" + app + "'>";

    form.innerHTML = html.join('');
    form.action = getAuthUrl(mailServer);
    form.method = "post";
    form.submit();
};

postAuthToServerHTML = function(mailServer, authToken, postLoginUrl) {
	tokenLifetime = getCookie("lifeTime");
	var form = document.createElement('form');
	document.body.appendChild(form);
	var html = new Array();
	var i = 0;
	html[i++] = "<input type='hidden' name='authToken' value='" + authToken + "'>";
	html[i++] = "<input type='hidden' name='atl' value='" + tokenLifetime + "'>";
	html[i++] = "<input type='hidden' name='client' value='standard'>";
	form.innerHTML = html.join('');
	form.action = getAuthUrlHTML(mailServer);
	form.method = "post";
	form.submit(); 
};

oromailHandleSuccess = function(authToken, tokenLifetime, mailServer, app) {
    postAuthToServer(mailServer, authToken, tokenLifetime, "", app);
};

oromailHandleHTMLSuccess = function(authToken, mailServer, postLoginUrl) {
    postAuthToServerHTML(mailServer, authToken, postLoginUrl);
};

/*
 * Methods to init Oromail
 */

function showOromailInitError() {
	document.location = "http://oromail.orolix.com.br/home/error";
}

function initHome() {
    if(getCookie(DONT_CHECK_AJAX) == "1") {
        initAjax();
        return true;
    } else {
        confirmOromailAjax();
        return false;
    }
}

function initAjax() {
    if(getCookie("OROMAIL_WEBSERVER") != null) {
        oromailHandleSuccess(getCookie("OROMAIL_AUTH_TOKEN"), getCookie("lifeTime"), getCookie("OROMAIL_WEBSERVER"), 'a');
    } else {
        showOromailInitError();
    }   
}

function initHTMLComponent(postLoginUrl) {
    if(getCookie("OROMAIL_WEBSERVER") != null) {
        oromailHandleHTMLSuccess(getCookie("OROMAIL_AUTH_TOKEN"), getCookie("OROMAIL_WEBSERVER"), postLoginUrl);
    } else {
        showOromailInitError();
    }
}

function initHTML() {
    initHTMLComponent();
}

function initHTMLContacts() {
    initHTMLComponent("/h/search?st=contact");
}

function initHTMLCompose() {
    initHTMLComponent("/h/search?action=compose");
}

function initHTMLOptions() {
    initHTMLComponent("/h/options");
}

/*
 * Methods for checking and confirming low resolution with Oromail Ajax
 */

var DONT_CHECK_AJAX = "dontCheckAjax";

function confirmOromailAjax() {
    // Show shadow background
    divShadow = document.getElementById('shadow');
    divShadow.style.display = 'block';
    
    // Show dialox box
    divConfirm = document.getElementById('confirmBox');
    divConfirm.style.display = 'block';
    cHeight =  divConfirm.style.height != "" ? parseInt(divConfirm.style.height) : 300;
    divConfirm.style.top = getScrollY() +(getWindowHeight() - 300)/2 + "px";
}

function callbackfunction(data) {
}

function setDontAsk(url, callback) {
    dontAsk = document.getElementById('dontAsk');
    if(dontAsk.checked) {
        var myAjax = new Ajax.Request(url, {
            method: "get",
            parameters: 'dca=1',
            onComplete: callback
        });
    }
}

function closeConfirm() {
    // Hide shadow background
    divShadow = document.getElementById('shadow');
    divShadow.style.display = 'none';
    
    // Show dialox box
    divConfirm = document.getElementById('confirmBox');
    divConfirm.style.display = 'none'
    
    setDontAsk("http://oromail.orolix.com.br/home/", callbackfunction);
}
