// JavaScript Document
//var adresse = "http://localhost:8888/mobcom/mobcom/";

var roll = true;

var decal = -247;
var menu = false;

/*function deroul(objet, sens)
{		
	affiche(objet, decal, 0, sens);
}*/

function deroul(pos, objectif)
{
	$('menu_gauche').style.marginLeft = pos+'px';
	$('menu_droite').style.marginRight = pos+'px';

	pos += 4;
	
	if(pos <= objectif)
	{
		setTimeout('deroul('+pos+','+objectif+')',5);
	} else
	{
		$('menu_gauche').style.marginLeft = objectif+'px';
		$('menu_droite').style.marginRight = objectif+'px';
	}
}

function enroul(objet, sens)
{
	range(objet, 0, decal, sens);
}

function cache(pos, objectif, reponse) 
{
	$('menu_gauche').style.marginLeft = pos+'px';
	$('menu_droite').style.marginRight = pos+'px';

	pos -= 4;
	
	if(pos >= objectif)
	{
		setTimeout('cache('+pos+','+objectif+', "'+reponse+'")',5);
	} else
	{
		var Req = new Request(
			{
				url: adresse+"menu/connect.php", 
				method: 'post', 
				data: 'reponse='+reponse,
				onSuccess: function(responseText, responseXML)
				{
					$('contenu').innerHTML = responseText;
					charge_gauche(reponse);
				}
			}
		);
		Req.send();
	}
}

function charge_gauche(reponse)
{
	var Req = new Request(
		{
			url: adresse+"menu/jdb.php", 
			method: 'post', 
			data: 'reponse='+reponse,
			onSuccess: function(responseText, responseXML)
			{
				$('menu_gauche').innerHTML = responseText;
				deroul(-247, 0);
			}
		}
	);
	Req.send();	
}

function affiche(objet, pos, objectif, sens) 
{
	if(sens == 0)
	{
		$(objet).style.marginLeft = pos+'px';
	} else
	{
		$(objet).style.marginRight = pos+'px';
	}
	
	pos += 4;
	
	if(pos <= objectif)
	{
		setTimeout('affiche("'+objet+'",'+pos+','+objectif+','+sens+')',5);
	} else
	{
		if(sens == 0)
		{
			$(objet).style.marginLeft = objectif+'px';
		} else
		{
			$(objet).style.marginRight = objectif+'px';
		}
	}
}

function range(objet, pos, objectif, sens) 
{
	if(sens == 0)
	{
		$(objet).style.marginLeft = pos+'px';
	} else
	{
		$(objet).style.marginRight = pos+'px';
	}
	
	pos -= 4;
	
	if(pos >= objectif)
	{
		setTimeout('range("'+objet+'",'+pos+','+objectif+','+sens+')',5);
	} else
	{
		if(sens == 0)
		{
			$(objet).style.marginLeft = objectif+'px';
		} else
		{
			$(objet).style.marginRight = objectif+'px';
		}
	}
}

function connexion()
{
	if($('code').value == "" || $('pass').value == "")
	{
		alert('Tous les champs de connexion sont obligatoires.');
	} else
	{
		var Req = new Request(
			{
				url: adresse+"connexion/connec.php", 
				method: 'post', 
				data: 'code='+$('code').value+'&pass='+$('pass').value,
				onSuccess: function(responseText, responseXML)
				{
					var reponse = responseText.split("!!--!!");
					
					if(reponse[0] == 'ok')
					{
						menu = true;
						cache(0, -247, reponse[1]);
						//enroul('menu_droite', 0);
					} else
					{
						alert(responseText);
					}
				}
			}
		);
		Req.send();
	}
}

function connexion_partenaire()
{
	if($('code').value == "" || $('pass').value == "")
	{
		alert('Tous les champs de connexion sont obligatoires.');
	} else
	{
		var Req = new Request(
			{
				url: adresse+"partenaire/connexion/connec.php", 
				method: 'post', 
				data: 'code='+$('code').value+'&pass='+$('pass').value,
				onSuccess: function(responseText, responseXML)
				{
					if(responseText == '')
					{
						charge('partenaire.php');
					} else
					{
						alert(responseText);
					}
				}
			}
		);
		Req.send();
	}
}

function rollover(id)
{
	if(roll)
	{
		$('#'+id).css('color', '#960').css('backgroundImage', 'url(img/barre_menu.png)');
		roll = false;
	} else
	{
		$('#'+id).css('color', '#999').css('backgroundImage', '');
		roll = true;
	}
}

function in_array (needle, haystack, argStrict) 
{ 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

function array_key_exists ( key, search ) 
{
    if (!search || (search.constructor !== Array && search.constructor !== Object)){
        return false;
    }
 
    return key in search;
}


function select_case(nouv, pos)
{
	$('tinymce_cache').style.display = "none";
	$('btn_supr_textarea').style.display = "none";
	textarea = window.tinyMCE.get("contenu_textarea");
	$('taille_cell').style.display = "none";
	$('taille_plus_cell').value = nouv;
	$('taille_plus_pos').value = pos;
	textarea.setContent('');
	
	if(zone != null)
	{
		tabl1 = zone.split("_");
		tabl2 = selection .split("_");
		
		if(tabl1.length > 4)
		{
			tabl1_row_max = parseInt(tabl1[4]) - 1 + parseInt(tabl1[1]);
			tabl1_col_max = parseInt(tabl1[5]) - 1 + parseInt(tabl1[3]);
		} else
		{
			tabl1_row_max = tabl1[1];
			tabl1_col_max = tabl1[3];
		}
		
		if(tabl2.length > 4)
		{
			tabl2_row_max = parseInt(tabl2[4]) - 1 + parseInt(tabl2[1]);
			tabl2_col_max = parseInt(tabl2[5]) - 1 + parseInt(tabl2[3]);
		} else
		{
			tabl2_row_max = tabl2[1];
			tabl2_col_max = tabl2[3];
		}
		
		if(tabl1_row_max > tabl2_row_max)
		{
			row_max = tabl1_row_max;
		} else 
		{
			row_max = tabl2_row_max;
		}
		
		if(tabl1[1] > tabl2[1])
		{
			row_min = tabl2[1];
		} else
		{
			row_min = tabl1[1];
		}
		
		if(tabl1_col_max > tabl2_col_max)
		{
			col_max = tabl1_col_max;
		} else
		{
			col_max = tabl2_col_max;
		}
		
		if(tabl1[3] > tabl2[3])
		{
			col_min = tabl2[3];
		} else
		{
			col_min = tabl1[3];
		}
		

		new_data = verif(row_min, row_max, col_min, col_max);
		
		for(row = new_data['row_min']; row <= new_data['row_max']; row++)
		{
			for(col = new_data['col_min']; col <= new_data['col_max']; col++)
			{
				if(!in_array("row_"+row+"_col_"+col, cell_delete))
				{
					if(array_key_exists("row_"+row+"_col_"+col, cell_fusion))
					{
						$(cell_fusion["row_"+row+"_col_"+col]).style.backgroundColor = "";
						$(cell_fusion["row_"+row+"_col_"+col]).style.MozOpacity = "1";
						$(cell_fusion["row_"+row+"_col_"+col]).style.opacity = "1";
					} else
					{
						$("row_"+row+"_col_"+col).style.backgroundColor = "";
						$("row_"+row+"_col_"+col).style.MozOpacity = "1";
						$("row_"+row+"_col_"+col).style.opacity = "1";
					}
				}
				zone = nouv;
			}
		}
		zone = null;
		selection = null;
					
		var Req = new Request(
			{
				url: adresse+"menu/utilisateur/email/ajout/index.php", 
				method: 'post', 
				data: 'pos='+pos,
				onSuccess: function(responseText, responseXML)
				{
					$('description_news').innerHTML = responseText;
				}
			}
		);
		Req.send();
	} else
	{
		if(selection == null)
		{
			$(nouv).style.backgroundColor = "#9FF";
			$(nouv).style.MozOpacity = "0.3";
			$(nouv).style.opacity = "0.3";
			selection = nouv;
			
			
			tabl1 = nouv.split("_");
			
			$('description_news').innerHTML = "";
			
			if(tabl1.length > 4)
			{
				var Req = new Request(
					{
						url: adresse+"menu/utilisateur/email/fusion/delete.php", 
						method: 'post', 
						data: 'case=row_'+tabl1[1]+"_col_"+tabl1[3]+'&pos='+pos,
						onSuccess: function(responseText, responseXML)
						{		
							$('taille_cell').style.display = "Block";
							$('description_news').innerHTML = responseText+ "<br /><br />";
						}
					}
				);
				Req.send();
			} 
			
			var Req = new Request(
				{
					url: adresse+"menu/utilisateur/email/ajout/choix.php", 
					method: 'post', 
					data: 'case='+nouv+'&pos='+pos,
					onSuccess: function(responseText, responseXML)
					{
						reponse_div = responseText.split('..//..');
						if(reponse_div[0] == "texte")
						{			
							$('tinymce_cache').style.display = "Block";
							$('btn_supr_textarea').style.display = "Block";
							$('cell_textarea').value = nouv;
							$('pos_textarea').value = pos;
							$('cell_del_textarea').value = nouv;
							$('pos_del_textarea').value = pos;
							textarea.setContent(reponse_div[1]);
						} else
						{
							$('description_news').innerHTML += reponse_div[1];
						}	
						$('taille_cell').style.display = "Block";
					}
				}
			);
			Req.send();
			
		} else
		{
			if(selection == nouv)						/* On clic sur la meme case pour deselectionner */
			{
				$(nouv).style.backgroundColor = "";
				$(nouv).style.MozOpacity = "1";
				$(nouv).style.opacity = "1";
				selection = null;
				
				
				var Req = new Request(
					{
						url: adresse+"menu/utilisateur/email/ajout/index.php", 
						method: 'post', 
						data: 'pos='+pos,
						onSuccess: function(responseText, responseXML)
						{		
							$('description_news').innerHTML = responseText;
						}
					}
				);
				Req.send();
			} else										/* on clic sur une autre case pour selectionner une zone */
			{
				tabl1 = nouv.split("_");
				tabl2 = selection .split("_");
				
				if(tabl1.length > 4)
				{
					tabl1_row_max = parseInt(tabl1[4]) - 1 + parseInt(tabl1[1]);
					tabl1_col_max = parseInt(tabl1[5]) - 1 + parseInt(tabl1[3]);
				} else
				{
					tabl1_row_max = tabl1[1];
					tabl1_col_max = tabl1[3];
				}
				
				if(tabl2.length > 4)
				{
					tabl2_row_max = parseInt(tabl2[4]) - 1 + parseInt(tabl2[1]);
					tabl2_col_max = parseInt(tabl2[5]) - 1 + parseInt(tabl2[3]);
				} else
				{
					tabl2_row_max = tabl2[1];
					tabl2_col_max = tabl2[3];
				}
				
				if(tabl1_row_max > tabl2_row_max)
				{
					row_max = tabl1_row_max;
				} else 
				{
					row_max = tabl2_row_max;
				}
				
				if(tabl1[1] > tabl2[1])
				{
					row_min = tabl2[1];
				} else
				{
					row_min = tabl1[1];
				}
				
				if(tabl1_col_max > tabl2_col_max)
				{
					col_max = tabl1_col_max;
				} else
				{
					col_max = tabl2_col_max;
				}
				
				if(tabl1[3] > tabl2[3])
				{
					col_min = tabl2[3];
				} else
				{
					col_min = tabl1[3];
				}
				
				
				new_data = verif(row_min, row_max, col_min, col_max);
				
				
				for(row = parseInt(new_data['row_min']); row <= parseInt(new_data['row_max']); row++)
				{
					for(col = parseInt(new_data['col_min']); col <= parseInt(new_data['col_max']); col++)
					{
						if(!in_array("row_"+row+"_col_"+col, cell_delete))
						{
							if(array_key_exists("row_"+row+"_col_"+col, cell_fusion))
							{
								$(cell_fusion["row_"+row+"_col_"+col]).style.backgroundColor = "#9FF";
								$(cell_fusion["row_"+row+"_col_"+col]).style.MozOpacity = "0.3";
								$(cell_fusion["row_"+row+"_col_"+col]).style.opacity = "0.3";
							} else
							{
								$("row_"+row+"_col_"+col).style.backgroundColor = "#9FF";
								$("row_"+row+"_col_"+col).style.MozOpacity = "0.3";
								$("row_"+row+"_col_"+col).style.opacity = "0.3";
							}
						}
						zone = nouv;
					}
				}
				
				nb_row = row_max - row_min + 1;
				nb_col = col_max - col_min + 1;
				
				
				var Req = new Request(
					{
						url: adresse+"menu/utilisateur/email/fusion/index.php", 
						method: 'post', 
						data: 'case=row_'+row_min+'_col_'+col_min+'&pos='+pos+'&nb_row='+nb_row+'&nb_col='+nb_col,
						onSuccess: function(responseText, responseXML)
						{		
							$('description_news').innerHTML = responseText;
						}
					}
				);
				Req.send();
			}
		}
	}
}

function verif(row_min, row_max, col_min, col_max)
{	
	valeur = new Array();
	valeur['row_min'] = row_min;
	valeur['col_min'] = col_min;
	valeur['row_max'] = row_max;
	valeur['col_max'] = col_max;
	
	
	for(row = row_min; row <= row_max; row++)
	{
		for(col = col_min; col <= col_max; col++)
		{
			if(array_key_exists("row_"+row+"_col_"+col, cell_fusion))
			{
				nvl_zone = cell_fusion["row_"+row+"_col_"+col].split("_");
				
				row_max_nv = parseInt(nvl_zone[4]) - 1 + parseInt(nvl_zone[1]);
				col_max_nv = parseInt(nvl_zone[5]) - 1 + parseInt(nvl_zone[3]);
				
				if(nvl_zone[1] < valeur['row_min'])
				{
					valeur['row_min'] = nvl_zone[4];
				}
				
				if(nvl_zone[3] < valeur['col_min'])
				{
					valeur['row_min'] = nvl_zone[4];
				}
				
				if(row_max_nv > valeur['row_max'])
				{
					valeur['row_max'] = row_max_nv;
				}
				
				if(col_max_nv > valeur['col_max'])
				{
					valeur['col_max'] = col_max_nv;
				} 
			}	
			
			if(in_array("row_"+row+"_col_"+col, cell_delete))
			{
				for(i = 0; i < cell_liste.length; i++)
				{
					fusion = cell_fusion[cell_liste[i]].split("_");
					
					
					max_row_fusion = parseInt(fusion[1]) - 1 + parseInt(fusion[4]);
					max_col_fusion = parseInt(fusion[3]) - 1 + parseInt(fusion[5]);
					
					if(row >= parseInt(fusion[1]) && row <= max_row_fusion && col >= parseInt(fusion[3]) && col <= max_col_fusion)
					{
						if(parseInt(fusion[1]) < valeur['row_min'])
						{
							valeur['row_min'] = fusion[1];
						}
						
						if(parseInt(fusion[3]) < valeur['col_min'])
						{
							valeur['row_min'] = fusion[3];
						}
						
						if(max_row_fusion > valeur['row_max'])
						{
							valeur['row_max'] = row_max_nv;
						}
						
						if(max_col_fusion > valeur['col_max'])
						{
							valeur['col_max'] = col_max_nv;
						}
					}
				}
			}
		}
	}
	
	return valeur;
}


function choix_cell(choix, cell, pos)
{
	$('tinymce_cache').style.display = "none";
	$('div_choix').innerHTML = "";
	if(choix == "vide")
	{
		$(cell).innerHTML = "";
	} else if(choix == "texte")
	{
		$('tinymce_cache').style.display = "Block";
		$('cell_textarea').value = cell;
		$('pos_textarea').value = pos;
	} else
	{
		var Req = new Request(
			{
				url: adresse+"menu/utilisateur/email/ajout/"+choix+"/index.php", 
				method: 'post', 
				data: 'case='+cell+'&pos='+pos,
				onSuccess: function(responseText, responseXML)
				{
					$('div_choix').innerHTML = responseText;
				}
			}
		);
		Req.send();
	}
}

function modif_cell(cote, sens, pos, cell)
{
	var Req = new Request(
		{
			url: adresse+"menu/utilisateur/email/ajout/taille.php", 
			method: 'post', 
			data: 'case='+cell+'&pos='+pos+'&sens='+sens+'&cote='+cote,
			onSuccess: function(responseText, responseXML)
			{
				$('newsletter').innerHTML = responseText;
			}
		}
	);
	Req.send();
}

