// This file contains common form validation routines.

function isValidEmail( emailAddr ) {
	// Checks email address for proper format.
	// Acceptable format is xx@xx.xx

	// A valid email address has to have the following:
	// 1. length greater than 4 chars
	// 2. must have an @ char 
	// 3. cannot have embedded spaces
	// 4. gotta have a dot after @ somewhere
		
	if (( emailAddr.length < 4) ||
		( emailAddr.indexOf("@") < 0 ) ||
		( emailAddr.indexOf(" ") >= 0) ||
		( emailAddr.indexOf(".") < 0))
		return false;
			
	// NOTE: is this a valid check?
	if (emailAddr.indexOf("@") > emailAddr.lastIndexOf("."))
		return false;
		
	// if we get here, then it passed all checks.
	return true;
}

function trim( s ) {
	// Trims leading and trailing blanks.

	if (s == null) 
	  return "";
	  		
	return ltrim(rtrim(s));
}

function isValidURL( URL ) {
	// Very basic URL format checker
	// Checks for the following:
	// 1. url starts with http://
	// 2. url at least 12 chars long (assuming shortest
	//    possible url would be "http://x.x.x")

	if (((URL.substr(0,7).toLowerCase() != "http://") && (URL.substr(0,8).toLowerCase() != "https://"))
		|| (URL.length < 12))
		return false;
	
	return true;
	
}

function ltrim( s ) {
	// Trims leading blanks.

	var idx = 0;
	
	while (idx<s.length && s.charAt(idx) == " ")
		idx++;

	return s.substr(idx);		
}

function rtrim( s ) {
	// Trims trailing blanks.

	var idx = s.length-1;

	while (idx>=0 && s.charAt(idx) == " ")
		idx--;
			
	return s.substr(0,idx+1);
	
}

function isValidUSZip( zip ) {
	// Validates a US Zip code:
	
	// Checks for the following:
	// 1. length is 5 or 10 chars long
	// 2. format is 99999 or 99999-9999
	// 3. zip+4 parts are numeric, separated by a dash

	// check for valid length
	// NOTE: consider lead/trailing spaces insigificant (database code
	// NOTE: ... should trim values as needed before storing)
	if ((trim(zip).length != 5) && (trim(zip).length != 10))
	  return false;

	// 5-char zip must be all numeric
	if (zip.length == 5) {
		if (isNaN(zip))
			return false;
	}
	
	// 10-char zip must be numeric, sep by dash
	if (zip.length == 10) {
		if (zip.charAt(5) != '-') 
			return false;
		// break up zip+4
		var zip5 = zip.substr(0,5);
		var zip4 = zip.substr(6,4);
		if (isNaN(parseInt(zip5)) || isNaN(parseInt(zip4)))
			return false;
		// must do this check (without parseInt) since above code
		// ... fails to catch embedded alpha chars (eg. 123x5)
		if (isNaN(zip5) || isNaN(zip4))
			return false;
	}	  

	// We're OK if we get this far.
	return true;
	
}

function isValidBZUserName( bzUserName ) {
	// Checks a user name to ensure that no special characters
	// ... are included.  This is because the username may become
	// ... the leading part of an email address,
	// ... username@buzzsaw.com, for example.
	
	// Checks for the following:
	// 1. no periods
	// 2. no characters not suitable for inet email addresses.
	//    ... the following are allowed: numbers, A-Z, a-z, underscore (_),
	//    ... tilde (~), dashes (-), and colon (:)

	// NOTE: server-side validation is also done

	var notAllowed = ' !@#$%^&*()+|=/;",<>?{}[]';
	notAllowed += '\\';
	notAllowed += '\'';
		
	for (var idx=0; idx < notAllowed.length; idx++)  {
		if (bzUserName.indexOf( notAllowed.charAt(idx) ) > -1)
			return false;
	}
	
	return true;
}

//***********************************************************
// platform and browser checking 
//***********************************************************

function isMac()  {
	// This function checks the browser version info 
	// ... to determine the platform.
	
	// get platform info 
	var platform = navigator.appVersion.toLowerCase();
	
	// indexOf returns -1 if string is not found
	return (platform.indexOf("mac") >= 0);
	
}

function isWin() {
	// This function checks the browser version info 
	// ... to determine the platform.
	
	// get platform info 
	var platform = navigator.appVersion.toLowerCase();
	
	// indexOf returns -1 if string is not found
	return (platform.indexOf("win") >= 0);
}

function isUnix() {
	// This function checks the browser version info 
	// ... to determine the platform.
	
	// get platform info 
	var platform = navigator.appVersion.toLowerCase();
	
	// indexOf returns -1 if string is not found
	return (platform.indexOf("nix") >= 0);
}

function isOther() {

	return !(isMac() || isWin() || isUnix());
	
}


