
function $$(id) { return document.getElementById(id);}
function aggr_freq(hash) { var total=0; for (ch in hash) { total+=hash[ch]; } return total}


var lch_freq={'9':30,'a':355,'b':226,'c':480,'d':277,'e':250,'f':249,'g':130,'h':171,'i':175,'j':59,'k':35,'l':191,'m':267,'n':100,'o':125,'p':401,'q':20,'r':300,'s':580,'t':263,'u':60,'v':72,'w':186,'x':0,'y':27,'z':2};
var rch_freq={'9':30,'a':685,'b':31,'c':83,'d':51,'e':845,'f':34,'g':19,'h':235,'i':470,'j':0,'k':10,'l':183,'m':64,'n':248,'o':798,'p':93,'q':7,'r':440,'s':60,'t':157,'u':321,'v':29,'w':26,'x':75,'y':27,'z':0};
var total_lch_freq = aggr_freq(lch_freq);
var total_rch_freq = aggr_freq(rch_freq);
var	az27 = ['9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
// '9' indicates digits and other chars 
var char_size = az27.length; 


stop_words= ['fig', 'figure', 'table', 'eq', 
	'does','did',
'a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against',
	'all', 'almost', 'alone', 'along', 'already', 'also', 'although', 'always',
	'am', 'among', 'amongst', 'amoungst', 'amount', 'an', 'and', 'another',
	'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'around',
	'as', 'at', 'back', 'be', 'became', 'because', 'become', 'becomes',
	'becoming', 'been', 'before', 'beforehand', 'behind', 'being', 'below',
	'beside', 'besides', 'between', 'beyond', 'bill', 'both', 'bottom', 'but',
	'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could',
	'couldnt', 'cry', 'de', 'describe', 'detail', 'do', 'done', 'down', 'due',
	'during', 'each', 'eg', 'either', 'else', 'elsewhere',
	'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone', 'everything',
	'everywhere', 'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire',
	'first', 'for', 'former', 'formerly', 'forty', 'found',
	'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has',
	'hasnt', 'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby',
	'herein', 'hereupon', 'hers', 'herself', 'him', 'himself', 'his', 'how',
	'however', 'hundred', 'i', 'if', 'in', 'inc', 'indeed', 'interest',
	'into', 'is', 'it', 'its', 'itself', 'keep', 'last', 'latter', 'latterly',
	'least', 'less', 'ltd', 'made', 'many', 'may', 'me', 'meanwhile', 'might',
	'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must',
	'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next',
	'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now',
	'nowhere', 'of', 'off', 'often', 'on', 'once', 'only', 'onto', 'or',
	'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over',
	'own', 'part', 'per', 'perhaps', 'please', 'put', 'rather', 're', 'same',
	'see', 'seem', 'seemed', 'seeming', 'seems', 'serious', 'several', 'she',
	'should', 'show', 'side', 'since', 'sincere', 'so', 'some',
	'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere',
	'still', 'such', 'system', 'take', 'than', 'that', 'the', 'their',
	'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby',
	'therefore', 'therein', 'thereupon', 'these', 'they', 'thick', 'thin',
	'third', 'this', 'those', 'though', 'through', 'throughout', 'thru',
	'thus', 'to', 'together', 'too', 'top', 'toward', 'towards',
	'twenty', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via',
	'was', 'we', 'well', 'were', 'what', 'whatever', 'when', 'whence',
	'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein',
	'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who',
	'whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with', 'within',
	'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves'
];

stop_word_hash={}; for (var i=0;i<stop_words.length;i++) {stop_word_hash[stop_words[i]]=1}

function sortByValue(keyArray, valueMap) { return keyArray.sort(function(a,b){return valueMap[b]-valueMap[a];}); }
function hash2key(hash) {keys=[]; for (var k in hash) keys.push(k); return keys}
function compute_freq_words(text) {
	var wf = {}
	var pre_wf = {}

	// check if the pasted text is of the format "term:freq,term:freq,..."
	var tf_arr = trim(text).split(/[,;\n\r]+/);
	var is_term_freq_format = true;
	for (i=0;i<tf_arr.length;i++) {
		if (trim(tf_arr[i]).length>0 && ! /\w+\s*:\s*\d+/.test(tf_arr[i]) ) {
			is_term_freq_format = false;
			break;
		}
	}
	if (is_term_freq_format) {
		for (i=0;i<tf_arr.length;i++) {
			var tf = tf_arr[i].split(/:/);
			wf[trim(tf[0])] = trim(tf[1]);
		}
	} 
	else {
		var words = text.split(/(\d|\W)+/);
		for (i=0;i<words.length;i++) {
			w = words[i].toLowerCase();
			if (!( w in stop_word_hash) && w.length>1) {
				if (w in pre_wf) 
					pre_wf[w]++;
				else 
					pre_wf[w]=1;
			}
		}
		wf = {}
		plurals = {}
		singulars = {}
		for (w in pre_wf) {
			singular_w = singular_form(w, pre_wf);
			if (w != singular_w) {
				wf[singular_w] = pre_wf[w] + pre_wf[singular_w];
				plurals[w]=1;
				singulars[singular_w]=1;
			}
		}
		for (w in pre_wf) 
			if (! plurals[w] && ! singulars[w])
				wf[w] = pre_wf[w];
	}
	return wf; 
}

function singular_form(w,hash) {
	len = w.length;
	w2 = null;
	if (w.slice(len-3,len)=='ies') {
		w2 = w.slice(0,w.length-3)+'y';
	} else if (w.slice(len-2,len)=='es') {
		w2 = w.slice(0,w.length-2);
		if (! (w2 in hash)) 
			w2 = w.slice(0,w.length-1); // nurses : nurse; phases: phase; judges: judge
	} else if (w.slice(len-1,len)=='s') {
		w2 = w.slice(0,w.length-1);
	}
	if (w2 && (w2 in hash))
		return w2;
	else
		return w;
}

function trim(str) {
	if (!str) return '';
	str = str.replace(/^\s+/,'');
	for (var i = str.length-1; i > 0; --i ) {
		if( /\S/.test( str[i] ) ) {
			str = str.substring( 0, i+1 );
			break;
		}
	}
	return str;
}

function nl2br_js(myString){ return myString.replace( /\n/g, '<br />\n' ); }

function AE_CreateXMLHttpRequest() {
    var req = null;
    try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
	    req = null;
        }
    }
    if (!req && typeof XMLHttpRequest!='undefined') {
        req = new XMLHttpRequest();
    }
    return req;
}
function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

window.size = function()
{
	var w = 0;
	var h = 0;
	if(!window.innerWidth) //IE
	{
		if(!(document.documentElement.clientWidth == 0)) { //strict mode
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else { //quirks mode
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	else { //w3c
		w = window.innerWidth;
		h = window.innerHeight;
	}
	var w_bias=17, h_bias=17;
	return {width:(w-w_bias),height:(h-h_bias)};
}
