/** Ouverture d'une nouvelle fenetre */
function popupWindow( link )
{
	window.open( link, '', '' );
}

/** Ouverture d'une nouvelle fenetre des CGV */
function popupAddMediaWindow( link, name )
{
	window.open( link, name, 'width=600, height=230, channelmode=no, resizable=no, location=no, menubar=no, status=no, scrollbars=yes, toolbar=no' );
}

/** Ouverture d'une nouvelle fenetre de s?lection de m?dia pour un post */
function popupSelectValidMediaWindow( link, name )
{
	window.open( link, name, 'width=600, height=600, channelmode=no, resizable=no, location=no, menubar=no, status=no, scrollbars=yes, toolbar=no' );
}

/** Ouverture d'une nouvelle fenetre des CGV */
function popupCGVWindow( link )
{
	window.open( link, '', 'width=600, height=600, channelmode=no, resizable=no, location=no, menubar=no, status=no, scrollbars=yes, toolbar=no' );
}

/** Ouverture d'une nouvelle fenetre des CGV */
function popupCdtWindow( link )
{
	window.open( link, '', 'width=600, height=500, channelmode=no, resizable=no, location=no, menubar=no, status=no, scrollbars=yes, toolbar=no' );
}

/** Ouverture d'une nouvelle fenetre en 1024 * 768 */
function popupBigWindow( link )
{
	window.open( link, '', 'width=1024, height=768, channelmode=yes, resizable=yes, location=yes, menubar=yes, status=yes, scrollbars=yes, toolbar=yes' );
}

/** Ouverture d'une nouvelle fenetre pour le profil */
function popupProfileWindow( link )
{
	window.open( link, '', 'width=675, height=700, channelmode=no, resizable=no, location=no, menubar=no, status=yes, scrollbars=yes, toolbar=no' );
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/** Active le lien sur la fen?tre m?re et ferme la fen?tre courante */
function openParentWindowAndClose( link )
{
	window.opener.location.href = link;
	if (window.opener.progressWindow)
	{
		window.opener.progressWindow.close();
	}
	window.close();
}

/** ------------------------------------------------- */
/** 	Appel du post du formulaire de paiement		  */
/** ------------------------------------------------- */
function submitPaymentForm()
{
	document.PaymentRequest.submit();
}

/** ------------------------------------------------- */
/** 	Appel du post du formulaire d'autentification */
/** ------------------------------------------------- */
function submitAuthenticateMenuForm()
{
	document.getElementById('authenticateMenuForm').submit();
}

/** ------------------------------------------------- */
/** 	Appel du post du formulaire de recherche de blog */
/** ------------------------------------------------- */
function submitSearchMenuForm()
{
	document.getElementById('searchMenuForm').submit();
}

/** ------------------------------------------------- */
/** 	Appel du post du formulaire de recherche tri?e de blog */
/** ------------------------------------------------- */
function submitSearchSortedForm()
{
	document.getElementById('searchSortedForm').submit();
}

/** ------------------------------------------------- */
/** 	Validation du formulaire de profil	 		  */
/** ------------------------------------------------- */
function validateAdminUserProfileForm()
{
	var sText = "";
	var vCount = 0;	
	
	// validation des champs
	var t_firstName = document.getElementById( 'firstName' );
	var t_lastName = document.getElementById( 'lastName' );
	var t_password = document.getElementById( 'password' );
	var t_password2 = document.getElementById( 'password2' );
	var t_description = document.getElementById( 'description' );

	if ( t_firstName.value == "" )
	{
		vTemp = "";
		if ( vCount >= 1 )
		{
			vTemp = ",";
		}
		sText += vTemp + " 'Pr?nom' est un champ obligatoire";
		vCount++;
	}

	if ( t_lastName.value == "" )
	{
		vTemp = "";
		if ( vCount >= 1 )
		{
			vTemp = ",";
		}
		sText += vTemp + " 'Nom' est un champ obligatoire";
		vCount++;
	}

	if ( t_password.value != "" && t_password.value != t_password2.value )
	{
		vTemp = "";
		if ( vCount >= 1 )
		{
			vTemp = ",";
		}
		sText += vTemp + " 'Confirmation du mot de passe' doit ?tre identique au premier mot de passe";
		vCount++;
	}

	if ( t_description.value.length >= 4000 )
	{
		vTemp = "";
		if ( vCount >= 1 )
		{
			vTemp = ",";
		}
		sText += vTemp + " 'Description' ne doit pas contenir plus de 4000 caract?res";
		vCount++;
	}

	if ( vCount > 0 )
	{
		if ( vCount > 1 )
		{
			sText = "Les champs suivants sont invalides : " + sText;
		}
		else
		{
			sText = "Le champ suivant est invalide : " + sText;
		}
		alert( sText );
		return false;
	}
	
	return true;
}

/** ------------------------------------------------- */
/** 	Validation du formulaire de param?tres 		  */
/** ------------------------------------------------- */
function validateAdminUserParametersForm()
{	
	var sText = "";
	var vCount = 0;	
	
	// validation des champs
	var t_title = document.getElementById( 'title' );
	var t_header = document.getElementById( 'headerChapo' );

	var t_ftpHost = document.getElementById( 'freeFTPHost' );
	var t_ftpPort = document.getElementById( 'freeFTPPort' );
	var t_ftpLogin = document.getElementById( 'freeFTPLogin' );
	var t_ftpPassword = document.getElementById( 'freeFTPPassword' );
	var t_ftpPassword2 = document.getElementById( 'freeFTPPassword2' );
	var t_ftpDirectory = document.getElementById( 'freeFTPDirectory' );
	var t_ftpHTTPURL = document.getElementById( 'freeHTTPURL' );
	
	var t_isFreeFormula = document.getElementById( 'isFreeFormula' );

	if ( t_header.value.length >= 800 )
	{
		vTemp = "";
		if ( vCount >= 1 )
		{
			vTemp = ",";
		}
		sText += vTemp + " 'Chap? ne doit pas contenir plus de 400 caract?res";
		vCount++;
	}
	
	if ( t_isFreeFormula == "True" )
	{
		if ( t_ftpHost.value == "" )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'Nom du serveur FTP' est un champ obligatoire";
			vCount++;
		}
		if ( t_ftpPort.value == "" )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'Port FTP' est un champ obligatoire";
			vCount++;
		}	
		if ( t_ftpLogin.value == "" )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'Identifiant FTP' est un champ obligatoire";
			vCount++;
		}
		if ( t_ftpDirectory.value == "" )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'R?pertoire des sons' est un champ obligatoire";
			vCount++;
		}
		if ( t_ftpHTTPURL.value == "" )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'URL d'acc?s aux sons HTTP' est un champ obligatoire";
			vCount++;
		}
		if ( t_ftpPassword.value != "" && t_ftpPassword.value != t_ftpPassword2.value )
		{
			vTemp = "";
			if ( vCount >= 1 )
			{
				vTemp = ",";
			}
			sText += vTemp + " 'Confirmation du mot de passe' doit ?tre identique au premier mot de passe";
			vCount++;
		}
	}

	if ( vCount > 0 )
	{
		if ( vCount > 1 )
		{
			sText = "Les champs suivants sont invalides : " + sText;
		}
		else
		{
			sText = "Le champ suivant est invalide : " + sText;
		}
		alert( sText );
		return false;
	}
	
	return true;
}



/** ---------------------------------------------------------------------------- */
/** 				M?thodes de d?tection de l'OS courant						 */
/** ---------------------------------------------------------------------------- */

function isWindowsOS()
{
	return navigator.appVersion.indexOf("Win") != -1;
}

function isLinuxOS()
{
	return navigator.appVersion.indexOf("Linux") != -1;
}

function isUnixOS()
{
	return navigator.appVersion.indexOf("X11") != -1;
}

function isMacOS()
{
	return navigator.appVersion.indexOf("Mac") != -1;
}

function isLinuxOrUnixOS()
{
	return isLinuxOS() || isUnixOS();
}







function checkAuthenticateEnter( e )
{
	// e is event object passed from function invocation
	var characterCode; // literal character code will be stored in this variable
	
	if(e && e.which) { //if which property of event object is supported (NN4)
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		doAuthenticateFromFields();
	}

}

/** ------------------------------------------------- */
/** 	Initialisation de la zone d'ajout d'ami 	  */
/** ------------------------------------------------- */
function updateAddFriendZone( blogId )
{
    var xmlHttpReq = false;
    var self = new Object();
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var requestString = './frontUser.do?method=ajaxCanAddFriendToCurrentLoggedUser&blogId=' + blogId;
    self.xmlHttpReq.open('POST', requestString, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq && self.xmlHttpReq.readyState == 4) {
	        rootResponse = self.xmlHttpReq.responseXML.documentElement;
	        if ( rootResponse.getAttribute( 'status' ) == 'true' )
	        {
				document.getElementById( 'addFriendZone' ).innerHTML = '<a href="javascript:doAddFriendToBlog(\'' + blogId + '\');">[ + ] ' + rootResponse.getAttribute( 'message' ) + '</a>';
			}
			else
			{
				document.getElementById( 'addFriendZone' ).innerHTML = rootResponse.getAttribute( 'message' );
			}
        }
    }
    self.xmlHttpReq.send('');
}


/** ------------------------------------------------- */
/** 	Mise ? jour de la zone d'ajout d'ami 		  */
/** ------------------------------------------------- */
function doAddFriendToBlog( blogId ) 
{
    var xmlHttpReq = false;
    var self = new Object();
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var requestString = './frontUser.do?method=ajaxAddFriendToCurrentLoggedUser&blogId=' + blogId;
    self.xmlHttpReq.open('POST', requestString, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq && self.xmlHttpReq.readyState == 4) {
	        rootResponse = self.xmlHttpReq.responseXML.documentElement;
			document.getElementById( 'addFriendZone' ).innerHTML = rootResponse.getAttribute( 'message' );
        }
    }
    self.xmlHttpReq.send('');
}

/** ------------------------------------------------- */
/** 	Authentification d'un user					  */
/** ------------------------------------------------- */
function doAuthenticate( blogName, password, blogId, webSiteZone ) 
{
    var xmlHttpReq = false;
    var self = new Object();
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    contextZonePath = '..';
    canUpdateAddFriendZone = 'false';
    if ( webSiteZone == 'front' )
	{
		contextZonePath = '.';
	}
	else if ( webSiteZone == 'frontUser' )
	{
		canUpdateAddFriendZone = 'true';
	}
	else if ( webSiteZone == 'community' )
	{
		contextZonePath = '.';
	}
    
    var requestString = contextZonePath + '/authenticate.do?method=ajaxAuthenticate&blogName=' + blogName + '&password=' + password;
    self.xmlHttpReq.open('POST', requestString, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq && self.xmlHttpReq.readyState == 4) {
	        rootResponse = self.xmlHttpReq.responseXML.documentElement;
	        if ( rootResponse.getAttribute( 'status' ) == 'true' )
	        {	        
	        	if ( canUpdateAddFriendZone == 'true' )
	        	{
	        		if ( blogId != -1 )
	        		{
						updateAddFriendZone( blogId );
					}
				}
				updateLoginZone( null, webSiteZone );
			}
			else
			{
				updateLoginZone( rootResponse.getAttribute( 'message' ), webSiteZone );
			}
        }
    }
    self.xmlHttpReq.send('');
}

/** ------------------------------------------------- */
/** 	Mise ? jour de la zone d'autentification	  */
/** ------------------------------------------------- */
function updateLoginZone( message, webSiteZone ) 
{
    var xmlHttpReq = false;
    var self = new Object();
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    contextZonePath = '..';
    if ( webSiteZone == 'front' )
	{
		contextZonePath = '.';
	}
	else if ( webSiteZone == 'community' )
	{
		contextZonePath = '.';
	}
    var requestString = contextZonePath + '/authenticate.do?method=ajaxGetLoggedUser';   
    self.xmlHttpReq.open('POST', requestString, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq && self.xmlHttpReq.readyState == 4) {
        
	        rootResponse = self.xmlHttpReq.responseXML.documentElement;
	        if ( rootResponse.getAttribute( 'status' ) == 'true' )
	        {
	        
	        	htmlAccessBlog = '';
				if ( webSiteZone == 'adminUser' )
				{
					htmlAccessBlog = '<a href="' + contextZonePath + '/' + rootResponse.getAttribute( 'blogName' ) + '/frontUser.do?method=getHomePage&blogName=' + rootResponse.getAttribute( 'blogName' ) + '">' + rootResponse.getAttribute( 'messageAccessBlog' ) + '</a>';
				}
				else
				{
					htmlAccessBlog = '<a href="' + contextZonePath + '/admin/adminUser.do?method=getHomePage">' + rootResponse.getAttribute( 'messageAdminBlog' ) + '</a>';
				}
	        
   				document.getElementById( 'notLoggedZone' ).style.display = 'none';
   				document.getElementById( 'loggedZone' ).style.display = 'block';

				document.getElementById( 'loggedZone' ).innerHTML = 
				'<h3 class="menuTitle">Mon Audioblog</h3>' +
				'<table class="menuContentTable">' +
				'<tr><td colspan="2"><p class="menuContent">' +
				'<a href="' + contextZonePath + '/admin/adminUser.do?method=getHomePage">' + rootResponse.getAttribute( 'firstName' ) +
				' ' + rootResponse.getAttribute( 'lastName' ) + '</a> ' + rootResponse.getAttribute( 'messageIsConnected' ) +
				'</p></td></tr>' + 
				'<tr><td colspan="2"><p class="menuContent"><br />' +
				htmlAccessBlog +
				'</p></td></tr>' + 
				'<tr><td colspan="2"><p class="menuContent">' +
				'<a href="javascript:doLogout(\'' + webSiteZone + '\');">' + rootResponse.getAttribute( 'messageToDisconnect' ) + '</a>' +
				'<br /><a href="http://www.arteradio.com/forum/forums/show/801.page" class="menu" target="_blank">Forum</a>' +
				'</p></td></tr>' + 
				'</table>';
			}
			else
			{
				htmlMessage = '';
				if ( message != null )
				{
					document.getElementById( 'notLoggedMessageZone' ).innerHTML = message;
				}
				document.getElementById( 'notLoggedZone' ).style.display = 'block';
				document.getElementById( 'loggedZone' ).style.display = 'none';
				
			}
        }
    }
    self.xmlHttpReq.send('');
}

/** ------------------------------------------------- */
/** 			Logout du user						  */
/** ------------------------------------------------- */
function doLogout( webSiteZone )
{
    var xmlHttpReq = false;
    var self = new Object();
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
	contextZonePath = '..';
    if ( webSiteZone == 'front' )
	{
		contextZonePath = '.';
	}
	else if ( webSiteZone == 'community' )
	{
		contextZonePath = '.';
	}
    
    var requestString = contextZonePath + '/authenticate.do?method=ajaxLogout';
    self.xmlHttpReq.open('POST', requestString, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq && self.xmlHttpReq.readyState == 4) {
	        rootResponse = self.xmlHttpReq.responseXML.documentElement;
	        if ( rootResponse.getAttribute( 'status' ) == 'true' )
	        {
	        	updateLoginZone( null, webSiteZone );
	        	if ( webSiteZone == 'adminUser' )
	        	{
	        		document.location = '../front.do?method=getHomePage';
	        	}
	        	updateAddFriendZone( 0 );
			}
        }
    }
    self.xmlHttpReq.send('');
}