/**
 * External login_auto script useful for Demo purposes
 * @Installation:
 * 			copy this script into webmail directory and change sServerURL and sClientUrl paths below
 * @Date:	4.7.2007 9:04:47
 **/

function oExt_Logincn (){


	this.sServerURL = 'proxy.php';					//proxy.php path (or webmail.php path if it is in the same domain)
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="1")
{this.sClientUrl = 'http://222.73.234.230/webmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="2")
{this.sClientUrl = 'http://222.73.234.230/englishwebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="3")
{this.sClientUrl = 'http://222.73.234.230/tcwebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="4")
{this.sClientUrl = 'http://222.73.234.230/jpwebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="5")
{this.sClientUrl = 'http://222.73.234.230/krwebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="6")
{this.sClientUrl = 'http://222.73.234.230/dewebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="7")
{this.sClientUrl = 'http://222.73.234.230/ruwebmail/'};
if (document.form.loginlang.options[document.form.loginlang.options.selectedIndex].value=="8")
{this.sClientUrl = 'http://222.73.234.230/webmail/';
if(confirm("靠谱企业邮箱支持多达21种国际语言，您是否要查看如何修改默认语言?"))
    {//如果是true ，那么就把页面转向
location.href="http://www.corp-email.com/manual/change-corp-email-lang-webmai/";
    }
    else
    {//否则说明下了，赫赫
	}};

	//Mozilla & MSIE7
	if (window.XMLHttpRequest)
		this.oXMLHttp = new XMLHttpRequest();
	else
	//MSIE 6
	if(!navigator.__ice_version && window.ActiveXObject)
		this.oXMLHttp = new ActiveXObject('Microsoft.XMLHTTP');
};

oExt_Logincn.prototype.response = function(sInput){

	var xOutput = null;
	try {
		xOutput = new ActiveXObject('Microsoft.XMLDOM');
		xOutput.async = false;
		xOutput.resolveExternals = false;
    	xOutput.validateOnParse = false;
		xOutput.loadXML(sInput);

		if (xOutput.parseError.errorCode)
			alert(	"Error code: "+ xOutput.parseError.errorCode +
                    "\nLine: " + xOutput.parseError.line + ':'+ xOutput.parseError.linePos +
					"\nReason: "+ xOutput.parseError.reason +
					"\n" + xOutput.parseError.srcText);
	}
	catch (e) {
		var xParser = new DOMParser();
		xOutput = xParser.parseFromString(sInput, 'text/xml');
	}
	
	this.oXMLHttp.open('POST', this.sServerURL, false);
	this.oXMLHttp.send(xOutput);

	return this.oXMLHttp.responseXML;
};

oExt_Logincn.prototype.login = function(user,pass,rsa){
    if (!this.oXMLHttp || !user || (!pass && !rsa)) return;

	//Escape username
    var text = document.createTextNode(user);
    var div = document.createElement('div');
	    div.appendChild(text);
    user = div.innerHTML;

	var tmp;
	try{
		if(!rsa)
            pass = this.getrsa(user,pass);
		else
		    pass = rsa;

		//SET LOGIN info
		tmp = this.response('<iq type="set"><query xmlns="webmail:iq:auth"><username>'+user+'</username><digest>'+pass+'</digest><method>RSA</method></query></iq>');
		var sid = tmp.getElementsByTagName('iq')[0].getAttribute('sid');
		if (!sid) throw '用户名或密码错误';
		this.redirect(sid);
	}
	catch(e){
		alert('警告 - '+e);
		document.getElementById("aftersubmit").style.display="None";
	}
};

oExt_Logincn.prototype.getrsa = function(user,pass){
		//GET RSA public key
		tmp = this.response('<iq type="get" format="xml"><query xmlns="webmail:iq:auth"><username>'+user+'</username><method>RSA</method></query></iq>');
		var key = tmp.getElementsByTagName('hashid')[0].firstChild.nodeValue;

		//Prepare RSA library
		var rsa = new RSAKey();
			rsa.setPublic(key, '10001');
			
		return rsa.encrypt(pass);
};

oExt_Logincn.prototype.loginform = function(elm){
	this.login(elm.username.value,elm.password.value);
	return false;
};

oExt_Logincn.prototype.redirect = function(sid){
	document.location.href = this.sClientUrl+'?sid='+sid;
};




/*	AUTO LOGIN

DO NOT follow this steps!

	Step1: Uncomment for popup with your RSA */
//document.write(Ext_Login.getrsa('demo','demopass'));

/*	Step2: Reload login.html page, RSA key should apear as text in the body */

/*	Step3: Comment Step1 */

/*	Step4: Paste RSA key from page instead of 'myrsa' and undo your changes on Step1 */
//Ext_Login.login('demo','','myrsa');
