var SendMailUrl = "remind_sendmail.php";
var AuthorizationUrl = "remind_auth.php";

function authorization(authcode) {
	param = 'auth='+authcode;
	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.');
	
	param = 'login='+document.getElementById('login').value+'&email='+document.getElementById('email').value+
			'&image='+document.getElementById('image').value;
		//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 + ")");
					
					if(!myJSONObject.response[0].iserror) { //nie ma bledu
						document.getElementById("lo").style.color = "black";
						document.getElementById("em").style.color = "black";
						document.getElementById("im").style.color = "black";
						document.getElementById("submit").disabled = true;
					}
					else { //jest blad
						if(myJSONObject.response[0].login) {
							document.getElementById("lo").style.color = "red";
						}
						else {
							document.getElementById("lo").style.color = "black";
						}
						if(myJSONObject.response[0].email) {
							document.getElementById("em").style.color = "red";
						}
						else {
							document.getElementById("em").style.color = "black";
						}
						if(myJSONObject.response[0].image) {
							document.getElementById("im").style.color = "red";
						}
						else {
							document.getElementById("im").style.color = "black";
						}
						
						document.getElementById("image").value = "";
						
						obj_code = document.getElementById("code");
						var d = new Date();
						obj_code.innerHTML = "<img src=\"code.php?id=" + String(d.getTime()) +"\">";
					}
					
					obj_msg.innerHTML = myJSONObject.response[0].msg;
					obj_msg.style.display = "block";
			}
		}
	};

	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();