var home = 'http://venus.iritrack.net/PHA09_front/web/index.php/';

function executeAsynchronousRequest(page, bloc)
{ 	
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;}
        }
     }
    
    xhr.open( "POST", page,  true); 
    
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if((xhr.status  == 200) || (xhr.status  == 0)) 
              {
                if(bloc != null){document.getElementById(bloc).innerHTML = xhr.responseText;}
              }
              else 
              {  
            	  //DO NOTHING
            	  //document.getElementById("test").innerHTML = "Received:" + xhr.responseText;	 //document.ajax.dyn="Error code " + xhr.status;            	  
              }

            if(bloc != null){setTimeout("location.reload()", 0);}
         }
    }; 
   
   xhr.send('done');
} 

function executeSynchronousRequest(page, bloc)
{ 	
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.open( "POST", page,  true); 
    
         if(xhr.readyState  == 4)
         {
              if((xhr.status  == 200) || (xhr.status  == 0)) 
              {
                 document.getElementById(bloc).innerHTML = xhr.responseText;
                 alert(xhr.status);
              }
              else 
              {  
            	  //DO NOTHING
            	  //document.getElementById("test").innerHTML = "Received:" + xhr.responseText;	 //document.ajax.dyn="Error code " + xhr.status;            	  
              }
         }
    
   xhr.send('done');
} 

function changeLanguage(lang, target)
{
	targetPage = home+'display/changeLanguage?language='+lang;
	//targetPage = 'http://tdmic/MDS/web/index.php/display/changeLanguage?language='+lang;
	executeAsynchronousRequest(targetPage, target);
	
	//while(document.getElementById(target).innerHTML != 'done'){};
	//setTimeout("location.reload()", 1000);
	//location.reload();
}

function deleteFilter(filter, offset, target)
{
	//targetPage = 'http://tdmic/MDS/web/index.php/display/deleteFilter?filter='+filter;
	targetPage = home+'display/deleteFilter?filter='+filter+'&offset='+offset;
	executeAsynchronousRequest(targetPage, target);
	//setTimeout("location.reload()", 1000);
    //location.reload();
}

function addFilter(filter, offset, target)
{
	//targetPage = 'http://tdmic/MDS/web/index.php/display/addFilter?filter='+filter;	
	targetPage = home+'display/addFilter?filter='+filter+'&offset='+offset;
	executeAsynchronousRequest(targetPage, target);
	//setTimeout("location.reload()", 1000);
    //location.reload();
}

function resetFilters(target)
{
	//targetPage = 'http://tdmic/MDS/web/index.php/display/resetFilters';
	targetPage = home+'display/resetFilters';
	executeAsynchronousRequest(targetPage, target);
	//setTimeout("location.reload()", 1000);
}

function showPilot(id) {
	
	if(document.getElementById(id).style.display == 'none')
	{
		document.getElementById(id).style.display = 'inline';
	}
	else
	{
		document.getElementById(id).style.display = 'none';
	}
}

function hideColumn(id_cp, id_etape)
{
	
	var elementHeaderIsHidden = 'cp'+id_cp+'_header';
	var elementHeaderIsShowed = 'cp'+id_cp+'_hidden_header';
	var elementIdHidden = 'cp'+id_cp+'_row';
	var elementIdShowed = 'cp'+id_cp+'_hidden_row';

	var showVisibility;
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		showVisibility = 'block';
	}
	else
	{
		showVisibility = 'table-cell';
	}	
	
	setVisibility(elementHeaderIsHidden, 'none');
	setVisibility(elementIdHidden, 'none');
	
	setVisibility(elementHeaderIsShowed, showVisibility);
	setVisibility(elementIdShowed, showVisibility);
	
	targetPage = home+'display/collapseCP?id_cp='+id_cp+'&id_etape='+id_etape;
	executeAsynchronousRequest(targetPage, null);
	
}

function showColumn(id_cp, id_etape)
{
	var elementHeaderIsShowed = 'cp'+id_cp+'_header';
	var elementHeaderIsHidden = 'cp'+id_cp+'_hidden_header';
	var elementIdShowed = 'cp'+id_cp+'_row';
	var elementIdHidden = 'cp'+id_cp+'_hidden_row';
	
	var showVisibility;
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		showVisibility = 'block';
	}
	else
	{
		showVisibility = 'table-cell';
	}
	
	setVisibility(elementHeaderIsHidden, 'none');
	setVisibility(elementIdHidden, 'none');	
	setVisibility(elementHeaderIsShowed, showVisibility);
	setVisibility(elementIdShowed, showVisibility);

	targetPage = home+'display/expandCP?id_cp='+id_cp+'&id_etape='+id_etape;
	executeAsynchronousRequest(targetPage, null);
}

function showHideBloc(bloc)
{		
	if(document.getElementById(bloc).style.display == 'none')
	{
		setVisibility(bloc, 'block');
	}
	else	
	{	
		setVisibility(bloc, 'none');
	}
}

function setVisibility(bloc, visibility)
{
	
	/*
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		//SPECIAL PROCESSING FOR CRAPPY UNNORMALIZED BROWSER (SPIT)
		var elementsArray = document.all(bloc);
		var ct = 0;
		while(ct<elementsArray.length)
		{
			elementsArray[ct].style.display = visibility;
			ct = ct + 1;
		}
	}
	else
	{
	*/
		/* NO NATIVE GETELEMENTSBYNAME JAVASCRIPT FUNCTION HENCE THIS FUNCTION MIGHT BE A LITTLE HEAVY WHEN THERE ARE MANY BLOCKS TO HIDE/SHOW */
		var c = document.getElementsByTagName("body")[0].getElementsByTagName("*");
		for (var x=0;x<c.length;x++) 
		{
			if(c[x].id.indexOf(bloc) == 0)
		    {
		    	try
		    	{
		    		c[x].style.display = visibility;
		    	}
		    	catch(exception)
		    	{
		    		//Do nothing
		    	}
		    }
		}
	//}
}

function deleteSearch(target)
{
	targetPage = home+'display/deleteSearch';
	executeAsynchronousRequest(targetPage, target);
	setTimeout("location.reload()", 500);
}

/* First mootools try */
function ExpandBlock(blockToEnlarge, blockToDisplay, ahstart, ahend) {
	var elementToEnlarge = document.getElementById(blockToEnlarge);
	var elementToDisplay = document.getElementById(blockToDisplay); 
	var displayStyle;
	
	var myEffect = new Fx.Morph(elementToEnlarge, {duration: 'long', transition: Fx.Transitions.Sine.easeOut});
	 
	if(elementToDisplay.style.display == 'none')
	{
		displayStyle = 'block';
		hstart = ahstart;
		hend = ahend;
	}
	else
	{
		displayStyle = 'none';
		hstart = ahend;
		hend = ahstart;
	}
	
	myEffect.start({
	    'height': [hstart, hend] //Morphs the 'height' style from 10px to 100px.
	    //'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
	});

	elementToDisplay.style.display = displayStyle;
	
}

function changePageNumber(pageNumber)
{
	targetPage = home+'display/changePageNumber?nbResults='+pageNumber;
	executeAsynchronousRequest(targetPage, null);
	targetLocation = window.location+'/pstart/1';
	setTimeout("window.location = targetLocation", 1000);	
}

function setRankStage(stageId)
{
	targetPage = home+'display/setRankStage?stageId='+stageId;
	executeAsynchronousRequest(targetPage, null);
	setTimeout("location.reload()", 1000);		
}

function changeAutoRefreshStatus(status)
{
	targetPage = home+'display/changeAutoRefreshStatus?status='+status;
	executeAsynchronousRequest(targetPage, null);
	reset_timeout();
	//setTimeout("location.reload()", 1000);	
}