// Gencon global javascript file Grant Tegtmeier 8/29/06
// $Id: gencon.js,v 1.6 2008/01/30 22:59:34 kentg Exp $
// $Source: /cvs/rr/final/gencon_files/gencon.js,v $

// Simple limit function, can be used in any text field
// cid is the ID of a count-down field ID (<span> etc.)
// use null if none.
function gcTxtCount(field, cid, limit, status) {
	if (field.value.length > limit) {
	    if (status < 150) {
		field.value = field.value.substring(0, limit);
		return true;
	    }
	}
	var cnt = document.getElementById(cid);
	if (cnt != null) {
		var inner = limit - field.value.length;
		if (inner < 0.1 * limit) {
	    inner = '<span style="color:red;">' + inner + '</span>'
				}
		cnt.innerHTML = inner;
	}
	return false;
}

// Eliminate CR, TAB and multiple blanks in field
// iff corresponding option is true
function gcTxtFilter(field, cid, limit, cr, tab, blbl, status) {
	var chk = field.value;
	if (cr)
		chk = chk.replace(/[\n\r]/g, " ");
	if (tab)
		chk = chk.replace(/\t/g, " ");
	if (blbl)
		chk = chk.replace(/ +/g, " ");
	if (chk != field.value) 
		field.value = chk;
	if (limit < 1)
		return true;
	return gcTxtCount(field, cid, limit, status);
}

// Ajax spell checking routines
// Copyright 9/6/06 by Grant Tegtmeier for Gen Con LLC

// Substitutes chosen words into spell field and
// removes choice from list.
function gcSpChange(elmid, badword, goodword, optionid) {
	textBlock = document.getElementById(elmid);
	regex = new RegExp('\\b' + badword + '\\b', 'g');
	textBlock.value = textBlock.value.replace(regex,goodword);
	option = document.getElementById(optionid);
	option.style.display = 'none';
}

var gcSpAjRequest;   // XML request object for ajax
var gcSpellId = '';  // ID of the current spelling display area

// Ajax completion: Plug received XHTML into the
// "gcSpellId" area when Ajax completes
function gcSpAjaxDone () {
	if (gcSpAjRequest.readyState == 4) {
		if (gcSpAjRequest.status == 200) {
	    if (gcSpellId != '') {
				var t = document.getElementById(gcSpellId);
				t.innerHTML = '';
				var r = gcSpAjRequest.responseText;
				t.innerHTML = r;
	    }
		}
	}
}

// Conditions request and initates an Ajax spelling session
function gcSpellDo(field, show) {
	var sh = document.getElementById(show);
	if (sh == null) {
		window.alert('SpellDo: show id="'+show+'" is not in this document.');
		return false;
	}
	var tx = document.getElementById(field);
	if (tx == null) {
		window.alert('SpellDo: field id="'+field+'" is not in this document.');
		return false;
	}
	var text = tx.value;
	if (text == null)  {
		window.alert('SpellDo: field id="'+field+'" has no .value part.');
		return false;
	}
	gcSpellId = show;
	var xurl = gcPath + 'ajaxspell.cgi?id=' + field
		+ '&show=' + show
		+ '&txt=' + escape(text);
	if (window.XMLHttpRequest) { // which support base is on user's system
		gcSpAjRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		gcSpAjRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	gcSpAjRequest.open("GET", xurl, true);
	gcSpAjRequest.onreadystatechange = gcSpAjaxDone;
	gcSpAjRequest.send(null);
	return false;
}

// Clears the identified area
function gcClear(id) {
	var it = document.getElementById(id);
	if (it.innerHTML == null) return false;
	it.innerHTML = '';
}

// Clear all radio buttons
function gcClearRadioButtons(radioname) { 
    for (i=0; i<radioname.length; i++) { 
	radioname[i].checked = 0;
    }
}
