function getRoot(response)
{
    if (document.all) return response.responseXML;
    return response.responseXML.documentElement;
}

function getElementText(obj)
{
    if (document.all) return obj.text;
    else
    {
	if (obj.textContent) return obj.textContent;
    }
}

window.ajaxRequestBuilder = function()
{
    var result = null;
    if (window.XMLHttpRequest)
    {
	result = new XMLHttpRequest();
	if (result.overrideMimeType) result.overrideMimeType("text/xml");
    }
    else if (window.ActiveXObject)
    {
	try
	{
	    result = new ActiveXObject("Msxml2.XMHTTP");
	}
	catch (exc) {
	    try {
		result = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (exc) {}
	}
    }
    return result;
}

function ajaxQuery(url, fctChanged)
{
    var request = new ajaxRequestBuilder();
    request.open("GET", url, true);
    request.onreadystatechange = function () {
	if (request.readyState==4 && request.status==200) fctChanged(request);
    }
    request.send("");
}

function updateForServiceChanged(request)
{
    /* Mise a jour de la description */
    var xmlRoot=getRoot(request);
    /* Affichage de la description */
    var xmlDesc=xmlRoot.getElementsByTagName("description");
    if (1==xmlDesc.length) document.getElementById("serviceDescription").innerHTML=getElementText(xmlDesc[0]);
    var xmlSource=xmlRoot.getElementsByTagName("source");
    if (1==xmlSource.length) document.getElementById("serviceSource").innerHTML="<strong>Source:</strong> "+getElementText(xmlSource[0]);
    var xmlInclude=xmlRoot.getElementsByTagName("include");
    if (xmlInclude.length>0)
    {
	var s="";
	s="<strong>Services inclus:</strong><br />";
	for(var i=0;i<xmlInclude.length;i++) s+="&nbsp;"+getElementText(xmlInclude[i])+"<br />";
	document.getElementById("serviceInclus").innerHTML=s;
    }
    /* Remplissage du bloc */
    /* Affichage du bloc */
    document.getElementById("serviceDetails").className="displayed";
}

function updateForService()
{
    var item=document.getElementById("serviceItems");
    if (""!=item.value)
    {
	var url=toolRoot+"/portal_MDV_resourceHandler/@@ajaxGetServiceInfo?serviceId="+item.value;
	ajaxQuery(url, updateForServiceChanged);
    }
    else
    {
	alert("Veuillez choisir un service dans la liste.");
	document.getElementById("serviceDetails").className="hidden";
	document.getElementById("serviceDescription").innerHTML="Sélectionnez un service dans la liste à gauche.";
	document.getElementById("serviceSource").innerHTML="";
	document.getElementById("serviceInclus").innerHTML="";
    }
}

function checkPosIntValue(item)
{
    var fVal=parseFloat(item.value);
    var iVal=parseInt(item.value);
    var res=iVal
    if (isNaN(iVal))
    {
	item.value="0";
	res=0;
    }
    if (iVal<0)
    {
	item.value="0";
	res=0;
    }
    if (iVal!=fVal)
    {
	item.value="0";
	res=0;
    }
    return res;
}

function displayFloat(value)
{
    return Math.round(value*100)/100;
}

function diapositivePrice(maxValue, price)
{
    this.maxValue=maxValue;
    this.price=price;
}

function appendDiapoPrice(maxValue, price)
{
    diapositivePrices[diapositivePrices.length] = new diapositivePrice(maxValue, price);
}

function vinylPrice(maxValue, price)
{
    this.maxValue=maxValue;
    this.price=price;
}

function appendVinylPrice(maxValue, price)
{
    vinylPrices[vinylPrices.length]=new vinylPrice(maxValue, price);
}

function findPrice(number, theArray, defaultValue)
{
    var foundMaxNum=-1;
    var foundPrice=-1;
    for(var i=0;i<theArray.length;i++)
    {
	if ((theArray[i].maxValue>=number) && (theArray[i].maxValue<foundMaxNum || -1==foundMaxNum))
	{
	    foundMaxNum=theArray[i].maxValue;
	    foundPrice=theArray[i].price;
	}
    }
    if (-1==foundPrice) return defaultValue;
    return foundPrice;
}

function filmDescription(newId, dureeFilm, prixUnitaire)
{
    this.id=newId;
    this.duree=dureeFilm;
    this.prix=prixUnitaire;
}

function appendFilm(newId, dureeFilm, prixUnitaire)
{
    filmList[filmList.length] = new filmDescription(newId, dureeFilm, prixUnitaire);
}

function enterPressed(evt)
{
    if (evt.keyCode==13) { updatePrices(); return false; }
    return true;
}

function updatePrices()
{
    /* Films */
    var dureeTotal=0;
    var sousTotalFilm=0;
    if (document.getElementById("sousTotalFilm")!=null)
    {
	var totalFilmNb=0;
	for(var it=0;it<filmList.length;it++)
	{
	    var valId=filmList[it].id;
	    /* Lecture du nombre */
	    var filmObj=document.getElementById("filmOption-"+valId);
	    var filmNb=checkPosIntValue(filmObj);
	    totalFilmNb+=filmNb;
	    /* Calcul du prix unitaire */
	    var unitPrice=filmNb*filmList[it].prix;
	    document.getElementById("totalFilmOption-"+valId).innerHTML=displayFloat(unitPrice)+" &euro;";
	    /* Calcul du prix total */
	    sousTotalFilm+=unitPrice;
	    /* Calcul de la duree total */
	    dureeTotal+=filmNb*filmList[it].duree;
	}
	if ((dureeTotal<allowedFilmDuration)&&(totalFilmNb>0))
	{
	    sousTotalFilm=prixFilmMinimum;
	    document.getElementById("alertFilm").style.display="block";
	}
	else document.getElementById("alertFilm").style.display="none";
	
	/* Prise en compte de l'option Gold */
	var useGold=document.getElementById("filmOptionGold").checked;
	/* Mise a jour des donnees */
	var nbDVDForFilm=0;
	if (0!=totalFilmNb) nbDVDForFilm=Math.ceil(dureeTotal/durationPerDVD);
	nbCopiesObj=document.getElementById("nbFilmCopie");
	nbCopiesGoldObj=document.getElementById("nbFilmCopieGold");
	if (useGold) {
	    sousTotalFilm += nbDVDForFilm*filmPrixOptionGold;
	    document.getElementById("optionGoldSsTotal").innerHTML=displayFloat(nbDVDForFilm*filmPrixOptionGold)+" &euro;";
	    document.getElementById("optionGoldNbDVD").innerHTML=nbDVDForFilm;
	}
	else {
	    document.getElementById("optionGoldSsTotal").innerHTML="0 &euro;";
	    document.getElementById("optionGoldNbDVD").innerHTML="0";
	}
	var nbCopiesVal=checkPosIntValue(nbCopiesObj);
	document.getElementById("nbFilmCopieNombre").innerHTML=nbDVDForFilm;
	var totalPrixFilmCopie=nbDVDForFilm*nbCopiesVal*prixFilmCopie;
	document.getElementById("nbFilmCopieTotal").innerHTML=displayFloat(totalPrixFilmCopie)+" &euro;";
	sousTotalFilm+=totalPrixFilmCopie;
	var nbCopiesGoldVal=checkPosIntValue(nbCopiesGoldObj);
	document.getElementById("nbFilmCopieGoldNombre").innerHTML=nbDVDForFilm;
	var totalPrixFilmCopieGold=nbDVDForFilm*nbCopiesGoldVal*prixFilmCopieGold;
	document.getElementById("nbFilmCopieGoldTotal").innerHTML=displayFloat(totalPrixFilmCopieGold)+" &euro;";
	sousTotalFilm+=totalPrixFilmCopieGold;
	document.getElementById("nombreDVDFilm").innerHTML=nbDVDForFilm;
	document.getElementById("sousTotalFilm").innerHTML=displayFloat(sousTotalFilm)+" &euro;";
	document.getElementById("dureeTotalFilm").innerHTML=dureeTotal+" min";}
	
    /* Cassettes */
    var sousTotalCassette=0;
    var k7NbObj=document.getElementById("nbCassette");
    if (k7NbObj!=null)
    {
	var k7CpyObj=document.getElementById("copieCassette");
	var k7NbVal=checkPosIntValue(k7NbObj);
	var k7CpyVal=checkPosIntValue(k7CpyObj);
	var k7CpyGldObj=document.getElementById("copieCassetteGold");
	var k7CpyGldVal=checkPosIntValue(k7CpyGldObj);
	var k7Prix=document.getElementById("prixCassette");
	var k7CpyPrix=document.getElementById("prixCopieCassette");
	var k7CpyGoldPrix=document.getElementById("prixCopieGoldCassette");
	var priceCassette= 0;
	if (0!=k7NbVal) priceCassette=firstK7+(k7NbVal-1)*otherK7;
	k7Prix.innerHTML = priceCassette + " &euro;";
	var priceCassetteCopie=0;
	if (0!=k7CpyVal) priceCassetteCopie=k7CpyVal*k7NbVal*copyK7;
	k7CpyPrix.innerHTML=priceCassetteCopie + " &euro;";
	var priceCassetteCopieGold=0;
	if (0!=k7CpyGldVal) priceCassetteCopieGold=k7CpyGldVal*k7NbVal*copyGoldK7;
	k7CpyGoldPrix.innerHTML=priceCassetteCopieGold+" &euro;";
	useGold=document.getElementById("cassetteOptionGold").checked;
	if (useGold) {
	    document.getElementById("nbCassetteOptionGold").innerHTML=k7NbVal;
	    document.getElementById("prixCassetteOptionGold").innerHTML=k7NbVal*prixCassetteOptionGold+" &euro;";
	    sousTotalCassette+=k7NbVal*prixCassetteOptionGold;
	}
	else {
	    document.getElementById("nbCassetteOptionGold").innerHTML="0";
	    document.getElementById("prixCassetteOptionGold").innerHTML="0 &euro;";
	}
	sousTotalCassette+=priceCassette+priceCassetteCopie+priceCassetteCopieGold;
	document.getElementById("sousTotalCassette").innerHTML=sousTotalCassette + " &euro;";
    }
    
    /* Diapositives */
    var sousTotalDiapo=0;
    var diapNbObj=document.getElementById("nbDiapositive");
    var diapCpyObj=document.getElementById("copieDiapositive");
    var diapCpyGldObj=document.getElementById("copieDiapositiveGold");
    var diapDVDObj=document.getElementById("nbDiapositiveDVD");
    if (diapNbObj!=null)
    {
	var diapNbVal=checkPosIntValue(diapNbObj);
	var diapCpyVal=checkPosIntValue(diapCpyObj);
	var diapCpyGldVal=checkPosIntValue(diapCpyGldObj);
	if ((diapNbVal<diapoNumMin) && (diapNbVal!=0))
	{
	    diapNbVal=diapoNumMin;
	    diapNbObj.value=diapoNumMin;
	    document.getElementById("alertDiapo").style.display="block";
	}
	else document.getElementById("alertDiapo").style.display="none";
	var diapPrice=findPrice(diapNbVal, diapositivePrices, diapoDefaultPrice);
	var nbDVD=0;
	if (0!=diapNbVal) nbDVD=Math.ceil(diapNbVal/diapoPerDVD);
	diapDVDObj.innerHTML=nbDVD;
	var prixTot=diapPrice*diapNbVal;
	document.getElementById("prixDiapositive").innerHTML=displayFloat(prixTot)+" &euro;";
	document.getElementById("nbCopieDiapositiveDVD").innerHTML=nbDVD*diapCpyVal;
	var diapCopyPrice=nbDVD*diapCpyVal*diapoPriceCopyDVD;
	document.getElementById("prixCopieDiapositive").innerHTML=displayFloat(diapCopyPrice)+" &euro;";
	document.getElementById("nbCopieDiapositiveDVDGold").innerHTML=nbDVD*diapCpyGldVal;
	var diapCopyGoldPrice=nbDVD*diapCpyGldVal*prixDiapoCopieGold;
	document.getElementById("prixCopieDiapositiveGold").innerHTML=displayFloat(diapCopyGoldPrice)+" &euro;";
	useGold=document.getElementById("diapoOptionGold").checked;
	if (useGold) {
	    document.getElementById("nbDiapoCopieGold").innerHTML=nbDVD;
	    document.getElementById("prixDiapoOptionGold").innerHTML=nbDVD*diapoOptionGold;
	    sousTotalDiapo=nbDVD*diapoOptionGold;
	}
	else {
	    document.getElementById("nbDiapoCopieGold").innerHTML="0";
	    document.getElementById("prixDiapoOptionGold").innerHTML="0 &euro;";
	}
	sousTotalDiapo+=prixTot+diapCopyPrice+diapCopyGoldPrice;
	document.getElementById("sousTotalDiapositive").innerHTML=displayFloat(sousTotalDiapo)+" &euro;";
    }
    /* Vinyl */
    var sousTotalVinyl=0;
    var vinylNbObj=document.getElementById("nbVinyl");
    var vinylCpyObj=document.getElementById("copieVinyl");
    var vinylDVDObj=document.getElementById("nbVinylDVD");
    if (vinylNbObj!=null)
    {
	var vinylNbVal=checkPosIntValue(vinylNbObj);
	var vinylCpyVal=checkPosIntValue(vinylCpyObj);
	if ((vinylNbVal<vinylNumMin) && (vinylNbVal!=0))
	{
	    vinylNbVal=vinylNumMin;
	    vinylNbObj.value=vinylNumMin;
	}
	var vinylPrice=findPrice(vinylNbVal, vinylPrices, vinylDefaultPrice);
	var nbDVD=0;
	if (0!=vinylNbVal) nbDVD=Math.ceil(vinylNbVal/vinylPerDVD);
	vinylDVDObj.innerHTML=nbDVD;
	var prixTot=vinylNbVal*vinylPrice;
	document.getElementById("prixVinyl").innerHTML=displayFloat(prixTot)+" &euro;";
	document.getElementById("nbCopieVinylDVD").innerHTML=nbDVD*vinylCpyVal;
	var vinylCopyPrice=nbDVD*vinylCpyVal*vinylPriceCopyDVD;
	document.getElementById("prixCopieVinyl").innerHTML=displayFloat(vinylCopyPrice)+" &euro;";
	sousTotalVinyl=prixTot+vinylCopyPrice;
	document.getElementById("sousTotalVinyl").innerHTML=displayFloat(sousTotalVinyl)+" &euro";
    }
    /* Final */
    document.getElementById("totalFinal").innerHTML=displayFloat(sousTotalFilm+sousTotalCassette+sousTotalDiapo+sousTotalVinyl)+" &euro;";
}
