/********************************************************
 *
 *	lib_dataFormats.js
 *
 *	This library contains a number of functions for verifying
 *	that string data is compliant to a various format.
 *
 *	There will be 2 modes for all functions, regular and
 *	strict. Regular will verify that the correct number of
 *	digits or characters in the proper order are present.
 *	Strict will do the same check as regular, but in addition
 *	check to make sure the data is in one of the acceptable
 *	formats for that data type.
 *	
 *	Usage
 *	----------------------
 *	In the javascript on the file including this file, type the following:
 *	myReference = new specialDataTypes();	// This creates a reference to the set of dataFormats functions
 *	myReference.isCurrency(string);			// Call the functions like so, they will return boolean values
 *
 ********************************************************/
 function specialDataTypes() {
  
  	//------------------------------------------------------------------------
	//	Public Method List
	//	All functions take a string value as the first parameter
	//	All functions return a boolean
	//------------------------------------------------------------------------  
  	this.isBlank = isBlank;
	this.isNumeric = isNumeric;
	this.isInteger = isInteger;
	this.isAlphaNumeric = isAlphaNumeric;
  	this.isCurrency = isCurrency;
	this.isEmailAddress = isEmailAddress;
	this.isPhoneNumber = isPhoneNumber;
	this.isZipCode = isZipCode;
	this.isState = isState;
	this.isCreditCard = isCreditCard;
	this.isSocialSecurityNumber = isSocialSecurityNumber;
	

	
	////////////////////////////////////////
	//			isBlank
	///////////////////////////////////////
	function isBlank(str) { 
		if(trim(str) == '') return true;
		
		return false;
	}



	////////////////////////////////////////
	//			isInteger
	////////////////////////////////////////
	function isInteger(val) {
		for (var i=0; i < val.length; i++) {
			if (!isDigit(val.charAt(i))) { return false; }
			}
		return true;
	}

	
	
	////////////////////////////////////////
	//			isCurrency
	///////////////////////////////////////
	function isCurrency(str) { 
		// match #*.##
		str = trim(str);
		
		var indexOfDot = str.indexOf('.');
		var numDots = 0;
		var numPrefix = 0;
		var numSuffix = 0;
		
		for(i=0; i<str.length; i++) {
			if (i < indexOfDot) { numPrefix += 1; }
			else if (i > indexOfDot) { numSuffix += 1; }
			
			if( str.charAt(i) == '.' ) { numDots += 1; }
			else if(!isDigit(str.charAt(i))) { return false; }
		}
		
		if ( indexOfDot > 0 && numDots == 1 && numPrefix > 0 && numSuffix == 2 ) { return true; }
		else { return false; }		
	}

	
	
	////////////////////////////////////////
	//		isSocialSecurityNumber
	///////////////////////////////////////
	function isSocialSecurityNumber(str) { 
		str = getNumeric(str);
		if(str.length == 9) { return true; }
		
		return false;
	}	

	
	
	////////////////////////////////////////
	//			isPhoneNumber
	///////////////////////////////////////
	function isPhoneNumber(str) { 
		str = getNumeric(str);
		if ( str.length != 10) { return false; }

				// Strict will test for
				// a set of 3, a set of 3, a set of 4
				// around the first set of 3 can be "(|)"
				// in between the sets can be a space or "-"
			
		return true;
	}
	

	
	////////////////////////////////////////
	//			isZipCode
	///////////////////////////////////////
	function isZipCode(str) { 
		str = getNumeric(str);
		if ( str.length != 5 ) { return false; }
			
			// Strict will test for
			// ##### or #####-####
		return true;
	}	
	
	

	////////////////////////////////////////
	//			isEmail
	///////////////////////////////////////
	function isEmailAddress(str) { 
	   var indexAt  = str.indexOf("@");
	   var indexDot = str.indexOf(".");
	   var indexLastDot = str.lastIndexOf(".");
	
		if ( str.length > 0 ) {
			if ( (indexDot < 0) || (indexAt < 0) ) return false;
			else if ( indexLastDot <= (indexAt+1) ) return false;
		}
	
		return true;
	}

	
	
	////////////////////////////////////////
	//			isCreditCard
	///////////////////////////////////////	
	function isCreditCard(creditcard_string) {
		// check mod10
		creditcard_string = getNumeric(creditcard_string);
		if(!creditcard_string.length) { return false; }
		
		var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
		var checkdigit = 0;
		var tempdigit;
	
		for (var i = 0; i < creditcard_string.length; i++) {
			tempdigit = eval(creditcard_string.charAt(i))
	
			if (doubledigit) {
				tempdigit *= 2;
				checkdigit += (tempdigit % 10);
	
				if ((tempdigit / 10) >= 1.0) {
					checkdigit++;
				}
	
				doubledigit = false;
			}
			else {
				checkdigit += tempdigit;
				doubledigit = true;
			}
		}
		
			// Strict will test for
			// if 16 digit: 4 sets of 4 with either spaces or dashes in between
			// if 15 digit: set of 4, 6, 5 with either spaces or dashes in between
			// if 14 digit, 4,6,4 ....
		
		return (checkdigit % 10) == 0 ? true : false;
    }	
	
	
	////////////////////////////////////////
	//			isAlphaNumeric
	///////////////////////////////////////		
	function isAlphaNumeric(str) {
		for(i=0; i<str.length; i++) {
			if(!isDigit(str.charAt(i)) && ! isAlpha(str.charAt(i))) { return false; }
		}

		return true;
	}


	
	////////////////////////////////////////
	//			isNumeric
	//	Tests for a positive float value
	///////////////////////////////////////			
	function isNumeric(val) {
		var dp = false;
		for (var i=0; i < val.length; i++) {
			if (!isDigit(val.charAt(i))) { 
				if (val.charAt(i) == '.') {
					if (dp == true) { return false; } // already saw a decimal point
					else { dp = true; }
				}
				else {
					return false; 
				}
			}
		}
		
		return true;
	}
	
	
	

	////////////////////////////////////////
	//		isState(string)
	//  	Returns true if string is a 
	//		US State Abbreviation
	////////////////////////////////////////
	function isState(val) {
		Val = val+"";
		if (val.length != 2) { return false; }
		if (val.charAt(0) == ' ' || val.charAt(1) == ' ') { return false; }
		
		var string="AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY";
		if (string.indexOf(val.toUpperCase()) != -1) {
			return true;
			}
		return false;
	}


	
	
	
	/****************************************************************
	 ****************************************************************
	 *																*
	 *			UTILITY FUNCTIONS USED BY MAIN METHODS				*
	 *																*
	 *****************************************************************
	 ****************************************************************/
	
	//-------------------------------------------------------------------
	//	isStrict, returns boolean. Checks a functions paramters to determine
	//	if it is being used in strict mode.
	//-------------------------------------------------------------------
	function isStrict(anArray) {
		if(anArray.length > 1) {
			if(anArray[1]) { return true; }
		}
		
		return false;
	}
	
	
	//-------------------------------------------------------------------
	// getNumeric(value)
	//   Returns a string with only its numeric values remaining
	//-------------------------------------------------------------------
	function getNumeric(val) {
		var numericString = '';
		for (var i=0; i < val.length; i++) {
			if (isDigit(val.charAt(i))) { 
				numericString += val.charAt(i);
			}
		}
		
		return numericString;
	}
	


	//-------------------------------------------------------------------
	// isDigit(value)
	//   Returns true if value is a 1-character digit
	//-------------------------------------------------------------------
	function isDigit(num) {
		var string="1234567890";
		if (string.indexOf(num) != -1) {
			return true;
			}
		return false;
	}
	
	//-------------------------------------------------------------------
	// isAlpha(value)
	//   Returns true if value is a 1-character letter
	//-------------------------------------------------------------------
	function isAlpha(num) {
		var string="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		if (string.indexOf(num) != -1) {
			return true;
			}
		return false;
	}	
	
	
	//-------------------------------------------------------------------
	// Trim functions
	//   Returns string with whitespace trimmed
	//-------------------------------------------------------------------
	function Ltrim(str) {
		for (var i=0; str.charAt(i)==" "; i++);
		return str.substring(i,str.length);
	}
	
	function Rtrim(str) {
		for (var i=str.length-1; str.charAt(i)==" "; i--);
		return str.substring(0,i+1);
	}
	
	function trim(str) {
		return Ltrim(Rtrim(str));
	}		
		
  
 } // End of this library
