// (c) 2008-2009 Olivier Gourment, Smarter Portal inc. http://www.smarterportal.com

function AssertException(message) { this.message = message; } // http://aymanh.com/
AssertException.prototype.toString = function () {
	return 'AssertException: ' + this.message;
}
 
function assert(exp, message) {
	if (!exp) {
		throw new AssertException(message);
	}
}

function fwload() {
	
	var name = document.getElementById("yourname")
	name.focus();
	name.onkeyup = choose;
	document.getElementById("fwform").action = "javascript:save()";
}

function _choose_frameworks(name) {

    var webframeworks = [
            'Dotnetnuke',
            'Apache Struts',
            'Ruby on Rails',
            'Spring Framework',
            'Zope3',
            'Apache Cocoon',
            'Django',
            'Helma',
            'Google Web Toolkit',
            'Zend framework',
            'OpenLaszlo',
            'TurboGears',
            'CherryPy',
            'Catalyst',
            'Pylons',
            'Symfony',
            'Tapestry',
            'WebObjects',
            'WebWork',
            'Wicket',
            'OpenACS',
            'Prado',
            'AppFuse',
            'Code Igniter',
            'Grails',
            'CakePHP',
            'Interchange',
            'JavaServer Faces',
            'JBoss Seam',
            'Solar',
            'Mason',
            'Seagull',
            'Fusebox',
            'Akelos',
            'Seaside',
            'Shale',
            'ZK',
            'WACT',
            'PHP on TRAX',
            'ThinWire',
            'RIFE',
            'Ajax.NET',
            'Camping',
            'Mach-II',
            'SmartClient',
            'Webware for Python',
            'Merb',
            'Ramaze',
            'Spyce',
            'Mentawai',
            'PhpOpenbiz',
            'Karrigell',
            'Cooee',
            'Model-Glue',
            'MonoRail',
            'Nitro',
            'Horde',
            'ColdBox',
            'ColdSpring',
            'Maypole',
            'KohanaPHP',
            'Qcodo',
            'Reasonable Server Faces',
            'Zoop',
            'Odin Assemble',
            'Agavi',
            'CSLA',
            'Aranea MVC',
            'Ajile',
            'web2py',
            'jZeno',
            'ColdFusion on Wheels',
            'phpXCore',
            'PHP for applications',
            'Porcupine',
            'Iowa',
            'Grok',
            'Quixote',
            'Jifty',
            'Newow',
            'ZNF',
            'Makumba',
            'ItsNat',
            'DIY Framework',
            'phpHtmlLib',
            'OpenXava',
            'Stripes',
            'onTap',
            'Echo2',
            'Cerise',
            'Phocoa',
            'PageKit',
            'TinyMVC',
            'IT Mill Toolkit',
            'Hamlets',
            'FUSE',
            'Simplicity',
            'Play',
            'ztemplates',
            'Lion',
            'BareBones',
            'DragonPHP',
            'LightVC'
            ];

    function getUniqueUppercaseInitials(str) {
		var words = str.toUpperCase().split(' ');
		var currchar;
		var newstr = '';
		for (var i=0 ; i < words.length ; i++)
		{
			currchar = words[i].replace(/^\s+|\s+$/g, '').charAt(0) // http://www.nicknettleton.com/
            if (newstr.indexOf(currchar) < 0) {
                newstr += currchar;
            }
		}
        return newstr;
    }

	assert( getUniqueUppercaseInitials("oli gou") == "OG",     "getUniqueUppercaseInitials." );
	assert( getUniqueUppercaseInitials("") == "",              "getUniqueUppercaseInitials.." );
	assert( getUniqueUppercaseInitials(" ") == "",             "getUniqueUppercaseInitials..." );
	assert( getUniqueUppercaseInitials("a") == "A",            "getUniqueUppercaseInitials...." );
	assert( getUniqueUppercaseInitials("oli B. gou") == "OBG", "getUniqueUppercaseInitials....." );
	assert( getUniqueUppercaseInitials(" a  b   c ") == "ABC", "getUniqueUppercaseInitials......" );

    var fw_unique_uppernames = [];
    for (var fw = 0; fw < webframeworks.length; fw++) {
		var newstr = '';
		for (var ch=0 ; ch < webframeworks[fw].length ; ch++) {
			var currchar = webframeworks[fw][ch];
			if (currchar == currchar.toUpperCase()) {
				if (newstr.indexOf(currchar) < 0) {
	                newstr += currchar;
	            }
			}
		}
		fw_unique_uppernames.push(newstr);
    }

    name = getUniqueUppercaseInitials(name);
    var fwmatches = [];
    var max = 0;
    for (fw = 0; fw < webframeworks.length; fw++) {
		var count = 0;
		// for each letter in the unique initials of the name
		for (var ch2 = 0; ch2 < name.length; ch2++) {
		    // look if there is a match in the framework unique letters
		    var match = (fw_unique_uppernames[fw].indexOf(name[ch2]) >= 0);
		    if (match) count++;
		}
		fwmatches.push(count);
		if (count > max) max = count;
    }

    return {'webframeworks':webframeworks, 'fwmatches':fwmatches, 'max':max};
}

function choose() {

    var text;
    var name = document.getElementById("yourname").value;

    if (name.length > 99)
    {
        text = 'Name is too long.';
    }
    else
    {
        if (name.toUpperCase().indexOf("<SCRIPT>") >= 0) {
            text = ''; // Name contains forbidden tag
        }
        else
        {
            var chosen = _choose_frameworks(name);
            var webframeworks = chosen.webframeworks;
            var max = chosen.max;
            var fwmatches = chosen.fwmatches;

            if (max == 0) text = 'No matches found.'
            else {
                text = '';

                for (var fw = 0; fw < webframeworks.length; fw++) {
					if (fwmatches[fw] == max) {
					    text += webframeworks[fw] + "(" + fwmatches[fw] + ") ";
					}
                }
            }
        }
    }

    document.getElementById("log").innerHTML = text;
}

function save() {
	
    var name = document.getElementById("yourname").value;
    var results = document.getElementById("log").innerHTML;
    var list = document.getElementById("fwlist");

    list.innerHTML = list.innerHTML + "<li>" + name + ": " + results + "</li>"
}
