// JavaScript Document

function createRequestObject() 
{
	var ro; 
	if(window.ActiveXObject){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	ro = new XMLHttpRequest();
	}
	return ro;
}

function showPopUpArt(id)
{
	id=id.toString();
	var artmail = "art_"+id;
	var referralObj = document.getElementById(artmail);
	var articolText=document.getElementById('popUpArt_'+id);	
	articolText.style.visibility='visible';
	var cords = getPos(referralObj);
	var leftStr =  (cords.x-270) + "px";
	var topStr = (cords.y-100) + "px";
	articolText.style.left=leftStr;
	articolText.style.top=topStr;
}

function hidePopUpArt(id)
{
	id=id.toString();
	var artmail = "art_"+id;
	var referralObj = document.getElementById(artmail);
	var articolText=document.getElementById('popUpArt_'+id);	
	articolText.style.visibility='hidden';
}


function preventEventToPropagate(ev)
{
	if(document.all)
	{
		ev.returnValue=false;
		ev.cancelBubble = true;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

function hidePopUpMail()
{
	var alertTable = (document.getElementById('popUp'));
	if(!alertTable)
		return false;
	alertTable.style.visibility = 'hidden';
	return;
}

function showPopUpMail(id)
{
	id=id.toString();
	var artmail = "artmail_"+id;
	var raspuns=document.getElementById('raspuns');	
	var referralObj = document.getElementById(artmail);
	var alertTable = (document.getElementById('popUp'));
	var expTitle = (document.getElementById('exp_title'));	
	var contentText = (document.getElementById('popUpMailTextContainer'));
	
	if(expTitle.innerHTML!="Trimite articolul prin mail unui prieten")
		expTitle.innerHTML="Trimite articolul prin mail unui prieten";
		
	if(contentText.style.display=='none')
		contentText.style.display='block';

	if(raspuns.style.display=='block')
		raspuns.style.display='none';
	
	document.body.onclick=hidePopUpMail;

	var cords = getPos(referralObj);
	var leftStr =  (cords.x+20) + "px";
	var topStr = (cords.y-100) + "px";

	alertTable.style.left=leftStr;
	alertTable.style.top=topStr;
	actual_art=id;
	
	if(!alertTable)
		return false;
	alertTable.style.visibility = 'visible';
	return;
}

function verify()
  {
	var error = "";
	var yourName=document.getElementById('yourname');
	var yourEmail=document.getElementById('yourmail');
	var emailTo=document.getElementById('email_addresses');
	var EmailValid=checkMail(yourEmail.value);
//	alert(EmailValid);
	if (yourName.value.length==0) error += "Nu ati introdus numele dumneavoastra\n";
	if (yourEmail.value.length==0) error += "Nu ati introdus adresa dumneavoastra de e-mail\n";
	if (emailTo.value.length==0) error += "Introduceti cel putin o adresa de e-mail pentru destinatar\n";
	if (!EmailValid)	error += "Introduceti o adresa de e-mail valida pentru expeditor\n";

	if (error!="") alert(error);
			  else mailit();
  }


function mailit()
  {
	var raspuns=document.getElementById('raspuns');	
	var contentDiv=document.getElementById('popUpMailTextContainer');
	var yourName=document.getElementById('yourname');
	var yourEmail=document.getElementById('yourmail');
	var mailAddresses=document.getElementById('email_addresses');
		
	http = new createRequestObject();
	posting = '/articol/'+actual_art+'/things/';
	posting += yourName.value+')';
	posting += '('+yourEmail.value+')';
	posting += '('+mailAddresses.value;
	
	http.onreadystatechange = function()
	{
		 if(http.readyState==4)
		{
			raspuns.style.display='block';
			raspuns.innerHTML=http.responseText;
			contentDiv.style.display='none';
			yourName.value='';
			yourEmail.value='';
			mailAddresses.value='';
		}
	}
	http.open('GET','/mailing_articol'+posting);
	http.send(null);
  }

function checkMail(mailAddress) 
{
	var checkRegExpStr = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	return checkRegExpStr.test(mailAddress);
}

__=function positioningNamespace() {
	function gP(e){
		var left=0;
		var top=0;
		while (e.offsetParent){
			left+=e.offsetLeft-e.scrollLeft;
			top+=e.offsetTop;
			e=e.offsetParent;
		}
		left+=e.offsetLeft-e.scrollLeft;
		top+=e.offsetTop;
		return {x:left, y:top};
	}
     window.getPos = gP;
}();

