var maceServiceURL = MACEConstants.componentsURL + "user/php/register.php";
var maceVerifyURL = MACEConstants.componentsURL + "user/php/verify.php";
var maceCheckURL = MACEConstants.componentsURL + "user/php/check_existing_username.php";
var maceCheckEmailURL = MACEConstants.componentsURL + "user/php/check_existing_email.php";
// just quick
function RegisterApp() {

	RegisterApp.baseConstructor.call(this, null, "registerApp"); 
	this.componentController = new ComponentController();
	
	// Registers this app itself to subscribe to events
		this.componentController.register(this);

		this.loginComponent = new LoginComponent($("#loginComponent"));
		this.componentController.register(this.loginComponent);
		
		this.componentController.init();
		this.initDomElements();
}
RegisterApp.extend(Component);

RegisterApp.prototype.initDomElements=function(){
	this.$detailsContainer=$("#detailsContainer");
	this.$detailsFrame=$("#detailsFrame");
	
	var _this=this;
	
	$("#privacyLink").click(
		function(){
		
			_this.showTerms("privacy");
			return false;
		}
	);
	//
	$("#usageLink").click(
		function(){
			_this.showTerms("usage");
			return false;
		}
	);
}
/***********************************************************************/

RegisterApp.prototype.showTerms=function(which){

	if(which == "privacy")
		var url=MACEConstants.rootURL + "pages/register/privacy.php";
	if(which == "usage")
		var url=MACEConstants.rootURL + "pages/register/usage.php";

	var _this=this;
	console.log("hier url::"+url);
	this.$detailsContainer.modal({
		onOpen: function(dialog) {
			dialog.overlay.show();
			dialog.container.show();
			dialog.data.show();
			_this.$detailsFrame.attr("src",url);

		},

		onClose: function (dialog) {

			
			_this.$detailsFrame.attr("src",null);
	        $.modal.close(); 
		},
		overlayId:'detailsModalOverlay',
		containerId:'detailsModalContainer'
	});
}

/***********************************************************************/
function register() {
	console.log("registering...");
	var o={};
	o.userName=$("#username").val();
	o.firstName=$("#firstname").val();
	o.lastName=$("#lastname").val();
	o.password=$("#password").val();
	o.email=$("#email").val();
		
	$.getJSON(maceServiceURL, o,
		function (results) {
			if(results.success)
				onRegistrationResult(results);
			else
				onRegistrationError(results.message);
		}, 
		onRegistrationError
	);
	
	displayMessage("loading", "Registering...");
}

function onRegistrationResult(d){
	//displayActivity(false);
	//this should be themessage when emailVerification is done
	//var txt="<p>Welcome "+d.userName+" <br />Registration was successful. <br />An Email has been sent to you with your Activation Link. Please follow the instructions there.</p>";
	//displayMessage("confirmation", txt);	
	
	displayMessage("confirmation", "Registration was successful. <br/>An Email has been sent to you with your Activation Link. Please follow the instructions there.<br/><br/><span class='adviceMessage'>Without these Activation you won't be able to login ! <br/><br/>Please also check your Junk Folder!</span>");	
	$("#reg_form").hide();
}

function onRegistrationError(msg){
	//displayMessage("error", "Some error ocurred. Please eat the manual.");
	displayMessage("error", msg);
	console.error("Error in calling registration server.");
}

function displayMessage(className, text){
	if(!className || !text){
		// hide
		$("#messageField").hide();
	} else {
		// show advice
		$("#messageField").show();	
		$("#messageField").html("<div class='"+className+"Message'>"+text+"</div>");
	}
}


$(document).ready(function() {

	$(document).bind("keydown",function(event){
		lastKeyPressCode = event.keyCode;

		switch(event.keyCode) {


			case 13:

				checkValues();
				break;


			default:
			
				break;
		}
	});
});
/* Tinas checking methods */


	/***********************************************************************/
function checkValues(){
		flag=-1;
	
	if(!check_fname()){	
		flag=1;
	}
   else if(!check_mail()){
			flag=1;
			
	}
	else if(!a_pass_check()){
			flag=1;
	}
	else if(!pass_double()){
			flag=1;
	}
	else if(!check_captcha()){
			flag=1;
	}
	else if(!check_accept()){
			flag=1;
	}
	

	if(flag==-1){
		displayMessage();
		check_ex_uname(1);
		//register();
	} 
}
/***********************************************************************/
function check_accept(){
		if($("#accept")[0].checked != true){
			displayMessage("advice","You have to accept MACE Terms of Service and MACE Privacy Policy ");	
			return false;
		}
		else{
			displayMessage();
			return true;
			}
		
}
/***********************************************************************/
function a_pass_check(){
	//var pass= document.form.password.value;	

	var pass= $("#password").val();	  
	if(pass.length <6){
		displayMessage("advice","Your Password should contain 6 characters or more!");	
		return false;
	}
	else{
		displayMessage();
		return true;
	}	
		
}

		
/***********************************************************************/	


function pass_double(){
	var pass=  $("#password2").val();	  
	if(pass !=  $("#password").val() ){
		displayMessage("advice","Your Passwords do not match!");
		return false;
	}
	else{
		displayMessage();
		return true;
		}	
		
}

		
/***********************************************************************/

function check_mail(){
	var mail=$("#email").val();
	
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!(filter.test(mail))) { 
	      	displayMessage("advice","Please enter a valid email address!"+mail);	
	}
	else{
		displayMessage();
	   //here by aloe anfragen ob email noch frei
	   	$.getJSON(maceCheckEmailURL, {email:$("#email").val()},
	   		function (results) {
	   			if(!results.available)
	   				displayMessage("advice",results.message);	
	   			else{
	   				return true;
	   			}
	   		}
	   		//onRegistrationError
	   	);
		
		return true;
	}

}
/***********************************************************************/

function check_fname(){

	if($("#firstname").val() =="" || $("#lastname").val()==""){	
		displayMessage("advice","Please submit your fullname!");	
		return false;
	}
	else{

		displayMessage();
		return true;
	}

}

/***********************************************************************/

function check_ex_uname(mode){

    var username=$("#username").val();
    
	if(username =="" ){	
		displayMessage("advice","Please submit your loginname!");	
		return false;
	}
	else if(!containsAlphabets(username) ){
        displayMessage("advice","usernames should only contain A-Za-z0-9._ ");	
	    return false;
    }
	else{
		displayMessage();
		$.getJSON(maceCheckURL, {username:username},
			function (results) {
				if(!results.success)
					displayMessage("advice",results.message);	
				else{
					if(results.exists)
						displayMessage("advice",results.message);	
					if(mode==1)
						register();
				}
			}
			//onRegistrationError
		);
		return true;
	}
	

}
/***********************************************************************/
function containsAlphabets(checkString) {
        var tempString="";
        var regExp = /^[A-Za-z0-9._]$/;
    
        if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
              return false;
            }
          }
        }
        else
        {
          return false;
        }
        return true;
}
/***********************************************************************/

function check_captcha(){

	if($("#captcha").val()==""){
		displayMessage("advice","Please type the captcha code");	
		return false;
	}
	else{
		displayMessage();
		return true;	
	}


}
/***********************************************************************/


