/*
 * Author:  Fadi Chamieh
 * Purpose: Trim a string from leading and trailing spaces
 */

function trimSpaces ( str ) {

	var trimmed = '';
	var start_edge = 0;
	var end_edge = 0;

	// Detect start edge
	for(var i = 0; i < str.length; i++)
		if (str.charAt(i) != ' ') {
			start_edge = i;
			break;
		}
	// Detect end edge
	for(i = str.length - 1; i > 0; i--)
		if (str.charAt(i) != ' ') {
			end_edge = i;
			break;
		}
	// Get what's between edges
	for(i = start_edge; i <= end_edge; i++)
		trimmed += str.charAt(i);
		
	if (trimmed == ' ') trimmed = '';
		
	return trimmed;

} // end function trimSpaces


function areCharsIn( checkStr, validCharsStr ){
	
	var checkOK = validCharsStr;
	var allValid = true;
	checkStr += '';
	
	// if empty return false
	if (checkStr == '') return false;
	
	for (i = 0;  i < checkStr.length;  i++)	{
		
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j)) 
				break;
			
		if (j >= checkOK.length) {
			allValid = false;
			break;
		}
	}
	return ( allValid );
}


/*
 * Author:  Fadi Chamieh
 * Purpose: [boolean] check if a string is alpha-numeric  
 */
function isAlphaNumeric( checkStr ) {
	return areCharsIn(checkStr, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_");
}


/*
 * Author:  Fadi Chamieh
 * Purpose: [boolean] check if a string is numeric  
 */
function isNumeric( checkStr ) {
	return areCharsIn(checkStr, "0123456789");
}


/*
 * Author:  Fadi Chamieh
 * Purpose: [boolean] check if none of the "Check boxes" has been checked
 */
function noneChecked (frm, name, start, limit) {
	var flag = true;
		
	for (var i = start; i <= limit; i++) {
		var chk = eval("frm." + name + i);
		if (chk.checked) {
			flag = false;
			break;
		}
	}
		
	return flag;
}

/*
 * Author :  Fadi Chamieh
 * Purpose:  use an popup-ASP image viewer scaled to exact dimensions 
 *            (width, height) of contained image
 */
function ViewPhoto( viewerURL, imgURL ) {

	w=1;h=1;

	var url = viewerURL + "?imgURL=" + imgURL;
	
	options = "width="+w+", height="+h+",toolbar=0,location=0,status=0,menubar=0,resizable=1,scrollbars=0";
	
	var w = window.open(url, 'imageViewer', options);
	w.focus();
}


/*
 * Author :  Fadi Chamieh
 * Purpose:  return true if argument is a "valid" email
 */
function isValidEmail( testEmail ) {
	if (testEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) 
		return false;
	else 
		return true;
}
