/******** include this in global.js ******************/

/*
function showPopupLayer()
{
	$A(document.getElementsByTagName('select')).each(function(el) { Element.hide(el); } );

	$("PopupBg").style.display = "inline";
	$("PopupWindow").style.display = "inline";
	
	// resize and position
	var sizes = getPageSize();

	var widthTotal = sizes[0];
	var heightTotal = sizes[1];

	$('PopupBg').style.width = (widthTotal) + "px";
	$('PopupBg').style.height = (heightTotal) + "px";
	
	var dimension = Element.getDimensions('PopupWindow');
	var screenDim = getScreenSize();
	var screenScroll = getScrollXY();
	
	var pos_x = (screenDim.width - dimension.width) / 2 + screenScroll.x;
	var pos_y = (screenDim.height - dimension.height) / 2 + screenScroll.y;
	
	$('PopupWindow').style.left = (pos_x-50) + "px";
	//$('PopupWindow').style.top = pos_y + "px";	
	$('PopupWindow').style.top = "200px";	
}


function showPopup()
{
	new Effect.Opacity('PopupBg', {duration:1, from:0, to:0.7});
	new Effect.Opacity('PopupWindow', {duration:1, from:0, to:1});
	
	window.setTimeout("showPopupLayer();", 100);
}
/*
function closePopup()
{
	$A(document.getElementsByTagName('select')).each(function(el) { Element.show(el); } );
	
	$("PopupBg").style.display = "none";	
	$A($$('.popup')).each(function(element){
		element.style.display = "none";
	});
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return { x : scrOfX, y : scrOfY };
}


function getScreenSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return { width : myWidth, height : myHeight };
}
function checkMail(mail)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(mail)) 
		return true;
	else 
		return false;
}
function trim(string)
{
	return string.replace(/(^\s*)|(\s*$)/g,'');
} 
*/
var SendToFriend = {
	checkform : function ()
	{
		if(!$F("prenom"))
		{
			alert(msg_send_to_a_friend1);
			return false;
		}
		if(!$F("email"))
		{
			alert(msg_send_to_a_friend2);
			return false;
		}
		if(!$F("email_ami1"))
		{
			alert(msg_send_to_a_friend3);
			return false;
		}
		if(!checkMail($F("email")))
		{
			alert(msg_send_to_a_friend4);
			return false;
		}
		if(!checkMail($F("email_ami1")))
		{
			alert(msg_send_to_a_friend5);
			return false;
		}
		if($F("email_ami2")&&!checkMail($F("email_ami2")))
		{
			alert(msg_send_to_a_friend6);
			return false;
		}
		if($F("email_ami3")&&!checkMail($F("email_ami3")))
		{
			alert(msg_send_to_a_friend7);
			return false;
		}
		return true;
	},
	send_to_a_friend : function ()
	{
		$("box").innerHTML = '<a href="javascript:void(0);"><img src="/extension/action/design/standard/images/ajax-loader.gif" alt="Loading" title="Loading" /></a>';
		
		if(!this.checkform())
		{
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="SendToFriend.send_to_a_friend();">> Validar</a>';
			return false;
		}
		var url = "/action/send_to_a_friend";
		var param = "prenom=" + $F("prenom");
		 param = param + "&email=" + $F("email") ;
		 param = param + "&commentaire=" + encodeURIComponent($F("commentaire")) ;
		 param = param + "&email_ami1=" + $F("email_ami1") ;
		 param = param + "&email_ami2=" + $F("email_ami2") ;
		 param = param + "&email_ami3=" + $F("email_ami3") ;
		 param = param + "&url=" + $F("url") ;
		 
		new Ajax.Request(url, {
			 method:'post',
			 parameters: param,
			 onComplete:this.send_finish
			 });
	},

	cancel_send : function ()
	{
		$("prenom").value="";
		$("email").value="";
		$("commentaire").value="";
		$("email_ami1").value="";
		$("email_ami2").value="";
		$("email_ami3").value="";
		$("url").value="";
		$("box").innerHTML = '<a href="javascript:void(0);" onclick="SendToFriend.send_to_a_friend();">> Validar</a>';
		closePopup();
	},
	send_finish : function (ajax)
	{
		var res = eval('('+ajax.responseText+')');
		if(trim(res.res) == "OK")
		{
			alert(msg_send_to_a_friend8);
			$("prenom").value="";
			$("email").value="";
			$("commentaire").value="";
			$("email_ami1").value="";
			$("email_ami2").value="";
			$("email_ami3").value="";
			$("url").value="";
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="SendToFriend.send_to_a_friend();">> Validar</a>';
			closePopup();
		}
		else
		{
			var msg = msg_send_to_a_friend9 + "\n";
			res.mail_echec.each(function(mail) {
				msg = msg + mail + "\n";
			});
			res.mail_echec.count
			alert(msg);
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="SendToFriend.send_to_a_friend();">> Validar</a>';
		}
	}
}

var AskQuestion = {
	checkform : function ()
	{
		if(!$F("question"))
		{
			alert(msg_ask_question1);
			return false;
		}
		return true;
	},
	ask_question : function ()
	{
		$("box").innerHTML = '<a href="javascript:void(0);"><img src="/extension/action/design/standard/images/ajax-loader.gif" alt="Loading" title="Loading" /></a>';
		
		if(!this.checkform())
		{
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="AskQuestion.ask_question();">> Validar</a>';
			return false;
		}
		var url = "/action/ask_question";
		var param = "question=" + encodeURIComponent($F("question")) ;
		 param+= "&captcha=" + $F("captcha") ;
		 param+= "&section_id=" + $F("section_id") ;
		new Ajax.Request(url, {
			 method:'post',
			 parameters: param,
			 onComplete:this.ask_finish
			 });
	},

	cancel_question : function ()
	{
		$("question").value="";
		$("box").innerHTML = '<a href="javascript:void(0);" onclick="AskQuestion.ask_question();">> Validar</a>';
		closePopup();
	},
	ask_finish : function (ajax)
	{
		var res = eval('('+ajax.responseText+')');
		if(trim(res.res) == "OK")
		{
			alert(msg_ask_question2);
			$("question").value="";
			$("captcha").value="";
			$("captcha_img").src = '/action/captcha/'+ Math.floor(Math.random()*100000);
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="AskQuestion.ask_question();">> Validar</a>';
			
			closePopup();
		}
		else
		{
			alert(res.error);
			//alert(msg_ask_question3);
			$("box").innerHTML = '<a href="javascript:void(0);" onclick="AskQuestion.ask_question();">> Validar</a>';
		}
	}
}
