﻿/**
	The file contains functions that checks validation of entered fields in forms
	Written by Druzhinin Yuriy
**/
/*
	All "isValid..." functions check only value or text parameters!
	functions return true if the text is correct, false - not
*/
//function removes spaces at the beginning/end of the text, removes repeated spaces
function removeSpaces(text) {
	text = text.replace(/^\s+|\s{2}|\s+$/g, '');
}
//function checks removes spaces and check if the text is not empty
function isNotNull(text) {
	removeSpaces(text);
	if(text) return true;
	return false;
}
// function checks if the zip code is correct
// returns the Country Name if the Zip is right, false - else
function isValidZip(text) {
	removeSpaces(text);
	if((/^[a-z]{1}\d{1}[a-z]{1}\s?\d{1}[a-z]{1}\d{1}$/i).test(text)) return "Canada";
	if((/^(\d{5})$/i).test(text)) return "USA";
	return false;
}
//function  checks email, can be all formats including: John Smith <john.smith@every.mail.com>
function isValidEmail(email) {
	removeSpaces(email);
	return (/^\"?[a-z0-9áéíñóúüÁÉÍÑÓÚÜàèìòùûâêôî_\-\s\.]*\"?\s?[<]?\s?([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}\s?[>]?$/i).test(email);
}
// function checks if the content text is correct (not includes some special symbols)
function isValidContent(text) {
	removeSpaces(text);
	return (/^[^\Q+\[\](){}\/<>\\^*$#+=\E]*$/).test(text); /* \Q there is unexpected symbols \E */
}
// function checks if the name of the user is right
function isValidName(text) {
	removeSpaces(text);
	return (/^[\w\s\-\_]*$/i).test(text);
}
// function checks if FuneralHome name is correct and removes unexpected spaces
function isValidFuneralHomeName(text) {
	removeSpaces(text);
	return (/^[\s\w\'\"\&\/\,\.\(\)\-áéíñóúüÁÉÍÑÓÚÜàèìòùûâêôî]{2,}$/i).test(text);
}
//function checks if entered description or message is correct
function isValidDescription(text) {
	return (/^[\w\s\d\.\,\b\&\%\$\@\*\(\)\'\"\#\:\;\-áéíñóúüÁÉÍÑÓÚÜàèìòùûâêôî]*$/i).test(text);
}
// english/spanish letters and spaces
function isAlphaOnly(text) {
	return (/^[\s\w\-a-záéíñóúüÁÉÍÑÓÚÜàèìòùûâêôî]*$/i).test(text);
}
// function checks if the address is correct
function isValidAddress(text) {
	removeSpaces(text);
	return (/^[\s\w\,\.\'\#\&\(\)\/\-áéíñóúüÁÉÍÑÓÚÜàèìòùûâêôî]{2,}$/i).test(text);
}
//checks if only digit here
function isDigitOnly(text) {
	removeSpaces(text);
	return (/^[0-9]*$/).test(text);
}


