<commit_before>var sakai = sakai || {};

sakai.newaccount = function(){
	
	checkingUserExists = false;
	
	$("input").keypress(function (e) {
		if (e.which == 13){
			doSave();
		}
	});
	
	$("#save_account").bind("click", function(ev){
		doSave();
	});
	
	resetErrorFields = function(){
		$("#firstname_label").css("color","");
		$("#lastname_label").css("color","");
		$("#email_label").css("color","");
		$("#username_label").css("color","");
		$("#password_label").css("color","");
		$("#password_repeat_label").css("color","");
		$("#uword_label").css("color","");
	}
	
	setErrorField = function(id){
		$("#" + id).css("color","rgb(250,0,0)");
	}
	
	setError = function(string){
		$("#warning1").show();
		$("#spacer").show();
		$("#alert_id1").html(string);
	}
	
	hideError = function(){
		$("#warning1").css("height","1em");
		$("#warning1").hide();
		$("#spacer").hide();
	}
	
	$("#hideSuccess").bind("click", function(ev){
		$("#saveinfo1").hide();
		$("#spacer").hide();
	});
	
	$("#hideError").bind("click", function(ev){
		hideError();
	})

	$("#checkUserName").bind("click", function(){
		checkingUserExists = true;
		$("#saveinfo1").hide();
		$("#spacer").hide();
		resetErrorFields();
		hideError();
		checkUserName();
		//doCreateSite();
	});
	
	checkUserName = function(){
		
		//check empty
		$("#saveinfo1").hide();
		$("#spacer").hide();
		
		var value = $("#username").attr("value");
		if (!value || value.replace(/ /g, "") == ""){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username shouldn't be empty");
			return false;
		}
		
		//check spaces
		if (value.indexOf(" ") != -1){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username shouldn't contain spaces");
			return false;
		}
		
		//check length of username
		if (value.length < 3){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username should be at least 3 characters long");
			return false;
		}
		
		//check existence
		sdata.Ajax.request({
            httpMethod: "GET",
            url: "/rest/user/" + value + "/exists?sid=" + Math.random(),
            onSuccess: function(data){
				resetErrorFields();
				setErrorField("username_label");
				setError("The username already exists");
				return false;
			}, 
			onFail : function(data){
				if (checkingUserExists){
					resetErrorFields();
					$("#username_label").css("color","#2E8A28");
					return true;
				} else {
					doCreateSite();
				}	
			}	
		});
		
	}
	
	doCreateSite = function(){
		var firstname = $("#firstname").attr("value");
		var lastname = $("#lastname").attr("value");
		var email = $("#email").attr("value");
		var username = $("#username").attr("value");
		var password = $("#password").attr("value");
		
		var data = {"userType":"default","firstName":firstname, "lastName":lastname, "email":email, "password":password, "eid": username};
		sdata.Ajax.request({
        	url :"/rest/user/new",
        	httpMethod : "POST",
        	postData : data,
        	contentType : "application/x-www-form-urlencoded",
            onSuccess : function(data) {
				
				var resp = eval('(' + data + ')');
				var uuid = resp.uuid;
				
				// Save profile information for this new user
				
				var k = ["firstName","lastName","email"];
				var a = ["u","u","u"];
				
				// Construct contactinfo string
				
				var v = [$("#firstname").val(),$("#lastname").val(),$("#email").val()];
				
				var url = "/rest/patch/f/_profiles/" + uuid + "/profile.json";
				var tosend = {"k":k,"a":a,"v":v};
				
				sdata.Ajax.request({
		        	url : url,
		        	httpMethod : "POST",
		        	postData : tosend,
		        	contentType : "application/x-www-form-urlencoded",
		            onSuccess : function(data) {
						
						$("#saveinfo1").show();
						$("#spacer").show();
						$("#check_availability").hide();
						$("#save_account").hide();
						$("#cancel_button").text("Go Back");
						
					},
					onFail: function(data){
						resetErrorFields();
						setError("<b>Oops</b> A problem has occured. Please try again");
					}
				});
				
			},
			onFail: function(data){
				resetErrorFields();
				setError("<b>Oops</b> A problem has occured. Please try again");
			}<commit_msg>SAK-15560<commit_after>var sakai = sakai || {};

sakai.newaccount = function(){
	
	checkingUserExists = false;
	
	$("input").keypress(function (e) {
		if (e.which == 13){
			doSave();
		}
	});
	
	$("#save_account").bind("click", function(ev){
		doSave();
	});
	
	resetErrorFields = function(){
		$("#firstname_label").css("color","");
		$("#lastname_label").css("color","");
		$("#email_label").css("color","");
		$("#username_label").css("color","");
		$("#password_label").css("color","");
		$("#password_repeat_label").css("color","");
		$("#uword_label").css("color","");
	}
	
	setErrorField = function(id){
		$("#" + id).css("color","rgb(250,0,0)");
	}
	
	setError = function(string){
		$("#warning1").show();
		$("#spacer").show();
		$("#alert_id1").html(string);
	}
	
	hideError = function(){
		$("#warning1").css("height","1em");
		$("#warning1").hide();
		$("#spacer").hide();
	}
	
	$("#hideSuccess").bind("click", function(ev){
		$("#saveinfo1").hide();
		$("#spacer").hide();
	});
	
	$("#hideError").bind("click", function(ev){
		hideError();
	})

	$("#checkUserName").bind("click", function(){
		checkingUserExists = true;
		$("#saveinfo1").hide();
		$("#spacer").hide();
		resetErrorFields();
		hideError();
		checkUserName();
		//doCreateSite();
	});
	
	checkUserName = function(){
		
		//check empty
		$("#saveinfo1").hide();
		$("#spacer").hide();
		
		var value = $("#username").attr("value");
		if (!value || value.replace(/ /g, "") == ""){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username shouldn't be empty");
			return false;
		}
		
		//check spaces
		if (value.indexOf(" ") != -1){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username shouldn't contain spaces");
			return false;
		}
		
		//check length of username
		if (value.length < 3){
			resetErrorFields();
			setErrorField("username_label");
			setError("The username should be at least 3 characters long");
			return false;
		}
		
		//check existence
		sdata.Ajax.request({
            httpMethod: "GET",
            url: "/rest/user/" + value + "/exists?sid=" + Math.random(),
            onSuccess: function(data){
				resetErrorFields();
				setErrorField("username_label");
				setError("The username already exists");
				return false;
			}, 
			onFail : function(data){
				if (checkingUserExists){
					resetErrorFields();
					$("#username_label").css("color","#2E8A28");
					return true;
				} else {
					doCreateSite();
				}	
			}	
		});
		
	}
	
	doCreateSite = function(){
		var firstname = $("#firstname").attr("value");
		var lastname = $("#lastname").attr("value");
		var email = $("#email").attr("value");
		var username = $("#username").attr("value");
		var password = $("#password").attr("value");
		
		var data = {"userType":"default","firstName":firstname, "lastName":lastname, "email":email, "password":password, "eid": username};
		sdata.Ajax.request({
        	url :"/rest/user/new",
        	httpMethod : "POST",
        	postData : data,
        	contentType : "application/x-www-form-urlencoded",
            onSuccess : function(data) {
				
				var resp = eval('(' + data + ')');
				var uuid = resp.uuid;
				
				// Save profile information for this new user
				
				var k = ["firstName","lastName","email"];
				var a = ["u","u","u"];
				
				// Construct contactinfo string
				
				var v = [$("#firstname").val(),$("#lastname").val(),$("#email").val()];
				
				// Construct URL using SHA algorithm
				
				var hashed = sha1Hash(uuid);
				var folder1 = hashed.substring(0,2).toUpperCase();
				var folder2 = hashed.substring(2,4).toUpperCase();
				
				var url = "/rest/patch/p/" + folder1 + "/" + folder2 + "/" + uuid + "/profile.json";
				var tosend = {"k":k,"a":a,"v":v};
				
				sdata.Ajax.request({
		        	url : url,
		        	httpMethod : "POST",
		        	postData : tosend,
		        	contentType : "application/x-www-form-urlencoded",
		            onSuccess : function(data) {
						
						$("#saveinfo1").show();
						$("#spacer").show();
						$("#check_availability").hide();
						$("#save_account").hide();
						$("#cancel_button").text("Go Back");
						
					},
					onFail: function(data){
						resetErrorFields();
						setError("<b>Oops</b> A problem has occured. Please try again");
					}
				});
				
			},
			onFail: function(data){
				resetErrorFields();
				setError("<b>Oops</b> A problem has occured. Please try again");
			}
{'fraction_non_alphanumeric': 0.2205104621752127, 'fraction_numerical': 0.0068981375028742235, 'mean_word_length': 3.192289156626506, 'pattern_counts': {'":': 18, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '937430', 'n_tokens_mistral': 3363, 'n_tokens_neox': 2766, 'n_words': 516}