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);
}
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        //((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function getCookiesTest() {
	var r_var = '';
	sccode = getCookie('sccode');
	locationcode = getCookie('locationcode');
	r_var = 'sccode: ' + sccode + ' locationcode: ' + locationcode;
	document.write(r_var);
}
/*
	This function in IE will return all allowed attributes for an element (whether they are explicitly entered in the HTML source or not)
	In other browsers such as Safari, MOZ, FireFox, it only returns the attributes explicitly entered.
	This function can be used in conjuction with the "getAttribute()" and "setAttribute()" methods 

	Example on detecting if an attribute exists
		if(!element.getAttribute('attribute_name'))
	Examples on setting the attribute to a specific value
		element.setAttribute('attributeName','attributeValue')
		theParagraph.setAttribute('align','center')	
*/
function displayAllAttributesOfAnElement(element, attribNameStr) {
	var attributesStr = "";
	for( var x = 0; x < element.attributes.length; x++ ) {
		nodeName = element.attributes[x].nodeName;
		nodeValue = element.attributes[x].nodeValue;
		if( attribNameStr != "" && attribNameStr != null) {
			if( FindNoCase(attribNameStr, nodeName.toLowerCase() ) > -1 )  
				attributesStr += "nodeName: " + nodeName + " nodeValue: " + nodeValue + "\n";
		}
		else {
			attributesStr += "nodeName: " + nodeName + " nodeValue: " + nodeValue + "\n";
		}
	}
	return attributesStr;
}

