/* 
  this file contains a coolection of useful functions for manipulating strings
 */

// pad string str up from the left (if fromLeft = true) or from the right up to length len with character padChar
function padString(str,len,fromLeft,padChar){
  var strLen
	var padStr
	var padCharStr
	var idx

	strLen = (new String(str)).length
	if (strLen >= len) return str

	padStr = "";
	padCharStr = new String(padChar);
	for (idx = 1; idx <= (len-strLen); idx++){
	  padStr = padStr + padCharStr
	}

	if (fromLeft) {
	  return padStr + str
	} else {
	  return str + padStr
	}
}

// returns a decimal number for the dollar amount represented by the value parameter
// handles the case wher value is of the form $123.22 
function getDollarAmt(value){
   var rExp	

 	 // trim the string
   rExp = / /gi;
   value = value.replace(rExp,'')

   rExp = /\$/gi;
   value = value.replace(rExp,'')

   if (value.length == 0) return 0.00
 
	 return parseFloat(value)
}

// this function returns true if value represents a zero dollar amount
// otherwise returns false
function iszeroamt(val){
  return ( (val == "") || (val == 0) || (val == 0.00) )
}

// this function formats the string str according to the format specified by the fmt parameter
// fmt can be one of:
//    currency - format str to the ####.## format   
function format_string(str,fmt){
  var pos

  if (fmt == "currency"){
	  pos = str.indexOf(".")
		len = str.length
		if (pos == -1) {
		  str = str + ".00"
		} else {
		  if (len == pos + 1) {
        str = str + ".00"
			} else {
		    if (len == pos + 2) {
				  str = str + "0"
				} else {
				  str = str.substring(0,pos) + "." + str.substring(pos+1,pos+3)
				}
			}
		}  
	}
	return str
}

function parseFloatSafe(value){
  var parseValue

  if (value == "") { return 0.00 }
  if (value == " ") { return 0.00 }
  if (isNaN(value)) { return 0.00 }
  parseValue = parseFloat(value)
  if (!parseValue) {
    return 0.00 
  }

  return parseValue
}

function parseIntSafe(value){
  var parseValue

  if (value == "") return 0.00
  if (value == " ") return 0.00
  if (isNaN(value)) return 0.00
  parseValue = parseInt(value)
  if (!parseValue) return 0.00

  return parseValue
}

// *tamara 07/05
function striptags(str) {
  var re= /<\S[^>]*>/g; 
  return str.replace(re,""); 
}

function add_slashes(str) {
  var new_str

  new_str = str.replace('"','\"')
  return new_str
}

function is_valid_phone(val) {
  var filter  

  filter = /^\d{3}[\-]\d{3}[\-]\d{4}$/;
  return filter.test(val)
}

function is_valid_email(val) {
  var filter  

  filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return filter.test(val)
}

function is_valid_zip_code(val) {
  var filter  

  filter = /^\d{5}([\-]\d{4})?$/;
  return filter.test(val)
}

function is_valid_date(str) {
  var o_date 
	var is_valid
	
	try {
	  o_date = new Date(str)
		is_valid = (o_date.getDate() > 0)
	} catch {
	  is_valid = false
	}
}