
//====================================== Domain & URL ======================================//

/**
 * Get current domain name.
 */
function getDomain() {
	return (window.location.href.match(/:\/\/(.[^/]+)/)[1]).replace('www.','');
}

/**
 * Set cookie
 * @param name Cookie name
 * @param value Cookie Value
 * @param expiredays Number of days this cookie expires
 */
function setCookie(name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = name + "=" + escape(value) + ";domain=." + getDomain() + ";path=/" + (expiredays == null ? "" : ";expires=" + exdate.toUTCString());
}

/**
 * Get cookie value
 * @param name Cookie name
 */
function getCookie(name) {
	if (document.cookie.length > 0) {
		var c_start = document.cookie.indexOf(name + "=");
		if (c_start != -1) {
			c_start = c_start + name.length + 1;
			var c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

//====================================== Formatting ======================================//

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

/**
 * Is a valid URL.
 * @return bool
 */
function isURL(str) {
	return /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/.test(str);
}

/**
 * Format decimal number to French number.
 * @example "1250.75" to "1 250,75"
 * @param number Decimal number
 * @return string French number
 */
function format_french_number(number) {

	number += '';
	var x = number.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	var with_space = x1 + x2;

	return with_space.replace(".", ",");
}

//====================================== Various Functions ======================================//

/**
 * Show bookmark/favorite dialog.
 * @param url Absolute URL
 * @param title Title
 */
function bookmark(url, title) {
	// IE
	if (document.all) {
		window.external.AddFavorite(url, title);
	}
	// Firefox
	else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
}

/**
 * Trim all text inputs and texareas in specified form.
 * @param form Form DOM Element, Form jQuery Object or Form ID
 */
function trimForm(form) {

	if (form instanceof HTMLFormElement || form instanceof jQuery) {
		form = jQuery(form)
	}
	else {
		form = jQuery("#" + form);
	}

	// trim all fields
	form.find(":text,textarea").each(function(index, domElement) {
		jQuery(this).val(jQuery.trim(jQuery(this).val()));
	});
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

