//Browser Support Code


function AjaxEnabled()
{
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
	
}



function AjaxCall(action,ArrayQuery){
	  	
var ajaxRequest = AjaxEnabled();

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState != 4) 
			{  
			
				if(document.getElementById('infoContainer'))
				{
				//document.getElementById('infoContainer').style.visibility = 'visible';
				//document.getElementById('info').value = ajaxRequest.readyState; 
				}
			
			}
			
			if(ajaxRequest.readyState == 4) 
			{  
			
				if(document.getElementById('infoContainer'))
				{
				//document.getElementById('infoContainer').style.display = 'none';
				//document.getElementById('info').value = ''; 
				}
				
				if(ajaxRequest.responseText !='')
				{
				eval(action)(ajaxRequest); 
				}
			}
		
		}
		
		var queryString = '';
		
		for(i=0;i<ArrayQuery.length;i++) { queryString += ArrayQuery[i]+'&'; }
		
		queryString = queryString.substring(0,queryString.length-1);
		
		ajaxRequest.open("POST", 'php_Ajax/'+action+".php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
		ajaxRequest.send(queryString); 

}



//fonctions Parametres
/*
function saveNewsletterSubject(ajaxRequest){ }

function UpdateBgColor(ajaxRequest) { MainFrame.style.backgroundColor = ajaxRequest.responseText; }

function UpdateFgColor(ajaxRequest)
{
var NewsletterContent = document.getElementById('NewsletterContent');
NewsletterContent.style.backgroundColor = ajaxRequest.responseText;
}

function SwitchTypeDelimitationSections(ajaxRequest)
{
AjaxCall('IncludeNewsletter',[]);
}
/
/*Appel */


//Génération de l'arborescence des rubrique 
/*function GenListRub(ajaxRequest)
{ 
document.getElementById('GenRub').innerHTML = ajaxRequest.responseText;
}

function BoxAddItem(ajaxRequest)
{ 
document.getElementById('BoxItem').innerHTML = ajaxRequest.responseText;

/* On ajoute l'evenement sur le boutton une fois sa crétation */
//var BTAddRub = document.getElementById('BT_AddRub');
/*hookEvent(BTAddRub,'mousedown',InsertRub);
}
*/




/*
//fonctions Hierarchy
function SuppFooterHeader(ajaxRequest)
{
AjaxCall('SwitchLeftFrameContent',['PageId=hierarchy'] );
AjaxCall('IncludeNewsletter',[]);
}

function SuppArticle(ajaxRequest)
{
AjaxCall('SwitchLeftFrameContent',['PageId=hierarchy'] );
AjaxCall('IncludeNewsletter',[]);
}


function SuppSection(ajaxRequest)
{
AjaxCall('SwitchLeftFrameContent',['PageId=hierarchy'] );
AjaxCall('IncludeNewsletter',[]);
}


function MoveArticle()
{
AjaxCall('IncludeNewsletter',[]);
}


function MoveSection()
{
AjaxCall('IncludeNewsletter',[]);
}



//fonctions générales
function IncludeNewsletter(ajaxRequest)
{
var MainFrame = document.getElementById('MainFrame');

MainFrame.innerHTML = ajaxRequest.responseText;
MainFrameScrollHeight = MainFrame.scrollHeight;

var NewsletterContent = document.getElementById('NewsletterContent');
NewsletterContent.style.height = MainFrameScrollHeight+'px';

var ArticlesItems = getElementsByClassName('NewsletterArticle', 'DIV', NewsletterContent);
var SectionsTitresItems = getElementsByClassName('NewsletterSectionTitre', 'DIV', NewsletterContent);

for(j=0;j<SectionsTitresItems.length;j++)
	{
	hookEvent(SectionsTitresItems[j],'mousedown', EditSection);
	hookEvent(SectionsTitresItems[j],'mouseover', RollOver);
	hookEvent(SectionsTitresItems[j],'mouseout', RollOut);
	}

for(j=0;j<ArticlesItems.length;j++)
	{
	hookEvent(ArticlesItems[j],'mousedown', EditArticle);
	hookEvent(ArticlesItems[j],'mouseover', RollOverArticle);
	hookEvent(ArticlesItems[j],'mouseout', RollOutArticle);
	}

}





function UpdateColorSelection(ajaxRequest)
{
var ColorSelection = document.getElementById('ColorSelection');
ColorSelection.innerHTML = ajaxRequest.responseText;

var Palette = document.getElementById('ColorSelection');

	var _SquareColorSelection =getElementsByClassName('SquareColorSelection', 'DIV', Palette);
	for(i=0;i<_SquareColorSelection.length;i++)  
	{  
	hookEvent(_SquareColorSelection[i],'mousedown', AddCouleurs );  
	}
	
}


function SwitchLeftFrameContent(ajaxRequest)
{
var _LeftFrameContent = document.getElementById('LeftFrameContent');
_LeftFrameContent.innerHTML = ajaxRequest.responseText;

if(document.getElementById('BlocAddSection'))
	{
	InitHierarchy();
	}
	
	if(document.getElementById('BgColor'))
	{
	InitParameters();
	}
	
}



function SaveSectionName()
{
	
}


function UpdateSectionColor()
{
	
}


//Fonctions éditions
function InitMenuOptions(ajaxRequest)
{
	
_MainMenuOptionsContainer = document.getElementById('MainMenuOptionsContainer');
_MainMenuOptionsContainer.innerHTML = ajaxRequest.responseText;


var editorContainer = document.getElementById('editorContainer');
var menuEdit = document.getElementById('menuEdit');
var imgEdit = document.getElementById('menuImg');

	var EditBoutons = menuEdit.getElementsByTagName('IMG');

	for(i=0;i<EditBoutons.length;i++) 
	{
	 
	if(EditBoutons[i].name =='colorPicker') { hookEvent(EditBoutons[i],'mousedown', ShowPalette); }
	else { hookEvent(EditBoutons[i],'mousedown', ChangeText); }
	
	hookEvent(EditBoutons[i],'mouseover', swapImage); 
	hookEvent(EditBoutons[i],'mouseout', swapImage); 
	
	}


	_blocCouleur = document.getElementById('blocCouleur');
	hookEvent(_blocCouleur,'mousedown', doColor);
	
	var _MyYopLibrary = document.getElementById('MyYopLibrary');
		
		hookEvent(_MyYopLibrary,'mousedown', OpenMyLibrary);
		hookEvent(_MyYopLibrary,'mouseover', RollOver);
		hookEvent(_MyYopLibrary,'mouseout', RollOut); 

		var items = imgEdit.getElementsByTagName('img');
		
		var AlignValue = null;
		for(i=0;i<items.length;i++) 
		{ 
			var state = 'Idle';
			if(AlignValue == items[i].getAttribute('OptionValue')) { state = 'Selected'; }
			items[i].src = 'pix/admin/popuparticle/imgAlign_'+items[i].getAttribute('OptionValue')+'_'+state+'.gif';
			hookEvent(items[i], 'mousedown', AlignImage );  
			}

	
	
	
	
	
	var _addSection = document.getElementById('addSection');
	hookEvent(_addSection, 'mousedown', BeforeShowPopup);
																  
	InitPalette();
}


function ShowPopup(ajaxRequest)
{
	
_PopupContainer = document.getElementById('PopupContainer');
_PopupContent = document.getElementById('PopupContent');

_PopupContent.innerHTML = ajaxRequest.responseText;
_PopupContainer.style.visibility = 'visible';
_PopupContainer.style.top = 150+'px';

hookEvent(document.getElementById('SectionColor'), 'mousedown', ShowPalette)	
setEvents(_PopupContent.getElementsByTagName("input"));
var Titles = _PopupContent.getElementsByTagName("input");
for(i=0;i<Titles.length;i++){ if(Titles[i].getAttribute('langue') != null){ hookEvent(Titles[i],'change', updateSectionName);  } }	

}

function InitIframeContentMainFrame(ajaxRequest)
{
switchMenuOptions('textEdition');
IdArticle = document.getElementById('CURRENT_ARTICLE').value;
var ArticleOver = document.getElementById('Article'+IdArticle);		
var Height = ArticleOver.offsetHeight;
var Width = ArticleOver.offsetWidth;
var iframe = document.getElementById('TextEditionArea');
ArticleOver.className = 'NewsletterArticleSelected';
ArticleOver.innerHTML = '';
iframe.style.borderWidth = 0+'px';
ArticleOver.appendChild(iframe);
iframe.style.visibility = 'visible';
iframe.style.position = 'relative';
iframe.style.height = Height-2+'px';
iframe.style.width = Width+'px';
iframe.contentWindow.document.write(ajaxRequest.responseText);
iframe.contentWindow.document.designMode = "on";
hookEvent(iframe.contentWindow.document,'keyup', ResizeIframe);
hookEvent(iframe.contentWindow.document,'mouseup', TextSelectionFormat);
iframe.contentWindow.document.close();

_MainFrame = document.getElementById('MainFrame');
hookEvent(_MainFrame,'mousedown',saveChanges);
}

function SaveIframeContentMainFrame()
{
switchMenuOptions('GeneralOptions');
var iframe = document.getElementById('TextEditionArea');
unhookEvent(iframe.contentWindow.document,'keyup', ResizeIframe);
unhookEvent(iframe.contentWindow.document,'mouseup', TextSelectionFormat);

if(document.getElementById('BlocAddSection')) { AjaxCall('SwitchLeftFrameContent',['PageId=hierarchy'] ); }

AjaxCall('IncludeNewsletter',[]);
}



function UpdateAlignImgArticle(ajaxRequest)
{
typeAlign = ajaxRequest.responseText;
IframeContent = document.getElementById('TextEditionArea').contentWindow;
img = IframeContent.document.body.getElementsByTagName('IMG');
img[0].align = typeAlign;

	if(typeAlign == 'left') 
	{ 
	img[0].style.marginRight = 8+'px';
	img[0].style.marginLeft = 0+'px';
	}
	
	if(typeAlign == 'right') 
	{ 
	img[0].style.marginRight = 0+'px';
	img[0].style.marginLeft = 8+'px';
	}

}


function UpdateImgArticle()
{
var id_article = document.getElementById('CURRENT_ARTICLE').value;
var _SelectedLangue = document.getElementById('CURRENT_LANGUAGE').value;
AjaxCall('InitIframeContentMainFrame',['id_article='+id_article,'Langue='+_SelectedLangue]);
}
*/