/**
* Factory System Installation
*
* @author	  RaphaeL Pralat <raph@plus2.fr>
* @version    CVS: $Id: User.js,v 1.10 2007-09-20 10:02:29 cvs Exp $
* @uses       http://prototype.conio.net/
* @uses       http://bennolan.com/behaviour/
* @uses       http://script.aculo.us
*/

var						FactoryUser = {

	rules :				{

	'#a_connexion' : function(el) {
		el.onclick = function() {
			if($('connexion').style.display == 'none'){
			    if($('connection_click')) {
			        $('connection_click').style.display = 'none';
			    }
        		$('a_connexion').className = "on";
        		$('a_connexion').blur();
        		Effect.Appear('connexion',{duration:0.3});
        	}/*else{
        	    $('connection_click').style.display = 'block';
        		$('a_connexion').className = "";
        		$('a_connexion').blur();
        		Effect.Fade('connexion',{duration:0.3});
        	}*/
		}
	}, 
	    
	'.input2' : function(el) {
			el.onkeypress = function(event) {
				if(event == null) {
					e = window.event.keyCode;
				}
				else {
					e = event.keyCode;
				}
				if(e == 13) {
					form = $('frm_connexion').serialize();
					//new Ajax.Updater('connection', '/ajax/login', {
					new Ajax.Request('/ajax/login', {
						parameters	: form,
						method		: 'post',
						onComplete	: FactoryUser.testLogin
					});
				}
			}
		},

		/**
		* quick key for little login
		*
		*/
		'.input2LittleLogin' : function(el) {
			el.onkeypress = function(event) {
				if(event == null) {
					e = window.event.keyCode;
				}
				else {
					e = event.keyCode;
				}
				if(e == 13) {
					form = $('littleFormLogin').serialize();
					new Ajax.Updater('littleConnexion', '/ajax/userLittleLogin', {
						parameters	: form,
						method		: 'post',
						onComplete	: FactoryUser.reloadAndValid
					});
					new Ajax.Updater('rightContact', '/ajax/PublicContactForm', {onComplete : FactoryUser.reload});
				}
			}
		},
		'.littleloginclose' : function(el) {
			el.onclick = function(){
				$('littleConnexion').style.display = 'none';
			}
			
		},
		/**
		* Ajax Admin Login
		*
		*/
		'#adminLogin' : function(el) {
			el.onclick = function() {
				$('formAdminLogin').submit();
			}
		},

		/**
		* Ajax User Login
		*
		*/
		'#userLogin' : function(el) {
			el.onclick = function() {
				form = $('frm_connexion').serialize();
				//new Ajax.Updater('connection', '/ajax/login', {
				new Ajax.Updater('errLogin', '/ajax/login', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.chkLogin
				});

			}
		},
		
		'#userLittleLogin' : function(el) {
			el.onclick = function() {
				form = $('littleFormLogin').serialize();
				new Ajax.Updater('littleConnexion', '/ajax/userLittleLogin', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reloadAndValid
				});
				new Ajax.Updater('rightContact', '/ajax/PublicContactForm', {onComplete : FactoryUser.reload});
			}
		},


		/**
		* Ajax user login for little form connexion
		*
		*/
		'#userTest' : function(el){
			el.onclick = function() {
				new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
			}
		},

		'#userTest2' : function(el){
			el.onclick = function() {
				new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
			}
		},

		'#userTest3' : function(el){
			el.onclick = function() {
				new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
			}
		},

		'#userTest4' : function(el){
			el.onclick = function() {
				new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
			}
		},

		'#userTest5' : function(el){
			el.onclick = function() {
				new Ajax.Updater('littleConnexion2', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
			}
		},

		'.offerAnswertest' : function(el){
			el.onclick = function() {
				userid = findParamInClass('userid', this);
				if(!userid){
				new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : FactoryUser.showLogin});
				}
			}
		},
		
		/**
		* Ajax User Disconnect
		*
		*/
		'#userDisconnect' : function(el) {
			el.onclick = function() {
				//new Ajax.Updater('connection', '/ajax/disconnect', {
				//new Ajax.Updater('connect', '/ajax/disconnect', {
				new Ajax.Request('/ajax/disconnect', {
					parameters	: '',
					method		: 'post',
					onComplete	: FactoryUser.disconnectAndChange
				});

			}
		},

		/**
		* Ajax User Inscription
		*
		*/
		'#userInscription' : function(el) {
			el.onclick = function() {
				form = $('formInscription').serialize();
				//$('formInscriptionSubmit').innerHTML = '<img src="/images/themes/default/common/loading.gif">';
				new Ajax.Updater('inscriptionBox', '/ajax/inscription', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.inscriptionRedirect
				});
			}
		},
		
		'#userInscriptionPub' : function(el) {
			el.onclick = function() {
				form = $('formInscription').serialize();
				//$('formInscriptionSubmit').innerHTML = '<img src="/images/themes/default/common/loading.gif">';
				new Ajax.Updater('inscriptionPub', '/ajax/inscriptionPub', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.inscriptionPub
				});
			}
		},

		/**
		* Ajax User Recruiter Inscription
		*
		*/
		'#userInscriptionRecruiter' : function(el) {
			el.onclick = function() {
				form = $('formInscription').serialize();
				//$('formInscriptionSubmit').innerHTML = '<img src="/images/themes/default/common/loading.gif">';
				new Ajax.Updater('inscriptionBox', '/ajax/inscriptionRecruiter', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});

			}
		},

		/**
		* Ajax User Show CGU
		*
		*/
		'#userShowCgu' : function(el) {
			el.onclick = function() {
				if(($('cgu').style.display == 'block')) {
					$('pub').style.display = 'block';
					$('cgu').style.display = 'none';
				} else {
					$('pub').style.display = 'none';
					$('cgu').style.display = 'block';
				}

			}
		},

		/**
		* Ajax User Forgot Pass
		*
		*/
		'#userForgotPass' : function(el) {
			el.onclick = function() {
				form = $('formForgotPass').serialize();
				new Ajax.Updater('displayForgotPass', '/ajax/forgotPass', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},

		/**
		* Ajax User Add Relation
		*
		*/
		'.ajaxAddUserRelation' : function(el) {
			el.onclick = function() {
				this.innerHTML = 'attente de validation';
				var userId = findParamInClass('userId', this);
				new Ajax.Request('/ajax/addUserRelation', {
					parameters	: 'userId=' + userId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},

		/**
		* Ajax User Validate Relation
		*
		*/
		'.ajaxValidUserRelation' : function(el) {
			el.onclick = function() {
				var relationId = findParamInClass('relationId', this);
				$('relation_' + relationId).style.display = 'none';
				new Ajax.Updater('desktopRelationBox', '/ajax/validUserRelation', {
					parameters	: 'relationId=' + relationId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},
		
		'.validUserRelation' : function(el) {
			el.onclick = function() {
				var relationId = findParamInClass('relationId', this);
				new Ajax.Updater('desktopRelationDiv', '/ajax/validUserRelation', {
					parameters	: 'relationId=' + relationId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},

		/**
		* Ajax User Delete Relation
		*
		*/
		'.ajaxDeleteRelation' : function(el) {
			el.onclick = function() {
				var relationId = findParamInClass('relationId', this);
				new Ajax.Updater('desktopRelationDiv', '/ajax/deleteUserRelation', {
					parameters	: 'relationId=' + relationId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},
		
		/**
		* Ajax User Search Relation
		*
		*/
		'#ajaxSearchRelation' : function(el) {
			el.onclick = function() {
				
				var keyword = $F('searchkeyword');
				if (keyword != ''){
					
				new Ajax.Updater('searchRelation', '/ajax/searchUserRelation', {
					parameters	: 'keyword=' + encodeURIComponent(keyword),
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
				$('searchkeyword').value = '';
				}
			}
		},
		

		/**
		* User Form Recommandation
		*
		*/


		'#UserFormRecommendation' : function(el) {
			el.onclick = function() {
//				$('divFormRecommandation').style.top = ($('UserFormRecommendation').offsetTop-105)+'px';
//				$('divFormRecommandation').style.left = $('UserFormRecommendation').offsetLeft + 'px';
				$('divFormRecommandation').style.display = 'block';
			}
		},
		
		'#ajaxHideRecommandation' : function(el) {
			el.onclick = function() {
				$('divFormRecommandation').style.display = 'none';
			}
		},
			
		/*
		* valid and submit the recommendation
		*/
		
		'#sendReferenceText' : function(el) {
			el.onclick = function() {
				form = $('formRecommendation').serialize();
				new Ajax.Request('/ajaxyoujob/insertUserRecommendation', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
				$('divFormRecommandation').style.display = 'none';
			}
		},
		
		
		/**
		* User Validate Recommendation
		*
		*/
		'.validUserRecommendationAction' : function(el) {
			el.onclick = function() {
				var recommendationId = findParamInClass('recommendationId', this);
				//$('recommendation_' + recommendationId).style.display = 'none';
				new Ajax.Updater('desktopRecommendationAction', '/ajax/validUserRecommendation', {
					parameters	: 'recommendationId=' + recommendationId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},

		/**
		* User Delete Recommendation
		*
		*/
		'.deleteRecommendationAction' : function(el) {
			el.onclick = function() {
				var recommendationId = findParamInClass('recommendationId', this);
				new Ajax.Updater('desktopRecommendationAction', '/ajax/deleteUserRecommendation', {
					parameters	: 'recommendationId=' + recommendationId,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},


		/**
		*
		* Change User Information
		*
		*/
		'.ajaxUserInfoEdit' : function(el) {
			el.onclick = function() {
				var form = $('userInfoForm').serialize();
				var type = findParamInClass('type', this);
				form = "type=" + type + "&" + form;

				new Ajax.Updater('Userinfo', '/ajax/userInfoEdit', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reloadAndMember
				});
			}
		},
		
		
		/**
		*
		* Add an User spoken Language
		*
		*/
		'.ajaxUserAddLang' : function(el) {
			el.onclick = function() {
				var form = $('userInfoForm').serialize();
				
				new Ajax.Updater('spokenLanguage', '/ajax/userAddLang', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},
	
		/**
		*
		* Add an User spoken Language
		*
		*/
		'.ajaxUserDelLang' : function(el) {
			el.onclick = function() {
				var form = $('userInfoForm').serialize();
				var type = findParamInClass('place', this);
				form = "delete_lang=" + type + "&" + form;

				new Ajax.Updater('spokenLanguage', '/ajax/userDelLang', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},
		

		
		
		/**
		*
		* Change User Profil
		*
		*/
		'.userProfilEditAction' : function(el) {
			el.onclick = function() {
				$('err1').innerHTML='';
				var chk = $F('urlPersonalize');
				re = /\W/;
				if (!re.test(chk) && $F('urlPersonalize') !=''){
				var form = $('profilForm').serialize();
				new Ajax.Updater('personalizeUrlContent', '/ajax/userProfilEdit', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
				}
				else {
					$('err1').innerHTML='Seuls les caracteres alphanumérique sont autorisés.';
				}
				
			}
		},

		/* validate form to change password (case forgotpass) */
		'#ajaxChangePass' :function (el) {
			el.onclick = function() {
				form = $('changePass').serialize();
				new Ajax.Updater('contentBox','/ajax/userForgotPass', {
					parameters	: form,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},

		/*delete Candidature */
		'.delCandidature' :function (el) {
		    el.onclick = function(){
		        answerId = findParamInClass('id', this);
		        new Ajax.Request('/ajax/delCandidature',{
		            parameters : 'answerId='+answerId,
		            method     : 'post',
		            onComplete : FactoryUser.reloadList
		        });
		    }
		},
		
		'.showhidedetail' : function (el) {
			el.onclick = function (){
				sid = findParamInClass('sid', this);
				if($(sid).style.display == "none"){
					this.innerHTML = "-";
					$(sid).style.display="";
				}
				else {
					this.innerHTML = "+";
					$(sid).style.display="none";				
				}
			}
		
		},

		'.ajaxVideoOrderSelect' : function (el) {
			el.onchange = function (){
				mediaId = findParamInClass('mediaId',this);
				mediaPos = $('ajaxVideoOrderSelect'+mediaId).value;
				new Ajax.Updater('modulevideo','/ajax/videoOrderSelect', {
					parameters	: 'mediaId='+mediaId+'&mediaPos='+mediaPos,
					method		: 'post',
					onComplete	: FactoryUser.reload
				});
			}
		},
		'.menuElementChangebg' : function (el) {
			el.onmouseover = function() {
				this.style.background = "#CCCCCC";				
			}
			el.onmouseout = function() {
				this.style.background = "#EEEEEE";				
			}
		},
		'.menuChangebg' : function (el) {
			el.onmouseover = function() {
				this.style.background = "#FF6F00";
						
			}
			el.onmouseout = function() {
				this.style.background = "none";				
			}
		},		
		'.showMessage' : function (el) {
			el.onclick = function() {
				openId = findParamInClass('id',this);
				openId = "messageDiv" + openId;
				$(openId).style.display = 'block';				
			}
		},

		'.messageDivClose' : function (el) {
			el.onclick = function() {
				closeId = findParamInClass('id',this);
				closeId = "messageDiv" + closeId;
				$(closeId).style.display = 'none';				
			}
		},

		'lastbutnotleast': {}
	}, /** rules **/

	'reload' :	function(ajax) {
		Behaviour.apply(FactoryUser.rules);
	},
	
	'reloadList' :	function(ajax) {
	    history.go(0);
		Behaviour.apply(FactoryUser.rules);
	},


	'disconnectAndChange' : function() {
		//Behaviour.apply(FactoryUser.rules);
		window.location.href = "/";
	},

	/**
	* Redirection quand la connection n'est pas bonne
	*
	*/
	'testLogin' :	function() {
		window.location.href = "/membre";
	},
	'chkLogin' :	function() {
	    if($('errLogin').innerHTML == ""){
		  window.location.href = "/membre";
	    }
	},
	// redirection
	'inscriptionRedirect' :	function() {
		//Behaviour.apply(FactoryUser.rules);
		if($('inscriptionBox').innerHTML.indexOf('boîte mail') > 1) {
			//window.location.href = "/membre/video/log";
			window.location.href = "/membre/video";
		}
		Behaviour.apply(FactoryUser.rules);
	},

	'inscriptionPub' :	function() {
		//Behaviour.apply(FactoryUser.rules);
		if($('inscriptionPub').innerHTML.indexOf('boîte mail') > 1) {
			window.location.href = "/";
		}
		Behaviour.apply(FactoryUser.rules);
	},
	
	'showLogin' :	function() {
		$('littleConnexion').style.display = 'block';
		Behaviour.apply(FactoryUser.rules);
	},

	'showLogin2' :	function() {
		$('littleConnexion2').style.top = ($('userTest5').offsetTop-105)+'px';
		$('littleConnexion2').style.left = $('userTest5').offsetLeft + 'px';
		$('littleConnexion2').style.display = 'block';
		Behaviour.apply(FactoryUser.rules);
	},

	'reloadAndValid' :	function() {
		if($('littleMsgError').innerHTML == "") {
			// update the connection module
			//new Ajax.Updater('connection', '/ajax/login', {
			new Ajax.Updater('connect', '/ajax/login', {
				parameters	: form,
				method		: 'post',
				onComplete	: FactoryUser.testLogin
			});
			// hide the little conenction module
			$('littleConnexion').style.display = 'none';
		}
		Behaviour.apply(FactoryUser.rules);
	},
	
	'reloadAndHigh' :	function() {
		new Effect.Highlight('msg', {startcolor:'#FFFFFF', endcolor:'#FF7F00', duration:3});
		Behaviour.apply(FactoryUser.rules);
	},
	
	'reloadAndMember' : function() {
		window.location.href = "/membre/";
	}

}

function findParamInClass(param, el) {
	var regexp = new RegExp(param + '_([A-Za-z0-9/:?&\-\._]+)');
	var mymatch = regexp.exec(el.className);
	if(mymatch) {
		return mymatch[1];
	}
	return false;
}

function userLoginKeydown(event){
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13 && $('login').value != '' && $('login').value != 'Login' && $('password').value != '' && $('password').value != 'Password'){ 
		form = $('frm_connexion').serialize();
		//new Ajax.Updater('connection', '/ajax/login', {
		new Ajax.Updater('errLogin', '/ajax/login', {
			parameters	: form,
			method		: 'post',
			onComplete	: FactoryUser.chkLogin
		});
	}
	else{
	return false;
	} 
}
Behaviour.register(FactoryUser.rules);