function checkEmailAddress(email) {
    // the following expression must be all on one line...
    var goodEmail = email.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
    if ( goodEmail ) {
        return true;
    } else {
        return false;
    }
}

function trim(string) {
    return string.replace(/(^\s+)|(\s+$)/g, "");
}

function ajaxErr(response) {
    alert(response.message);
}

function strip_tags (str, allowed_tags) {
    // Strips HTML and PHP tags from a string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strip_tags
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Tomasz Wesolowski
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';

    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };

    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }

    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);

    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }

        // Save HTML tag
        html = matches[key].toString();

        // Is tag not in allowed list? Remove from str!
        allowed = false;

        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;

            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }

        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }

    return str;
}

function change_box(obj){
    classes = $(obj).attr('class').split(' ');
    params = classes[0].split("_");
    
    show = "show";
    show = ($("#" + params[2]).is(":visible") ? "hide" : "show");
    collapse_box(params[0], params[1], params[2], show);
    $.ajax({
        url: 'ajax/block_collapse.php',
        type: "POST",
        data: {
            name: params[2],
            action: show
        },
        success: function(){
            
        },
        complete: function(){
            
        },
        dataType: 'json'
    });
    return false;
}

function collapse_box(style, button, box, action){
    if (action == "hide")
    {
        $("#" + button).attr('src', '/images/boxes/icons/more_' + style + '.png')
        $("#" + box).hide();
        $(".c #more").html("more");
    }
    else
    {
        $("#" + button).attr('src', '/images/boxes/icons/hide_' + style + '.png')
        $("#" + box).show();
        $(".c #more").html("hide");
    }
    
    
    return false;
}

function getAnchor() {
    var parts = window.location.href.split("#");
    if (parts.length < 2) {
        return;
    }
    return parts[1];
}

function deleteElement(ar, id)
{
    var temp = Array();
    for (var i = 0; i < ar.length; i++)
    {
        if (ar[i] != id) temp[temp.length] = ar[i];
    }
    return temp;
}

function checkPassword(pass)
{
    var result = '';
    if (pass.length < 6) {
        result = 'Password\'s length should be at least 6 characters.';
    } else if (pass.length > 12) {
        result = 'Password\'s maximum length is 12 characters.';
    } else if (pass.replace(/[a-zA-Z0-9]/g, '').length > 0) {
        result = 'Password cannot contain spaces or special characters.';
    }
    return result;
}

$(document).ready(function()
{   
    if($.browser.msie)
    {
        $img = $("label img");
        $img.click(function(e)
        {
            $("#" + $(this).parent().attr("for"))
            .change()
            .click();
        }); 
    }
    
    // Signin
    $('.btn-signin').click(function() { 
        $.blockUI({ 
            theme: false,
			draggable: true,
			css: { backgroundColor: '#ffffff',
			       color: '#666666',
			       padding: '30px 40px',
			       border: '10px solid #7f7f7f',
			       top:  ($(window).height() - 500) /2 + 'px', 
                   left: ($(window).width() - 400) /2 + 'px',
			       width: '300px' },
            title: 'Join',
            message: $('#signinDialogue')
        });
			$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
			$('.esc').click($.unblockUI);
			$.blockUI.defaults.css = {};
    });
    
    // Must Update Password (only if dialog present)
    $('#updatePasswordDialogue').each(function() { 
        $.blockUI({
            theme: false,
            draggable: true,
            css: { backgroundColor: '#ffffff',
                   color: '#666666',
                   padding: '30px 40px',
                   border: '10px solid #7f7f7f',
                   top:  ($(window).height() - 500) /2 + 'px', 
                   left: ($(window).width() - 400) /2 + 'px',
                   width: '300px' },
            title: 'Join',
            message: $('#updatePasswordDialogue')
        });
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
            $('.esc').click($.unblockUI);
            $.blockUI.defaults.css = {};
    });
    
});
