/*
 * -File	$Id: form.js,v 1.11 2004/06/22 21:36:31 openface Exp $
 * -License	LGPL (http://www.gnu.org/copyleft/lesser.html)
 * -Copyright	2001, The Turing Studio, Inc.
 * -Author	alex black, enigma@turingstudio.com
 * -Author	manuel holtgrewe <purestorm at teforge dot org>
 */

/*
 * This script contains required javascript used by form input nodes.
 */

// {{{ constants
var NODE_PATH_SEPARATOR = "__";
// }}}



/**
 * this is part of a workaround to using images as submit buttons, but still
 * having netscape submit the form when the user hits 'enter'.
 * it scans all keys pressed - if the key is 'enter' is submits the form.
 * right now, it can only be used on a form with an index of 0.
 * 
 * the other bit of the code is:
 * 
 * if (navigator.appName == "Netscape"){
 * document.captureEvents(Event.KEYPRESS);
 * document.onkeypress = checkInputKey;
 * }
 * 
 * @author	Alex Black
 * @param	k	the kay passed in to check
 */
function checkInputKey(k) {
  	var key = escape(k.which);
  	if ( key == '3'){
			document.forms[0].submit();
  	}
}

/**
 * This function will check all the checkboxes in a particular form. It has been
 * abstracted to be used with any form, which means that you have to pass in
 * everything:
 *
 * -ckval is the value to set the checkbox to, can be "true" or "false"
 * -form_index is the integer index of the form. typically, this is 0, but if you
 * have some other forms on your page before the checkbox form, you'll need this.
 *
 * example:
 * checkTheBoxes('true', '25', '0');
 *
 * @author	Alex Black
 * @param	ckval	the value to set the element.checked attribute to
 * @param	form_index	the integer index of the form to check all the boxes in
 */
function checkTheBoxes(ckval, form_index) {
	var form_length = document.forms[form_index].elements.length;
	if (form_length != 0) {
		for (var i = 0; i <= form_length-1; ++i) {
			if (document.forms[form_index].elements[i].type == "checkbox") {
				document.forms[form_index].elements[i].checked = ckval;
			}
		}
	}
}




/**
 * This is a workaround for the onFocus behaviour of mac browsers
 * which display a colored border around the a focused image enclosed
 * by a href tag. It's used like this:
 *
 * <a href="/url/" onFocus="Blur(this);">Url</a>
 *
 * @author	Andreas Aderhold, a.aderhold@thyrell.com
 * @param	obj	the object to blur
 */

function Blur(_obj) {
	if(_obj.blur) {
		_obj.blur();
	}
}

function setFocus(_id) {
  el = xGetElementById(_id);
  el.focus();
}

/**
	* Sets a message in the window status bar.
	*/
function WindowStatus(msg) {
    window.status=msg;
		return true;
}


/**
 * This function is used for checkboxes to allow
 * checkbox text to be used to select/deselect the
 * checkbox
 */
function SetChecked(FormName, ElementName, Index) {
    if (Index==null) {
        var target = document.forms[FormName].elements[ElementName];
    } else {
        var target = document.forms[FormName].elements[ElementName][Index];
    }
    if (target.checked==true) {
        target.checked=false;
    } else {
        target.checked=true;
    }
}

/**
 */
function RadioSetChecked(FormName, ElementName, Index) {
    if (Index==null) {
        var target = document.forms[FormName].elements[ElementName];
    } else {
        var target = document.forms[FormName].elements[ElementName][Index];
    }
    if (target.checked==false) {
        target.checked=true;
    }
}

/* 
 * This function returns an element object in an elegant
 * cross-browser fashion.
 */

function xGetElementById(sId) {
    var ele = null;
    if (document.getElementById)
        ele = document.getElementById(sId);
    else if (document.all)
        ele = document.all[sId];
    else if (document.layers)
        ele = nnGetElementById(sId);
    return ele;
}

function nnGetElementById(sId, oParent) {
    var i, layer, found=null;
    if (!oParent)
        oParent = window;
    for (i = 0; i < oParent.document.layers.length; i++) {
        layer = oParent.document.layers[i];
        if (layer.id == sId)
            return layer;
        if (layer.document.layers.length)
            found = nnGetElementById(sId,layer);
        if (found)
            return found;
    }
    return null;
}

function updateColorPicker(sForm, sInput) {
    var elemArray = new Array(3);
    elemArray[0] = document.forms[sForm].elements[sInput + '_red'];
    elemArray[1] = document.forms[sForm].elements[sInput + '_green'];
    elemArray[2] = document.forms[sForm].elements[sInput + '_blue'];

    var color = '#';

    for (var i = 0; i < 3; i++) {
        for (var j = 0; j < elemArray[i].length; j++) {
            if (elemArray[i][j].checked) {
                color = color + elemArray[i][j].value;
                break;
            } else
                if (j = elemArray[i].lenght - 1)
                    color = color + '00';
        }
    }

    document.forms[sForm].elements[sInput].value = color;
    document.getElementById(sForm + NODE_PATH_SEPARATOR + sInput + '_sample').style.background = color;
}

function recoverColorPicker(sForm, sInput) {
    var hexColor = '';
    var colors = new Array(3);
    var colorNames = new Array('red', 'green', 'blue');

    // extract the color components
    hexColor = document.forms[sForm].elements[sInput].value;
    for (var i = 0; i < 3; i++) {
        colors[i] = hexColor.substring(1 + i*2, 1 + i*2 + 2);
    }

    // check the radio box where applicable
    for (var i = 0; i < 3; i++) {
        var radioName = sInput + '_' + colorNames[i];
        var count = document.forms[sForm].elements[radioName].length;
        for (var j = 0; j < count; j++) {
            if (document.forms[sForm].elements[radioName][j].value == colors[i]) {
                document.forms[sForm].elements[radioName][j].checked = true;
                var value = document.forms[sForm].elements[radioName][j].value;
            }
        }
    }

    // update preview color
    document.getElementById(sForm + NODE_PATH_SEPARATOR + sInput + '_sample').style.background = hexColor;
}

// Add a dropdown item (s) to the dropdown dd, and selected (1,0).
function addItem(dd, s, selected, value) {
  var i = dd.length;
  if (selected)
    var t = new Option(s, s, 0, 1);
  else  
    var t = new Option(s, s, 0, 0);
  if (value != '')
    t.value = value;

  dd.options[i] = t;
}        

