var g_strLastLinkElementHTML = null;
var g_objLastElementId = null;
var g_objLoupeImg = new Image();
g_objLoupeImg.src = 'img/loupe.png';



var g_intLastArticleId = "-1";

function pip ( p_intId ) {
	window.location.href = "boutiqueSession.php?ACTION=pip&ID="+p_intId+"&Q="+document.getElementById("pipCount"+p_intId).value;
}
var g_intTotalCommande = 0;
function commandePort( p_boolPort ) {
	if (g_intTotalCommande == 0) { 
		g_intTotalCommande = eval(document.getElementById("totalCommande").innerHTML);
	}
	document.getElementById("totalCommande").innerHTML = ( p_boolPort ? g_intTotalCommande+",00 € + port ("+document.getElementById("port").innerHTML+",00 €) = " + eval(g_intTotalCommande + eval(document.getElementById("port").innerHTML)) : g_intTotalCommande )
}
function showArticle( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'article','');
	document.getElementById("description"+l_intArticleId).style.display = "block";
	document.getElementById("article"+l_intArticleId).style.background = "#c7c29a";
	
	if (g_intLastArticleId!="-1" && g_intLastArticleId!=l_intArticleId) {
		document.getElementById("description"+g_intLastArticleId).style.display = "none";
		document.getElementById("article"+g_intLastArticleId).style.background = "#adac73";
		document.getElementById("imgTheLoupe"+g_intLastArticleId).style.display = "none";
	}
	g_intLastArticleId = l_intArticleId;

}
function hideArticle() {
	
	if (g_intLastArticleId!="-1") {
		document.getElementById("description"+g_intLastArticleId).style.display = "none";
		document.getElementById("article"+g_intLastArticleId).style.background = "#adac73";
		document.getElementById("imgTheLoupe"+g_intLastArticleId).style.display = "none";
		g_intLastArticleId = -1;
	}

}

function showZoom( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgTheLoupe','');
	var l_strImgSrc = document.getElementById("imgTheMini"+l_intArticleId).src;
	document.getElementById("imgTheZoom").src = replace(l_strImgSrc,'mini','');
	document.getElementById("imgZoom").style.display = "block";
	// menu autres images à zoomer
	var l_intNbreImage = document.getElementById("imgnbre"+l_intArticleId).innerHTML;
	if ( l_intNbreImage > 1 ) {
		var l_strMenuHTML = '';
		for ( var l_intCount = 1; l_intCount <= l_intNbreImage; ++l_intCount ) {
			l_strMenuHTML += 
				'<img src="'+replace(l_strImgSrc,'1',''+l_intCount)+'" alt="imgZoomMini'+l_intCount+'" id="imgZoomMini'+l_intCount+'" onClick="changeZoom(this);" onMouseover="showZoomLoupe(this);">'+
				'<div style="position:absolute;"><img src="img/loupe.png" alt="imgZoomLoupe'+l_intCount+'" id="imgZoomLoupe'+l_intCount+'" onClick="changeZoomLoupe(this);" onMouseout="hideZoomLoupe(this);" class="imgZoomLoupe"></div>';
		}
		document.getElementById("menuZoom").innerHTML = l_strMenuHTML;
	}
}
function changeZoom( p_objId ) {
	document.getElementById("imgTheZoom").src = 'img/waiting.png';
	//alert(p_objId.src);
	document.getElementById("imgTheZoom").src = replace(p_objId.src,'mini','');

}
function changeZoomLoupe( p_objId ) {
	//document.getElementById("imgTheZoom").src = 'img/waiting.png';
	var l_intArticleId = replace(p_objId.id,'imgZoomLoupe','');
	//alert(p_objId.id);
	//document.getElementById("menuSelect").innerHTML = p_objId.id + ' img src = ' + replace(document.getElementById("imgZoomMini"+l_intArticleId).src,'mini','');
	document.getElementById("imgTheDivZoom").innerHTML = '<img src="' + replace(document.getElementById("imgZoomMini"+l_intArticleId).src,'mini','') + '" class="imgTheZoom" id="imgTheZoom"  onClick="hideZoom();">';

}
function hideZoom() {
	document.getElementById("imgZoom").style.display = "none";
	document.getElementById("imgTheZoom").src = 'img/waiting.png';
	document.getElementById("menuZoom").innerHTML = '';
}
function showZoomLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgZoomMini','');
	document.getElementById("imgZoomLoupe"+l_intArticleId).style.display = "block";
}
function showBoutiqueLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'img','');
	document.getElementById("imgTheLoupe"+l_intArticleId).style.display = "block";
}
function hideZoomLoupe( p_objId ) {
	p_objId.style.display = "none";
}
function hideLoupe( p_objId ) {
	p_objId.style.display = "none";
}
function hideTheLoupe( p_objId ) {
	var l_intArticleId = replace(p_objId.id,'imgSide','');
	if ( document.getElementById("imgTheLoupe"+l_intArticleId) != null ) document.getElementById("imgTheLoupe"+l_intArticleId).style.display = "none";
}


function showLoupe( p_objElement ) {
	if (p_objElement.id!=g_objLastElementId) {
		if (g_objLastElementId!=null) { 
			document.getElementById(g_objLastElementId).innerHTML = g_strLastLinkElementHTML;
		}
		g_strLastLinkElementHTML = p_objElement.innerHTML;
		document.getElementById(p_objElement.id).innerHTML = '<span id="loupePosition" onClick="window.location.href=\''+p_objElement.href+'\';"><img src="'+g_objLoupeImg.src+'"></span>'+g_strLastLinkElementHTML;
	}
	g_objLastElementId = p_objElement.id;
}
var g_objLastElementRow = null;
function rowShow( p_objElement ) {
	if (g_objLastElementRow != null ) {
		var l_objCell = g_objLastElementRow.getElementsByTagName('td');
		l_objCell[0].style.color = "black";
		l_objCell[0].style.background = "transparent";
		l_objCell[1].style.color = "black";
		l_objCell[1].style.background = "transparent";
		l_objCell[2].style.color = "black";
		l_objCell[2].style.background = "transparent";
		l_objCell[3].style.color = "black";
		l_objCell[3].style.background = "transparent";
	}
	var l_objCell = p_objElement.getElementsByTagName('td');
	l_objCell[0].style.color = "white";
	l_objCell[0].style.background = "url('img/calqueGray.png')";
	l_objCell[1].style.color = "white";
	l_objCell[1].style.background = "url('img/calqueGray.png')";
	l_objCell[2].style.color = "white";
	l_objCell[2].style.background = "url('img/calqueGray.png')";
	l_objCell[3].style.color = "white";
	l_objCell[3].style.background = "url('img/calqueGray.png')";
	g_objLastElementRow = p_objElement;
}
function rowShowPhoto( p_objElement ) {
	if (g_objLastElementRow != null ) {
		var l_objCell = g_objLastElementRow.getElementsByTagName('td');
		l_objCell[0].style.color = "white";
		l_objCell[0].style.background = "transparent";
		l_objCell[1].style.color = "white";
		l_objCell[1].style.background = "transparent";
	}	
	var l_objCell = p_objElement.getElementsByTagName('td');
	l_objCell[0].style.color = "black";
	l_objCell[0].style.background = "url('img/calqueBlanc.png')";
	l_objCell[1].style.color = "black";
	l_objCell[1].style.background = "url('img/calqueBlanc.png')";
	g_objLastElementRow = p_objElement;
}
var l_intLastMenuId = 0;
function openMenu( p_objElement ) {
	closeMenu();
	var l_intId = replace(p_objElement.id,'menu','');
	document.getElementById('underMenu'+l_intId).style.display = 'block';
	l_intLastMenuId = l_intId;
}
function closeMenu() {
	if (l_intLastMenuId != 0) document.getElementById('underMenu'+l_intLastMenuId).style.display = 'none';
}
var g_strLastImgRoll = '';
var g_objLastImgElement = null;
var g_intImgId = 0;
var g_boolRoll = false;
function rollImg( p_objElement ) {
	if (g_intImgId == 0) {
		g_strLastImgRoll = p_objElement.src;
		g_intImgId = replace(p_objElement.id,'img','');
		g_objLastImgElement = p_objElement;
		g_objLastImgElement.src = g_objImages[g_intImgId][2].src;
		spendTime();
	}
}
function rollImgTrans() {
	if (g_objImages[g_intImgId]!=null) g_objLastImgElement.src = g_objImages[g_intImgId][1].src;
}
function rollImgOut() {
	if (g_strLastImgRoll!='') g_objLastImgElement.src = g_strLastImgRoll;
	g_strLastImgRoll = '';
	g_intImgId = 0;
}
function spendTime() {
	setTimeout("rollImgTrans()",120);
}
var g_objImages = new Array();
function initIMG() {
	var l_objBody = document.getElementsByTagName('body');
	g_strPageName = l_objBody[0].id;
		
	var l_objMenuLinks = document.getElementById("menuPage").getElementsByTagName('img');
	for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
		loadImageRoll(replace(l_objMenuLinks[l_intCount].id,'img',''),l_objMenuLinks[l_intCount].src);
	}
	
	if (g_strPageName == "groupe") {
		if ( document.getElementById("membres") != null ) {
			var l_objMenuLinks = document.getElementById("membres").getElementsByTagName('img');
			for ( var l_intCount = 0; l_intCount < l_objMenuLinks.length; ++l_intCount ) {
				loadImageRoll(replace(l_objMenuLinks[l_intCount].id,'img',''),l_objMenuLinks[l_intCount].src);
			}
		}
	}
	if ( document.getElementById("formComment") != null ) {
		document.getElementById("formComment").action = 'saveComment.php';
	}
	if ( document.getElementById('timeShow') != null ) {
		checkTimeShow( document.getElementById('timeShow') );
	}
	if ( document.getElementById('contactForm') != null ) {
		document.getElementById('contactForm').action = 'saveEmail.php';
		document.getElementById('contactText').focus();
	}
	if (document.getElementById('livraisonForm') != null) {
		document.getElementById('livraisonForm').action = 'saveEmail.php';
	}
}
function loadImageRoll ( p_strImageId, p_strImageName ) {
	g_objImages[p_strImageId] = new Array();
	for ( var l_intCount = 0; l_intCount < 3; ++l_intCount ) {
		g_objImages[p_strImageId][l_intCount] = new Image();
		g_objImages[p_strImageId][l_intCount].src = replace(p_strImageName,'.png',( l_intCount==1? 'ON': (l_intCount==2?'OFF':'') )+'.png');
	}
}

function checkDate( p_objDate ) {
	
}

function checkTelValue( p_objTelElement ) {
	p_objTelElement.value = replace(p_objTelElement.value," ","");
	p_objTelElement.value = replace(p_objTelElement.value,".","");
	p_objTelElement.value = replace(p_objTelElement.value,"-","");
}

function checkTimeShow( p_objTimeShow ) {
	var l_intTarifBase = 2;
	var l_intSalaire = document.getElementById('TarifMusicien').innerHTML;
	var l_intTarifIncrement = document.getElementById('TarifIncrement').innerHTML;
	var l_intInitTime = document.getElementById('InitTime').innerHTML;
	p_objTimeShow.value = replace(p_objTimeShow.value,' ','');
	var l_intPartNumberShow = ( p_objTimeShow.value.indexOf('x') > -1 ? parseInt(p_objTimeShow.value.substring(0,p_objTimeShow.value.indexOf('x'))) : 1 );
	var l_intPartTimeShow = parseInt(p_objTimeShow.value.substring(p_objTimeShow.value.indexOf('x')+1,p_objTimeShow.value.length));
	p_objTimeShow.value = l_intPartNumberShow+' x '+l_intPartTimeShow+' min';
	
	if ( l_intPartNumberShow*l_intPartTimeShow < l_intInitTime ) l_intPartNumberShow = l_intPartNumberShow * ( 1 + ((l_intPartNumberShow*l_intPartTimeShow/l_intInitTime*5)+l_intInitTime-(l_intPartNumberShow*l_intPartTimeShow))/(l_intInitTime+(l_intPartNumberShow*l_intPartTimeShow)) );
	
	document.getElementById('soloSelect').innerHTML = 'solo : guitare et chant ('+Math.round(Math.round(Math.round(l_intTarifBase*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('duoSelect').innerHTML = 'duo : guitare et chant, flûte traversière ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement*1)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('trioSelect').innerHTML = 'trio : guitare et chant, flûte traversière, violoncelle ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement*2)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	document.getElementById('quartetSelect').innerHTML = 'quartet : guitare et chant, flûte, violoncelle, batterie ('+Math.round(Math.round(Math.round((l_intTarifBase+l_intTarifIncrement*3)*l_intPartNumberShow*l_intPartTimeShow*l_intSalaire/l_intInitTime)/10))*10+' €)';
	changePrice( document.getElementById('formuleShow') );
}
function changePrice( p_objElementSelect ) {
	var l_arrayOptions = p_objElementSelect.getElementsByTagName('option');
	for ( var l_intCount = 0; l_intCount < l_arrayOptions.length; ++l_intCount ) {
		if ( l_arrayOptions[l_intCount].selected ) {
			
			var l_arrayPriceSelect1 = l_arrayOptions[l_intCount].innerHTML.split("(");
			var l_arrayPriceSelect2 = l_arrayPriceSelect1[1].split(")");
			//alert( parseInt(l_arrayPriceSelect2[0]) );
			document.getElementById('price').value = parseInt(l_arrayPriceSelect2[0]);
			
		}
	}
}


/////////////////////////////////////////////////////////
//// REPLACE STRING /////////////////////////////////////
/////////////////////////////////////////////////////////
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
