/* JavaScript for restore password popup, performing 
 * password restore query. */
 
/* Variables for original content - for restoring */
var restore_pw_orig_txt_cont = null;
var restore_pw_orig_btn_value = null;
var restore_pw_orig_btn_href = null;
/* Variable determine if the content was mangled and should be restored */
var should_restore_restore_pw_cont = false;
/* Saving old content */
function save_popup_cont() {
	restore_pw_orig_txt_cont = document.getElementById('RestorePassword_txtField').innerHTML;
	restore_pw_orig_btn_value = document.getElementById('RestorePopup_btnSend').innerHTML;
	restore_pw_orig_btn_href = document.getElementById('RestorePopup_btnSend').getAttribute('href');
}
/* Restoring pop-up content after mangling */ 
function renew_popup_cont() {
	document.getElementById('RestorePassword_txtField').innerHTML = restore_pw_orig_txt_cont;
	document.getElementById('RestorePopup_btnSend').innerHTML = restore_pw_orig_btn_value;
	document.getElementById('RestorePopup_btnSend').setAttribute('href', restore_pw_orig_btn_href);
	$('#RestorePassword_userInput').show();
} 
 
/* Open popup */
function restore_pw_popup_show() {
 	$('#restore_popup_window').fadeIn(500);
 }
/* Close and restore content of popup */
function restore_pw_popup_hide() {
	$('#restore_popup_window').fadeOut(100);
	/* on close restoring old value of the content —if required */
	if (should_restore_restore_pw_cont)	renew_popup_cont();
}

/* Send data to the web-service and reprt results */
function restore_pw_popup_send() {
	/* Saving old values - they would be changed */
	should_restore_restore_pw_cont = true;
	save_popup_cont();
	
	var textField = document.getElementById('RestorePassword_txtField');
	var inputField = document.getElementById('RestorePassword_inpEmail')
	/* Retieving and preparing data */
	if (inputField.value == '') {
		textField.innerHTML = 'Необходимо указать корректный адрес электронной почты';
	}
	else {
		var emailStr = encodeURIComponent(inputField.value);
		if (!emailStr) {
			return false;
		}
	
		/* Sending data to the web-service */
	
		/* handler of web-service request */
		function http_request_handler() {
			if(this.readyState == 4 && this.status == 200) {
				if(this.responseXML != null) {
					var txt=this.responseXML.documentElement.childNodes[0].nodeValue;
					if (txt == "true") { 
						textField.innerHTML = "Новый пароль отправлен на указанный почтовый ящик.";
						var btn_send = document.getElementById('RestorePopup_btnSend');
						btn_send.innerHTML = "Закрыть";
						btn_send.setAttribute('href', 'javascript:restore_pw_popup_hide()');
						$('#RestorePassword_userInput').hide();
					}
					else {
						textField.innerHTML = "Сожалеем, указанный email не зарегистрирован, попробуйте еще.";
					}
				}
			}	
		}
		var client = new XMLHttpRequest();
		client.onreadystatechange = http_request_handler;
		client.open("GET", "/WebSrv/Calendar/Users.asmx/RestorePassword?email=" + emailStr);
		client.send();
	}
}

