// JavaScript Document: Common validation routines.
// @collected by: Jose F. D'Silva

/**
 * Return the string stripped of leading and trailing white-spaces.
*/
function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

/**
 * Convert string to Title Case.
*/
function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

/**
 * Return TRUE if an empty string.
*/
function isEmpty(s) {
		return (trim(s)=='');
}

/**
 * Return TRUE if only digits are present.
*/
function isNumeric(s) {
	for(i=0; i < s.length; i++) {
		c = s.charAt(i);
		if(c < '0' || c > '9')
			return false;
	}
	return true;
}

/**
 * Count number of digits in the string.
*/
function countDigits(s) {
	nd = 0; //number of digits.
	for(i=0; i < s.length; i++) {
		c = s.charAt(i);
		if(c >= '0' && c <= '9')
			++nd;
	}
	return nd;
}

/**
 * Return the number of words in a string.
*/
function getWordCount(s) {
	var char_count = s.length;
	var fullStr = s + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	
	if(fullStr.length < 2) {
		word_count = 0;
	}
	return word_count;
}

/**
 * Return the first blank field of the form object.
*/
function getBlankField(ofrm) {
	for(i=0; i < ofrm.elements.length; i++) {
		v = trim(ofrm.elements[i].value);
		if(v=='')
			return ofrm.elements[i];
	} //endfor
	return null;
}

/**
 * Return TRUE if string has a valid email format.
*/
function isEmail(str) {
	var at='@';
	var dot='.';
	var lat = str.indexOf(at)
	var lstr = str.length
	var ldot = str.indexOf(dot)

	if(str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
		return false;

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
		return false;

	if (str.indexOf(at, (lat + 1)) != -1)
		return false;

	if (str.substring(lat-1, lat) == dot || str.substring(lat+1, lat+2) == dot)
		return false;

	if (str.indexOf(dot, (lat+2)) == -1)
		return false;

	if (str.indexOf(" ") != -1)
		return false;
		
	return true;
}

/**
 * Return TRUE if string has a valid URL format.
*/
function isURL(url) {
	var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	
	return RegExp.test(url);
} 

/**
 * Return TRUE if the social security number matches a Swedish SSN pattern, FALSE otherwise.
 * e.g. 123456-1234 or 1234561234
*/
function isSwedishSocialSecurityNumber(s) {
	var RegExp = /^(\d{6})(-?)(\d{4})$/;
	return RegExp.test(s);
}

/**
 * Return TRUE if the organization number matches a Swedish Organization Number pattern, FALSE otherwise.
 * e.g. 123456-1234 or 1234561234
*/ 
function isSwedishOrganizationNumber(s) {
	var RegExp = /^(\d{6})(-?)(\d{4})$/;
	return RegExp.test(s);
}

/**
 * Test if a variable is defined.
 * @param object (e.g.) window, document.
*/
function isDefined(object, variable) {
	return (typeof(eval(object)[variable]) != 'undefined');
}

