var newWin = null;

// Close a popup
function closeWin(){
	if (newWin != null){
		if(!newWin.closed)
		newWin.close();
		}
}

// open a popup
function Popup(strURL,strType,strHeight,strWidth) {
closeWin();
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location, height="+strHeight+",width="+strWidth;
newWin = window.open(strURL, 'newWin', strOptions);
newWin.focus();
}

// Set welcome page to LaVieImmo
function LVIenPageAccueil(){
	if (document.all){
		lienLVIenHP = document.getElementById('lienLVIenPageAccueil');
		lienLVIenHP.style.behavior = "url(#default#homepage)";
		lienLVIenHP.setHomePage("http://www.lavieimmo.com");
	}
	else{
		alert("Votre navigateur n'est pas compatible avec cette fonction");
	}
}

// Add LaVieImmo to Bookmarks
function LVIenFavoris(){
	if (document.all) {
		window.external.AddFavorite("http://www.lavieimmo.com",'La vie Immobilière');
	}
	else {
		alert("Votre navigateur n'est pas compatible avec cette fonction.");
	}
}

// Get elementsby ClassName
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// All JS functions to be initialised
function _onLoad_init() {
	// Init autocompletion 'Prix au m² des villes'
	ajaxehome(); 
	// Mouse over Observers on Header menu
    //Event.observe($('header_logo'),'mouseover',_do_hide_subMenus);
    //Event.observe($('header_pub'),'mouseover',_do_hide_subMenus);
    //Event.observe($('nav_bar'),'mouseover',_do_hide_subMenus);
    //Event.observe($('global_table'),'mouseover',_do_hide_subMenus);	
}

// Autocompletion pour la zone de recherche de la home
function ajaxehome () {
    new Ajax.Autocompleter (
                        'choix_home',      // ID of the source field
                        'suggestions_form_home',  // ID of the DOM element to update
                        'ajax/_get_ajax_ville.php', // Remote script URI
                        {method: 'post', paramName: 'lieu_without_region', afterUpdateElement: getSelectionId_1}
                        ); 

}

function ajaxecote () {
    new Ajax.Autocompleter (
                        'choix',      // ID of the source field
                        'suggestions_form_cote',  // ID of the DOM element to update
                        'ajax/_get_ajax_ville.php', // Remote script URI
                        {method: 'post', paramName: 'lieu_without_region', afterUpdateElement: getSelectionId_1}
                        ); 

}


function ajaxeannonce () {
    new Ajax.Autocompleter (
                        'saisie_ville',      // ID of the source field
                        'suggestions_form_annonce',  // ID of the DOM element to update
                        '../ajax/_get_ajax_ville.php', // Remote script URI
                        {method: 'post', paramName: 'lieu_only_ville', afterUpdateElement: getSelectionId_1}
                        ); 

}


function getSelectionId_1(text, li) {
    $('selection_dept_cp_ville_1').value=li.id;
    $('saisie_dept_cp_ville_1').style.background = '#FFFFFF' ;
    return true;
}


function getSelectionId_1(text, li) {
    $('selection_dept_cp_ville_1').value=li.id;
    $('saisie_dept_cp_ville_1').style.background = '#FFFFFF' ;
    return true;
}

// Login request
function _doLogin() {
	
	var redir = '';
	// Check login field
	if ( $('login').value == '' ) {
		alert('Veuillez entrez votre identifiant') ;
		return false;	
	}
	
	// Check login field
	if ( $('pass').value == '' ) {
		alert('Veuillez entrez votre mot de passe') ;
		return false;	
	}	
	
	// Display ajax indicator
	$('mail_button').className = 'loading' ;

	if($('comment').value != '') {
	var redir = $('comment').value;
	}

	// Send mail and display status
	new Ajax.Updater('content_zone', 'doLogin_modalBox.php?url='+redir+'',  
		{asynchronous:false, 
		 parameters:Form.serialize($('form_login')), 
		 onComplete: _doLogin_success
		});	
	
	
}

// When login is good => redirect page
function _doLogin_success() {	
	if ( document.getElementById("form_login") == null ) {
		Modalbox.resize(0, -170) ;
		setTimeout('_doLogin_redirect()', 2000) ;
	}
}


// Redirect depends on usr_type
function _doLogin_redirect() {
	// Redirect for part or member
	if(document.getElementById('redir') != null) {
	window.location.replace(document.getElementById('redir').value);	
	}	
	
	else if ( document.getElementById("particulier") == null ) {
		window.location.replace('membre-professionnel-de-l-immobilier.html');
	}	
	else {
		window.location.replace('index.html');
	}	
	
}


// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

// Display a photo in AJAX 
function _show_photo(img_photo) {
	// Replace actual photo with AJAX indicator
	//$('big_photo_container').innerHTML = '<div align="center" style="width:360px;padding:5px;float:left;margin: 5px;margin-right:10px;"><img src="images/ajax-loader.gif"></div>' ;
	$('big_photo_container').innerHTML = '<img src="images/ajax-loader.gif">' ;
	// Load picture in cache
	simplePreload( img_photo ); 
	// When done, display image
	$('big_photo_container').innerHTML = '<img align=center src="' + img_photo + '" id="photo_container">' ; 
}

// Show and Hide layers

function hide_layer(nr)
{	
	if (document.layers && document.layers[nr].display != 'none')
	{
		document.layers[nr].display = 'none';
		document.layers[nr].visibility = 'hidden';
	}
	else if (document.all && document.all[nr].style.display != 'none')
	{
		document.all[nr].style.display = 'none';
		document.all[nr].style.visibility = 'hidden';
	}
	else if (document.getElementById && document.getElementById(nr).style.display != 'none')
	{		
		document.getElementById(nr).style.display = 'none';
		document.getElementById(nr).style.visibility = 'hidden';
	}
}

function show_layer(nr)
{
	if (document.layers && document.layers[nr].display != 'block')
	{
		document.layers[nr].display = 'block';
		document.layers[nr].visibility = 'visible';	
	}
	else if (document.all && document.all[nr].style.display != 'block')
	{
		document.all[nr].style.display = 'block';
		document.all[nr].style.visibility = 'visible';	
	}
	else if (document.getElementById && document.getElementById(nr).style.display != 'block')
	{
		document.getElementById(nr).style.display = 'block';
		document.getElementById(nr).style.visibility = 'visible';		
	}
}	


// Design menu
var current_menu;
function _overBgMenu(obj){
	if ( $(obj).id != current_menu )  {
		if ( $(current_menu) != null )	{
			//alert($(obj).id+' '+ ' ' +current_menu) ;
			$(current_menu).style.backgroundImage='url(../../i/icons/bg_h30_off.gif)';
		}
		$(obj).style.backgroundImage='url(../../i/icons/bg_h30_on.gif)';
	}
	current_menu = $(obj).id ;	
}


//	ACCOREDON EFFECT ON MEMEBER MENU
//  In my case I want to load them onload, this is how you do it!
// 
//Event.observe(window, 'load', loadAccordions, false);

//
//	Set up all accordions
//
var accordeon_menu_Accordion;
function loadAccordions(activ_menu) {
	// If main menu
	if ($('accordeon_menu_container') != null ) {
		accordeon_menu_Accordion = new accordion('#accordeon_menu_container');		
	}
}

// Show a specific accordion, take num accordion in param
function _activateAccordion(num) {	
	accordeon_menu_Accordion.activate($$('#accordeon_menu_container .accordion_toggle')[num]);
}
function _deactivateAccordion(num) {	
	accordeon_menu_Accordion.deactivate($$('#accordeon_menu_container .accordion_toggle')[num]);
}

// Reload submenu
function _reload_member_subMenu() {
	// Reload subenu
	var ajax = new Ajax.Updater(
	'member_sub_menu', 
	'module/membre/refresh_subMenu.php?date='+ encodeURIComponent((new Date()).getTime()),
	{
		asynchronous:true, 
		method:'get',
		onComplete:function(){ 
			accordeon_menu_Accordion.refresh() ;
			new Effect.Highlight('member_sub_menu', {duration: 1.0}) ;				
		}
	}			
	);			
}

// Store an annonce
function store_memoAnnonce(id) {
	// Request to store annonce and update divs
	var ajax = new Ajax.Updater(
	'memo_ajax', 
	'module/annonces/ajax/memo_annonce.php?date='+ encodeURIComponent((new Date()).getTime())+'&id=' + id,
	{
		asynchronous:true, 
		method:'get',
		onSuccess:function(){ 
			new Effect.Highlight('memo_ajax', {duration: 0.5}) ;								
		},
		onComplete:function(){ 
			$("memo_ajax_menu").innerHTML = $("memo_ajax").innerHTML ;
			accordeon_menu_Accordion.refresh() ;
			new Effect.Highlight('memo_ajax_menu', {duration: 0.5}) ;			
		}
	}			
	);	
}
// Delete a selected annonce
function del_memoAnnonce(obj, id) {
	// Delete annonce and Reload subenu
	var ajax = new Ajax.Request(
	'module/annonces/ajax/memo_annonce.php?date='+ encodeURIComponent((new Date()).getTime())+'&delete=true&id=' + id,
	{
		asynchronous:true, 
		method:'get',
		onSuccess:function(){ 
			parent.$(obj).innerHTML = '' ;
			_reload_member_subMenu() ;	
			//new Effect.Highlight('member_sub_menu', {duration: 0.5}) ;
		}
	}			
	);
}

// Store a search as alert
var store_memoAlert_obj ;
function store_memoAlert(obj) {
	// Display input box
	store_memoAlert_obj = obj ;
	// Look if not yet stored
	var ajax = new Ajax.Request(
	'module/annonces/modalBox/_store_memoAlert.php?date='+ encodeURIComponent((new Date()).getTime())+'&if_exists=true',
	{
		asynchronous:true, 
		method:'get',
		onComplete:function(ajaxResponse){ 
			// Already stored
			if ( ajaxResponse.responseText == 'msg_300' ) {
				$(obj).innerHTML = '<strong>Alerte déjà enregistrée</strong>' ;
				new Effect.Highlight($(obj), {duration: 0.5}) ;
			// Show modal box
			} else if ( ajaxResponse.responseText == 'msg_200' ) {
				Modalbox.show('Enregistrez la recherche en cours', '/module/annonces/modalBox/_store_memoAlert.php', {width: 600, height: 250});						
			}			
			//new Effect.Highlight('member_sub_menu', {duration: 0.5}) ;
		}
	}			
	);
}
// Store a search request
function _store_memoAlert() {
	// Check login field
	if ( $('alert_nom').value == '' ) {
		$('alert_nom').focus() ;		
		alert('Veuillez donnez un nom à votre alerte') ;
		return false;	
	}
	
	// Display ajax indicator
	$('mail_button').className = 'loading' ;
	
	// Send mail and display status
	new Ajax.Request('/module/annonces/modalBox/_store_memoAlert.php',  
		{asynchronous:false, 
		 parameters:Form.serialize($('form_memoAlert')),
		 onComplete:function(ajaxRequest) { 
			if ( ajaxRequest.responseText == 'msg_100' ) {
				Modalbox.hide() ;				
				_reload_member_subMenu() ;				
				$(store_memoAlert_obj).innerHTML = '<strong>Alerte enregistrée</strong>' ;						
			}
		}
		});		
}
// Delete a selected annonce
function del_memoAlert(obj, id) {
	// Delete annonce and Reload subenu
	var ajax = new Ajax.Request(
	'module/annonces/modalBox/_store_memoAlert.php?date='+ encodeURIComponent((new Date()).getTime())+'&delete=true&id=' + id,
	{
		asynchronous:true, 
		method:'get',
		onComplete:function(){ 
			parent.$(obj).innerHTML = '' ;
			_reload_member_subMenu() ;
			//new Effect.Highlight('member_sub_menu', {duration: 0.5}) ;				
		}
	}			
	);
}

// Store a search as alert
var store_memoSearch_obj ;
function store_memoSearch(obj) {
	// Display input box
	store_memoSearch_obj = obj ;
	// Look if not yet stored
	var ajax = new Ajax.Request(
	'module/annonces/modalBox/_store_memoSearch.php?date='+ encodeURIComponent((new Date()).getTime())+'&if_exists=true',
	{
		asynchronous:true, 
		method:'get',
		onComplete:function(ajaxResponse){ 
			// Already stored
			if ( ajaxResponse.responseText == 'msg_300' ) {
				$(obj).innerHTML = '<strong>Recherche déjà enregistrée</strong>' ;
				new Effect.Highlight($(obj), {duration: 0.5}) ;
			// Show modal box
			} else if ( ajaxResponse.responseText == 'msg_200' ) {
				Modalbox.show('Enregistrez la recherche en cours', '/module/annonces/modalBox/_store_memoSearch.php', {width: 600, height: 150});						
			}			
			//new Effect.Highlight('member_sub_menu', {duration: 0.5}) ;
		}
	}			
	);
}
// Store a search request
function _store_memoSearch() {
	// Check login field
	if ( $('alert_nom').value == '' ) {
		$('alert_nom').focus() ;		
		alert('Veuillez donnez un nom à votre recherche') ;
		return false;	
	}
	
	// Display ajax indicator
	$('mail_button').className = 'loading' ;
	
	// Send mail and display status
	new Ajax.Request('/module/annonces/modalBox/_store_memoSearch.php',  
		{asynchronous:false, 
		 parameters:Form.serialize($('form_memoSearch')),
		 onComplete:function(ajaxRequest) { 
			if ( ajaxRequest.responseText == 'msg_100' ) {
				Modalbox.hide() ;				
				_reload_member_subMenu() ;				
				$(store_memoSearch_obj).innerHTML = '<strong>Recherche enregistrée</strong>' ;			
			}
		}
		});		
}
// Delete a selected annonce
function del_memoSearch(obj, id) {
	// Delete annonce and Reload subenu
	var ajax = new Ajax.Request(
	'module/annonces/modalBox/_store_memoSearch.php?date='+ encodeURIComponent((new Date()).getTime())+'&delete=true&id=' + id,
	{
		asynchronous:true, 
		method:'get',
		onComplete:function(){ 
			parent.$(obj).innerHTML = '' ;
			_reload_member_subMenu() ;				
			//new Effect.Highlight('member_sub_menu', {duration: 0.5}) ;
		}
	}			
	);
}