var digits = "0123456789";
// reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... )       
//
// Inserts formatting characters or delimiters within TARGETSTRING.
//
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;
}
// Returns true if all characters in string s are numbers.
//
function isInteger (s)
{   var i;
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!((c >= "0") && (c <= "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//
// Returns true if all characters in string s are alpha.
//
function isAlphaNumeric (s)
{   var i;
    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!(((c >= "A") && (c <= "Z")) || ((c >= "a") && (c <= "z")) || ((c >= "0") && (c <= "9")) || (c == "_"))) return false;
    }
    // All characters are numbers.
    return true;
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

// This function will replace all occurences of string X with string Y in the argument
function ReplaceString(argvalue, x, y) {
	var leading, trailing, i;

	if (argvalue == "") return argvalue;
	i = argvalue.indexOf(x);
	while (i != -1) {
//	while ((i = argvalue.indexOf(x)) != -1) {
		leading = argvalue.substring(0, i);
		trailing = argvalue.substring(i + x.length, argvalue.length);
		argvalue = leading + y + trailing;
		i = argvalue.indexOf(x, i + y.length);
	}
	return argvalue;
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

