// Email validation
function loadedIsValidEmail(elem, id_to_apply_error_class){

    var my_value = getElementValue(elem);

    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (!emailFilter.test(my_value) || illegalChars.test(my_value)) {
        var my_class = document.getElementById(id_to_apply_error_class).className;
        document.getElementById(id_to_apply_error_class).className = my_class.replace(/error/i,'');
        document.getElementById(id_to_apply_error_class).className += ' error';
    }

}

// Function to trigger onblur on tab out
function loadedTabBlur(e,elem)
{
    if (window.event){
        keynum = e.keyCode
    } else if (e.which) {
        keynum = e.which
    }

    if (keynum == 9 && elem.onblur){
        elem.onblur();
    }
}

// Element showing/hiding

function toggleElement(element_id)
{
    var element = document.getElementById(element_id);
    if (element) {
        if (element.style.display == "") {
            element.style.display = "none";
        } else {
            element.style.display = "";
        }
    }
}

function showElement(element_id,type)
{

    if (type == '' || type == undefined){
        type = 'block';
    }

    var element = document.getElementById(element_id);
    if (element) {
        element.style.display = type;
    }
}

function hideElement(element_id)
{
    var element = document.getElementById(element_id);
    if (element) {
        element.style.display = "none";
    }
}

function radioToggle(elem,show,id){

    if (elem.checked == true){
        if (show == true){
            showElement(id);
        } else {
            hideElement(id);
        }
    } else {
        if (show == true){
            hideElement(id);
        } else {
            showElement(id);
        }
    }

}

// Check if a value is numeric
function is_numeric(value)
{
   if ((isNaN(value)) || (value.length == 0))
      return false;
   else
      return true;
}

// Get value of any form element

function getElementValue(elem)
{

    if (elem.tagName == 'SELECT'){
        return elem.options[elem.selectedIndex].value;
    } else if (elem.tagName == 'TEXTAREA'){
        return elem.innerHTML;
    } else if (elem.tagName == 'INPUT'){
        var my_type = elem.getAttribute('type');
        if (my_type == 'text'){
            return elem.value;
        } else if (my_type == 'radio' || my_type == 'checkbox'){
            if (elem.checked == true){
                return 1;
            } else {
                return "";
            }
        }
    }

}
// Check if form element is empty or has a value

function checkIfEmpty(elem, id_to_apply_error_class)
{

    var my_value = getElementValue(elem);

    if (my_value == "") {
        var my_class = document.getElementById(id_to_apply_error_class).className;
        document.getElementById(id_to_apply_error_class).className = my_class.replace(/error/i,'');
        document.getElementById(id_to_apply_error_class).className += ' error';
    }
}

function checkIfValue(elem, id_to_remove_error_class)
{

    var my_value = getElementValue(elem);

    if (my_value != "") {
        var my_class = document.getElementById(id_to_remove_error_class).className;
        document.getElementById(id_to_remove_error_class).style.display = '';
        document.getElementById(id_to_remove_error_class).className = my_class.replace(/error/i,'');
    }
}

// in_array prototype

Array.prototype.in_array = function(search_term) {
    var i = this.length;
    if (i > 0) {
        do {
            if (this[i] === search_term) {
                return true;
            }
        } while (i--);
    }
    return false;
}


// Add event listeners for IE

function addEvent(obj,evt,fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evt,fn,false);
    } else if (obj.attachEvent) {
        obj.attachEvent('on'+evt,fn);
    }
}

function alignMapLabels (map_div) {
    var lis = document.getElementById(map_div).childNodes;
    for (var n=0; n < lis.length; n++){
        if (lis[n].className == "right"){
            lis[n].style.left = (lis[n].offsetLeft - (lis[n].offsetWidth - 10)) + "px";
        }
    }
}


