function trim(strToTrim){
	var strBlank = strToTrim.substring(0, 1);
	while (strBlank == " ")
	{
		strToTrim = strToTrim.substring(1, strToTrim.length);
		strBlank = strToTrim.substring(0, 1);
	}
	
	strBlank = strToTrim.substring(strToTrim.length - 1, strToTrim.length);

	while (strBlank == " ")
	{
		strToTrim = strToTrim.substring(0, strToTrim.length-1);
		strBlank = strToTrim.substring(strToTrim.length-1, strToTrim.length);
	}
	
	return strToTrim;
}

function OnSubmitLogin(){
	var u=trim(document.loginInfo.u.value);
	var index = u.indexOf("@");
	var domain = "tiscali.it";
	if(index!=-1 && u.length > index){
	
		domain = u.substring(index+1);
		document.loginInfo.u.value = u.substring(0,index);
	}
	else{
		document.loginInfo.u.value = u;
	}
	
	
	if(domain=="tiscalinet.it" || domain=="tiscali.it")
			domain="tiscali.it";
		else
			domain="xxxx";
	
	document.loginInfo.d.value = domain;
	document.loginInfo.submit();
	
}


function isSSLConnectionRequested() {
	if( window.document.loginInfo.ssl ) {
		return window.document.loginInfo.ssl.checked;
	} else {
		return false;
	}
}

function removeUsernameFromUrl(url) {
	var username = /(.*)u=[^&]*([&]|$)(.*)/;
	var result = url.match(username);
	if( result != null ) {
		url = result[1] + result[3];
	}
	return url;
}
function removeTokenFromUrl(url) {
	var username = /(.*)t=[^&]*([&]|$)(.*)/;
	var result = url.match(username);
	if( result != null ) {
		url = result[1] + result[3];
	}
	return url;
}
function removeDomainFromUrl(url) {
	var username = /(.*)d=[^&]*([&]|$)(.*)/;
	var result = url.match(username);
	if( result != null ) {
		url = result[1] + result[3];
	}
	return url;
}


function setAction() {
	var protocol = window.location.protocol;
	var hostname = "mail.tiscali.it";
	var port = window.location.port;
	var httpport="80";
	// If the httpport is not set, well use the current port.
	if( httpport == "0" ) {
		httpport = port;
	}
	var sslport = "443";
	if( isSSLConnectionRequested()) {
	// If a secure connection is required or requested, we'll use one.
		protocol = "https://"
		port = sslport;
	} else {
		protocol = "http://"
		port = httpport;
	}
		
	var authenticateUrl = "/cp/ps/main/login/Authenticate?l=it&d=tiscali.it";
	authenticateUrl = removeUsernameFromUrl(authenticateUrl);
	authenticateUrl = removeTokenFromUrl(authenticateUrl);
	authenticateUrl = removeDomainFromUrl(authenticateUrl);
	// Remove unecessary query parameters from the url. The username is
	// supplied by the login form. The token will be created upon
	// a successful login.
	// Construct the complete login url.
	var url = protocol + hostname + ":" + port + authenticateUrl;
	window.document.loginInfo.action = url;


}
