Event.observe(window, 'load' , function() {
	// Le due funzioni seguenti non si devono attivare nella Home
	// dove mancano i DIV necessari ad applicarle
	if ($('password_container')!=null && buggedKHTML ) {
		$('txtpassword').hide();
		$('password_label').show();
		$('password').show();
	}
	
	if ($('confirm_password_container')!=null && buggedKHTML ) {
		$('txtconfirm_password').hide();
		$('confirm_password_label').show();
		$('confirm_password').show();
	}
	
	if ( isKHTML ) {
		//if (isMinSafari2 == false) {
		if (isMinSafari2 == false) {
			if ( $('form_submitGreen') ) $('form_submitGreen').remove();
			if ( $('form_submitGreen2') ) $('form_submitGreen2').show();
		}
	} else {
		if ( $('form_submitGreen2') ) $('form_submitGreen2').remove();
	}
	
});

function inlineEdit(elementId, externalControlId, fieldname, tablename, nrows, ncols, key) {
	var url = 'ajaxsrv.php?module=fe_user_subscription&key=' + key;

		var editor = new Ajax.InPlaceEditor(
			elementId,
			url,
			{
				externalControl: externalControlId,
				externalControlOnly: true,
				callback: function(form, value) { return 'tablename=' + tablename + '&fieldname=' + fieldname + '&elementId=' + elementId + '&fieldvalue=' + escape(value) },
				rows: nrows,
				cols: ncols,
				clickToEditText: '',
				ajaxOptions: { evalScripts: true}
			}
		);
}


function inlineCollectionEdit(elementId, externalControlId, fieldname, tablename, key) {
	var url = 'ajaxsrv.php?module=fe_user_subscription&key=' + key;

		var editor = new Ajax.InPlaceCollectionEditor(
			elementId,
			url,
			{
				externalControl: externalControlId,
				externalControlOnly: true,
				callback: function(form, value) { return 'tablename=' + tablename + '&fieldname=' + fieldname + '&elementId=' + elementId + '&fieldvalue=' + escape(value) },
				loadCollectionURL: 'ajaxsrv.php?module=fe_user_subscription&collection=categories'
			}
		);
}


function inlineEditErrorRemove(elementId) {
	//Remove the error field if present
	if($('validation-error_' + elementId)) $('validation-error_' + elementId).remove();
}


var usernameVerification = function(element) {
	new Ajax.Updater('username_verification_container', 'ajaxsrv.php', {
		method: 'post',
		parameters: {
			action: 'verify_user',
			module: 'fe_user_subscription',
			username: $F('username')
		}
	});
}


var hInputValues = $H({
		name : 'NAME', 
		company : 'COMPANY',
		email : 'EMAIL',
		username : 'USERNAME',
		txtpassword : 'PASSWORD',
		txtconfirm_password : 'CONFIRM PASSWORD',
		password : 'PASSWORD',
		confirm_password : 'CONFIRM PASSWORD',
		security_code : 'ENTER SECURITY CODE'
});


var hInputValuesToValidate = hInputValues.clone();
	//Remove the fields that don't need validation'
	['company'].each( function(key) {
		hInputValuesToValidate.unset(key);
	});
	

var myrules = {
	'#name' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('name')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('name')); }
	},
	/*'#fe_group' : function(el){
		el.onclick = function() { 
			if (this.checked==true) { 
				$('applicants_only_fields').show();
			} else {
				$('applicants_only_fields').hide();
			}
		 }
	},*/
	'#company' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('company')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('company')); }
	},
	'#email' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('email')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('email')); }
	},
	'#username' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('username')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('username')); },
		el.onchange = function(){ 
			//$('username_verification_container').update('<- username verification');
			if($('username_verification_container')) {
				usernameVerification($('username_verification_container'));
			}
		}
	},
	'.username_verification' : function(el){
		el.onclick = function() { usernameVerification(this); }
	},
	'#security_code' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('security_code')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('security_code')); }
	},
	'#txtpassword' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('txtpassword')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('txtpassword')); }
	},
	'#txtconfirm_password' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('txtconfirm_password')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('txtconfirm_password')); }
	},
	'#password' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('password')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('password')); }
	},
	'#confirm_password' : function(el){
		el.onblur = function(){ blurTextboxes(this, hInputValues.get('confirm_password')); },
		el.onfocus = function(){ focusTextboxes(this, hInputValues.get('confirm_password')); }
	}
};

Behaviour.register(myrules);

Validation.addAllThese([
	['blank', 'Please insert a value.', {
		notOneOf : hInputValuesToValidate.values(), // value does not equal anything
		isNot : ''
	}]
]);
