/**
 * @author noehmeier-marcel
 */
function RegisterUser(rpath, sid)
{
	this.rpath				= rpath+'bilder/';
	this.session_id			= sid;
	
	this.username			= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Username ist bereits registriert. Bitte nehmen Sie einen anderen!', 'Eingabe korrekt']
	};
	this.email				= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Email schon vorhanden. Bitte andere eingeben.', 'Eingabe korrekt']
	};
	this.password			= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Die Eingabe ist korrekt, jedoch stimmt sie nicht mit der Wiederholung über ein!', 'Eingabe korrekt']
	};
	this.passwordRep		= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Die Eingabe ist korrekt, jedoch stimmt sie nicht mit dem ersten Passwort über ein!', 'Eingabe korrekt']
	};
	this.captcha			= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Eingabe unzureichend', 'Eingabe korrekt']
	};
	this.useterms			= {
		'obj' : null,
		'actionObj' : null,
		'validateResult' : false,
		'title_header' : 'Meldung:',
		'title_body' : 'Noch keine Eingabe getätigt!',
		'title_text' : ['Eingabe Fehlerhaft','Eingabe unzureichend', 'Eingabe korrekt']
	};
	
	
	this.getSessionID		= function( id ){ this.session_id = id; }
	
	this.tt					= null;

	
	this.setTitle				= function(obj, h, b){
		obj.title_header = h;
		obj.title_body = b;
	}
	
	this.displayTT = function(obj)
	{
		this.tt = {
			tt_frame : document.getElementById('tt_frame'),
			tt_header : document.getElementById('tt_header'),
			tt_body : document.getElementById('tt_body')
		}
		this.tt.tt_frame.style.display = 'block';
		this.tt.tt_frame.style.zIndex = '55';
		//alert('left: '+obj.actionObj.style.left+'px\ntop: '+obj.actionObj.style.top+'px');
		this.tt.tt_frame.style.left = '500px';
		this.tt.tt_frame.style.top = '100px';
		
		this.tt.tt_header.innerHTML = obj.title_header;
		this.tt.tt_body.innerHTML = '<span>'+obj.title_body+'</span>';
		//alert(obj.title_body);
	}
	
	this.hide = function(){rUser.tt.tt_frame.style.display = 'none';}
	
	
	/**
	 * AdderMethod
	 * @param {Object} username
	 * @param {Object} aObj
	 */
	this.addUsernameObj		= function( username, aObj ){
		this.username.obj = username;
		this.username.actionObj = document.getElementById( aObj );
	}
	
	/**
	 * AdderMethod
	 * @param {Object} mail
	 * @param {Object} aObj
	 */
	this.addMailObj			= function( mail, aObj ){
		this.email.obj = mail;
		this.email.actionObj = document.getElementById( aObj );
	}
	
	/**
	 * AdderMethod
	 * @param {Object} pw
	 * @param {Object} aObj
	 */
	this.addPasswordObj		= function( pw, aObj ){
		this.password.obj = pw;
		this.password.actionObj = document.getElementById( aObj );
	}
	
	/**
	 * AdderMethod
	 * @param {Object} pwr
	 * @param {Object} aObj
	 */
	this.addPasswordRepObj 	= function( pwr, aObj ){
		this.passwordRep.obj = pwr;
		this.passwordRep.actionObj = document.getElementById( aObj );
	}
	
	/**
	 * AdderMethod
	 * @param {Object} capchta
	 * @param {Object} aObj
	 */
	this.addCaptchaObj		= function( capchta, aObj ){
		this.captcha.obj = capchta;
		this.captcha.actionObj = document.getElementById( aObj );
	}
	
	/**
	 * AdderMethod
	 * @param {Object} capchta
	 * @param {Object} aObj
	 */
	this.addUsetermsObj		= function( useterms, aObj ){
		this.useterms.obj = useterms;
		this.useterms.actionObj = document.getElementById( aObj );
	}
	
	/* Ab hier die CheckMethoden */
	
	this.checkUsername		= function( userObj,actionObj ){
		this.addUsernameObj(userObj,actionObj);
		var jSonRequest = new Json.Remote("register.php?action=checkusername&username="+this.username.obj.value,{
			autoCancel: true,
			onComplete : function(username){
				rUser.showCheckResult(rUser.checkValues(/^[0-9a-zA-z]{5,12}$/,rUser.username),rUser.username, username.ret );
			}
		}).send();
		
		//this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.username),this.username);
	}
	
	this.checkMail			= function( mailObj,actionObj ){
		this.addMailObj(mailObj,actionObj);
		var jSonRequest = new Json.Remote("register.php?action=checkmail&mail="+this.email.obj.value,{
			autoCancel: true,
			onComplete : function(mail){
				rUser.showCheckResult(rUser.checkValues(/^[_a-zA-Z0-9-](\.{0,1}[_a-zA-Z0-9-])*@([_a-zA-Z0-9-]{2,}\.){0,}[_a-zA-Z0-9-]{3,}(\.[_a-zA-Z]{2,4}){1,2}$/,rUser.email),rUser.email,mail.ret);
			}
		}).send();
	}
	
	this.checkPassword		= function( pwObj,actionObj ){
		this.addPasswordObj(pwObj,actionObj);
		this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.password),this.password,false);
		if( this.passwordRep.obj != null ){
			//alert('PWR: '+this.password.obj);
			this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.passwordRep),
												this.passwordRep,
												(this.password.obj.value == this.passwordRep.obj.value )
								);
			if( (this.password.obj.value == this.passwordRep.obj.value) ){
				this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.passwordRep),this.passwordRep,true);
				this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.password),this.password,true);
			}
		}else{
			this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.password),this.password,false);
		}
		
		
	}
	
	this.checkPasswordRep 	= function( pwrObj,actionObj ){
		this.addPasswordRepObj(pwrObj,actionObj);
		this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.passwordRep),this.passwordRep,false);
		//prüfen ob PWR schon eingegeben wurde
		if( this.password.obj != null ){
			//alert('PWR: '+this.password.obj);
			this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.password),
												this.password,
												this.password.obj.value == this.passwordRep.value
								);
			if( (this.password.obj.value == this.passwordRep.obj.value) ){
				this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.passwordRep),this.passwordRep,true);
				this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.password),this.password,true);
			}
		}else{
			this.showCheckResult(this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.passwordRep),this.passwordRep,false);
		}
	}
	
	this.checkCaptcha			= function( capchtaObj,actionObj ){
		this.addCaptchaObj(capchtaObj,actionObj);
		get = '&captcha='+this.captcha.obj.value;
		var jSonRequest = new Json.Remote("register.php?action=checkcaptcha"+get,{onComplete: function(mycaptcha){
			rUser.showCheckResult(mycaptcha.ret,rUser.captcha);
		}}).send();
	}
	
	this.checkUseterms			= function( useterms,actionObj ){
		this.addUsetermsObj(useterms,actionObj);
		this.showCheckResult(this.useterms.obj.checked,this.useterms);
	}
	
	this.checkActivation		= function(actForm)
	{
		get =	'&username='+actForm.username.value+
				'&affirmcode='+actForm.affirmcode.value+
				'&SID='+this.session_id;		
		var jSonRequest = new Json.Remote("register.php?action=checkActivation"+get,{onComplete: function(activation){
			if( activation.ret ){
				$('register').innerHTML = 'Vielen Dank für die Aktivierung Ihres Accounts. Sie können sich nun rechts oben einloggen!'
			}else{
				$('register').innerHTML				='	<div id="activation_content">'+
													 ''+
													 'Der Aktivierungscode ist nicht richtig! Bitte geben Sie ihn erneut ein!<br /><br />'+
													 '		<form name="register" action="#" method="post" onsubmit="rUser.checkActivation( this ); return false;">'+
													 '		<table class="register_user">'+
													 '			<tr>'+
													 '				<td class="left">Nickname:</td>'+
													 '				<td class="input"><input type="text" name="username" title="" /></td>'+
													 '			</tr>'+
													 '			<tr>'+
													 '				<td class="left">Bestätigungscode:</td>'+
													 '				<td class="input"><input type="text" name="affirmcode" title="" /></td>'+
													 '			</tr>'+
													 '			<tr>'+
													 '				<td colspan="2" align="right"><button class="register" type="submit">Abschicken</button><button class="register" type="reset">Zurücksetzen</button></td>'+
													 '			</tr>'+	
													 '		</table>'+
													 '		</form>'+
													 '	</div>';
			}
		}}).send();
	}
	
	this.sendRegister			= function()
	{
		if( this.username.validateResult == true 
		&& this.email.validateResult == true 
		&& this.password.validateResult == true 
		&& this.passwordRep.validateResult == true 
		&& this.passwordRep.value == this.password.value
		&& this.captcha.validateResult == true 
		&& this.useterms.validateResult == true )
		{
			get =	'&username='+this.username.obj.value+
					'&mail='+this.email.obj.value+
					'&password='+this.password.obj.value+
					'&SID='+this.session_id;
			var jSonRequest = new Json.Remote("register.php?action=saveregister"+get,{onComplete: function(reg){
				if( reg.ret == true ){
					$('register_header').innerHTML = 'Aktivierung ihres Accounts auf Cyberday.de';
					$('register').innerHTML			='	<div id="activation_content">'+
													 ''+
													 'Ihnen wurde eine Email mit dem Bestätigungscode zugesand. Bitte geben Sie diesen hier ein um Ihren Account freizuschalten!<br /><br />'+
													 '		<form name="activate" action="#" method="post" onsubmit="rUser.checkActivation( this ); return false;">'+
													 '		<table class="register_user">'+
													 '			<tr>'+
													 '				<td class="left">Nickname:</td>'+
													 '				<td class="input"><input type="text" name="username" title="" value="'+rUser.username.obj.value+'" /></td>'+
													 '			</tr>'+
													 '			<tr>'+
													 '				<td class="left">Bestätigungscode:</td>'+
													 '				<td class="input"><input type="text" name="affirmcode" title="" /></td>'+
													 '			</tr>'+
													 '			<tr>'+
													 '				<td colspan="2" align="right"><button class="register" type="submit">Abschicken</button><button class="register" type="reset">Zurücksetzen</button></td>'+
													 '			</tr>'+		
													 '		</table>'+
													 '		</form>'+
													 '	</div>';
				}else{
					alert('Konnte Registrierung nicht ausführen. (296)');
				}
			}}).send();			
		}else{
			alert('Sie haben noch nicht alle Felder korrekt ausgefüllt!');
		}
	}
	
	this.getActivationForm = function(){
		$('register_header').innerHTML = 'Aktivierung ihres Accounts auf Cyberday.de';
		$('register').innerHTML			='	<div id="activation_content">'+
										 ''+
										 'Ihnen wurde eine Email mit dem Bestätigungscode zugesand. Bitte geben Sie diesen hier ein um Ihren Account freizuschalten!<br /><br />'+
										 '		<form name="activate" action="#" method="post" onsubmit="return false;">'+
										 '		<table class="register_user">'+
										 '			<tr>'+
										 '				<td class="left">Nickname:</td>'+
										 '				<td class="input"><input type="text" name="username" title="" value="" /></td>'+
										 '			</tr>'+
										 '			<tr>'+
										 '				<td class="left">Bestätigungscode:</td>'+
										 '				<td class="input"><input type="text" name="affirmcode" title="" /></td>'+
										 '			</tr>'+
										 '			<tr>'+
										 '				<td colspan="2" align="right"><button class="register" onclick="rUser.checkActivation( document.activate );" type="button">Abschicken</button><button class="register" type="reset">Zurücksetzen</button></td>'+
										 '			</tr>'+		
										 '		</table>'+
										 '		</form>'+
										 '	</div>';
	}
	
	/* Hilfsmethode */
	
	this.checkValues		= function(regex,userObj)
	{
		return regex.test(userObj.obj.value);
	}
	
	this.showCheckResult	= function(proof,obj,optError)
	{
		optError = optError == undefined ? true : optError;
		if( proof && optError == false ){
			obj.actionObj.style.backgroundColor = "#FFAE00";
			
			obj.actionObj.src = this.rpath+'haken.gif';
			this.setTitle(obj,'Meldung',obj.title_text[1]);
			return obj.validateResult = false;
		}else if( proof ){
			//alert('regex passt!\nValue: '+inputObj.value+'\nRückgabe: '+rUsername.test(inputObj.value));
			obj.actionObj.style.backgroundColor = "#59BB36";
			obj.actionObj.src = this.rpath+'haken.gif';
			this.setTitle(obj,'Meldung',obj.title_text[2]);
			return obj.validateResult = true;
		}else{
			//alert('regex passt ned!\nValue: '+inputObj.value+'\nRückgabe: '+rUsername.test(inputObj.value));
			obj.actionObj.style.backgroundColor = "#711615";
			obj.actionObj.src = this.rpath+'x.gif';
			this.setTitle(obj,'Meldung',obj.title_text[0]);
			return obj.validateResult = false;
		}
	}
}


