//**** GENERAL FUNCTIONS ****

function openPage(page) {
	window.open(page, '_self');
}

function openNewPage(page) {
	window.open(page, '_blank');
}

function showHide(id){
	if (document.getElementById(id).style.display == 'none')
		show(id);
	else
		hide(id);
}

function show(id){
	document.getElementById(id).style.display = 'inline';
}

function hide(id){
	document.getElementById(id).style.display = 'none';
}

function imgCopyright(copyrightText){
	alert(copyrightText);
}

function emailProtect(domain, id) {
	location.href = "mailto:" + id + "@" + domain;
}

function checkElement(elmt){
	if(elmt.type == "checkbox" && !elmt.checked && !elmt.disabled)
		elmt.checked = "checked";
}

function uncheckElement(elmt){
	if(elmt.type == "checkbox" && elmt.checked && !elmt.disabled)
		elmt.checked = "";
}

function checkAllCbx(frm){
	for(i=0; i<frm.length; i++)
		checkElement(frm.elements[i]);
}

function uncheckAllCbx(frm){
	for(i=0; i<frm.length; i++)
		uncheckElement(frm.elements[i]);
}

//**** SPECIFIC FUNCTIONS ****

/*
 * get a format price thanks to format ID according to a format array that contains elements like [a,b] where 'a' is a format ID and 'b' is its price.
 * @param paramFormatId : id of format we want the price.
 * @param paramFormatArray : array with correspondance between IDs and prices.
 * @return : price of parameter paramFormatId
 */
function getNewFormatPrice(paramFormatId, paramFormatArray){
	for(i=0; i<paramFormatArray.length; i++){
		current=paramFormatArray[i];
		if(current[0] == paramFormatId){
			return current[1];
		}
	}
	
	return -1; // error : paramFormatId not found in paramFormatArray
}



//**** PAGE LOGIC FUNCTIONS ****
/* all those functions are page-dependent */

/*
 * Set values corresponding to selected format : formatId, formatPrice, formatName.
 * @param frm : document form where to set input values.
 * @param paramFormatId : selected format id (used to determine formatPrice).
 * @param paramFormatName : selected format name.
 * @param paramFormatArray : array with correspondance between IDs and prices.
 */
function setFormatValues(frm, paramFormatId, paramFormatName, paramFormatArray, currencySuffix) {
	newPrice = getNewFormatPrice(paramFormatId, paramFormatArray);
	if(newPrice >= 0){
		document.getElementById('formatPriceArea').innerHTML = newPrice+' '+currencySuffix;  // in addCartArea and modificationCartArea
		frm.formatId.value = paramFormatId; // in gallery and diapo
	}
	else {
		document.getElementById('formatPriceArea').innerHTML = '-';  // in addCartArea and modificationCartArea
	}
	return true;
}


function checkWithdrawalMethod(withdrawalMethodId){
	
	//domicile
	if(withdrawalMethodId == 1){
		
		//required fields for coordinates
		document.getElementById('labelCivility').className = 'formRequiredField';
		document.getElementById('labelName').className = 'formRequiredField';
		document.getElementById('labelFirstName').className = 'formRequiredField';
		document.getElementById('labelAddress').className = 'formRequiredField';
		document.getElementById('labelZipCode').className = 'formRequiredField';
		document.getElementById('labelCity').className = 'formRequiredField';
		document.getElementById('labelCountry').className = 'formRequiredField';
		document.getElementById('labelEmail').className = 'formRequiredField';
		
		//show delivery checkbox
		show('rowCbxDelivery');
	}
	
	//shop
	else if (withdrawalMethodId == 2 || withdrawalMethodId == 3){
	
		//required fields for coordinates
		document.getElementById('labelAddress').className = 'formNotRequiredField';
		document.getElementById('labelZipCode').className = 'formNotRequiredField';
		document.getElementById('labelCity').className = 'formNotRequiredField';
		document.getElementById('labelCountry').className = 'formNotRequiredField';
		
		//hide delivery
		document.getElementById('cbxDelivery').checked = '';
		hide('deliveryArea');
		hide('rowCbxDelivery');
	}
}