
var signUpR;
var logOnR;

window.onload = function () {
	signUpR = document.getElementById('signUpResponse');
	logOnR = document.getElementById('logOnResponse');
	
	document.getElementById('logOn').action = "javascript:logOnSubmit(document.getElementById('logOn'))";
	document.getElementById('signUp').action = "javascript:signUpSubmit(document.getElementById('signUp'))";
	
	var closeBox = document.createElement('div');
	closeBox.innerHTML = "X";
	closeBox.style.backgroundColor = "#ff6600";
	closeBox.style.color = "white";
	closeBox.style.display = "inline";
	closeBox.style.position = "absolute";
	closeBox.style.right = "-3";
	closeBox.style.top = "-3";
	closeBox.style.cursor = "pointer";
	closeBox1 = closeBox.cloneNode(true);
	closeBox.onclick = function(){signUpR.parentNode.style.display = "none";}
	closeBox1.onclick = function(){logOnR.parentNode.style.display = "none";}
	signUpR.parentNode.appendChild(closeBox);
	logOnR.parentNode.appendChild(closeBox1);
}

function signUpValidator(theForm){

	var p1 = theForm.userPass1.value;
	var p2 = theForm.userPass2.value;
	
	if(p1.length < 6){
		return "Password should be longer then 5 characters";
	}
	else {
		if(p1 != p2){
			return "Passwords do not match!";
		}
	}
	
	return "valid";
}

function signUpSubmit(theForm){
	
	document.getElementById('sLoadImg').style.visibility = "visible";	
	var vaild = signUpValidator(theForm);
	
	if(vaild == "valid"){ // form ok
		ajaxSignUp(theForm);
	}else{
		signUpR.parentNode.style.display = "block";
		signUpR.innerHTML = vaild;
		document.getElementById('sLoadImg').style.visibility = "hidden";
	}
	
}

function ajaxSignUp(theForm){
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			signUpR.parentNode.style.display = "block";
			signUpR.innerHTML = ajaxRequest.responseText;
			document.getElementById('sLoadImg').style.visibility = "hidden";
		}
	}
	
	var query = "?uid="+theForm.uid.value+"&userPass=";
	var upass = hex_md5(theForm.userPass1.value);
	query += upass;
	
	ajaxRequest.open("GET", "ldap_signUp_send.php"+query, true);
	ajaxRequest.send(null); 
}

function logOnValidator(theForm){

	var pass = theForm.userPass.value;
	
	if(pass.length < 4){
		return "Password should be longer then 3 characters";
	}
	
	return "valid";
}

function logOnSubmit(theForm){

	document.getElementById('lLoadImg').style.visibility = "visible";	
	var vaild = logOnValidator(theForm);

	if(vaild == "valid"){ // form ok
		ajaxLogOn(theForm);
	}else{
		logOnR.parentNode.style.display = "block";
		logOnR.innerHTML = vaild;
		document.getElementById('lLoadImg').style.visibility = "hidden";
	}
}

function ajaxLogOn(theForm){
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ajaxRequest.responseText == "go"){
				window.location = 'bracket_main.php';
			}else{
				logOnR.parentNode.style.display = "block";
				logOnR.innerHTML = ajaxRequest.responseText;
				document.getElementById('lLoadImg').style.visibility = "hidden";
			}
		}
	}
	
	var query = "?uid="+theForm.uid.value+"&userPass=";
	var upass = hex_md5(theForm.userPass.value);
	query += upass;
	
	ajaxRequest.open("GET", "ldap_logOn_send.php"+query, true);
	ajaxRequest.send(null); 
}