/*****************************************
 *
 *  general javascript for akaroosto.com
 *
 *  you're welcome to use this script for your own
 *  purposes, just please link back to http://akaroosto.com/
 *
 *****************************************/

function entityEncodeAll(str)
/*
 * returns a string that will have all characters encoded of the form
 * &#NNN; where NNN is the ASCII code point
 */
{
    var ret = ''; // our return, a string of XHTML &#NNN; encoded chars

    for(var i = 0; i < str.length; i++) {
	ret += '&#' + str.charCodeAt(i) + ';';
    }
    
    return ret;
}

function entityEncodeXHTML(str)
/*
 * returns a string that will have all xhtml special chars encoded
 */
{
    var ret = ''; // our return, string with '\'','"','&', '<', and '>' encoded

    for(var i = 0; i < str.length; i++) {
	switch(str.charAt(i)) {
	case "'" :
	    ret += '&#39;'; break;
	case '"' :
	    ret += '&quot;'; break;
	case '&' :
	    ret += '&amp;'; break;
	case '<' :
	    ret += '&lt;'; break;
	case '>' :
	    ret += '&gt;'; break;
	default:
	    ret += str.charAt(i);
	}
    }
    
    return ret;
}

function strCountSubstr(haystack, needle)
/*
 * returns a tally of the non-overlapping occurences of the string needle 
 * contained in the string haystack
 * ie- strCountSubstr('ratatatat', 'tat') == 2, (not 3)
 */
{
    var pos = 0; // current position in traversal of haystack
    var cnt = 0; // current count of needle in haystack & the return

    // quick out
    if (haystack == '' || needle == '') return 0;

    while(pos + needle.length <= haystack.length) {
	if( (pos = haystack.indexOf(needle, pos)) == -1) {
	    break;
	} else {
	    cnt++;
	    pos += needle.length;
	}
    }

    return cnt;
}