// some utils from http://www.geocities.com/cascutn_cnairda/10.1.2.html
//reformat examples:
//   "(123) 456-7890" :  reformat("1234567890", "(", 3, ") ", 3, "-", 4)
//    "123-45-6789"   :  reformat("123456789", "", 3, "-", 2, "-", 4)
function reformat (s){
  var arg;
  var sPos = 0;
  var resultString = "";
  for (var i = 1; i < reformat.arguments.length; i++) {
    arg = reformat.arguments[i];
    if (i % 2 == 1) resultString += arg;
    else {
      resultString += s.substring(sPos, sPos + arg);
      sPos += arg;
    }
  }
  return resultString;
}

// check for valid email address
/* JR changed on 11/21/2006
function isEmail(obj)
{
   // an empty string passes as ok
   if(0==obj.value.length) return true;

	var result = false;
	var theStr = new String(obj.value);
	var index = theStr.indexOf("@");
	
	if (index > 0)
	{
		if (-1 != theStr.indexOf("@", index + 1))
		{
			retVal = false;
		}
		else
		{
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1))
				result = true;
		}
	}
  
	return result;
}
*/

// check for valid email address
function isEmail(obj)
{
	// an empty string passes as ok
	if(0==obj.value.length) return true;
   
	return isEmailTxt(obj.value);
}

// check for valid email address
function isEmailTxt(txt)
{
	// an empty string passes as ok
	if(0==txt) return true;
   
	var result = false;
	var theStr = new String(trim(txt));
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if(document.layers||document.getElementById||document.all)
   	{
		if(filter.test(theStr))
			result = true;
   	}
   	else
   	{
	   return true;
   	}

	return result;
}


function stripCharsNotInBag(s, bag)
{	
	var i;
	var returnString = "";

	 for (i = 0; i < s.length; i++)
	 {	  
		  var c = s.charAt(i);
		  if (bag.indexOf(c) != -1) returnString += c;
	 }

	 return returnString;
}

// check the phone number
// an empty string passes as ok
// formatted:  stripped
function doPhone(obj)	
{
	// an empty string passes as ok
	if(0 == obj.value.length) return true;

	s = stripCharsNotInBag (obj.value, '1234567890');
	if (s.length != 10)
	{
		return false;
	}
	else
	{
		// formatted xxx-xxx-xxxx
		// obj.value = reformat(stripCharsNotInBag (obj.value, '1234567890'), '', 3, '-', 3, '-', 4);
		// no reformat - just stripped
		obj.value = s;
	
		return true;
	}
}

// check the name
// the only thing that passes is non-empty string containing alpha characters
// and up to one single quote
function doCheckName(obj)	
{
	var i;
	var s = obj.value;
	var sLower = s.toLowerCase();
	var foundQuote = false;

	// an empty string passes IS NOT ok
	if(0 == s.length) return false;

	 for (i = 0; i < s.length; i++)
	 {	  
		  var c = s.charAt(i);
			if ((c.toLowerCase() >= "a" && c.toLowerCase() <= "z") ||
				 ((c == "'") && !foundQuote))
			{
				if (c == "'")
				{
						foundQuote = true;
				}
				continue;
			}
			else
				return false;
	 }
	 if(i == 1 && foundQuote)
	 	return false;

	 return true;
}

// check the filename
// an empty string passes as ok
function doFileName(obj)	
{
	var i;
	var s = obj.value;
	var sLower = s.toLowerCase();

	// an empty string passes as ok
	if(0 == s.length) return true;

	if (-1 == sLower.indexOf(".jpg"))
	{
		if (-1 == sLower.indexOf(".gif"))
			return false;
	}
	 for (i = 0; i < s.length; i++)
	 {	  
		  var c = s.charAt(i);
			if ((c >= "0" && c <= "9") ||
				 (c.toLowerCase() >= "a" && c.toLowerCase() <= "z") ||
				 (c == ".") || (c == "_"))
			{
				continue;
			}
			else
				return false;
	 }

	 return true;
}

/* Formats the cell to X decimal places */
function format(total,DecimalPlaces)
{
     // First verify incoming value is a number
     if(isNaN(total))
          return "0.00";
     
     // Second round incoming value to correct number of decimal places
     var RoundedTotal = total * Math.pow(10, DecimalPlaces);
     RoundedTotal = Math.round(RoundedTotal);
     RoundedTotal = RoundedTotal / Math.pow(10, DecimalPlaces);
     
     // Third pad with 0's if necessary the number to a string
     var Totalstring = RoundedTotal.toString(); // Convert to a string
     var DecimalPoint = Totalstring.indexOf("."); // Look for decimal point
     if(DecimalPoint == -1)
     {     // No decimal so we need to pad all decimal places with 0's - if any
          currentDecimals = 0;
          // Add a decimal point if DecimalPlaces is GT 0
          Totalstring += DecimalPlaces > 0 ? "." : "";
     }
     else
     {     // There is already a decimal so we only need to pad remaining decimal places with 0's
          currentDecimals = Totalstring.length - DecimalPoint - 1;
     }
     // Determine how many decimal places need to be padded with 0's
     var Pad = DecimalPlaces - currentDecimals;
     if(Pad > 0)
     {
          for(var count = 1; count <= Pad; count++)
               Totalstring += "0";
     }
     // Return formatted value
     return Totalstring;
}

/* delete all but the selected item from a combo box */
function delAllButSelected(combo)
{
	var i = 0;
	do {
		if(!combo.options[i].selected) {
			combo.options.remove(i);
		}
		else {
			i++
		}
	} while(i<combo.options.length);
}

/* trim all spaces before and after a string */
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
