/*
 * Simple UI management functions
 * Requires:
 *	functions.js to be loaded
 *	jxte.js to be loaded
 * 	div_manager.js to be loaded and a div object defining for UI elements to be rendered into
 */

// Required for navigation.xsl support
function flip_nb_images (var_name, img_url, img_count)
{
	var i = 0;

	for (i = 0; i < img_count; i++)
		document[var_name + "_" + i].src = img_url;
}

function submit_captcha_form (submit_url)
{
	var bool 	= true;
	var t_val 	= '';
	var regex	= /[&<>]/;

	// Unfortunately, not all browsers support nested forms, so we just have to try and make use of the page's generic form
	// This means we have a risk of clashing object names...
	var form_obj	= document.forms[0];

	t_val = form_obj.captcha_value.value;
	if (t_val == '' || !t_val.match(/^\d+$/))
		bool = false;

	t_val = form_obj.name.value;
	if (t_val == '' || t_val.length > 128 || t_val.match(regex))
		bool = false;

	t_val = form_obj.email.value;
	if (t_val.length > 128 || t_val.match(regex))
		bool = false;

	t_val = form_obj.comment.value;
	if (t_val == '' || t_val.length > 1024 ||  t_val.match(regex))
		bool = false;

	if (!bool)
		alert ("Unable to submit comment: please check the details provided.  " +
			"Please be aware that comments are restricted to 1024 characters and that the following special characters" +
			" cannot be used:\n\t& < >\n\nIf there is still a problem submitting your comment, please send an email to the" +
			" contact address given on the website's 'Contact' page.");

	if (bool)
	{
		form_obj.method = 'post';
		form_obj.action = submit_url;
		form_obj.submit();
	}

	return bool;
}

function get_element_position (element_obj)
{
        // Adapted from http://www.quirksmode.org/js/findpos.html
        // Element positions are determined relative to the parent, so we need to recurse back up the entire tree
        // Value returned is the (x,y) co-ordinate for the top-left corner of the element
        var pos_obj     = new Object();
        pos_obj.x       = 0;
        pos_obj.y       = 0;

        if (element_obj.offsetParent)
        {
                // Note the "eo = eo.op" in the while statement - this is not a bug!
                while (true)
                {
                        pos_obj.x       += element_obj.offsetLeft;
                        pos_obj.y       += element_obj.offsetTop;

                        if (element_obj.offsetParent)
                                element_obj = element_obj.offsetParent;
                        else
                                break;
                }
        }

        return pos_obj;
}


