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;
}
// 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;
}

// Added by kent 7/25/2001
function Rtrim(thestr){
	var cont = true;
	while (cont){
		trailing = thestr.substring(thestr.length-1,thestr.length);
		// if last char is a space, strip it off and continue testing the last space 
		if(trailing==" "){
			thestr = thestr.substring(0,thestr.length-1);
		}
		else{
			cont = false;
		}
	}
	return thestr;
}
// Added by kpb 11/26/2002
function Ltrim(thestr){
	var cont = true;
	while (cont){
		leading = thestr.substring(0,1);
		// if first char is a space, strip it off and continue testing the first space 
		if(leading==" "){
			thestr = thestr.substring(1,thestr.length);
		}
		else{
			cont = false;
		}
	}
	return thestr;
}
// Added by kpb 11/26/2002
function Trim(thestr){
	trimstr = thestr;
	trimstr = Rtrim(trimstr);
	trimstr = Ltrim(trimstr);
	
	return trimstr;
}
// Added by kent 2/22/2002
function writeoutput(div,msg){
	// for ie
	if (document.all) {
		var thediv = eval(div);
		thediv.innerHTML=msg;
	}
	// for netscape
	if (document.layers) {
		var thediv = eval("document."+div);
		thediv.document.write(msg);
		thediv.document.close();
	}
}
// Added by kent 3/11/2002
//CASE SENSITIVE Returns the first index of an occurrence of a substring in a string
function Find(substr, str) {
	i = str.indexOf(substr);
	return i;
}
// Added by kent 3/11/2002
//CASE IN-SENSITIVE Returns the first index of an occurrence of a substring in a string
function FindNoCase(substr, str) {
	str = str.toLowerCase();
	i = str.indexOf(substr);
	return i;
}

// Added by kent 3/11/2002
// This will tell us how many occurences of substr within a string
function OccursNoCase(substr, str){
	var i = 0;
	var x = 0;	// counter 
	if ((str == "")||(substr == "")) return x;
	tempstr = str.toLowerCase();
	i = tempstr.indexOf(substr);
	while (i != -1) {
		x = x + 1;
		leading_str = tempstr.substring(0, i);
		trailing_str = tempstr.substring(i + substr.length, tempstr.length);
		tempstr = leading_str + trailing_str;
		i = tempstr.indexOf(substr, i);
		//alert("tempstr: "+tempstr+"\ni: "+i);
	}
	return x;
}
// Added by kent 3/11/2002
//returns length of list 
function ListLen(inList,delim){
	var len = 0;
	if (inList.length){
		var myarray = inList.split(delim);
		len = myarray.length;
	}
	return len;
}

// Added by bryan 5/19/2004
// Tests a string for XSS...returns true if invalid XSS data found,  returns false if string is ok.
function xssCheck(thestring){
	if( thestring.search(/<[^>]+>/) ){
		return true;
	}else{
		return false;
	}
}

function stripExtendedASCII(content){
	// ADDED BY BRUDDEN 07/05/2006
	// remove extended ascii characters as described here
	// see http://www.activsoftware.com/support/kb/index.cfm?fuseaction=single&id=1093
	var re1 = new RegExp(String.fromCharCode(8211),"g");
	var re2 = new RegExp(String.fromCharCode(8216),"g");
	var re3 = new RegExp(String.fromCharCode(8217),"g");
	var re4 = new RegExp(String.fromCharCode(8220),"g");
	var re5 = new RegExp(String.fromCharCode(8221),"g");
	// More word extended dash ascii codes.  These include the "en dash" and "em dash"
	// more on these codes can be found here http://www.cs.tut.fi/~jkorpela/dashes.html
	var re6 = new RegExp(String.fromCharCode(8209),"g");
	var re7 = new RegExp(String.fromCharCode(8208),"g");
	// 8 takes care of the double dash found in "high<dash><dash>43%"
	var re8 = new RegExp(String.fromCharCode(8212),"g");
	var re9 = new RegExp(String.fromCharCode(8210),"g");
	var re10 = new RegExp(String.fromCharCode(8213),"g");
	var re11 = new RegExp(String.fromCharCode(8275),"g");
	var re12 = new RegExp("&#8217;","g");
	var re13 = new RegExp("&#8220;","g");
	var re14 = new RegExp("&#8221;","g");

	// Once the regex objects are created above, we still need to apply them to the content to remove the special characters
	content = content.replace(re1, "-"); 
	content = content.replace(re2, "'"); 
	content = content.replace(re3, "'"); 
	content = content.replace(re4, '"'); 
	content = content.replace(re5, '"'); 
	content = content.replace(re6, "-"); 
	content = content.replace(re7, "-"); 
	content = content.replace(re8, "-"); 
	content = content.replace(re9, "-"); 
	content = content.replace(re10, "-"); 
	content = content.replace(re11, "-"); 
	content = content.replace(re12, "'"); 
	content = content.replace(re13, '"'); 
	content = content.replace(re14, '"'); 

	return(content);
}
