// $Id: global_login.js 232 2009-12-02 20:46:43Z epinter $

//globalvars
var isIE=0;
var isIE6=0;
var isFF=0;
var isNN=0;
var isOP=0;

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_xchangeold='//www.picturemail.com.br/webmail/src/redirectox.php';
var url_xchange='//xchange2.picture.com.br/gw/login.php';
var url_offer_xchange="//www.picturemail.com.br/picturemailxchange/";
var url_offer_im="//www.picturemail.com.br/im/";
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 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').innerHTML=alerta_popup;
		return;
	} else {
		if(window.opera && ! popuptest.opera) {
			document.getElementById('alerta_popup').innerHTML=alerta_popup;
			return;
		}
		popuptest.close();
	}
}

function getFirstCookie(attr) {
        if(isCookiesEnabled()) {
		var ar_res_us=getcookie('picturemail_gl_'+attr);
		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() {
	if((document.getElementById('alwayssecure') && document.getElementById('alwayssecure').value=="1") || (document.getElementById('sslmode') && document.getElementById('sslmode').checked)) {
		url_proto='https:';
		setcookie('picturemail_gl_sslmode','1','10y');
	} else {
		setcookie('picturemail_gl_sslmode','','0');
		url_proto='http:';
	}
	var form=document.getElementById('global_login_form');
	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)
			document.getElementById('selectedapp').value=form.app[i].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","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=document.getElementById('selectedapp').value;
	if(appchecked=='webmail') {
		toggle_loading();
		create_app_webmail(form);
	} else if(appchecked=='webmailold') {
		toggle_loading();
		create_app_webmailold(form);
	} else if(appchecked=='im') {
		create_app_im(form);
	} else if(appchecked=='groupware') {
		toggle_loading();
		create_app_xchange(form);
	}
	if(isCookiesEnabled()) {
		if(document.getElementById('rememberme') && document.getElementById('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;
}

function offer_xchange(form) {
	if(! document.getElementById('rememberme').checked) form.g_password.value='';
	alert('Produto não contratado');
	var neww=window.open(url_proto+url_offer_xchange,"picturemaillogin_popup","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_im(form) {
	if(! document.getElementById('rememberme').checked) form.g_password.value='';
	alert('Produto não contratado');
	var neww=window.open(url_proto+url_offer_im,"picturemaillogin_popup","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,id) {
	newel=document.createElement("input");
	newel.setAttribute("type",type);
	newel.setAttribute("name",name);
	newel.setAttribute("value",value);
	return newel;
}

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

function create_app_xchangeold(form) {
	var neww=window.open("about:blank","picturemaillogin_popup","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",form.g_username.value);
	in_secretkey=create_html_input("hidden","secretkey",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(form) {
	var neww=window.open("about:blank","picturemaillogin_popup","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",form.g_username.value);
	in_passwd=create_html_input("hidden","passwd",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(form) {
	var neww=window.open("about:blank","picturemaillogin_popup","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",form.g_username.value);
	in_secretkey=create_html_input("hidden","secretkey",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(form) {
	var neww=window.open("about:blank","picturemaillogin_popup","resizable=yes,location=no,menubar=no,toolbar=no,top=0,width="+popup_w+",height="+popup_h,true);
	var myform=document.createElement("form");
	in_action=create_html_input("hidden","_action","login");
	in_timezone=create_html_input("hidden","_timezone","_default_");
	in_iamnotrc=create_html_input("hidden","iamnotrc","1");
	in_user=create_html_input("hidden","_user",form.g_username.value);
	in_pass=create_html_input("hidden","_pass",form.g_password.value);
	myform.appendChild(in_action);
	myform.appendChild(in_timezone);
	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 isHTTPS()
{
   return location.protocol == 'https:';
}

function evt_keyup_h_form(event) {
        var ev = event || window.event;
        var tgt = ev.target || ev.srcElement;
        if(ev.keyCode==13 || ev.keyCode==13) {
		if(tgt.id=='g_password') {
			verify_info();
		}
	}

}
