var div_node_send = null;
var div_node_flag = null;
var div_login = null;
var winW = 0, winH = 0;
var bexternal = false;

function checkMail(_email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(_email)) return true;
	else return false;
}

function calculateBrowserWidth() 
{
	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  winW = window.innerWidth;
	  winH = window.innerHeight;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth;
	  winH = document.body.offsetHeight;
	 }
	}
}

function removeSendBand()
{
	if (div_node_send != null)
	{
		div_node_send.style.display = "none";
		div_node_send.parentNode.removeChild(div_node_send);
		div_node_send = null;
	}
}


function removeLoginBand()
{
	if (div_login != null)
	{
		div_login.style.display = "none";
		div_login.parentNode.removeChild(div_login);
		div_login = null;
	}
}


function loginBand(nid, url_base, url_form, url_password, url_register)
{	
	var _top = 0;
	var _left = 0;

    if (nid > 0)
	{
		var element_name = "node_band_" + nid;
		_top = $(element_name).offsetTop + 300;
		_left = 0;
	}
	else
	{
		_top = 300;
		_left = 0;
	}
	
	bexternal = true;

	calculateBrowserWidth();	
	if (div_login != null)
		removeLoginBand();
	
	div_login = document.createElement('div');
	div_login.className = "login_form_band";
	div_login.style.top = _top + "px";
	_left = (winW/2) - (div_login.style.width/2) - 90;
	div_login.style.left = _left + "px";	
	
	document.body.appendChild(div_login);
		
	html = "";
	html += '<div class="bxclose"><a href="javascript:void(0);" onClick="removeLoginBand();">&nbsp;</a></div>';
	html += '<div class="bxtitle">Se connecter</div>';	
	html += '<div class="bxcontent" id="div_login_content">';
	html += '<form method="post" action="'+url_form+'" name="send_band_form">';
	html += 'Vous devez être connecté pour cette action!<br />';
	html += '<br />';
	html += 'Connectez vous si vous êtes déjà membre<br />';
	html += 'Ou inscrivez vous gratuitement !<br />'; 
	html += '<br />';
	html += 'Pseudo:<br /><input type="text" name="edit[name]" /><br />';
	html += 'Mot de passe:<br /><input type="password" name="edit[pass]" /><br />';
	
	
	html += '<table border="0" style="margin-top: 10px;"><tr><td>';
	html += '<input type="image" src="'+url_base+'/themes/tbands/images/connect_button.gif" border="0" />';
	html += '</td><td>';	
	html += '&nbsp;ou&nbsp;'; 
	html += '</td><a href="'+url_register+'"><img src="'+url_base+'/themes/tbands/images/register_button.gif" border="0"/></a><td>';
	html += '</td></tr></table>';
	html += '<a href="'+url_password+'">Mot de passe perdu ?</a><br />'; 
	
	html += '<input type="hidden" name="edit[form_id]" id="edit-user-login-block" value="user_login_block"  />';
	html += '<!-- <input type="submit" value="Se connecter" onClick="sendBandForm();" name="op"/> -->';	
	html += '</form>';
	
	html += '</div>';
		
	div_login.innerHTML = html;	
	
}


function sendBandExternal(nid)
{	
	var element_name = "node_band_" + nid;
	var _top = $(element_name).offsetTop + 10;
	var _left = 0;
	
	bexternal = true;

	calculateBrowserWidth();	
	if (div_node_send != null)
		removeSendBand();
	
	div_node_send = document.createElement('div');
	div_node_send.className = "send_band";
	div_node_send.style.top = _top + "px";
	_left = (winW/2) - (div_node_send.style.width/2) - 190;
	div_node_send.style.left = _left + "px";	
	
	document.body.appendChild(div_node_send);
		
	html = "";
	html += '<div class="bxclose"><a href="javascript:void(0);" onClick="removeSendBand();">&nbsp;</a></div>';
	html += '<div class="bxtitle">Envoyer ce groupe à un ami</div>';	
	html += '<div class="bxcontent" id="send_band_content">';
	html += '<form name="send_band_form">';
	html += 'Adresse email: <input type="text" name="email_address" />';
	html += '<input type="hidden" name="nid" value="' +nid+ '" />';
	html += '<input type="button" value="Envoyer" onClick="sendBandForm();"/>';
	html += '</form>';
	html += '</div>';
		
	div_node_send.innerHTML = html;	
	
}

function sendBand(nid)
{	
	var element_name = "node_band_" + nid;
	var _top = $(element_name).offsetTop + 300;
	var _left = 0;
	
	bexternal = true;

	calculateBrowserWidth();	
	if (div_node_send != null)
		removeSendBand();
	
	div_node_send = document.createElement('div');
	div_node_send.className = "send_band";
	div_node_send.style.top = _top + "px";
	_left = (winW/2) - (div_node_send.style.width/2) - 90;
	div_node_send.style.left = _left + "px";	
	
	document.body.appendChild(div_node_send);
		
	html = "";
	html += '<div class="bxclose"><a href="javascript:void(0);" onClick="removeSendBand();">&nbsp;</a></div>';
	html += '<div class="bxtitle">Envoyer ce groupe à un ami</div>';	
	html += '<div class="bxcontent" id="send_band_content">';
	html += '<form name="send_band_form">';
	html += 'Adresse email: <input type="text" name="email_address" />';
	html += '<input type="hidden" name="nid" value="' +nid+ '" />';
	html += '<input type="button" value="Envoyer" onClick="sendBandForm();"/>';
	html += '</form>';
	html += '</div>';
		
	div_node_send.innerHTML = html;	
	
}

function sendBandForm()
{
	var _email = document.send_band_form.email_address.value;
	var _nid = document.send_band_form.nid.value;
	
	if (checkMail(_email))
	{
		$('send_band_content').innerHTML = 'Envoi en cours ...';
		xajax_bands_xajax_send_node(_nid, _email);
	}
	else
	{
		showBandFormErrorEmail();
	}
}

function showBandFormErrorEmail()
{
	var _nid = document.send_band_form.nid.value;
	
	if (bexternal)
		$('send_band_content').innerHTML = 'Email invalide.<p><a href="javascript:void(0);" onClick="sendBandExternal(' + _nid + ')">Essayez à nouveau</a></p>';
	else
		$('send_band_content').innerHTML = 'Email invalide.<p><a href="javascript:void(0);" onClick="sendBand(' + _nid + ')">Essayez à nouveau</a></p>';
}


function bandFlagged(nid)
{	
	var element_name = "node_band_" + nid;
	var _top = $(element_name).offsetTop + 70;
	var _left = 0;

	calculateBrowserWidth();	
	if (div_node_flag != null)
		removeFlagBand();
	
	div_node_flag = document.createElement('div');
	div_node_flag.className = "flag_band";
	div_node_flag.style.top = _top + "px";
	_left = (winW/2) - (div_node_flag.style.width/2) - 90;
	div_node_flag.style.left = _left + "px";	
	
	document.body.appendChild(div_node_flag);
	
	html = "";
	html += '<div class="bxclose"><a href="javascript:void(0);" onClick="removeFlagBand();">&nbsp;</a></div>';
	html += '<div class="bxtitle">Groupe signalé comme spam/erreur</div>';	
	html += '<div class="bxcontent">';
	html += 'Merci de nous avoir signalé un spam/ereur pour ce groupe. Nous verifierons.';
	html += '</div>';
		
	div_node_flag.innerHTML = html;	
	
}

function removeFlagBand()
{
	if (div_node_flag != null)
	{
		div_node_flag.style.display = "none";
		div_node_flag.parentNode.removeChild(div_node_flag);
		div_node_flag = null;
	}
}

