// date functions

function getValiddateUTC(month, day, year, hour, minute) {
	if (chkDate(month, day, year)) {
		var date = new Date(year-0,month-1,day-0,hour-0,minute-0,0);
		return Date.UTC(chkY2k(date.getYear()),date.getMonth(),date.getDate(),hour,minute,0);
	} else {
		return null;
	}
}

// check functions

function chkY2k(number) {
	return (number < 1000) ? number + 1900 : number; 
}

function chkDate(month, day, year) {
	var unvaliddate = day + '/' + month + '/' + year;
	var date = new Date(year-0,month-1,day-0);
	var validdate = date.getDate() + '/' + (date.getMonth()+1) + '/' + chkY2k(date.getYear());
	
	if (unvaliddate != validdate) {
		return false;
	}
	return true;
}

function chkEmail(email) {
	if(!(email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/))) return false;
	return true;
}

function chkPhone(phone) {
    if (!(phone.match(/^\d{3}\-\d{3}\-\d{4}$/))) return false;
    return true;
}

// formatting functions

function format_toDecimal(n, d) {
	if(d == null) d = 0;
	n = Math.round(n * (Math.pow(10, d)));
	if(d != 0) n = parseFloat(n / Math.pow(10, d));
	if(d > 0) {
		s = n + ''; // convert to string
		if(s.indexOf('.') != -1) {
			f = s.substr(s.indexOf('.')+1);
			s = s.substr(0, s.indexOf('.'));
		} else {
			f = '';
		}
		for(i=f.length; i<d; i++) f+='0';
		n = s+'.'+f;
	}
	
	return n;
}

// display functions

function i_display_required(required) {
	var count = 0;
	var message = "The following fields are required:\n";
	for(var i=0; i<required.length; i++) {
		if(required[i] != "" || required[i] != null) {
			message = message + " - " + required[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}


function i_display_error(errors) {
	var count = 0;
	var message = "The following errors have occured:\n";
	for(var i=0; i<errors.length; i++) {
		if(errors[i] != "" || errors[i] != null) {
			message = message + " - " + errors[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}