var gk=window.Event?1:0; // navigateurs Gecko ou IE

function valideFormulaire(formulaire) {
	var inputs = formulaire.elements;
	for (i=0;i<inputs.length;i++) {
		if (inputs[i].className == "mandatory" && !testChampVide(inputs[i])) return false;
		// check password fields
		var id = inputs[i].id;
		if (id.substring(id.length-5) == "Verif")
		{
			var val1 = document.getElementById(id.substring(0, id.length-5)).value;
			var val2 = inputs[i].value;
			if (val1 != val2)
			{
				alert("Le champ " + inputs[i].title + " n'a pas été saisi de façon identique dans sa partie de vérification");
				return false;
			}
		}
	}
	return true;
}

function testChampVide(input) {
	var invalidField = false;
	if (input.value == "") 
	{
		invalidField = true;
	}
	else
	{
		if (input.value == "-1")
		{
			var autreField = document.getElementById("new" + input.id);
			if (autreField == null)
			{
				invalidField = true;
			}
			else if (autreField.value == "")
			{
				invalidField = true;
			}
		} 
	}
	
	if (invalidField) {
		alert("Le champ " + input.title + " est obligatoire, merci de le renseigner");
		return false;
	}
	return true;
}

var xhr_object = null;
var xhr_url = '/moV4/tools/wikiAction.php?id=';

function wikiAction(idToDelete, action)
{
 	var httpRequest = false;
	url = xhr_url + idToDelete + "&action=" + action;
	
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!httpRequest) {
        alert('Abandon :( Impossible de créer une instance XMLHTTP');
        return false;
    }
    httpRequest.onreadystatechange = function() { 
    	line = document.getElementById("wikiLine" + idToDelete);
    	list = line.parentNode;
    	list.removeChild(line);
    	//quand on a fini de supprimer le dernier element de la liste, on supprime l'option de modif
    	if (list.innerHTML == "") 
    	{
    		group = document.getElementById("wikidata");
    		group.parentNode.removeChild(group);
    	}
    };
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}

function wikiDelete(idToDelete) 
{
	if (confirm("Etes vous sûr(e) de vouloir supprimer cette modification ?"))
	{
		wikiAction(idToDelete, "delete");
	}
}

function wikiValide(idToValide) {
	if (confirm("Etes vous sûr(e) de vouloir valider cette modification ?"))
	{
		wikiAction(idToValide, "update");
	}
}

function showFicheSection(part) {
	var eleToSwitch = document.getElementById("section" + part);
	var imgToSwitch = document.getElementById("img" + part);
	if (eleToSwitch) {
		if (eleToSwitch.style.display == "none") {
			eleToSwitch.style.display = "block";
			imgToSwitch.src = "/moV4/design/fleche2.gif";
		} else {
			eleToSwitch.style.display = "none";
			imgToSwitch.src = "/moV4/design/fleche1.gif";
		}
	}
}

function showHideVersionsInList(rowCount, listId) {
	for (i=0;i<rowCount;i++) {
		row = document.getElementById('row_' + listId + "_" + i);
		if (row) {
			if (row.style.display == 'block') {
				row.style.display = 'none';
			} else {
				row.style.display = 'block';
			}
		}
	}
}

function switchNode(parentNode) {
	var nextOne = parentNode.nextSibling;
	while (nextOne != null && nextOne.id == "") {
		if (nextOne.style.display == "block") nextOne.style.display = "none";
		else {
			nextOne.style.display = "block";
			nextOne.className = "standard";
		}
		nextOne = nextOne.nextSibling;
	}
}

function setCookie(nom,valeur) {
	document.cookie = nom + "=" + escape(valeur)
}

function getCookie(nom) {
	deb = document.cookie.indexOf(nom + "=")
	if (deb >= 0) {
		deb += nom.length + 1
		fin = document.cookie.indexOf(";",deb)
		if (fin < 0) fin = document.cookie.length
		return unescape(document.cookie.substring(deb,fin))
	}
	return ""
}

function refreshList(pageNumber, listId, count, limit)
{
	url = "/moV4/tools/ajaxListAction.php?pageNumber=" + pageNumber + "&count=" + count + "&listId=" + listId + "&limit=" + limit;
	showAjaxContent(url, "listContent" + listId);
}

function showCollecContent(collecId, support, isEditing)
{
	url = '/moV4/tools/collecContentPart.php?collecId=' + collecId + '&supportId=' + support + '&isEditing=' + isEditing;
	showAjaxContent(url, "section" + support);
}

function showAjaxContent(url, listId)
{
 	var httpRequest = false;
 	var list = document.getElementById(listId);
 	//if (list.innerHTML == "")
 	{
 	    list.innerHTML = "<img src='/moV4/design/loading.gif' />Chargement en cours, merci de patienter ...";
	    if (window.XMLHttpRequest) { 
	        httpRequest = new XMLHttpRequest();
	        if (httpRequest.overrideMimeType) {
	            httpRequest.overrideMimeType('text/html');
	        }
	    } else if (window.ActiveXObject) {
	        try {
	            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	        } catch (e) {
	            try {
	                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch (e) {}
	        }
	    }

	    if (!httpRequest) {
	        return false;
	    }
	    httpRequest.onreadystatechange = function() {
	       if (httpRequest.readyState == 4)
	       {
	    	   list.innerHTML = httpRequest.responseText;
	       }
	    };
	    httpRequest.open('GET', url, true);
	    httpRequest.send(null);
 	}
}

function moveListItemUp(list) 
{
	for (i=0; i<list.options.length; i++) 
  {
		if (list.options[i].selected) 
    {
			if (i>0 && !list.options[i-1].selected) 
      {
				swapListItem(list, i, i-1);
				list.options[i-1].selected = true;
			}
		}
	}
	return true;
}

function moveListItemDown(list)
{
	for (i=list.options.length-1; i>=0; i--) 
  {
		if (list.options[i].selected) 
    {
			if (i != (list.options.length-1) && ! list.options[i+1].selected) 
      {
				swapListItem(list, i, i+1);
				list.options[i+1].selected = true;
			}
		}
	}
	return true;
}

function swapListItem(list, i, j)
{
	var o = list.options;
	if (i<0 || i>=o.length || j<0 || j>=o.length) { return false; }
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	return true;
}

function selectListAllItem(list)
{
  for (i=0; i<list.options.length; i++) 
  {
      if (!list.options[i].selected) 
    {
      list.options[i].selected = true;
      }
  }
}

function changePoid(target, versionId, increase)
{
	var poidHasChanged = false;
	var poidDisp = document.getElementById(target + "_" + versionId);
    if (increase && poidDisp.value < 9)
    {
    	poidDisp.value++;
        poidHasChanged = true;
    }
    else if (!increase && poidDisp.value > 0)
    {
    	poidDisp.value--;
        poidHasChanged = true;
    }
    
    // if poid has changed, indicate it to update variable
    if (poidHasChanged)
    {
    	document.getElementById("poid_" + target + "_" + versionId).value = poidDisp.value;
    	document.getElementById("toUpdate_" + target + "_" + versionId).value = "1";
    }
}

// display the div containing the full sized photo
function showFullPhoto(photoId, source, photoPath)
{
	var divPhoto = document.getElementById(photoId);
	// create div photo if it doesn't exist
	if (divPhoto == null || divPhoto == 'undefined')
	{
		divPhoto = document.createElement('div');
		divPhoto.id = photoId;
		divPhoto.style.margin = "0px";
		divPhoto.style.position = "absolute"; 
		divPhoto.style.top = "0px"; 
		divPhoto.style.left = "0px"; 
		divPhoto.style.width = "99%"; 
		divPhoto.style.height = "99%"; 
		divPhoto.onclick = function() {
			this.style.display = "none";
		};
		source.parentNode.appendChild(divPhoto);
		
		tablePhoto = document.createElement('table');
		tablePhoto.style.width = "100%";
		tablePhoto.style.height = "100%";
		divPhoto.appendChild(tablePhoto);
		
		tbodyPhoto = document.createElement('tbody');
		tablePhoto.appendChild(tbodyPhoto);
		trPhoto = document.createElement('tr');
		tbodyPhoto.appendChild(trPhoto);
		tdPhoto = document.createElement('td');
		trPhoto.appendChild(tdPhoto);
		tdPhoto.align = "center";
		tdPhoto.valign = "middle";
		
		tablePhoto2 = document.createElement('table');
		tablePhoto2.style.width = "800px";
		tablePhoto2.style.border = "1px solid black";
		tablePhoto2.style.backgroundColor = "rgb(204, 204, 204)";
		tdPhoto.appendChild(tablePhoto2);
		
		tbodyPhoto2 = document.createElement('tbody');
		tablePhoto2.appendChild(tbodyPhoto2);
		trPhoto2 = document.createElement('tr');
		tbodyPhoto2.appendChild(trPhoto2);
		tdPhoto2 = document.createElement('td');
		trPhoto2.appendChild(tdPhoto2);
		tdPhoto2.align = "center";
		tdPhoto2.valign = "middle";

		imgPhoto = document.createElement('img');
		tdPhoto2.appendChild(imgPhoto);
		imgPhoto.src = photoPath;

		// trick to get the alternative name of photo according to mini
		lastIndexOfMini = source.alt.lastIndexOf('mini');
		imgPhoto.alt = source.alt.substring(0, lastIndexOfMini) + " photo " + source.alt.substring(lastIndexOfMini + 4);

		trText = document.createElement('tr');
		tbodyPhoto2.appendChild(trText);
		tdText = document.createElement('td');
		trText.appendChild(tdText);
		tdText.innerHTML = "<i>Cliquer n'importe où pour faire disparaitre la photo.</i>";
	}		

	// make it work with fucking IEs... pageYOffset does not exist on those shitty browsers
	if (gk)
	{
		divPhoto.style.top = pageYOffset + "px";	
	}
	else
	{
		de=!document.documentElement.clientWidth?document.body:document.documentElement; // trick for fucking IE6
		divPhoto.style.top = de.scrollTop + "px";	
	}
	divPhoto.style.display="block";
}
