﻿// Generic javascript functions

	function Trim(s) {
		// Remove leading spaces and carriage returns
	  
		while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))	{
			s = s.substring(1,s.length);
		}

		// Remove trailing spaces and carriage returns

		while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))	{
			s = s.substring(0,s.length-1);
		}

		return s;
	}
	

        function ltrim(str){
                return str.replace(/^\s+/, '');
            }
        function rtrim(str) {
                return str.replace(/\s+$/, '');
            }
        function alltrim(str) {
                return str.replace(/^\s+|\s+$/g, '');
            }	
            
// 8-24-2006 : Kendall Beaman
// Function replaces all consecutive dashes with a single dash for a given form element
// frm is the form element you wish to process. Here's an example of how to call this function
// <form id="testForm" action="" method="post" onsubmit="filterDashes(getElementById('testForm'));">
// I recommend using the id attribute rather than name as name is deprecated and use the getElementById function.
// Could also use ' onsubmit="filterDashes(this);" which will work just fine

function filterDashes( frm )
{
	var str;
	
	var result;
	
	for (x=0; x < frm.elements.length; x++)
	{
		var pattern = /-{2,}/g
		str = frm.elements[x].value;
		
		if ( pattern.test(str) == true)
		{
						
			frm.elements[x].value = str.replace(pattern,"-");
		}
	}
}

// Function searches the entire document for forms and then goes through them to remove dashes
// Not sure if this is needed but it's here in case anyone wants to use it.

function allFilterDashes ( )
{
	var forms = document.getElementsByTagName("form");
	
	for ( var x = 0; x < forms.length; x++ )
		filterDashes(forms[x]);
}


//valid emails must not have :,';  characters and must have 
//form:  validChars@validChars.validChars
//top level domain specified must be at least two characters
function CheckEmail(address) 
    {
        var re = new RegExp(/^([0-9a-z]([-.\w]*[0-9a-z])*@([0-9a-z][-\w]*[0-9a-z]\.)+[a-z]{2,9})$/);
        if (address.match(re)) 
        {
            return true;
        }
            else
        {
            return false;
        } 
    } 
    
		// This is the script that handles adding specialties to the specialty lists
		function AddOption(SelectBox, promptString, MaxLen)  {
			// Define a new string
				var NewOpt = new String;
				NewOpt = "";
				
			// Define RegExp search pattern that looks for "--"
			    var pattern = /-{2,}/g
			
			// Prompt for the New Option and store it in NewOpt 
				//do {
					NewOpt = prompt(promptString, NewOpt);
					if (NewOpt == null) return;
					if (NewOpt.length > MaxLen) {
						alert("You may not enter a value longer than " + MaxLen + " characters.");
						NewOpt = NewOpt.substr(0, MaxLen);
					}
			//	} while (NewOpt.length > MaxLen);
				
			// If NewOpt is empty (no new option), then stop processing and make no changes
				if (NewOpt == null) return;
				NewOpt = Trim(NewOpt);
				if (NewOpt.length == 0) return;
				
			// If it gets here, NewSpec is *not* blank, so add it to the bottom of the specialty list SpecList
			    NewOpt = NewOpt.replace(pattern,'-');
				NewOption = new Option(NewOpt, NewOpt);
				SelectBox.options[SelectBox.length] = NewOption;
			
			// Make the new option the Selected option
				SelectBox.options[SelectBox.length - 1].selected = true;
				SelectBox.focus();
           

			// Now return control to the user
				return;
        }       

// test for integer - found on web
function isInteger(s)
{
    return Math.ceil(s) == Math.floor(s);
}


