
/* Login */

function InitLoginForm(objForm) {

	var boolDebug=false;
	var strHost='';

	var strRedirectTo=location.search.split('&redirectto=')[1];
	if (strRedirectTo && strRedirectTo.indexOf('&')>=0) strRedirectTo=strRedirectTo.split('&')[0];
	if (strRedirectTo) objForm.RedirectTo.value=unescape(strRedirectTo);

	strRedirectTo=objForm.RedirectTo.value;
	if (window.attachEvent) window.attachEvent('onload',function() {objForm.RedirectTo.value=strRedirectTo;}); //we must restore the field after the submit, for browsers that caches this value in the navigation history (Mozilla and others)...

	objForm.onsubmit=function() {

		if (window.event) window.event.returnValue=false;

		if (objForm.Username.value && objForm.Password.value) {

			if (objForm.RedirectTo.value.indexOf('://')>=0) {
				strHost=objForm.RedirectTo.value.split('/').slice(0,3).join('/');
				objForm.RedirectTo.value=objForm.RedirectTo.value.substr(strHost.length);
			}
			if (!strHost || !TryICM(objForm,strHost,boolDebug)) { //first try icm if a host was specified...
				SubmitLoginForm(objForm,strHost); //...otherwise continue as normal
			}
		}
		return false;
	}

	objForm.Username.focus();
	objForm.Username.select();	
}

function SubmitLoginForm(objForm,strHost,strRedirectedHost) {

	objForm.method='post';
	objForm.action=(strRedirectedHost ? strRedirectedHost : strHost)+'/names.nsf?login';
	objForm.submit();	
	if (!window.attachEvent) if (strHost) objForm.RedirectTo.value=strHost+objForm.RedirectTo.value; //we must restore the field after the submit, for browsers that caches this value in the navigation history (Mozilla and others)...
}

function GetBaseHRef() {
	var eltBase=document.getElementsByTagName('base')[0];
	return (eltBase && eltBase.getAttribute('href') ? eltBase.getAttribute('href') : '');
}

function TryICM(objForm,strHost,boolDebug) {

	var xmlhttp=tmt.CreateXMLHttp();
	if (!xmlhttp) return false;

	xmlhttp.open('GET',GetBaseHRef()+'../../tmt.http.trace?open&url='+escape(strHost),true);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState==4) {

		   	if (boolDebug) alert('Status: '+xmlhttp.status+'\n\n'+xmlhttp.getAllResponseHeaders()+'\n'+xmlhttp.responseText)

			if (xmlhttp.status<400) {

				var strHostHeaderName='Host';
				var strArrLines=xmlhttp.responseText.split('\n').join('\r').split('\r');

				for (var i=0; i<strArrLines.length; i++) if (strArrLines[i].indexOf(strHostHeaderName+': ')==0) {

					var strHostHeaderValue=strArrLines[i].substr(strHostHeaderName.length+2);
					if (strHostHeaderValue) {
						SubmitLoginForm(objForm,strHost,strHost.split('://')[0]+'://'+strHostHeaderValue);
						return;
					}
				}

				if (!confirm('Error: No Host header found in the response.\n\nContinue to standard login page?')) return;
			}
			else if (!confirm('Error: Invalid response status: '+xmlhttp.statusText+' ('+xmlhttp.status+')\n\nContinue to standard login page?')) return;

			SubmitLoginForm(objForm,strHost);
		}
	}
	xmlhttp.send(null)

	return true;
}
