﻿function showSubsForm(id, oBtn){
	if(document.getElementById('subsForm')){
		document.getElementById('subsForm').parentNode.removeChild(document.getElementById('subsForm'));
	}
	var oDiv = oBtn.parentNode;
	oDiv.style.position = 'relative';
	var h = oDiv.offsetHeight;
	var w = oDiv.offsetWidth;
	oSubs = document.createElement('div');
	oSubs.setAttribute("id", "subsForm")
	oSubs.style.position = 'absolute';
	var newH = 100;
	var newW = 200;
	oSubs.className = "subsTp";
	//oSubs.style.height = newH;
	//oSubs.style.width = newW;
	oSubs.style.top = -(Math.round((newH/2)-(h/2)));
	oSubs.style.left = -(Math.round((newW/2)-(w/2)));
	oSubs.style.border = '1px solid #AEAEAE';
	var email = window.email;
	document.getElementById('subsOk').setAttribute('onclick','subscribe('+id+', oSubs.getElementsByTagName(\'input\')[0].value); return false;');
	oSubs.innerHTML = document.getElementById('subsTp').innerHTML;
	oSubs = oDiv.appendChild(oSubs);
	if(email != undefined) oSubs.getElementsByTagName('input')[0].value = email;
	oSubs.getElementsByTagName('input')[0].focus();
}

function chekMail(email){
	var chkmail = /^[a-z,0-9]{1}[0-9,a-z,._+-]+@([a-z,0-9]{1}[0-9,a-z,_-]*[a-z,0-9]{1}[.])+[a-z,0-9]{2,5}$/i.exec(email);
	if(!chkmail){
		var aSpan = oSubs.getElementsByTagName('span');
		for (var i=0; i < aSpan.length; i++){			
			if(aSpan[i].id=='subsTp_alert') var oAlert = aSpan[i]; //= 'Вами был введен некорректный адрес электронной почты';	
			if(aSpan[i].id=='subsTp_invalid_mail') var tText = aSpan[i].innerHTML;
		}
		oAlert.innerHTML = tText;
		oAlert.style.display = 'block';		
		
		return false;
	}else{
		var aSpan = oSubs.getElementsByTagName('span');
		for (var i=0; i < aSpan.length; i++){			
			aSpan[i].style.display = 'none';
			return chkmail[0];
		}
	}
}

function subscribe(id, email){
	email = chekMail(email);
	if(email){
		var request = new httpRequest();
		var func = function(){
			var sss = request.responseText;
			if(sss != 'already_signed' && sss != 'signed') sss='error';
			var aSpan = document.getElementById('subsTp').getElementsByTagName('span');
			var tText	= 'error';
			for (var i=0; i < aSpan.length; i++){							
				if(aSpan[i].id=='subsTp_'+sss) tText = aSpan[i].innerHTML;
			}
			oSubs.removeChild(oInImg);			
			var oTable = document.createElement('table');
			oTable.className = "noborder";					
			var oTbody = document.createElement('tbody');
			oTbody = oTable.appendChild(oTbody);
			var oTr = document.createElement('tr');
			oTr = oTbody.appendChild(oTr);
			var oTd = document.createElement('td');		
			oTd = oTr.appendChild(oTd);			
			oTd.innerHTML = tText;//
			oTd.style.textAlign = "center";
			oTable = oSubs.appendChild(oTable);			
			timer = window.setTimeout(removeDiv,3000,oSubs);
			oSubs.setAttribute('onclick', 'window.clearTimeout(timer); removeDiv(oSubs);');			
		}
		var userid = window.userid;
		if(userid == undefined) var userid=0;
		var oInImg = document.createElement("img");	
		oInImg.width = 14;
		oInImg.height = 14;
		oInImg.src = "/i/progress_14x14.gif";
		oSubs.innerHTML = '';
		oSubs.appendChild(oInImg);
		makeRequest(request, func, '/subscribe/', 'POST', 'id='+id+'&email='+email);
	}
}

function removeDiv(oSubs){
	oSubs.parentNode.removeChild(oSubs)
}

function setActive(o){
	if(o.value == o.title) o.value = "";
}

function setFeelInActive(o){
	if(o.value.length == 0) o.value = o.title;
}
