
/*
@@@BUILDINFO@@@ Net.jsx 1.0.1.0    4/16/07
*/

acrobatconnect.net = {};

acrobatconnect.net.HTTPConnectionFactory = function()
{	
	if( acrobatconnect.net.webaccesslib == undefined)
	{		
		acrobatconnect.net.webaccesslib = new ExternalObject("lib:webaccesslib");	
	}
	return new HttpConnection();
}

acrobatconnect.net.serverURL = function(unparsedURL)
{
	lastSlash = unparsedURL.lastIndexOf("/");
	return unparsedURL.slice(0, lastSlash);
}

acrobatconnect.net.formatURL = function ( mtgUrl, cookie)
{
	return (mtgUrl + "?session=" + cookie + "&autostartsharing=true");
}

//Removes the protocol from the host
acrobatconnect.net.parseHost = function ( url ) 
{		
	//replace both https:// and http://
	url = url.replace( /.*:\/\// , '');	
	//replace eveything after the hostname
	url = url.replace(/\/.*/, '');	 	
	return url;
}

acrobatconnect.net.commonInfo = "/api/xml?action=common-info";
acrobatconnect.net.oldVersion = "breeze";
acrobatconnect.net.adminLink = "";
acrobatconnect.net.changePassLink = "";
acrobatconnect.net.latestCookie = "";
acrobatconnect.net.localizedError = ""; 
acrobatconnect.net.mtgPathParam = "&mtgpath=";
acrobatconnect.net.resolvedHostName = null; //leave blank to start

//( returns false for breeze old servers)
acrobatconnect.net.versionCheck = function(meetingURL)
{
	acrobatconnect.net.adminLink = ""; 
	acrobatconnect.net.changePassLink = "";
	
	var versionService = acrobatconnect.net.HTTPConnectionFactory();
	versionService.url = acrobatconnect.net.serverURL(meetingURL) + acrobatconnect.net.commonInfo 
				+ acrobatconnect.net.mtgPathParam + encodeURIComponent (acrobatconnect.net.parseAccountPath(meetingURL)); 

	versionService.requestheaders = ['Host', acrobatconnect.net.parseHost(versionService.url)];		
	versionService.sync = true;		
	versionService.timeout = 120;
	versionService.redirect = 5;
	
	versionService.execute();	
	
	
	if(versionService.fault != 0)
	{		
 		acrobatconnect.net.localizedError = acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/failedServer=TBT");	   		
		return false;
	}	
		
	var xmlResults = XML(versionService.response)
	var serverString = xmlResults.common.version;
	
	for(var itr = 0; itr < versionService.responseheaders.length; ++itr)
	{		
		if( versionService.responseheaders[itr] == "Set-Cookie" ){			
			acrobatconnect.net.latestCookie  =  versionService.responseheaders[itr+1]; 
			break;
		}
	}
	
	if(serverString)
	{
		var svrType = String(serverString).split('_')[0];
		var svrVer = String(serverString).split('_')[1];
				
		//If its an old breeze server,
		if( svrType == acrobatconnect.net.oldVersion && parseInt(svrVer) < 510)
		{
			acrobatconnect.net.localizedError = acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/oldServer=TBT");
			return false;					
		}		
	}	
	
	//Grab secure-host if available
	if(xmlResults.common.child('secure-host').length() > 0 )
	{
		acrobatconnect.net.resolvedHostName = xmlResults.common.child('secure-host').replace(/:.*/, '').toString();   //remove port number, (if there)			
		//$.write("\nSecure\n");
	}
	else if(xmlResults.common.host)
	{
		acrobatconnect.net.resolvedHostName = xmlResults.common.host.replace(/:.*/, '').toString();   //remove port number, (if there)			
		//$.write("\nNot Secure\n");
	}		
	//$.write("acrobatconnect.net.resolvedHostName" + acrobatconnect.net.resolvedHostName);
	//If an admin link exists use it
	if(xmlResults.common.links.length() > 0 )
	{		
		var links = xmlResults.common.links.link;
		var child = links.@id;
			
		for ( var id = 0; id < child.length(); id++ )
		{			
			if(child[id] == "admin"){
				acrobatconnect.net.adminLink = child[id].parent().@href;				
			}	
			if(child[id] == "change-password"){
				acrobatconnect.net.changePassLink = child[id].parent().@href;				
			}			
		}					
	}	
	/// Let everything else be valid  
	return true;				
}

acrobatconnect.net.parseAccountPath = function(url)
{
	url = url.replace (/.+:\/\//, '' );  //remove protocol	
	url = url.replace (/.*\//, '/' );  //remove hostname
	return url;
}


//Returns the breeze session or undefined 
acrobatconnect.net.login = function(username, passwd, URL, checkVersion)
{	
	if( checkVersion && acrobatconnect.net.versionCheck(URL) == false )
	{			
		return undefined;
	}	
	//$.write("in login");
	var loginService = acrobatconnect.net.HTTPConnectionFactory();
	loginService.url = acrobatconnect.net.resolvedHostName +  "/api/xml?action=login&login=" + encodeURIComponent (username) 
						+ "&password=" + encodeURIComponent (passwd) + acrobatconnect.net.mtgPathParam + encodeURIComponent (acrobatconnect.net.parseAccountPath(URL)); 
		
	loginService.async = false ; 
	loginService.timeout = 120;
	loginService.redirect = 5;
	
	//Set cookie
	loginService.requestheaders = ["Cookie", acrobatconnect.net.latestCookie ,
							    "Host", acrobatconnect.net.parseHost(acrobatconnect.net.resolvedHostName)  ];  //acrobatconnect.net.parseHost(loginService.url)
	
	loginService.execute();
	
	
	if(loginService.fault != 0)
	{			
		acrobatconnect.net.localizedError = acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/failedServer=TBT");	   	
		return undefined;
	}	
	var loginResponse = XML(loginService.response).status.@code ;
	
	//loginResponse =  "ok" or "no-access"
	if(loginResponse == "ok")
	{			
		var cookie = String(acrobatconnect.net.latestCookie).split("BREEZESESSION=")[1].split(';')[0];				
		return cookie;
	}
	
	
	acrobatconnect.net.localizedError = acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/failedLogin=TBT");	   
	return undefined;
}

acrobatconnect.net.loginStoredPrefs = function(checkVersion)
{
	var plaintext = acrobatconnect.tea.TEAdecrypt(app.preferences.acrobatconnect_password, app.preferences.acrobatconnect_login);		
	return acrobatconnect.net.login(app.preferences.acrobatconnect_login, 
								plaintext,
								app.preferences.acrobatconnect_meetingURL,
								checkVersion);
}


