function validate_email_address(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}

function ClearText(e,thevalue) {
    if (thevalue==e.value) e.value = "";
}

function RestoreText(e,thevalue) {
    if (e.value == "") e.value = thevalue;
}

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}

function formatTitle(title) {
    return 'NetPlanet Photo Gallery' + (title != '/' ? ' / ' + toTitleCase(title.substr(1, title.length - 2).replace(/\//g, ' / ')) : '');
}


// Custom SWFAddress and Ajax handling

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}

function appear(content, value) {
    if (typeof value == 'undefined') value = 0;
    var property = content.filters ? 'filter' : 'opacity';
    if (value == 100) {
        if (content.style.removeAttribute) {
            content.style.removeAttribute(property);
        } else {
            content.style[property] = 1;
        }
    } else {
        content.style[property] = content.filters ? 'alpha(opacity=' + value + ')' : value/100;
        setTimeout(function () {appear(content, value + 20)}, 50);
    }
}

function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var content = document.getElementById('contentPhotos');
            content.innerHTML = xhr.responseText;
            appear(content);
        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) {
    if (event.value != "/") {
        var path = event.path;
        if (path.substr(path.length - 1) != '/') {
            path += '/';
        }
        var parameters = '';
        for (var p in event.parameters) {
            parameters += '&' + p + '=' + event.parameters[p];
        }
        var xhr = getTransport();
        xhr.onreadystatechange = function() {
            updateChange(xhr);
        }
        xhr.open('get', '/datasource.html?swfaddress=' + event.path + parameters, true);
        xhr.send('');
        SWFAddress.setTitle(formatTitle(event.path));
    }
}

function copyLink() {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('Text', SWFAddress.getBaseURL() + SWFAddress.getValue());
    } else {
        alert('Unsupported browser.');
    }
}

//SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
