
var isMSIE = document.attachEvent != null;
var isGecko = !document.attachEvent && document.addEventListener;

/*****************************************************************************************/
function $(sId){ 
	return document.getElementById(sId); 
}

/*****************************************************************************************/
function $$(sId){ 
	return document.getElementsByTagName(sId); 
}


/*****************************************************************************************/
function eventTarget(event){
	var evt = event || window.event;
	return (evt.target || evt.srcElement);
}

/*****************************************************************************************/
function eventCancelBubble(event){
	if(event && event.stopPropagation) event.stopPropagation();
	else if(window.event) window.event.cancelBubble = true;
}


/*****************************************************************************************/
function reloadWindow(){
	window.location = window.location + '';
}


/*****************************************************************************************/
function isEmail(sEmail){
	return (
		sEmail &&
		sEmail.indexOf('@') > 0 &&
		sEmail.match(/^(?:[-a-z\d\+\*\/\?!{}`~_%&'=^$#]+(?:\.[-a-z\d\+\*\/\?!{}`~_%&'=^$#]+)*)@(?:[-a-z\d_]+\.){1,60}[a-z]{2,6}$/)
	) ? true : false;
}


/*****************************************************************************************/
function trim(sText){
	var sBeginExpr = /^\s+/;
	var sEndExpr = /\s+$/;
	
	sText = sText.replace( sBeginExpr , '');
	sText = sText.replace( sEndExpr , '');
	
	return sText;
}





/*****************************************************************************************/
var Forum = new function(){
	
	this.RemindBlock = null;
	this.Content = '';
	this.bProcessing = false;
	

	/***************************************/
	this.showReminder =  function(event){
		var evt = event || window.event;
		var pos = {
			x : evt.clientX ,
			y : evt.clientY
		};
		
		if(isMSIE){
			pos.x = evt.clientX + document.documentElement.scrollLeft;
			pos.y = evt.clientY + document.documentElement.scrollTop;
		}
		if(isGecko){
			pos.x = evt.clientX + window.scrollX;
			pos.y = evt.clientY + window.scrollY;
		}
					
		if(! Forum.RemindBlock ){
			Forum.RemindBlock = $('RemindBlock');
			Forum.RemindBlock = document.createElement('DIV');
			Forum.RemindBlock.id = 'RemindBlock';
			$$('BODY')[0].appendChild(Forum.RemindBlock);
		}
		Forum.RemindBlock.innerHTML = '<input type="text" /><input value="Напомнить" onclick="Forum.remind(event)" type="button" class="button" /><span>Введите адрес электронной почты,<br />и&nbsp;мы вышлем вам пароль.</span>';
		Forum.RemindBlock.style.top = pos.y + 20 + 'px';
		Forum.RemindBlock.style.left = pos.x - 20 + 'px';
		Forum.RemindBlock.style.display = 'block';
		
		Forum.setReminderEvents();

	}
	
	/***************************************/
	this.setReminderEvents = function(){
		Forum.RemindBlock.onmousedown = eventCancelBubble;
		
		document.onmousedown = 
		document.onkeydown = Forum.hideReminderEvent;
	}
	
	/***************************************/
	this.removeReminderEvents = function(){
		Forum.RemindBlock.onmousedown = null;
		document.onkeydown = null;
		document.onmousedown = null;	
	}
	
	/***************************************/
	this.hideReminderEvent = function(event){
		var evt = event || window.event;
		if(
			evt.type == 'mousedown' ||
			(evt.type == 'keydown' && evt.keyCode == 27)
		){
			Forum.RemindBlock.style.display = 'none';
			Forum.removeReminderEvents();
		}
	}
		
		
	
	
	/***************************************/
	this.remind =  function(event){
		var evt = event || window.event;
		if(!Forum.RemindBlock) return;
		
		var sMail = trim(Forum.RemindBlock.firstChild.value);
		if( !sMail || !isEmail(sMail) ) return;
		
		Forum.RemindBlock.childNodes[0].disabled = true;
		Forum.RemindBlock.childNodes[1].disabled = true;
		Forum.removeReminderEvents();
		
		Forum.send(
			'remindPass',
			sMail,
			function(rez){
				if(rez.success){
					Forum.RemindBlock.innerHTML = rez.text; //'Пароль был выслан на указанный вами адрес.'
				}
				else {
					Forum.RemindBlock.childNodes[0].disabled = false;
					Forum.RemindBlock.childNodes[1].disabled = false;
					Forum.RemindBlock.lastChild.innerHTML = rez.text;
				}
				Forum.setReminderEvents();
			}
		);
	}
	
	
	/***************************************/
	this.send =  function(sAct, sText, fRezFunc){
		if(! JsHttpRequest || Forum.bProcessing) return;
		Forum.bProcessing = true;
		
//		var intervalId = setTimeout("reloadWindow()", 15000);
		var oReqValues = new Object();
			 oReqValues['act'] = sAct;
			 oReqValues['text'] = sText;
		
		JsHttpRequest.query(
			'/forum/ajax.p3' ,
			oReqValues ,
         function(result, errors) {
//				clearTimeout(intervalId);
				Forum.bProcessing = false;
				
				if(result){
					fRezFunc(result);
				}
			} ,
			true // disable caching
		);
	}
	
}

