// JavaScript Document
// JavaScript used for validation

// Check if the value is a date in format dd.mm.yyyy
function isDate(value) {
	// regular expression to match required date format
	re = /^[0-3]?[0-9](\/|\.)[01]?[0-9](\/|\.)[1-9][0-9][0-9][0-9]$/;
	if (!value.match(re)) return false;	
	d = parseInt(value.substr(0, 2), 10);
	m = parseInt(value.substr(3, 2), 10);
	y = parseInt(value.substr(6, 4), 10);
	if (m < 1 || m > 12) return false;
	if (d < 1 || d > daysInMonth(m, y)) return false;
	return true;
}

// The year must be minimum 1753 for SQLServer datetime fields  
function dateValidation(form, input, value) {
	// regular expression to match required date format
	if (value == '') return true;
	if (!isDate(value)) return false;
	return parseInt(value.substr(6,4), 10) >= 1753;
}

function decimalValidation(ctrl, precision, scale) {
	var x = trim(ctrl.value);
	if (x == "") return true;
	if (x.match(/,/, '')) return false; // comma is not allowed as a decimal point
	x = parseFloat(x.replace(/'/g, ""));
	if (isNaN(x)) return false;
	x = Math.abs(x).toString();
	var index = x.search(/\./, '');
	if (index == -1) // if integer
		return x.length <= precision - scale;
	else
		return (index <= precision - scale && x.length - index - 1 <= scale);
}

// Return the formatted date
function dateFormat(d) {
	var today = new Date();
	var s = trim(d);
	var separator = new RegExp("[\\.,;: _\\-/\\\\]", "g");
	var a = s.split(separator, 3);
	var s2 = new String();
	
	if (a.length == 2) // Complete with the current year
		a[2] = today.getFullYear();
		 
	if ((a.length != 3) || isNaN(parseInt(a[0], 10)) || isNaN(parseInt(a[1], 10)) || isNaN(parseInt(a[2], 10))) return d; // Format not recognized

	// leading 0
	for (var i = 0; i <= 2; i++)
		if (a[i].length == 1) a[i] = "0" + a[i];

	// Year completion
	if (a[2].length == 2) {
		if (parseInt(a[2], 10) < 50)
			a[2] = "20" + a[2];
		else
			a[2] = "19" + a[2];
	}
	
	return a[0] + "." + a[1] + "." + a[2];
}

// Format the date value in the specified control
function onDateChange(ctrl) {
	ctrl.value = dateFormat(ctrl.value);
}

// Convert a date string from dd.mm.yyyy to yyyy-mm-dd
function convertDate(sDate) {
	if (sDate == "") 
		return "";
	else
		return sDate.substr(6, 4) + "-" + sDate.substr(3, 2) + "-" + sDate.substr(0, 2);
}

// Swiss decimal formatting (xxx'xxx.xx) (2 decimals by default)
function decimalFormat(valeur, decimals) {
	if (valeur === "") return "";
	if (decimals == null) decimals = 2; // default
	valeur = parseNumber(valeur);
	var deci = Math.round (Math.pow(10, decimals) * (Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val = Math.floor(Math.abs(valeur));
	if (deci==Math.pow(10, decimals)) {val=Math.floor(Math.abs(valeur)) + 1; deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<5;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i)) + "'" + val_format.substring(nb-(3*i));
		}
	}
	var decim=""; 
	for (var j=0;j<(decimals-deci.toString().length);j++) {decim+="0";}
	deci=decim+deci.toString();
	if (decimals > 0)	val_format=val_format+"."+deci;
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format;
}

// Format the decimal value in the specified control
function onDecimalChange(ctrl, decimals) {
	ctrl.value = decimalFormat(ctrl.value, decimals);
}

function parseNumber(s) {
	var reg = new RegExp("'", "g");
	var x = parseFloat(s.toString().replace(reg,''));
	if (isNaN(x)) x = 0;
	return x;
}

function validateUsername(form, ctrl, value) {
	var exp=new RegExp("^[a-zA-Z0-9\._]{4,30}$","g"); // 3-8 alphanumerics
	return exp.test(trim(value));
}

function validatePassword(form, ctrl, value) {
	return (!(value.length < 6 || value.length > 30));
}

function validatePassword2(form, ctrl, value) {
	return (form.password.value == value);
}

function daysInMonth(month,year) {
var m = [31,28,31,30,31,30,31,31,30,31,30,31];
if (month != 2) return m[month - 1];
if (year%4 != 0) return m[1];
if (year%100 == 0 && year%400 != 0) return m[1];
return m[1] + 1;
} 

// Return a date from a string in format dd.mm.yyyy 
function parseDate(sDate) {
	return new Date(sDate.substring(6, 10), sDate.substring(3, 5) - 1, sDate.substring(0, 2));
}

// Check the validity of the given period.
// Return false if date2 is anterior to date2
function checkPeriod(date1, date2) {
	if (date1 && date2)
		return parseDate(date1) <= parseDate(date2);
	else
		return true;
}
