// Include the miscFunctions code
 function GetLength( argStr)
{
	var str = new String()
	str.value = argStr	
	return str.length;
}

String.prototype.trim = function(  )
{
		var str
		var TrimStr = new String();
		var StartPos, EndPos;
		var ch;
		var string_length
		var EndFound, StartFound
		
		str = this.value;
		
		if (( str == null) || (str == "" )) return str; // return if we have an empty string

		  string_length= str.length;
		// We need to find the first non-blank character
		for ( blank_index = 0; blank_index <=string_length;  blank_index++)
		{
				ch = str.charAt( blank_index)
		 		if (ch  != " ") 	
				{ 	StartPos = blank_index;
					StartFound = true;
					break;						
				}
		} // end for 
		
		if (StartFound != true) return "";	 // we didn't find any non-blank characters
		
		// We need to find the last non-blank character					
		for ( blank_index = string_length;  blank_index >=  0;  blank_index--)
		{								
				ch = str.charAt( blank_index)
				//alert('Index: ' + blank_index + '\n\n char [' + ch + ']');
		 		if ( (ch  != " ") && ( ch !="") )
				{
					//alert('Character = [' + ch + ']');
					EndPos = blank_index;
					EndFound = true;
					//alert(' Breaking out : Returning: Index  = ' + blank_index) ;
					break;								
				}	
		} // end for 
		
		
		if (EndFound != true) 
		{
				//alert(' Returning: Empty String') ;
				return ""; // we didn't find any non-blank trailing characters			
		}
		else
		{
			TrimStr = str.substring( StartPos, EndPos+1);						
			return TrimStr
		}
}



function FormatValue( argFormatType, argStrToFormat )
{
	if (argStrToFormat == "" || argStrToFormat == null)	 return  argStrToFormat;
	
	switch( argFormatType )
	{
		case "phone": 
   				 var good ;
				 var OK ;
				 var i,s,checkVal;
				 OK = true;
			   	 good = "1234567890";
				 var sWorkingString = ""
				 var sFormattedString = ""
		
				  //First get the string to have only the 10 numbers
				  for (i=0; i <= argStrToFormat.length -1; i++)
				  {
						// Check character from our string against the acceptable characters.
						s =  argStrToFormat.charAt(i);
						checkVal = good.indexOf(s) ;
						if (checkVal == -1 || checkVal == null)  
							//skip it
							 ;
						else
							sWorkingString = sWorkingString + s;
				  }
 				//alert("sWorkingString:" + sWorkingString );
				// Now we add in the formatting (strings in Javascript are 0-based)
				for (i = 0 ; i<=9; i++)
				{
					s= sWorkingString.substr( i, 1);
					//alert("i/S:" + i + "/" + s);
					switch( i)
					{
						case 0:
							sFormattedString = "(" + s
							break;
						case 2:
							sFormattedString =  sFormattedString + s + ") ";
							break;
						case 5:
							sFormattedString =  sFormattedString +s + "-";
							break;
						default:
							sFormattedString =  sFormattedString + s;
							break;
					} // end switch					
				}// end for i
		} // end switch
		return sFormattedString;					 
} // end function



function ValidateValue( argFormatType, argStrToValidate, argArrayErrMsg)  // Arrays are the only way to pass by ref in JS
{
		var errMSG = ""
		var regExpPhone = /((([\(]?[0-9]{3}[\)]?)|([0-9]{3}-))\s*[0-9]{3}\s*[-]?\s*[0-9]{4}$)/;
		//var regExpEmail  = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		var regExpEmail  = /[\w-]+[\.\w]*@[\w][\.\-]?[\w]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		var regExpUSPostal  = /^\d{5}$|^\d{5}-\d{4}$/;
		var regExpCanadaPostal  = /[a-zA-Z]\d[a-zA-Z][\s]*\d[a-zA-Z]\d$/;
		var OK = false;
		var strValidate;

		var str = new String()
		str.value=argStrToValidate		
		strValidate = str.trim();	
		
//	alert("in validate value:  old =|" + argStrToValidate + "|, new string =|" + strValidate + "|");
//	alert("in validate value:  Format Type =|" + argFormatType + "| string=|" + strValidate + "|" );
//	if  (argStrToValidate == "") return true; //There was nothing to validate
	switch( argFormatType )
	{
		case "phone": 
				//alert("validating phone");
				if (regExpPhone.test( strValidate) != true)  
				{	OK = false;
					argArrayErrMsg[0] = "Phone number is not in a recognizable form. It must be simliar to (888) 321-1234\n";
				}
				else if (strValidate != "")	
				{
					strValidate = FormatValue("phone", strValidate);
					OK = true;
				}
				else
				{
					OK = false;
				}
			break;
		
		case "email":
				//alert("validating email");		
				if (strValidate != "") 
				{
					if (regExpEmail.test(strValidate  ) != true )  
					{	OK = false;
						argArrayErrMsg[0] ="Email is not in a recognizable form. It must be simliar to myName@myCompany.com\n";
					}
					else
						OK = true;					
				}
				else
				{
					argArrayErrMsg[0] ="Email is not in a recognizable form. It must be simliar to myName@myCompany.com\n";
					OK = false;
				}
			break;
			
	case "postal":
			if (strValidate != "") 
			{
				if (regExpUSPostal.test(strValidate) != true )  
					{	OK = false;
						//alert("failed US");
						// Try canadian Postal Code
						//if (regExpCanadaPostal.test(strValidate) != true )  
						//{					
							//alert("failed CANADA");		
							argArrayErrMsg[0] ="Postal code is not in a recognizable form. It must be simliar to the following:\nUS ZIP: 12345 or 12345-0020";
					//	}
					//	else
					//	{
					//		OK = true;	
					//	}
					}
					else
						OK = true;						
			}
	}

	return OK;
}



function ValidateNumber( as_value )
{
	var li_number
	li_number = parseInt( as_value );
	if ( isNaN(li_number) == false ) // we have a number 'Not a Number' = false
			return true;
	else
		   return false;
}

