/******************************************************************************
* gshpJsUtils.js
*******************************************************************************
Fonctions diverses
*******************************************************************************
*                                                                             *
* Copyright 2006									                          *
*                                                                             *
******************************************************************************/

//	-------------------------------------------------------------------------
//	Document writing
//	-------------------------------------------------------------------------
function gshpDocumentWrite( str ) 
{
	document.write(str);
} 

//	-------------------------------------------------------------------------
//	Showing or hiding of custom failure paragraph
//	-------------------------------------------------------------------------
function gshpShowCustomFailurePara( sId, bShow, sText )
{
	if (bShow == null) 	var bShow = true;
	if (sText == null) 	var sText = "";
	
	var failurePara = document.getElementById(sId + "_failure_custom");
	if (failurePara != null) {
		failurePara.innerHTML = sText;
		failurePara.style.display = (bShow ? "block" : "none");
	}
}

//	-------------------------------------------------------------------------
//	Formatting of price value
//	-------------------------------------------------------------------------
function gshpFormatPriceValue(money, unit)
{
	money = parseFloat(money);
	if (isNaN(money)) return "&nbsp;";
	var sign ="";
	if (money < 0) {
		money = -money;
		sign = "-";
	}
	var integer = Math.floor(money);
	var full = "" + integer;
	// NB : A cause des arrondis, on utilise round au lieu de floor et on ne va pas factoriser l'expression mathematique en ecrivant : 100 * (money - integer)
	// Cas à problème : 583.05 | 152.89
	var decimal = "" + Math.round((100 * money) - (100 * integer));
	if (decimal.length == 1) decimal = "0"+decimal;
	var res = null;
	var pos = full.length;
	while (pos > 3) {
		block = full.substr(pos - 3, 3);
		if (res == null) {
			res = block;
		} else {
			res = block + " "+res;
		}
		pos -= 3;
	}
	if (pos > 0) {
		block = full.substr(0, pos);
		if (res == null) {
			res = block;
		} else {
			res = block+" "+res;
		}
	}
	if (unit == "&#8364;") unit = String.fromCharCode(8364);
	if (unit == "£") {
		res = unit + "\240" + sign + res + "." + decimal;
	}
	else
	if ((unit != null) && (unit != "")) {
		res = sign + res + "," + decimal + "\240" + unit;
	}
	else {
		res = sign + res + "," + decimal;
	}
	return res;
}

//	-------------------------------------------------------------------------
//	Recovery of effective width and height maintaining ratio
//	-------------------------------------------------------------------------
function gshpGetEffectiveWidth(width, height, expectedWidth, expectedHeight)
{
	width = parseInt(width);
	height = parseInt(height);
	expectedWidth = parseInt(expectedWidth);
	expectedHeight = parseInt(expectedHeight);
	if(isNaN(expectedWidth) || expectedWidth == 0) {
		if(isNaN(expectedHeight) || expectedHeight == 0) return width;
		if(expectedHeight > height) return width;
		return Math.floor(width * expectedHeight / height);
	} else if(isNaN(expectedHeight) || expectedHeight == 0) {
		if(width < expectedWidth) return width;
		return expectedWidth;
	}
	if(width * expectedHeight > height * expectedWidth) {
		if(width < expectedWidth) return width;
		return expectedWidth;
	}
	if(height < expectedHeight) return width;
	return Math.floor(width * expectedHeight / height);
}

function gshpGetEffectiveHeight(width, height, expectedWidth, expectedHeight)
{
	width = parseInt(width);
	height = parseInt(height);
	expectedWidth = parseInt(expectedWidth);
	expectedHeight = parseInt(expectedHeight);
	if(isNaN(expectedHeight)|| expectedHeight == 0) {
		if(isNaN(expectedWidth) || expectedWidth == 0) return height;
		if(expectedWidth > width) return height;
		return Math.floor(height * expectedWidth / width);
	} else if(isNaN(expectedWidth) || expectedWidth == 0) {
		if(height < expectedHeight) return height;
		return expectedHeight;
	}
	if(height * expectedWidth > width * expectedHeight) {
		if(height < expectedHeight) return height;
		return expectedHeight;
	}
	if(width < expectedWidth) return height;
	return Math.floor(height * expectedWidth / width);
}