/*
@@@BUILDINFO@@@ Preferencs.jsx 1.0.1.0  4/16/07
*/
acrobatconnect.prefDlg = {};

acrobatconnect.prefDlg.en_USLayout =
{
	dialogWidth:500,
	colWidth:80,
	inputWidth:220,
	imageWidth:30,
	controlsWidth:365,
	rowHeight:20,
	rowPadding:5,
	border:10,
	buttonWidth1:155,
	buttonWidth2:155,
	imageHeight:30,
	lineBorder:5,
	promptHeight:30,
	titleHeight:70,
	buttonHeight:25,
	colPadding:15	
};

acrobatconnect.prefDlg.de_DELayout =
{
	dialogWidth:500,
	colWidth:100,
	inputWidth:220,
	imageWidth:30,
	controlsWidth:365,
	rowHeight:20,
	rowPadding:5,
	border:10,
	buttonWidth1:175,
	buttonWidth2:175,
	imageHeight:30,
	lineBorder:5,
	promptHeight:30,
	titleHeight:70,
	buttonHeight:25,
	colPadding:15	
};

acrobatconnect.prefDlg.fr_FRLayout =
{
	dialogWidth:500,
	colWidth:160,
	inputWidth:220,
	imageWidth:30,
	controlsWidth:355,
	rowHeight:20,
	rowPadding:5,
	border:10,
	buttonWidth1:190,
	buttonWidth2:120,
	imageHeight:30,
	lineBorder:5,
	promptHeight:30,
	titleHeight:70,
	buttonHeight:25,
	colPadding:15	
};

acrobatconnect.prefDlg.ja_JPLayout =
{
	dialogWidth:400,
	colWidth:70,
	inputWidth:220,
	imageWidth:30,
	controlsWidth:320,
	rowHeight:20,
	rowPadding:5,
	border:10,
	buttonWidth1:155,
	buttonWidth2:155,
	imageHeight:30,
	lineBorder:5,
	promptHeight:30,
	titleHeight:70,
	buttonHeight:25,
	colPadding:15	
};


acrobatconnect.prefDlg.changePasswordURL = "http://www.adobe.com/go/acrobatconnect_hosted_changepassword_" ;

acrobatconnect.doPrefs = function(dialog)
{	
	//alert($.locale);
	var staticLayout = acrobatconnect.prefDlg[$.locale +"Layout"];
	var rowHeight = staticLayout.rowHeight + staticLayout.rowPadding;
	var pane = dialog.addPanel(acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/meetings=TBT") );	
	pane.alignChildren = 'fill';
	pane.orientation = 'column';
	var containerBounds = pane.bounds;
	pane.msgPnl = pane.add('group');
	pane.msgPnl.alignChildren = "fill";	
	pane.msgPnl.bounds = {x:0, y:0, width:containerBounds.width, height:containerBounds.height}; 
	
	//Group Containers
	var imageGroup = pane.msgPnl.add("group", {x:staticLayout.border, 
													y:staticLayout.border, 
													width:staticLayout.dialogWidth,
													height:staticLayout.titleHeight});	
	var labelGroup = pane.msgPnl.add("group", {x:staticLayout.border, 
													y:staticLayout.border+staticLayout.titleHeight, 
													width:staticLayout.colWidth,
													height:3*rowHeight+2*staticLayout.border});
	var inputGroup = pane.msgPnl.add("group", {x:staticLayout.colWidth+staticLayout.border,
													y:staticLayout.border+staticLayout.titleHeight, 
													width:staticLayout.inputWidth,
													height:3*rowHeight+2*staticLayout.rowPadding});
	var controlsGroup = pane.msgPnl.add("group", {x:staticLayout.colWidth+staticLayout.border,
													y:3*rowHeight+2*staticLayout.border+staticLayout.titleHeight, 
													width:staticLayout.controlsWidth,
													height:containerBounds.height});
	//Image banner
	var logoImage = imageGroup.add("image", undefined, acrobatconnect.getScriptFolder()+"/Resources/Icons/prefsIcon.png" );													
	logoImage.bounds = {x:0, y:0, width:staticLayout.imageWidth, height:staticLayout.imageHeight};
	var titleText = imageGroup.add("statictext", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/MeetingFav/StartMeeting=TBT"));												
	titleText.bounds = {x:staticLayout.imageWidth, y:0, width:staticLayout.controlsWidth+staticLayout.colWidth, height:staticLayout.imageHeight};
	var headerLine = imageGroup.add("panel" );
	headerLine.bounds = {x:0,y:staticLayout.imageHeight+staticLayout.lineBorder/2,width:staticLayout.dialogWidth,height:0};
	var headerText = imageGroup.add("statictext", undefined, 
			acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/headerText=TBT"), {multiline:true});												
	headerText.bounds = {x:0, y:staticLayout.imageHeight+staticLayout.lineBorder, width:staticLayout.controlsWidth+staticLayout.colWidth, height:staticLayout.promptHeight};
	
	
	//Edit fields													 
	var meetingField = inputGroup.add("edittext" );
	var loginField = inputGroup.add("edittext" );
	var passField = inputGroup.add("edittext" , undefined, undefined, {noecho:true});	

	meetingField.bounds = {x:3,y:staticLayout.rowPadding, width:staticLayout.inputWidth-6, height:staticLayout.rowHeight};										 
	loginField.bounds = {x:3,y:rowHeight+staticLayout.rowPadding, width:staticLayout.inputWidth-6, height:staticLayout.rowHeight};
	passField.bounds = {x:3,y:2*rowHeight+staticLayout.rowPadding, width:staticLayout.inputWidth-6, height:staticLayout.rowHeight};

	//Static Text Fields														 
	var meetingText = labelGroup.add("statictext", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/meetingURLString=TBT"));
	var loginText = labelGroup.add("statictext", undefined,
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/loginString=TBT" ));
	var passText = labelGroup.add("statictext", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/passString=TBT"));
	meetingText.justify = 'right';
	loginText.justify = 'right';
	passText.justify = 'right';
	meetingText.bounds = {x:0, y:staticLayout.rowPadding, width:staticLayout.colWidth, height:staticLayout.rowHeight};
	loginText.bounds =  {x:0, y:meetingText.location[1]+rowHeight, width:staticLayout.colWidth, height:staticLayout.rowHeight};
	passText.bounds =  {x:0, y:loginText.location[1]+rowHeight, width:staticLayout.colWidth, height:staticLayout.rowHeight};
	
	//CheckBox / Buttons
	var rememberBox = controlsGroup.add("checkbox", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/rememberString=TBT"));
	rememberBox.bounds = {x:0,y:0, width:staticLayout.inputWidth, height:staticLayout.rowHeight};
	var changePass = controlsGroup.add("button", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/changePassString=TBT"));
	changePass.bounds = {x:0,y:rowHeight, width:staticLayout.buttonWidth1, height:staticLayout.buttonHeight};
	var accountBtn = controlsGroup.add("button", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/manageString=TBT"));
	accountBtn.bounds = {x:staticLayout.buttonWidth1+staticLayout.border,y:rowHeight, 
						width:staticLayout.buttonWidth2, height:staticLayout.buttonHeight};
	var passwordWarn = controlsGroup.add("statictext", undefined, 
		acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/informationString=TBT") ,
						{multiline:true});						
	passwordWarn.bounds = {x:0,y:staticLayout.buttonHeight + rowHeight + staticLayout.rowPadding, 
							width:staticLayout.controlsWidth, height:containerBounds.height};	
	
	meetingField.onChanging = acrobatconnect.prefDlg.clearPassField;
	loginField.onChanging = acrobatconnect.prefDlg.clearPassField;
	
	acrobatconnect.prefDlg.meetingField = meetingField;
	acrobatconnect.prefDlg.loginField = loginField;
	acrobatconnect.prefDlg.passField = passField;
	acrobatconnect.prefDlg.rememberBox = rememberBox;
	acrobatconnect.prefDlg.changePass = changePass;
	acrobatconnect.prefDlg.accountBtn = accountBtn;
	changePass.onClick = acrobatconnect.prefDlg.changePassHandler;
	accountBtn.onClick = acrobatconnect.prefDlg.manageAcctHandler;


	acrobatconnect.restoreFromPrefs() ;	

}

acrobatconnect.prefDlg.clearPassField = function()
{
	acrobatconnect.prefDlg.passField.text = "";
}

acrobatconnect.prefDlg.manageAcctHandler = function()
{
	if(acrobatconnect.prefDlg.meetingField.text.length <=0)
	{
		alert(acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/missingURL=TBT"));
		return;
	}
	
	if( !acrobatconnect.net.versionCheck(acrobatconnect.prefDlg.meetingField.text) )
	{
		alert( acrobatconnect.net.localizedError );
		return;
	}
	
	var breezeSession; 

	if(acrobatconnect.prefDlg.loginField.text.length > 0 && 
		acrobatconnect.prefDlg.passField.text.length > 0)
	{			
		breezeSession = acrobatconnect.net.login(acrobatconnect.prefDlg.loginField.text,
												acrobatconnect.prefDlg.passField.text,
												acrobatconnect.prefDlg.meetingField.text,
												false );		
	}
												
	if(breezeSession == undefined)  //if data invalid or not entered
	{
		breezeSession = acrobatconnect.prefDlg.loginPassDlg( acrobatconnect.prefDlg.loginField.text, acrobatconnect.prefDlg.passField.text, acrobatconnect.prefDlg.meetingField.text) ;		
		if(breezeSession == undefined)  //user cancelled
		{
			return;
		}	
		//alert(acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/missingLogin=Please enter your login and password."));
	}	
	//Add the admin link if supported ( will be blank string if not )
	var myinfo = acrobatconnect.net.adminLink;
	
	if(myinfo == "")  //for connect pro, just use the meeting path
		myinfo = acrobatconnect.net.parseAccountPath(acrobatconnect.prefDlg.meetingField.text);
	app.openUrl( acrobatconnect.net.resolvedHostName + myinfo + "?session=" + breezeSession );												
}


acrobatconnect.prefDlg.loginPassDlg = function( login, pass, meetingURL )
{	
	var win = new Window ("dialog", acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/logintitle=TBT"), undefined, {modal:true} );	
	win.alignChildren = "fill";
	var msgLabel = win.add("statictext", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/enterAccount=TBT"), {multiline:true });		
	var inputRow = win.add("group");
	inputRow.orientation = "row";
	inputRow.alignChildren = "fill";
	var labelColumn = inputRow.add("group");
	labelColumn.orientation = "column";
	labelColumn.alignChildren = "right";
	labelColumn.spacing = 18;
	var textFieldColumn = inputRow.add("group");
	textFieldColumn.orientation = "column";		
	labelColumn.add ( "statictext", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/loginString=TBT"));	
	labelColumn.add ( "statictext", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/passString=TBT"));
	var loginInput = textFieldColumn.add( "edittext",undefined, login, {preferredSize:[200,20]} ); 
	loginInput.preferredSize = [200,20];
	var passInput = textFieldColumn.add( "edittext",undefined, pass, {preferredSize:[200,20], noecho:true} );
	passInput.preferredSize = [200,20];	
	
	var outerBtnGroup = win.add("group");
	outerBtnGroup.orientation = "column";
	outerBtnGroup.alignChildren  = "right";
	
	var buttonRow = outerBtnGroup.add("group");
	buttonRow.orientation = "row";
	buttonRow.alignChildren = "right";	
	var okBtn = buttonRow.add("button", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/loginBtn=TBT"), {name:'ok'} );
	var cancelBtn = buttonRow.add("button", undefined, acrobatconnect.localize("$$$/CS3/AcrobatConnect/Preferences/closeBtn=TBT"), {name:'cancel'});
	var cookie = undefined;	
	okBtn.onClick = function()
					{ 						
						if(loginInput.text.length <= 0 || passInput.text.length <= 0)
						{
							alert(acrobatconnect.localize("$$$/CS3/AcrobatConnect/ContextMenu/missingLogin=TBT"));
							return;
						}						
						//$.write("meeting url:" + meetingURL);
						var checkVersion = false;
						cookie = acrobatconnect.net.login(loginInput.text, passInput.text, meetingURL, checkVersion );
						//alert("loginInput, passInput, URL" + loginInput.text + "," + passInput.text +"," + meetingURL);						
						if( cookie != undefined) 
						{							
							win.close( cookie ); 
						}
						else //login or pass wrong
						{
							alert( acrobatconnect.net.localizedError );
						}
					}	

	win.center();
	
	if( login.length <= 0)
	{
		loginInput.active = true;
	}
	else 
	{
		passInput.active = true;
	}
	
	win.show();		
	return cookie;		
}

acrobatconnect.prefDlg.changePassHandler = function()
{
	var breezeSession = acrobatconnect.net.login(acrobatconnect.prefDlg.loginField.text,
												acrobatconnect.prefDlg.passField.text,
												acrobatconnect.prefDlg.meetingField.text, true);
	if(breezeSession == undefined)
	{
		alert(acrobatconnect.net.localizedError);
		return;
	}
	if( acrobatconnect.net.changePassLink != "" )  //provided change pass url
	{		
		app.openUrl( acrobatconnect.net.changePassLink + "?session=" + breezeSession );
	}
	else //for everything else open the meeting url
	{										
		
		app.openUrl(  acrobatconnect.prefDlg.meetingField.text + "?session=" + breezeSession );
	}	
}


acrobatconnect.restoreFromPrefs = function()
{
	if(app.preferences.acrobatconnect_prefsSaved != undefined)
	{
		acrobatconnect.prefDlg.meetingField.text = app.preferences.acrobatconnect_meetingURL;
		acrobatconnect.prefDlg.loginField.text = app.preferences.acrobatconnect_login;	
		acrobatconnect.prefDlg.rememberBox.value = app.preferences.acrobatconnect_rememberBox;
		var plainTextPass = acrobatconnect.tea.TEAdecrypt(app.preferences.acrobatconnect_password, app.preferences.acrobatconnect_login);
		if(app.preferences.acrobatconnect_rememberBox)
			acrobatconnect.prefDlg.passField.text = plainTextPass;
	}
}

acrobatconnect.getClearPass = function()
{
	return acrobatconnect.tea.TEAdecrypt(app.preferences.acrobatconnect_password, app.preferences.acrobatconnect_login);
}

acrobatconnect.notifyHTML = function(password)
{	
	if(app.document && app.document.thumbnail == acrobatconnect.startMeeting)
	{		
		//var plainTextPass = acrobatconnect.tea.TEAdecrypt(app.preferences.acrobatconnect_password, app.preferences.acrobatconnect_login);		
		if( app.document.thumbnail == acrobatconnect.startMeeting)
		{ // str.replace (/'/g, "'");
			var filteredPass = app.preferences.acrobatconnect_rememberBox ? password : "";
			var jsString = "loginInfoUpdated('" + app.preferences.acrobatconnect_meetingURL + "','"+ 
							app.preferences.acrobatconnect_login + "','" +  filteredPass + "','" + 
							app.preferences.acrobatconnect_rememberBox  +"')";
		
			app.document.execJS( jsString ) ;			
		}
	}
	
}

acrobatconnect.savePrefs = function()
{
	app.preferences.acrobatconnect_prefsSaved = true;
	app.preferences.acrobatconnect_meetingURL = acrobatconnect.prefDlg.meetingField.text ;
	app.preferences.acrobatconnect_login = acrobatconnect.prefDlg.loginField.text ;
	app.preferences.acrobatconnect_rememberBox = acrobatconnect.prefDlg.rememberBox.value ;
	if(acrobatconnect.prefDlg.rememberBox.value)
	{
		var cipherText = acrobatconnect.tea.TEAencrypt(acrobatconnect.prefDlg.passField.text, app.preferences.acrobatconnect_login);
		app.preferences.acrobatconnect_password = cipherText;
	}
	else //Don't save password
	{
		app.preferences.acrobatconnect_password = "";
	}
	//Communicate with the StartMeetingPage (if open) about the new login/password		
	acrobatconnect.notifyHTML(acrobatconnect.prefDlg.passField.text);	
	//Update the url params to pass the flex on loading
	var theFile = File(acrobatconnect.flexURL());		
	acrobatconnect.startMeeting.displayPath = theFile ;					
}

