// JavaScript Document

function isPattern(avalue,pattern) {
	var regExp = new RegExp("^"+pattern+"$","");
	var correct = regExp.test(avalue);
	return correct;
}

function isNumeric(avalue) {
	return isPattern(avalue,"\\d+");
}
	
function isEmail(avalue) {
	return isPattern(avalue, "[a-zA-Z0-9_\\-]+(\\\.[_a-zA-Z0-9\\-]+)*@([_a-zA-Z0-9\\-]+\\\.)+([a-zA-Z]{2,6}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)");
}

function isMSN(avalue) {
	return isPattern(avalue,"\\w+\@msn\.com[\.]*\\w*|\\w+\@hotmail\.com[\.]*\\w*|\\w+\@live\.com[\.]*\\w*")
}

function isDomain(avalue) {
	return isPattern(avalue,"\\w+\.\\w*\.\\w*\.\\w{2,4}")
}

function isCurrency(avalue) {
	//un valor = '' devuelve true
/* /^\$?\-?([1-9]{1}[\d]{0,2}(\,[\d]{3})*(\.[\d]{0,2})?|[1-9]{1}\d*(\.[\d]{0,2})?|0(\.[\d]{0,2})?|(\.[\d]{1,2})?)$/.test('999,999.99') 
*/
	return isPattern(avalue,"\\$?\\-?([1-9]{1}[\\d]{0,2}(\\,[\\d]{3})*(\\.[\\d]{0,2})?|[1-9]{1}\\d*(\\.[\\d]{0,2})?|0(\\.[\\d]{0,2})?|(\\.[\\d]{1,2})?)");
}

function isNoAlpha(avalue) {
	return isPattern(avalue, "[^a-z,A-Z]+");
}

function number_format(expr, decplaces, dec_sep, thousands_sep) {
	if (!dec_sep) dec_dep = '.';
	if (!thousands_sep) thousands_sep = ',';
	
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) {
		str = "0" + str
	}
	// establish location of decimal point
	var decpoint = str.length - decplaces
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	
	var pos = 0
	var len = 3;
	var decimals = str.substring(0,decpoint);
	var thousands = (decimals.length <= 3) ? decimals : decimals.substr(decimals.length - 3, 3);
	for (var i = 1; i < (decimals.length / 3); i++) {
		if ((pos = decimals.length - (3 * (i + 1))) < 0) {
			len = (pos + 3);
			pos = 0;
		}
		thousands = decimals.substr(pos, len) + thousands_sep + thousands;
	}
	
	return thousands + dec_sep + str.substring(decpoint,str.length);
}
