/**
 * Copyright (c) 2006, ARC Technologies.
 * All Rights Reserved
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this application.
 *
 * File     $Id: StringFormat.js 1471 2010-12-22 15:13:28Z emann $ <br>
 * Author   Ed Mann <ed.mann at arctechnologies dot net>
 * @package eve.foundations.javascript
 */

/**
 * format Money
 * 
 * This function will take in a numeric only string and return a formated thousands cash string.
 * 
 * @return string 
 * @param string numeric only
 */
function Money(input){
	var num = input.value.replace(/\,/g,'').replace(/\$/g,'');
	if(!isNaN(num)){
	if(num.indexOf('.') > -1){
	num = num.split('.');
	num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
	if(num[1].length > 2){
	alert('You may only enter two decimals!');
	num[1] = num[1].substring(0,num[1].length-1);
	} 
	input.value = '$'+num[0]+'.'+num[1];
	} 
	else 
		{ 
		input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') 
		};
	}
	else{ alert('You may enter only numbers in this field!');
	input.value = input.value.substring(0,input.value.length-1);
	}
	}

/**
 * thousand
 * 
 * This function will read the value from an input field and format the stings to thousands. This only accepts numeric values.
 * @param {string} input 
 */
 function thousand(input){
	var num = input.value.replace(/\,/g,'');
	var count = 0;
	if(!isNaN(num)){
	if(num.indexOf('.') > -1){
	num = num.split('.');
	num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
	if(num[1].length > 4){
	alert('You may only enter four decimals!');
	num[1] = num[1].substring(0,num[1].length-1);
	} 
	input.value = num[0]+'.'+num[1];
	} 
	else 
		{ 
		input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') 
		};
	}
	else{ 
		if( count < 0) {
			count++;
		alert('You may enter only numbers in this field!');
	input.value = "";
		}
		else {
			input.value="";
		}
	}
	}
	
/**
 * check Numeric Only
 * 
 * Function will check a string to insure that it's all numeric.
 * @param {string} txt_num
 */
 function checkNumericOnly(txt_num)  {
	  num = parseFloat(txt_num);
	  if (num.toString() == "NaN") {
	    alert("One of the values is not a number.\n"
	      + "Please try again.");
	    num = "";
	    return;
	  }
	return num;
	}
	
/**
 * Urlencode
 * Found url escape codes from  http://www.dragonwinds.com/resources/html-codes.shtml
 * 
 */
 function urlEncode(url) {
     url = url.replace(/\//g,"%2F");
     url = url.replace(/\?/g,"%3F");
     url = url.replace(/=/g,"%3D");
     url = url.replace(/&/g,"%26");
     url = url.replace(/\s/g,"%20");
     url = url.replace(/\#/g,"%23");
     url = url.replace(/\$/g,"%24");
     url = url.replace(/\%/g,"%25");
     url = url.replace(/&/g," %26");
     url = url.replace(/\//g,"%2F");
     url = url.replace(/:/g,"%3A");
     url = url.replace(/;/g,"%3B");
     url = url.replace(/</g,"%3C");
     url = url.replace(/=/g,"%3D");
     url = url.replace(/>/g,"%3E");
     url = url.replace(/@/g,"%40");
     url = url.replace(/\[/g,"%5B");
     url = url.replace(/\\/g,"%5C");
     url = url.replace(/`/g,"%60");
     url = url.replace(/\{/g,"%7B");
     url = url.replace(/\|/g,"%7C");
     url = url.replace(/\}/g,"%7D");
     url = url.replace(/~/g,"%7E");
     return url;
   }
   
  /**
   * trim
   * 
   * Function will take the passed in string and remove the leading and trailing
   * spaces.
   * @param  {string} string to trim space from.
   * @return {string} input string without leading and trailing spaces
   */
  function trim(input) {
     return input.replace(/^\s+|\s+$/g, '');
   }

