var valid_user;
var checked_user;
var valid_pass;
var valid_pass_strength;
var checked_pass;
var valid_pass_b;
var checked_pass_b;
var valid_email;
var checked_email;
var valid_group;
var checked_group;
var timer;
var checker;

function loading_image(url) {
	objImage = new Image();
	objImage.src = url;
}

function xmlhttp_init() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		var xmlhttp = new XMLHttpRequest();
	}
	else {
		// code for IE6, IE5
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

function get_translation(url_pre,t_title,t_item) {
	xmlhttp=xmlhttp_init();
	xmlhttp.open("POST",url_pre+"wms/admin/admin_ajax.php",false);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("ajax_title=translation&title="+t_title+"&item="+t_item);
	return xmlhttp.responseText;
}

function showhide(files,image) {
	if (document.getElementById(files).style.display == 'block') {
		document.getElementById(files).style.display = 'none';
		document.getElementById(image).setAttribute('class', 'files_closed');
	}
	else {
		document.getElementById(files).style.display = 'block';
		document.getElementById(image).setAttribute('class', 'files_open');
	}
}

function password_strength(pass) {
	var score = 0;
	
	if(pass.length > 0) {
		score = score + pass.length;
	}
	if(pass.match(/[a-z]/)) {
		score = score + 1;
	}
	if(pass.match(/[A-Z]/)) {
		score = score + 5;
	}
	if(pass.match(/\d+/)) {
		score = score + 5;
	}
	if(pass.match(/(.*[0-9].*[0-9].*[0-9])/)) {
		score = score + 5;
	}
	if(pass.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) {
		score = score + 5;
	}
	if(pass.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {
		score = score + 5;
	}
	if(pass.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
		score = score + 2;
	}
	if(pass.match(/([a-zA-Z])/) && pass.match(/([0-9])/)) {
		score = score + 2;
	}
	if(pass.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {
		score = score + 2;
	}
	
	score = score * 2;
	if(score > 100) {
		score = 100;
	}
	return score;
}

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=='') {
			style.backgroundColor='#ff9999';
			alert(alerttxt);return false;
		} else {
			style.backgroundColor='';
			return true;
		}
	}
}

function validate_user(input, t_empty, t_exists, t_too_short, t_too_long, user, timeout) {
	checked_user = true;
	clearTimeout(timer);
	timer = setTimeout(function() {
		document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
		var field = document.getElementById(input);
		if(field.value == '') {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_empty;
			valid_user = false;
			return false;
		} else if(field.value.length < 4) {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
			valid_user = false;
			return false;
		} else if(field.value.length > 16) {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_long;
			valid_user = false;
			return false;
		} else {
			var xmlhttp = xmlhttp_init();
			xmlhttp.open("POST", "../../../wms/admin/admin_forms/forms_users_submit.php", true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					if (xmlhttp.responseText) {
						if (xmlhttp.responseText == 'exists') {
							field.style.borderColor = '#ff0000';
							document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_exists;
							valid_user = false;
							return false;
						} else {
							field.style.borderColor = '#00ff00';
							document.getElementById('error_users_'+ input).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
							valid_user = true;
							return true;
						}
					}
				}
			}
		};
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("form_title=user&form_type=check_user&user="+ field.value+ "&current="+ user);
	}, timeout);
}

function validate_set_pass(form, inputa, inputb, t_empty, t_re_empty, t_no_match, t_too_short, t_not_strong, p_strength, timeout) {
	checked_pass = true;
	var fielda = document.getElementById(inputa);
	var fieldb = document.getElementById(inputb);
	if(form == 'edit' && fielda.value == '' && fieldb.value == '') {
		score = p_strength;
	} else {
		score = password_strength(fielda.value);
	}
	document.getElementById('pass_strength').style.width = score+ 'px';
	document.getElementById('pass_strength').innerHTML = '<p>'+ score+ '%</p>';
	document.getElementById('set_pass_strength').value = ''+ score+ '';
	if(score >= 50) {
		document.getElementById('pass_strength_bar').style.borderColor = '#00ff00';
		document.getElementById('error_users_set_pass_strength').innerHTML = '';//<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
		valid_pass_strength = true;
	} else {
		document.getElementById('pass_strength_bar').style.borderColor = '#ff0000';
		document.getElementById('error_users_set_pass_strength').innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_not_strong;
		valid_pass_strength = false;
	}
	clearTimeout(timer);
	timer = setTimeout(function() {
		document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
		if(fieldb.value != '' && fieldb.value != null) {
			if(fielda.value == null || fielda.value == '') {
				fielda.style.borderColor = '#ff0000';
				document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_empty;
				valid_pass = false;
				if(fieldb.value.length < 6 ) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
					valid_pass_b = false;
				} else if(fieldb.value != fielda.value) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_no_match;
					valid_pass_b = false;
				} else {
					fieldb.style.borderColor = '#00ff00';
					document.getElementById('error_users_'+ inputb).innerHTML = '';//<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
					valid_pass_b = true;
				}
				return false;
			} else if(fielda.value.length < 6) {
				fielda.style.borderColor = '#ff0000';
				document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
				valid_pass = false;
				if(fieldb.value.length < 6 ) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
					valid_pass_b = false;
				} else if(fieldb.value != fielda.value) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_no_match;
					valid_pass_b = false;
				} else {
					fieldb.style.borderColor = '#00ff00';
					document.getElementById('error_users_'+ inputb).innerHTML = '';//<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
					valid_pass_b = true;
				}
				return false;
			} else if(score < 50) {
				fielda.style.borderColor = '#ff0000';
				document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />';
				valid_pass = false;
				if(fieldb.value.length < 6 ) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
					valid_pass_b = false;
				} else if(fieldb.value != fielda.value) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_no_match;
					valid_pass_b = false;
				} else {
					fieldb.style.borderColor = '#00ff00';
					document.getElementById('error_users_'+ inputb).innerHTML = '';//<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
					valid_pass_b = true;
				}
				return false;
			} else if(fielda.value != fieldb.value) {
				fielda.style.borderColor = '#00ff00';
				document.getElementById('error_users_'+ inputa).innerHTML = '';//<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
				valid_pass = true;
				if(fieldb.value.length < 6 ) {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
					valid_pass_b = false;
				} else {
					fieldb.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_no_match;
					valid_pass_b = false;
				}
				return false;
			} else {
				fielda.style.borderColor = '#00ff00';
				fieldb.style.borderColor = '#00ff00';
				document.getElementById('error_users_'+ inputa).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
				document.getElementById('error_users_'+ inputb).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
				valid_pass = true;
				valid_pass_b = true;
				return true;
			}
		} else {
			fieldb.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_re_empty;
			valid_pass_b = false;
			if(fielda.value == null || fielda.value == '') {
				if(form == 'edit') {
					fielda.style.borderColor = '#00ff00';
					fieldb.style.borderColor = '#00ff00';
					document.getElementById('error_users_'+ inputa).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
					document.getElementById('error_users_'+ inputb).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
					valid_pass = true;
					valid_pass_b = true;
					valid_pass_strength = true;
				} else {
					fielda.style.borderColor = '#ff0000';
					document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_empty;
					valid_pass = false;
				}
			} else if(fielda.value.length < 6) {
				fielda.style.borderColor = '#ff0000';
				document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ t_too_short;
				valid_pass = false;
			} else if(score < 50) {
				fielda.style.borderColor = '#ff0000';
				document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />';
				valid_pass = false;
			} else {
				fielda.style.borderColor = '#00ff00';
				document.getElementById('error_users_'+ inputa).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
				valid_pass = true;
				
			}
		}
	}, timeout);
}

function validate_pass(inputa, inputb, texta, textb, timeout) {
	checked_pass_b = true;
	clearTimeout(timer);
	timer = setTimeout(function() {
		var fielda = document.getElementById(inputa);
		var fieldb = document.getElementById(inputb);
		document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
		if(fieldb.value == null || fieldb.value == '') {
			if(fielda.value != '') {
				fielda.style.borderColor = '#00ff00';
				document.getElementById('error_users_'+ inputa).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
				valid_pass = true;
			}
			fieldb.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ texta;
			valid_pass_b = false;
			return false;
		} else if(fielda.value != fieldb.value) {
			fielda.style.borderColor = '#ff0000';
			fieldb.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ inputb).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ textb;
			document.getElementById('error_users_'+ inputa).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />';
			valid_pass = false;
			valid_pass_b = false;
			return false;
		} else {
			fielda.style.borderColor = '#00ff00';
			fieldb.style.borderColor = '#00ff00';
			document.getElementById('error_users_'+ inputb).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
			document.getElementById('error_users_'+ inputa).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
			valid_pass = true;
			valid_pass_b = true;
			return true;
		}
	}, timeout);
}

function validate_email(input, texta, textb, timeout) {
	checked_email = true;
	clearTimeout(timer);
	timer = setTimeout(function() {
		var field = document.getElementById(input);
		document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
		apos = field.value.indexOf('@');
		dotpos = field.value.lastIndexOf('.');
		length = field.value.length;
		if(field.value == null || field.value == '') {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ texta;
			valid_email = false;
			return false;
		} else if(apos < 1 || dotpos - apos < 2 || length - dotpos < 3) {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ textb;
			valid_email = false;
			return false;
		} else {
			field.style.borderColor = '#00ff00';
			document.getElementById('error_users_'+ input).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
			valid_email = true;
			return true;
		}
	}, timeout);
}

function validate_group(input,text) {
	checked_group = true;
	var field = document.getElementById(input);
	document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
	if(field.value == null || field.value == '') {
		field.style.borderColor='#ff0000';
		document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ text;
		valid_group = false;
		return false;
	} else {
		field.style.borderColor='#00ff00';
		document.getElementById('error_users_'+ input).innerHTML = ''; //<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />
		valid_group = true;
		return true;
	}
}

function validate_form_users_add() {
	if(valid_user && valid_pass && valid_pass_b && valid_email && valid_group) {
		document.getElementById('users_add_submit').disabled = false;
		document.getElementById('users_add_submit').style.color = '#000000';
		checker = setTimeout('validate_form_users_add(\'users_add_submit\')', 100);
	} else {
		document.getElementById('users_add_submit').disabled = true;
		document.getElementById('users_add_submit').style.color = '#666666';
		checker = setTimeout('validate_form_users_add(\'users_add_submit\')', 100);
	}
}
function validate_form_users_edit(form, user, p_strength, user_empty, user_exists, user_too_short, user_too_long, pass_empty, pass_re_empty, pass_no_match, pass_too_short, pass_not_strong, email_empty, email_invalid, group_not_selected) {
	if(!checked_user) {
		validate_user('user', user_empty, user_exists, user_too_short, user_too_long, user, 0);
	} else if(!checked_pass) {
		validate_set_pass(form, 'set_pass', 'set_pass_b', pass_empty, pass_re_empty, pass_no_match, pass_too_short, pass_not_strong, p_strength, 0);
	} else if(!checked_email) {
		validate_email('email', email_empty, email_invalid, 0);
	} else if(!checked_group) {
		validate_group('group', group_not_selected, 0);
	}
	if(valid_user && valid_pass && valid_pass_strength && valid_pass_b && valid_email && valid_group) {
		document.getElementById('users_edit_submit').disabled = false;
		document.getElementById('users_edit_submit').style.color = '#000000';
		checker = setTimeout(function() {
			validate_form_users_edit(form, user, p_strength, user_empty, user_exists, user_too_short, user_too_long, pass_empty, pass_re_empty, pass_no_match, pass_too_short, pass_not_strong, email_empty, email_invalid, group_not_selected);
		}, 100);
	} else {
		document.getElementById('users_edit_submit').disabled = true;
		document.getElementById('users_edit_submit').style.color = '#666666';
		checker = setTimeout(function() {
			validate_form_users_edit(form, user, p_strength, user_empty, user_exists, user_too_short, user_too_long, pass_empty, pass_re_empty, pass_no_match, pass_too_short, pass_not_strong, email_empty, email_invalid, group_not_selected);
		}, 100);
	}
}


function validate_form_users_editsdf(thisform) {
	with(thisform) {
		if(validate_required(user,'Naam moet worden ingevuld.')==false) {
			user.focus();
			return false;
		}
		if(validate_required(set_pass,'Wachtwoord moet worden ingevuld.')==false) {
			set_pass.focus();
			return false;
		}
		if(validate_required(set_pass_b,'Wachtwoord (herhaal) moet worden ingevuld.')==false) {
			set_pass_b.focus();
			return false;
		}
		if(validate_pass(set_pass,set_pass_b,'Wachtwoorden komen niet overeen')==false) {
			set_pass.value='';
			set_pass_b.value='';
			set_pass.focus();
			return false;
		}
		if(validate_email(email,'Geen geldig email adres.')==false) {
			email.focus();
			return false;
		}
		if(validate_required(group,'Groep moet worden geselecteerd.')==false) {
			group.focus();
			return false;
		}
	}
}

function hide(id) {
	document.getElementById(id).style.display = 'none';
}

function hide_loading(id) {
	hide(id);
	show_scroll();
}

function hide_scroll() {
	document.body.className = 'loading';
}

function show_scroll() {
	document.body.className = 'normal';
}

function show(id) {
	document.getElementById(id).style.display = 'block';
}

function fade_in(id) {
	show(id);
	
	clearTimeout(timer);
	timer = setTimeout(function() {
		var field = document.getElementById(id);
		document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/loading_mini.gif" alt="Loading, please wait..." />';
		apos = field.value.indexOf('@');
		dotpos = field.value.lastIndexOf('.');
		length = field.value.length;
		if(field.value == null || field.value == '') {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ texta;
			valid_email = false;
			return false;
		} else if(apos < 1 || dotpos - apos < 2 || length - dotpos < 3) {
			field.style.borderColor = '#ff0000';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_no_16.png" alt="Error" />'+ textb;
			valid_email = false;
			return false;
		} else {
			field.style.borderColor = '#00ff00';
			document.getElementById('error_users_'+ input).innerHTML = '<img class="form_validation" src="../../../wms/images/icon_yes_16.png" alt="OK" />';
			valid_email = true;
			return true;
		}
	}, timeout);
	
	
	
	
	
	fade_value = 0;
	fade_id = setInterval(f_fade_in(id, fade_value), 100);
	setTimeout(function() {
		clearInterval(fade_id);
	}, 2000);
}

function f_fade_in(id, fade_value) {
	if(fade_value < 100) {
		fade_value += 100;
	}
	document.getElementById(id).style.filter = 'alpha(opacity=' + fade_value + ')';
	if(fade_value == 100) {
		fade_value_b = '1';
	} else {
		fade_value_b = '0.' + fade_value;
	}
	document.getElementById(id).style.opacity = fade_value_b;
	document.getElementById(id).style.MozOpacity = fade_value_b;
	document.getElementById(id).style.KhtmlOpacity = fade_value_b;
}

function fade_out(id) {
	fade_value = 100;
	fade_id = setInterval(function(fade_value) {
		if(fade_value > 0) {
			fade_value -= 5;
		} else {
			clearInterval(fade_id);
		}
		fade_value_b = fade_value;
		document.getElementById(id).style.filter = 'alpha(opacity=' + fade_value_b + ')';
		if(fade_value == 100) {
			fade_value_b = '1';
		} else {
			fade_value_b = '0.' + fade_value;
		}
		document.getElementById(id).style.MozOpacity = fade_value_b;
		document.getElementById(id).style.KhtmlOpacity = fade_value_b;
		document.getElementById(id).style.opacity = fade_value_b;
	}, 200);
	hide(id);
}

function dim_window(content, showhide) {
	if(showhide == 'show') {
		show('dim_background');
		document.getElementById('dim_content').innerHTML = ''+ content;
	} else {
		document.getElementById('dim_content').innerHTML = content;
		hide('dim_background');
	}
}
