/*

 -------------------------------------------------------------------------------
|  Copyright (C) 2004 Azalea Technology. All rights reserved.                   |
|-------------------------------------------------------------------------------|
|  Unauthorized removal of this notice is considered a violation of the         |
|  license agreement under which this Code may be used. This work is protected  |
|  under United States copyright law and the similiar law(s) of other countries |
|  under which such as work is afforded legal protection, and upon conviction   |
|  of such a violation in a court of applicable jurisdiction, such person(s)    |
|  may be subject to the maximum allowable penalty as permitted under such law. |
|-------------------------------------------------------------------------------|
|  You acknowledge and agree that information presented to you through this     |
|  site (the "Web Site") is protected by all applicable copyrights, trademarks, |
|  service marks, patents or other proprietary rights and laws, and by virute   |
|  of accessing the Web Site, except as expressly authorized by the Azalea      |
|  Technology, LLC., you agree not to modify, rent, lease, loan, sell,          |
|  distribute, store, or create derivative works based on the Web Site, in      |
|  whole or in part.                                                            |
 -------------------------------------------------------------------------------

 Organization: MHC X-Ploration Corporation
       Domain: www.mhcexploration.com, www.mhcxploration.com
      Purpose: Text (string) manipulation functions
	     Date: Monday, March 15, 2004 11:00 PM CST
   Programmer: Benjamin Roberts (broberts@azaleatech.com)
               Azalea Technology, LLC.
               P.O. Box 131150
			   Tyler, TX 75713-1150
*/
//**Start Encode**

// REMOVES EXTRA SPACE FROM THE BEGINNING, MIDDLE, AND END OF A STRING
function trim(inString){
	var retVal = "";
	var start = 0;
	var space = false;
	var newstr="";
	while ((start < inString.length) && (inString.charAt(start) == ' ')) ++start;
	var end = inString.length;
	while ((end > 0) && (inString.charAt(end - 1) == ' ')) --end;
	retVal = inString.substring(start, end);
	for(var i=0;i<retVal.length;i++){
		if(!space) newstr+=retVal.charAt(i);
		if(retVal.charAt(i)==" " && retVal.charAt(i+1)==" ") space=true;
		else space=false;
	}
	retVal=newstr;
	return retVal;
}

// TAKES A TEXT STRING AND RETURNS THE STRING FORMATTED LIKE A PROPER NOUN SUCH AS A PERSON'S NAME
// MODIFIED 2003.09.10 TO HANDLE NAMES THAT CONTAIN "Mc" LIKE "McDonald"
function toProperCase(str){
	var newstr="";
	str=trim(str);
	if(isSpace(str)) return false;
	for(var i=0;i<str.length;i++){
		if(i==0 || str.charAt(i-1)==" ") newstr+=str.charAt(i).toUpperCase();
		else if((str.charAt(i-2)+str.charAt(i-1)).toUpperCase()=="MC") newstr+=str.charAt(i).toUpperCase();
		else newstr+=str.charAt(i).toLowerCase();
	}
	return newstr;
}

// EVALUATES A TEXT STRING AND TEST TO SEE IF IT IS BLANK OR CONTAINS ONLY BLANK SPACES
function isSpace(str){
	if(str=="") return true;
	if(str.replace(/ /g,"").length==0) return true;
	return false;
}

// DETERMINES WHETHER A STRING IS A VALID POSITIVE INTEGER
function isPosInt(str){
	var validChars = "0123456789";
	var validChar = true;
	if(str=="") return false;
	for(i=0;i<str.length&&validChar;i++){
		validChar = false;
		for(j=0;j<validChars.length&&!validChar;j++){
			if(str.charAt(i)==validChars.charAt(j))
				validChar = true;
		}
	}
	return validChar;
}

// DETERMINES WHETHER A STRING IS A VALID CURRENCY VALUE
function isCurrency(str){
	var validChars = "0123456789.";
	var validChar = true;
	if(str=="") return false;
	for(i=0;i<str.length&&validChar;i++){
		validChar = false;
		for(j=0;j<validChars.length&&!validChar;j++){
			if(str.charAt(i)==validChars.charAt(j))
				validChar = true;
		}
	}
	return validChar;
}

// DETERMINE WHETHER A GIVEN YEAR, MONTH, AND DAY FORM A VALID DATE
function isDate(year,month,day){
	month = month - 1;  // javascript month range : 0- 11
  	var tempDate = new Date(year,month,day);
  	if ( (y2kYearFix(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
    	return true;
  	else
		return false;
}

// SPLITS A SUPPOSED DATE STRING BY THE "/" SEPERATOR
function parseDateString(string_val,part){
	var date_array;
	date_array = string_val.split("/");
	return date_array[part]; 
}

// VALIDATES A STRING AS A VALID DATE
function validDate(string_val){
	var month,day,year;
	month = parseDateString(string_val,0);
	day   = parseDateString(string_val,1);
	year  = parseDateString(string_val,2);
	return isDate(year,month,day);
}

// ADDRESSES THE REPORTING OF A DATE'S YEAR AS A 2-DIGIT VS. A 4-DIGIT NUMBER
function y2kYearFix(d){ 
	return (d < 1000) ? d + 1900 : d;
}

// VALIDATES AN SUPPOSED EMAIL STRING
function validEmail(email){
	invalidChars = "/:,;";
	if (email == "") return false;
	if (email.indexOf("@mailinator.com")!=-1) return false; // AVOIDS MAILINATOR.COM EMAIL ADDRESSES
	for (i=0; i<invalidChars.length; i++){
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) return false;
	}
	atPos = email.indexOf("@",1);
	if (atPos == -1) return false;
	if (email.indexOf("@",atPos+1) > -1) return false;
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) return false;
	if (periodPos+3 > email.length) return false;
    return true;
}

// VALIDATES AN SUPPOSED EMAIL STRING
function validTextString(str){
	intUnicodeValue = 0;
	intBackup = 0;
	var valid = false;
	if (str == "") return false;
	for (i=0;i<str.length;i++){
		intUnicodeValue = str.charCodeAt(i)
		if((intUnicodeValue<32||intUnicodeValue>126)&&(intUnicodeValue!=13&&intUnicodeValue!=10)){
			if(i<35) intBackup = 0;
			else intBackup = 35;
			alert("Invalid Character Found!\n\n"+str.substr(i-intBackup,intBackup+1)+" << invalid character");
			return false;
		}
	}
	return true;
}

function cleanTextString(obj,str){
	obj.value = "";
	intUnicodeValue = 0;
	if (str == "") return false;
	for (i=0;i<str.length;i++){
		switch(str.charCodeAt(i)){
			case 8217:
				obj.value = obj.value+"'";
				break;
			default:
				obj.value = obj.value+str.charAt(i);
				break;
		}
	}
	return true;
}

// VALIDATES THE FORMAT OF THE PHONE NUMBER	
function valid_phone(phone_number){
	if(isSpace(phone_number)||phone_number.length<12) return false;
	for(var i=0;i<phone_number.length;i++){
		if(i!=3 && i!=7){
			if(isNaN(parseInt(phone_number.charAt(i)))) return false;
		}
		else if(phone_number.charAt(i)!="-") return false;
	}
	return true;
}

// VALIDATES A POSTAL CODE
function isValidPostalCode(country,postalcode){
	
	if(isSpace(postalcode)) return false;  // postal code is empty
	country = country.toLowerCase(); // lower case the country
	
	// Check United States Postal Codes
	if(country=="united states"){
		if(postalcode.length<5) return false; // US postal codes are 5 or more characters
		if(postalcode.length==5){
			if(!isPosInt(postalcode)) return false;
		}
		else{
			var FiveDigitPostalCode = postalcode.substr(0,5);
			if(!isPosInt(FiveDigitPostalCode)) return false; // First 5 characters should be digits
			
			var ZipPlus4 = postalcode.substr(5);
			if(ZipPlus4.length!=5) return false; // ZIP+4 must be 5 characters long including the "-"
			if(ZipPlus4.charAt(0)!="-") return false; // First character of ZIP+4 must be a "-"
			
			ZipPlus4 = ZipPlus4.substr(1) // Remove "-"
			if(!isPosInt(ZipPlus4)) return false;
		}
	}
	// Formats of international postal codes vary too much to attempt to validate format is non-US
	return true;
}

// FUNCTION TO REMOVE DOUBLE QUOTES FROM A STRING
function removeDoubleQuotes(str){
	var newStr = "";
	if(!str) return newStr;
	if(isSpace(str)) return newStr;
	
	for(var i=0;i<str.length;i++){
		if(str.charAt(i)!="\"") newStr += str.charAt(i);
	}
	return newStr;
}

// FUNCTION TO WRITE A LONG STRING WITH "X" NUMBER OF CHARACTERS PER LINE
function writeLongStringOnMultipleLines(str,charsPerLine,charSpacesPerLine){
	var newStr = ""
	var charSpace = ""
	
	if( (!isPosInt(charsPerLine)) || (!isPosInt(charSpacesPerLine)) || isSpace(str) ) return newStr;
	for(var k=0;k<charSpacesPerLine;k++) charSpace += " ";
	
	for(var i=0;i<str.length;i++){
		if(i>0) newStr += ("\n" + charSpace);
		for(var j=0;j<charsPerLine;j++){
			newStr += str.charAt(i);
			i++;
		}
	}
	return newStr
}