/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Global variables */
var popoutMenu=0;
var expandedCourses=0;

/* Functions */
$(document).ready(function(){
	$('#expandedCourses').hide();
	$('#popoutMenu').hide();
	$('#passwordChanger').hide();


	/* Check courses in expanded menu */
	$('li[class="expanded"]').click(function(){
		if($(this).hasClass('checkedCourse')){
			$(this).removeClass('checkedCourse');
		}
		else{
			$(this).addClass("checkedCourse");
		}
	});

	/* Blurs body and opens password changer */
	$('li[name="changePwd"]').click(function(e){
		e.stopPropagation();
		$('#popoutMenu').stop(false, false).slideUp(300);
		popoutMenu=0;
	    $('body *').not('#passwordChanger, #passwordChanger *').animate({
		opacity:0.4}, 400);
		$('#passwordChanger').fadeIn(300);
		$('#passwordChanger').css('z-index', 3000);
	});
	
	$('body').click(function(e){
		$('#formContainer').fadeOut(300);
		$('#passwordChanger').fadeOut(300);
		$('body *').animate({
		opacity:1}, 400);
		$('#changePassword')[0].reset();
	});
	
	$('#passwordChanger').click(function(e){
		e.stopPropagation();
	});
	
	$('#formContainer').click(function(e){
		e.stopPropagation();
	});
	
	$('#popoutLink').click(function(e){
		if(popoutMenu==0){
			$('#popoutMenu').stop(false, true).slideDown(300);
			popoutMenu=1;
			e.stopPropagation();
		// FIXA SÅ MENYN INTE KOMMER UPP IGEN
		}
		else if(popoutMenu==1){
			$('#popoutMenu').stop(false, false).slideUp(300);	
			popoutMenu=0;
			e.stopPropagation();
		}
	});
	
	$('#loginLink').click(function(e){
		var target = $('#formContainer');
		if(target.is(":hidden")){
			target.fadeIn(300);
			$('#username').focus();
			e.stopPropagation();
		}
		else{
			target.fadeOut(300);
		}
		
	});
});

window.onkeyup = function(event){
	if(event.keyCode==27){
		$('#formContainer').fadeOut(300);
		$('#passwordChanger').fadeOut(300);
		$('body *').animate({
		opacity:1}, 400);
		$('#changePassword')[0].reset();
	}
}

/* Closing password changer and reactivates body */
function cancelPasswordChange(){
	$('#passwordChanger').fadeOut(300);
	$('body *').animate({
	opacity:1}, 400);
	$('#changePassword')[0].reset();
}


/* Expand/Collapse courses in popout menu */
function expandCourses(){
	if(expandedCourses==0){
	$('#expandedCourses').stop(false, true).slideDown(300);
	expandedCourses=1;
	}
	else if(expandedCourses==1){
		$('#expandedCourses').stop(false, false).slideUp(300);	
		expandedCourses=0;
	}
}



/* Password validation */
var acceptedNewPwd = false;
var acceptedRepeatedPwd = false;

function emptyField(inputId){
	var id = document.getElementById(inputId);
	if(id.value.match(/^\s*$/)){
		return true;
	}
	else return false;
}

/* Checks input field for invalid characters */
function pwdValidCharacters(inputId){
	var id = document.getElementById(inputId);
	if(id.value.match(/^[A-Öa-ö0-9 _]*[A-Öa-ö0-9][A-Öa-ö0-9 _]*$/)){
		return true;
	}
	else return false;
}

/* Checks input field for minimum amount of characters(6) */
function pwdMininumValue(inputId){
	var id = document.getElementById(inputId);
	if(id.value.length > 5){
		return true;
	}
	else return false;
}

/* Checks if newPwd and repeatPwd are the same */
function pwdMatch(){
	var firstPwd = document.getElementById("newPwd").value;
	var secondPwd = document.getElementById("repeatPwd").value;
	if(firstPwd == secondPwd){
		return true;
	}
	else return false;
}

function validNewPwd(){
	var empty = emptyField("newPwd");
	var validchar = pwdValidCharacters("newPwd");
	var minimumchar = pwdMininumValue("newPwd");

	if (empty==false && validchar==true && minimumchar==true){
		document.getElementById("newPwd").className="Valid";
		$('.errorMessage[name="newPwd"]').animate({opacity:0}, 0);
		acceptedNewPwd=true;
	}
	else if(empty==true){
		acceptedNewPwd=false;
		document.getElementById("newPwd").className="";
		$('.errorMessage[name="newPwd"]').animate({opacity:0}, 0);
	}
	else if(empty==false && validchar==false || minimumchar==false){
		document.getElementById("newPwd").className="notValid";
		$('.errorMessage[name="newPwd"]').animate({opacity:1}, 0);
		acceptedNewPwd=false;
	}
}

function validRepeatedPwd(){
	var empty = emptyField("repeatPwd");
	var match = pwdMatch("repeatPwd");
	
	if(validNewPwd){
		if(empty==true){
			document.getElementById("repeatPwd").className="";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:0}, 0);
			acceptedRepeatedPwd=false;
		}
		else if(empty==false && match==false){
			document.getElementById("repeatPwd").className="notValid";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:1}, 0);
			acceptedRepeatedPwd=false;
		}
		else if(empty==false && match==true){
			document.getElementById("repeatPwd").className="Valid";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:0}, 0);
			acceptedRepeatedPwd=true;
		}
	}
	else{
		document.getElementById("repeatPwd").className="";
	}
}

/* baseURL skickas med som parameter */
function confirm(baseURL){
	if(acceptedNewPwd && acceptedRepeatedPwd){
		changePwd(baseURL);
	}
	else{
		alert("WRONG WRONG WRONG!! FILL OUT THE FORM AGAIN YOU FKIN PIECE OF SHIT!");
		return false;
	}
}

function changePwd(baseURL)
{
	var currentPwd = document.getElementById("currentPwd").value;
	var newPwd = document.getElementById("newPwd").value;
	var hintPwd = document.getElementById("hintPwd").value;

	$.ajax({
		type: 'POST',
		url: baseURL+'ajax/pwdchange',
		data: { 
			currentPwd : currentPwd,
			newPwd : newPwd,
			hintPwd : hintPwd
		},
		success:  function(result){
			var resultObject = JSON.parse(result);
			if(resultObject.status==true){
				alert('Password change is successful!');
				$('#formContainer').fadeOut(300);
				$('#passwordChanger').fadeOut(300);
				$('body *').animate({
				opacity:1}, 400);
				$('#changePassword')[0].reset();
			}
			else{
				alert('Wrong password!');
			}
		}
	});
}