// $Id: global_login.js 480 2011-04-18 17:40:09Z epinter $

//globalvars
var isIE=0;
var isIE6=0;
var isFF=0;
var isNN=0;
var isOP=0;
var g_form;
var vg_username;
var vg_password;
var apporig;

if(navigator.appName.toLowerCase().indexOf("firefox")!=-1 || navigator.appName.toLowerCase().indexOf("iceweasel")!=-1) {
        isFF=1;
}

if(navigator.appName.toLowerCase().indexOf("microsoft internet explorer")!=-1) {
        isIE=1;
        if(navigator.appVersion.toLowerCase().indexOf("msie 6.")!=-1) isIE6=1;
} else if(navigator.appName.toLowerCase().indexOf("netscape")!=-1 && !document.all && document.getElementById) {
        isNN=1;
} else if(navigator.appName.toLowerCase().indexOf("opera")!=-1) {
        if(parseFloat(navigator.appVersion) >= 9.10)
                isOP=1;
}

/*
if(! (isIE || isIE6) && ! isNN && ! isOP) {
        alert('Navegador Incompativel!!! n:'+navigator.appName.toLowerCase()+' v: '+navigator.appVersion.toLowerCase());
        window.location.href='http://www.google.com/';
}
*/
var url_proto='http:';
if(isHTTPS()) url_proto='https:';


var url_webmail='//webmail.picture.psi.br'; 
var url_webmail_light='//www.picturemail.com.br/webmail/src/redirect.php';
var url_xchange='//xchange.picture.com.br/gw/login.php';
var script_msexchange='/login/msexchangelogin.js';
var url_msex2010="//owa.exch.picturemail.com.br/owa/auth.owa";
var url_msex2010_destination="//owa.exch.picturemail.com.br/owa/";
var url_offer_msexchange="/servicos/microsoft-exchange.php";
var url_offer_xchange="/servicos/picturemail-xchange.php";
var url_offer_im="/servicos/picture-instant-messenger.php";
var url_im='//im.picture.com.br/templates/container.html';
var alerta_popup="Seu navegador bloqueou o pop-up! Para o pleno funcionamento do site, desative ou libere o bloqueador !";

var popup_w;
var popup_h;



function isHTTPS()
{
   return location.protocol == 'https:';
}

function toggle_loading( disable ) {
	if(disable==true && document.getElementById('loading')){ 
		document.getElementById('loading').style.display='none';
		return true;
	}

	if(document.getElementById('loading')) {
		if(document.getElementById('loading').style.display!="block") {
			document.getElementById('loading').style.display='block';
		} else document.getElementById('loading').style.display='none';
	}
	
}

function popup_test() {
	var popuptest=window.open("about:blank","picturemaillogin_popuptest_i","resizable=no,location=no,menubar=no,toolbar=no,top=0,width=1,height=1",true);
	if ((popuptest==null || typeof(popuptest)=="undefined") && document.getElementById('alerta_popup')) {
		document.getElementById('alerta_popup').innerHTML=alerta_popup;
		return;
	} else {
		if(window.opera && ! popuptest.opera) {
			document.getElementById('alerta_popup').innerHTML=alerta_popup;
			return;
		}
		if(popuptest!=null) popuptest.close();
	}
}

function getFirstCookie(attr) {
        if(isCookiesEnabled()) {
		var ar_res_us=getcookie('picturemail_gl_'+attr);
        var retval=ar_res_us[1];
        if(unescape(ar_res_us[1])!="undefined") return unescape(ar_res_us[1])
		return(ar_res_us[1]);
	} else {
		return false;
	}
}
function getUserFromCookie() {
	return getFirstCookie("user");
}
function getPassFromCookie() {
	return getFirstCookie("pass");
}

function getAppFromCookie() {
	return getFirstCookie("app");
}
function getRemembermeFromCookie() {
	return getFirstCookie("rememberme");
}
function getSslFromCookie() {
	return getFirstCookie("sslmode");
}

function definePopupSize() {
	if(screen.width > 1400) {
		popup_w=1200
		popup_h=screen.height-(screen.height*0.3);
	} else if(screen.width >= 1024) {
		popup_w=screen.width-(screen.width*0.1);
		popup_h=screen.height-(screen.height*0.2);
	} else {
		popup_w=800;
		popup_h=540;
	}
}

function verify_info(form) {
//	if((document.getElementById('alwayssecure') && document.getElementById('alwayssecure').value=="1") || (document.getElementById('sslmode') && document.getElementById('sslmode').checked)) {
	if((form.alwayssecure && form.alwayssecure.value=="1") || (form.sslmode!="undefined" && form.sslmode.checked)) {
		url_proto='https:';
		setcookie('picturemail_gl_sslmode','1','10y');
	} else {
		setcookie('picturemail_gl_sslmode','','0');
		url_proto='http:';
	}
	if(form.g_username.value == "" || form.g_password.value =="" || form.app.value=="") {
		alert('Digite o login e senha!');
		return "";
	}
	var appchecked;
	for(i=0;i<form.app.length;i++) {
		if(form.app[i].checked)
			form.selectedapp.value=form.app[i].value;
	}
    apporig=form.selectedapp.value;
	definePopupSize();
	if(document.getElementById('donttestacc') && document.getElementById('donttestacc').value=="1") {
		runapp(form);
		return;
	}
	if(isXHRSupported()) {
		testacc(form);
	} else { 
		alert('ERRO: Navegador não suportado');
		window.open("http://pt-br.www.mozilla.com/pt-BR/firefox/","picturemaillogin_getfirefox","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
		runapp(form);
	}
	return;
}


function runapp(form) {
	var appchecked=form.selectedapp.value;
    g_form=form;
	if(appchecked=='webmail') {
		toggle_loading();
		create_app_webmail();
	} else if(appchecked=='webmailold') {
		toggle_loading();
		create_app_webmailold();
	} else if(appchecked=='im') {
		create_app_im();
	} else if(appchecked=='groupware') {
		toggle_loading();
		create_app_xchange();
	} else if(appchecked=='exchange') {
		toggle_loading();
		create_app_msexchange();
	} else if(appchecked=='exchange2010') {
		toggle_loading();
		create_app_msexchange2010();
	}
	if(isCookiesEnabled()) {
		if(form.rememberme!="undefined" && form.rememberme.checked) {
			setcookie('picturemail_gl_rememberme','1','10y');
			setcookie('picturemail_gl_user',form.g_username.value,'10y');
			setcookie('picturemail_gl_pass',form.g_password.value,'10y');
		} else {
			setcookie('picturemail_gl_rememberme','0','10y');
			setcookie('picturemail_gl_user','','0');
			setcookie('picturemail_gl_pass','','0');
			form.g_password.value='';
		}
		setcookie('picturemail_gl_app',appchecked,'10y');
	}
	return;
}

    /* utility to add a script node to the current document
       and call the callback function when it is loaded.
       Should allow scripts to be loaded at any time during document
       life..and have code be kicked off after they are loaded.

       Browsers tested:
       FF 3.6.x     - WORKS
       Chrome 5.x   - WORKS
       Safari 4.x   - WORKS
       MS IE 8      - WORKS
       Opera 10.x   - WORKS
    */
function add_script(scriptURL, onloadCB) {
    var scriptEl    = document.createElement("script");
    scriptEl.type   = "text/javascript";
    scriptEl.src    = scriptURL;

    function calltheCBcmn() {
        onloadCB(scriptURL);
    }

    if(typeof(scriptEl.addEventListener) != 'undefined') {
        /* The FF, Chrome, Safari, Opera way */
        scriptEl.addEventListener('load',calltheCBcmn,false);
    }
    else {
        /* The MS IE 8+ way (may work with others - I dunno)*/
        function handleIeState() {
            if(scriptEl.readyState == 'loaded') {
              calltheCBcmn(scriptURL);
            }
        }
        var ret = scriptEl.attachEvent('onreadystatechange',handleIeState);
    }
    document.getElementsByTagName("head")[0].appendChild(scriptEl);
}

function offer_xchange() {
	if(! g_form.rememberme.checked) g_form.g_password.value='';
	alert('Produto não contratado');
	var neww=window.open(url_proto+url_offer_xchange,"picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	if (neww==null || typeof(neww)=="undefined") location.href=url_proto+url_offer_xchange;
}

function offer_msexchange() {
	if(! g_form.rememberme.checked) g_form.g_password.value='';
	alert('Produto não contratado');
	var neww=window.open(url_proto+url_offer_msexchange,"picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	if (neww==null || typeof(neww)=="undefined") location.href=url_proto+url_offer_msexchange;
}

function offer_msexchange2010() {
    offer_msexchange();
}

function offer_im() {
	if(! g_form.rememberme.checked) g_form.g_password.value='';
	alert('Produto não contratado');
	var neww=window.open(url_proto+url_offer_im,"picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	if (neww==null || typeof(neww)=="undefined") location.href=url_proto+url_offer_im;
}

function create_html_input(type,name,value) {
	newel=document.createElement("input");
    newel.setAttribute("type",type);
    newel.setAttribute("name",name);
    newel.setAttribute("value",value);
	return newel;
}

function create_app_im() {
	//form
	var container=document.getElementById('if_container');
	var fakeurl=Math.random();
	container.src='http:'+url_im+'#us='+escape(g_form.g_username.value)+';se='+escape(g_form.g_password.value)+';fake='+fakeurl;
	setTimeout("document.getElementById('if_container').src='about:blank'",5000);
}

function create_app_xchangeold() {
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	var myform=document.createElement("form");
	in_js=create_html_input("hidden","js_autodetect_results","SMPREF_JS_OFF");
	in_toservice=create_html_input("hidden","to_service","workgroup");
	in_loginusername=create_html_input("hidden","login_username",g_form.g_username.value);
	in_secretkey=create_html_input("hidden","secretkey",g_form.g_password.value);
	myform.appendChild(in_js);
	myform.appendChild(in_toservice);
	myform.appendChild(in_loginusername);
	myform.appendChild(in_secretkey);
	document.body.appendChild(myform);
	myform.name="loginForm";
	myform.method="POST";
	myform.action=url_proto+url_xchange;
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
	} else {
		myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	myform.submit();
}

function create_app_xchange() {
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	var myform=document.createElement("form");
	in_pwtype=create_html_input("hidden","passwd_type","text");
	in_actype=create_html_input("hidden","account_type","u");
	in_logindomain=create_html_input("hidden","logindomain","");
	in_login=create_html_input("hidden","login",g_form.g_username.value);
	in_passwd=create_html_input("hidden","passwd",g_form.g_password.value);
	myform.appendChild(in_pwtype);
	myform.appendChild(in_actype);
	myform.appendChild(in_logindomain);
	myform.appendChild(in_login);
	myform.appendChild(in_passwd);
	document.body.appendChild(myform);
	myform.name="loginForm";
	myform.method="POST";
	myform.action=url_proto+url_xchange;
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
	} else {
		myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	myform.submit();
}

function create_app_webmailold() {
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	var myform=document.createElement("form");
	in_js=create_html_input("hidden","js_autodetect_results","SMPREF_JS_OFF");
	in_loginusername=create_html_input("hidden","login_username",g_form.g_username.value);
	in_secretkey=create_html_input("hidden","secretkey",g_form.g_password.value);
	myform.appendChild(in_js);
	myform.appendChild(in_loginusername);
	myform.appendChild(in_secretkey);
	document.body.appendChild(myform);
	myform.name="loginForm";
	myform.method="POST";
	myform.action=url_proto+url_webmail_light;
	myform.target="picturemaillogin_popup";
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
	} else {
		myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	myform.submit();
}

function create_app_webmail() {
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	var myform=document.createElement("form");
	in_iamnotrc=create_html_input("hidden","iamnotrc","1");
	in_user=create_html_input("hidden","_user",g_form.g_username.value);
	in_pass=create_html_input("hidden","_pass",g_form.g_password.value);
	myform.appendChild(in_user);
	myform.appendChild(in_pass);
	myform.appendChild(in_iamnotrc);
	document.body.appendChild(myform);
	myform.name="form";
	myform.method="POST";
	myform.action=url_proto+url_webmail;
	myform.target="picturemaillogin_popup";
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
	} else {
		myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	myform.submit();
}

function create_app_msexchange2010() {
	var container=document.getElementById('if_container');
	var fakeurl=Math.random();
	container.src=url_proto+url_msex2010_destination+"/auth/picturemail.html?"+fakeurl;
    vg_username=g_form.g_username.value;
    vg_password=g_form.g_password.value;
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
    try {
        if(document.getElementById("loginForm")) {
            document.body.removeChild(document.getElementById("loginForm"));
        }
    } catch(e) { }
	var myform=document.createElement("form");
	in_login=create_html_input("hidden","username",g_form.g_username.value);
	in_passwd=create_html_input("hidden","password",g_form.g_password.value);
	in_flags=create_html_input("hidden","flags","0");
	in_destination=create_html_input("hidden","destination",url_proto+url_msex2010_destination);
	in_trusted=create_html_input("hidden","trusted","0");
	in_forcedownlevel=create_html_input("hidden","forcedownlevel","0");
	in_isutf8=create_html_input("hidden","isUtf8","1");
	myform.appendChild(in_login);
	myform.appendChild(in_passwd);
	myform.appendChild(in_flags);
	myform.appendChild(in_destination);
	myform.appendChild(in_trusted);
	myform.appendChild(in_forcedownlevel);
	myform.appendChild(in_isutf8);
    myform.name="logonForm";
    myform.id="loginForm";
    myform.method="POST";
	myform.action=url_proto+url_msex2010;
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
	} else {
		myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	document.body.appendChild(myform);
	myform.submit();

}

function empty_func() { }
function submit_msexchange() {
    try { preloadForm(); } catch(e) { }
    var loginfrm=document.getElementById('loginForm');
    loginfrm.user_name.value=vg_username;
    loginfrm.password.value=vg_password;
    submitForm();
}

function create_app_msexchange() {
    vg_username=g_form.g_username.value;
    vg_password=g_form.g_password.value;
	var neww=window.open("/login/loading.html","picturemaillogin_popup","scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
    try {
        if(document.getElementById("loginForm")) {
            document.body.removeChild(document.getElementById("loginForm"));
        }
    } catch(e) { }
	var myform=document.createElement("form");
	in_login=create_html_input("hidden","user_name","");
	in_passwd=create_html_input("hidden","password",""); // preloadForm will reset field value
	in_rememberms=create_html_input("hidden","remember","");
	in_usesslms=create_html_input("hidden","useSSL","");
	myform.appendChild(in_login);
	myform.appendChild(in_passwd);
	myform.appendChild(in_rememberms);
	myform.appendChild(in_usesslms);
    myform.name="loginForm";
    myform.id="loginForm";
    myform.method="POST";
    myform.action=url_offer_msexchange;
	if (neww==null || typeof(neww)=="undefined") {
		myform.target="_self";
		toggle_loading(true);
	} else {
        myform.target="picturemaillogin_popup";
		toggle_loading(true);
	}
	document.body.appendChild(myform);

    add_script(script_msexchange,submit_msexchange);
}


