<!--
/*
 -------------------------------------------------------------------------------
|  Copyright (C) 2003 DigitallyDistinct.com. All rights reserved.               |
|-------------------------------------------------------------------------------|
|  Unless otherwise specified, all code contained herein and that contained	in  |
|  the same directory location as this file, including this file, are the       |
|  property and creation of DigitallyDistinct.com, and to which                 |
|  DigitallyDistinct.com retains the sole intellectual copyright(s) as          |
|  permitted by the fullest extent of the law. No portion of this file may be   |
|  duplicated, redistributed or manipulated in any form. For copyright          |
|  questions, send correspondence to:                                           |
|                                                                               |
|  DigitallyDistinct.com                                                        |
|  Intellectual Property & Copyrights                                           |
|  5105 Old Bullard Rd. No. O24                                                 |
|  Tyler, TX 75703                                                              |
|  info@digitallydistinct.com                                                   |
 -------------------------------------------------------------------------------

 Organization: Valor Products
 			   P.O. Box 1271
			   Mineloa, TX 75773
			   info@seasilverbonus.com
       Domain: www.seasilverbonus.com
         
		 Date: Thursday, May 5, 2003
      Purpose: String function library
   
   Programmer: Benjamin Roberts
   			   DigitallyDistinct.com
			   5105 Old Bullard Rd. No. O24
			   Tyler, TX 75703-3152
               broberts@digitalldistinct.com
*/

// 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
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 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;
}

// 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;
	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;
}

//-->