/* Trims a string */
function trim(s) {
	if (s == null) { return ''; }
	s = s.replace(/^\s+/, '');
	s = s.replace(/\s+$/, '');
	return s;
}

/* Simple HTML-encoding routine. */
function clinica_html_encode(s) {
    if (s == null) { return ''; }
    s = s.replace(/&/g, '&amp;');
    s = s.replace(/>/g, '&gt;');
    s = s.replace(/</g, '&lt;');
    s = s.replace(/'/g, '&#39;');
    s = s.replace(/"/g, '&quot;');
    return s;
}

/* Open a link in a new window in an XHTML Strict compliant manner */
function new_window(anchor) {
    window.open(anchor.href);
    return false;
}
/* Configure all external links to open in a new window */
function config_ext_links() {
    var anchors = document.getElementsByTagName('a');
    for ( var i=0; i < anchors.length; i++ ) {
        var anchor = anchors[i];
        var rel = anchor.getAttribute('rel');
        if ( rel && rel.match(/\bext\b/) ) {
            anchor.onclick = new Function('return new_window(this);');
        }
    }
}

/* Hides the div identified by the specified ID */
function clinica_hide(id) {
    var div = document.getElementById(id);
    if (div != null) {
        div.className = 'invisible';
    } else {
        alert('clinica_hide() did not find element [' + id + ']');
    }
}

/* Reveals the div identified by the specified ID */
function clinica_reveal(id, new_class_name) {
    var div = document.getElementById(id);
    if (div != null) {
        if (new_class_name == null) {
            new_class_name = '';
        }
        div.className = new_class_name;
    } else {
        alert('clinica_reveal() did not find element [' + id + ']');
    }
}

/* Clears the inner HTML of the div identified by the specified ID */
function removeContent(id) {
    var selectedNode = document.getElementById(id);
    if (selectedNode != null) {
        selectedNode.innerHTML = "";
    }
}

/* Replaces the inner HTML of the div identified by the specified ID */
function replaceContent(id, text) {
        var selectedNode = document.getElementById(id);
        if(selectedNode != null) {
                selectedNode.innerHTML = text;
        }
}

/* Clears the value attribute of the field identified by the specified ID */
function clearFieldValue(id) {
	if (id != null) {
		var element = document.getElementById(trim(id));
		if (element != null) {
			element.value = "";
		}
	}
}

/* Gets the value attribute of the field identified by the specified ID */
function getFieldValue(id) {
	if (id != null) {
		var element = document.getElementById(trim(id));
		if (element != null) {
			var value = element.value;
			if (value != null) {
				return value;
			} else {
				return '';
			}
		}
	}
	return '';
}

/* Disables an element, changing value to specified text. Designed for submit buttons. */
function disable(id, new_text) {
	if (id != null) {
		var element = document.getElementById(trim(id));
		if (element != null) {
			element.disabled = true;
			new_text = trim(new_text);
			if (new_text != '') {
				element.value = trim(new_text);
			}
		}
	}
}

/* Enables an element, changing value to specified text. Designed for submit buttons. */
function enable(id, new_text) {
	if (id != null) {
		var element = document.getElementById(trim(id));
		if (element != null) {
			element.disabled = false;
			new_text = trim(new_text);
			if (new_text != '') {
				element.value = trim(new_text);
			}
		}
	}
}

/* Convenience method for setting focus on a from field. */
function focusToField(id) {
	if (id != null) {
		var element = document.getElementById(id);
		if (element != null) {
			element.focus();
		}
	}
}

function add_diagnostics() {
    document.write('<input type="hidden" name="browser_appname" value="'    + clinica_html_encode(navigator.appName)     + '"/>');
    document.write('<input type="hidden" name="browser_appversion" value="' + clinica_html_encode(navigator.appVersion)  + '"/>');
    document.write('<input type="hidden" name="browser_language" value="'   + clinica_html_encode(navigator.language)    + '"/>');
    document.write('<input type="hidden" name="browser_platform" value="'   + clinica_html_encode(navigator.platform)    + '"/>');
    document.write('<input type="hidden" name="browser_user_agent" value="' + clinica_html_encode(navigator.userAgent)   + '"/>');
    document.write('<input type="hidden" name="javascript_on" value="1"/>');
}

/* Generic way to toggle checkboxes on and off for an entire form. */
function flip_checkboxes(my_form) {

    // The current state of the all checkbox tells us how to set the individual checkboxes.
    if ( !my_form.all ) {
        // The 'all' checkbox is missing. Form coded incorrectly.
        alert('Form coding error. Master checkbox must have name "all" to use flip_checkboxes().');
        return;
    }

    check = ( my_form.all.checked ? true : false );
    // Iterate over elements and set checked state.
    for (var i = 0; i < my_form.elements.length; i++) {
        if (my_form.elements[i].name != 'all') {
            my_form.elements[i].checked = check;
        }
    }
    return;
}

/* Cookie management */
function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function read_cookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function delete_cookie(name) {
    createCookie( name, "", -1 );
}
