/*
* Sets up the share form
*/
Share={
	init:function(){
		Share.shareForm = new Forms.EnhancedForm($('share-form'),
		{
			submitOnSuccess:true,
			cancel:function(){ window.location = Share.urlTo; },
			validateSubmit:Share.validateShareForm
		});
		validators = Share.shareForm.submitValidators;
		
		// $('password-section').setDisplayed($('require-password').checked);

		// Hide the password section when the checkbox is clicked
		// $('require-password').onclick=function(){
		// 	Share.displayPasswordSection(this.checked);
		// };
		
		Share.overlay = new Overlay();
		
		Share.setFormEnabled($('enable-sharing').checked);
		
		$('enable-sharing').onclick=function(){
			Share.setFormEnabled(this.checked);			
		};
		
		$('make-public').onclick=function(){
				$('noteboard-address').innerHTML="http://jjot.com/"+Share.user + "/" + Share.boardNumber +
					($('make-public').checked ? "" : "?p=" + $('auth-token').value);
			};
	},
	setFormEnabled:function(enabled){
		if (enabled){
			Share.shareForm.submitValidators=validators;
			Share.overlay.remove();
		}else{
			// disable validation
			Share.shareForm.submitValidators=[];				
			Share.overlay.show($('sharing-details'),$('sharing-details').getSize().size.y);
		}
	},
	displayPasswordSection:function(display){
		var p = $('password-section');
		display ? p.show() : p.hide();		
		$('password').disabled = !display;
	},
	// This works in addition to the HTML validation
	validateShareForm:function(form){
		// var r = $('require-password');
		// if (r.checked && ! r.disabled){
		// 	var p = $('password');
		// 	if (p.value.empty()){
		// 		Forms.Validation.highlightError(p,"Please enter a password");
		// 		return false;
		// 	}else if (p.value.length<5){
		// 		Forms.Validation.highlightError(p, "Passwords should be at least 5 letters long");
		// 		return false;
		// 	}
		// }
		return true;
	}
};

window.addEvent('domready',Share.init);
