var SendMailUrl = "newaccount_sendmail.php";
var AuthorizationUrl = "newaccount_auth.php";
var CheckLoginUrl = "newaccount_login.php"

function checklogin() {
	var login = document.getElementById("login").value;
	param = 'login='+login;
	obj_check = document.getElementById("response");
	obj_check.innerHTML = '&nbsp;';
	httpSendMail.open("POST", CheckLoginUrl, true);
	httpSendMail.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpSendMail.send(param);
	
	httpSendMail.onreadystatechange = function () {
		if (httpSendMail.readyState == 4) {
			if (httpSendMail.status == 200) {
				
				var json_data = httpSendMail.responseText;
				eval("var myJSONObject = (" + json_data + ")");
				
				obj_check.style.fontWeight = "bold";
				
				if(!myJSONObject.response[0].iserror) { //nie ma bledu
					obj_check.style.color = "green";
				}
				else { //jest blad
					obj_check.style.color = "red";
					document.getElementById("login").focus();
				}
						
				obj_check.innerHTML = myJSONObject.response[0].msg;
			}
		}
	};

	return true;
}

function authorization(authcode, news, status) {
	param = 'auth='+authcode+'&news='+news+'&status='+status;
	httpSendMail.open("POST", AuthorizationUrl, true);
	httpSendMail.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpSendMail.send(param);
	
	httpSendMail.onreadystatechange = function () {
		if (httpSendMail.readyState == 4) {
			if (httpSendMail.status == 200) {
				obj_wait = document.getElementById("wait");
				obj_wait.style.display = "none";
				obj_msg = document.getElementById("msg");
				obj_msg.innerHTML = httpSendMail.responseText;
				obj_msg.style.display = "block";
			}
		}
	};

	return true;
}

function sendmail() {
	//window.alert('Send mail.');
	if(!document.getElementById("reg").checked) {
		window.alert("Aby za³o¿yæ konto nale¿y zaakceptowaæ regulamin!");
		return false;
	}
	if(!document.getElementById("accept").checked) {
		window.alert("Aby za³o¿yæ konto nale¿y wyraziæ zgodê\nna przetwarzanie danych osobowych!");
		return false;
	}
	
	param = 'login='+document.getElementById('login').value+'&email='+document.getElementById('email').value+
			'&editor='+document.getElementById('editor').checked+'&firstname='+document.getElementById('firstname').value+
			'&surname='+document.getElementById('surname').value+'&affiliation='+document.getElementById('affiliation').value+
			'&image='+document.getElementById('image').value+'&newsletter='+document.getElementById('newsletter').checked+
			'&reg='+document.getElementById('reg').checked+'&accept='+document.getElementById('accept').checked;
		//window.alert(param);
	obj_wait = document.getElementById("wait");
	obj_wait.style.display = "block";
	obj_msg = document.getElementById("msg");
	obj_msg.innerHTML = "";
	
	httpSendMail.open("POST", SendMailUrl, true);
	httpSendMail.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpSendMail.send(param);
	
	httpSendMail.onreadystatechange = function () {
		if (httpSendMail.readyState == 4) {
			if (httpSendMail.status == 200) {
					obj_wait.style.display = "none";
					var json_data = httpSendMail.responseText;
					eval("var myJSONObject = (" + json_data + ")");
					
					document.getElementById("lo").style.color = "black";
					document.getElementById("fn").style.color = "black";
					document.getElementById("sn").style.color = "black";
					document.getElementById("af").style.color = "black";
					document.getElementById("em").style.color = "black";
					document.getElementById("im").style.color = "black";

					if(myJSONObject.response[0].iserror) { //jest blad
						if(myJSONObject.response[0].login) {
							document.getElementById("lo").style.color = "red";
								
						}
						if(myJSONObject.response[0].firstname) document.getElementById("fn").style.color = "red";
						if(myJSONObject.response[0].surname) document.getElementById("sn").style.color = "red";
						if(myJSONObject.response[0].affiliation) document.getElementById("af").style.color = "red";
						if(myJSONObject.response[0].email) document.getElementById("em").style.color = "red";
						if(myJSONObject.response[0].image) document.getElementById("im").style.color = "red";
						
						document.getElementById("image").value = "";
						
						obj_code = document.getElementById("code");
						var d = new Date();
						obj_code.innerHTML = "<img src=\"code.php?id=" + String(d.getTime()) +"\">";
					}
					else { //nie ma bledu
						document.getElementById("submit").disabled = true;
					}
					
					//document.getElementById("response").innerHTML = "&nbsp;";
					obj_msg.innerHTML = myJSONObject.response[0].msg;
					obj_msg.style.display = "block";
					/*
					obj_wait.style.display = "none";
					obj_msg.innerHTML = httpSendMail.responseText;
					obj_msg.style.display = "block";
					*/
					/*
					obj_code = document.getElementById("code");
					if(obj_code.innerHTML == "<img src=\"code.php\">")
						obj_code.innerHTML = "<img src=\"code2.php\">";
					else
						obj_code.innerHTML = "<img src=\"code.php\">";
					obj_codeinput = document.getElementById("image");
					obj_codeinput.value = "";
					*/
			}
		}
	};

	return true;
}

/*
function checkregform() {
					
	sendmail();
				
	return true;
}
*/
function getHttpObject() {
	
    var xmlhttp;
    
	try { // Firefox, Opera 8.0+, Safari; create a javascript instance of the object.
		xmlhttp=new XMLHttpRequest();
	}
	catch (e) {
		try { // Internet Explorer
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); //If the Javascript version is greater than 5
		}
		catch (e) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //If not, then use the older active x object.
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
    
	return xmlhttp;
}

// initiates the object for sending data
var httpSendMail = getHttpObject();

/*
function checkform() {
	
	//window.alert('Check form1: ' + document.forms['naform'].firstname.value);
	//window.alert('Check form2: ' + document.forms['naform'].elements['firstname'].value);
	//window.alert('Check form3: ' + document.naform.firstname.value);
	//window.alert('Check form4: ' + document.naform.elements['firstname'].value);
	var str;
	
	str = document.naform.firstname.value.replace(/[^±¡æÆêÊ³£ñÑóÓ¶¦¼¬¿¯a-zA-Z0-9\-]/, "");
	if(str.length < 3) {
		window.alert('1!!!');
	}
	
	str = document.naform.surname.value.replace(/[^±¡æÆêÊ³£ñÑóÓ¶¦¼¬¿¯a-zA-Z0-9\-]/, "");
	if(str.length < 3) {
		window.alert('2!!!');
	}
	
	str = document.naform.affiliation.value.replace(/[^±¡æÆêÊ³£ñÑóÓ¶¦¼¬¿¯a-zA-Z0-9\-]/, "");
	if(str.length < 1) {
		window.alert('3!!!');
	}
	
	if(document.naform.email.value.search(/^[a-zA-Z0-9\_\-]+(\.[a-zA-Z0-9\_\-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9\-]+)*(\.[a-z]{2,4})$/) == -1) {
		window.alert('4!!!');
	}
	
	window.alert(str);
	
	return true;
}
*/

