/**
 * Global MBE javascript functions.
 * jQuery 1.3.x is required and must be called before this scripts
 */

/**
 * Is a valid email address.
 */
function isEmail(str) {
	return /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(str);
}

/**
 * Confirmation to reset form. This function use for input tag with type="image".
 */
function confirmReset(form) {
	
	if  (confirm("Voulez-vous vraiment réinitialiser ce formulaire ?")) {
		form.reset();
	}
	
	return false;
}

/**
 * Extract part of text between specified start and end from full text.
 * @return text
*/
function extractBetween(text, start, end) {
	var s = text.indexOf(start) + start.length;
	var e = text.lastIndexOf(end);
	var t = text.substring(s, e);
	return t;
}

/**
 * Make table row able to change background when mouse is hover.
 */
function tableHover(tableId) {
	jQuery(document).ready(function() {
		jQuery("#" + tableId + " tr").hover(function(){
			jQuery(this).find("td:not(:last-child)").addClass("table-cell-hover");
		}, function(){
			jQuery(this).find("td:not(:last-child)").removeClass("table-cell-hover");
		});
	});
}

/**
 * Calculate discount price from price and discount percent.
 * @return Discount price
 */
function calculateDiscount(price, percent) {
	return (price - ((percent * price) / 100)).toFixed(2);
}

/**
 * Calculate discount percent from price and discount price.
 * @return Discount percent
 */
function calculateDiscountPercent(price, discount) {
	return (((price - discount) / price) * 100).toFixed(2);
}
