function urlEncode(s)
{
    var SAFE_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*\'()';
    var HEX = '0123456789ABCDEF';
    var res = '';
    for(var i = 0; i < s.length; i++)
    {
        var ch = s.charAt(i);
        if(ch == ' ')
            res += '+';
        else if(SAFE_CHARS.indexOf(ch) >= 0)
            res += ch;
        else
        {
            var charCode = ch.charCodeAt(0);
            //Unicode characters using more than 8 bits cannot be encoded
            //using standard URL encoding and will then be replaced with a space
            res += (charCode < 256) ? ('%' + HEX.charAt((charCode >> 4) & 0xF)+ HEX.charAt(charCode & 0xF)) : '+';
        }
    }
	return res;
}

function urlDecode(s)
{
    var HEX = '0123456789ABCDEFabcdef';
    var res = '';
    var i = 0;
    while(i < s.length)
    {
        var ch = s.charAt(i);
        if (ch == '+')
        {
            res += ' ';
            i++;
        }
        else if(ch == '%')
        {
            if(i < s.length - 2 && HEX.indexOf(s.charAt(i + 1)) >= 0 && HEX.indexOf(s.charAt(i + 2)) >= 0)
            {
                res += unescape(s.substr(i, 3));
                i += 3;
            }
            else
            {
                res += '%[ERROR]';
                i++;
            }
        }
        else
        {
            res += ch;
            i++;
        }
    }
	return res;
}

function serialize(array)
{
    var ret = '';
    for(var i = 0; i < array.length - 1; i++)
        ret += array[i] + '|';
    ret += array[array.length - 1];
    return ret;
}

function deserialize(s)
{
    return s.split('|');
}

function openWindow(url, name, width, height)
{
    window.open(url, name, 'resizable=0, left=' + ((screen.width - width) / 2) + ', top=0, width=' + width + ', height=' + height);
}

function openPicture(url, title, captions, width, height, length)
{
	window.open('gallery.html?url=' + urlEncode(url) + '&title=' + urlEncode(title) + '&captions=' + urlEncode(serialize(captions)) + '&length=' + length + '&width=' + width + '&height=' + height, 'gallery', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0, height=' + (height + 74) + ', width=' + (width + 24));
}

function openGallery(url, title, captionKey, width, height, length)
{
	window.open('gallery.html?url=' + urlEncode(url) + '&title=' + urlEncode(title) + '&captionKey=' + captionKey + '&length=' + length + '&width=' + width + '&height=' + height, 'gallery', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0, height=' + (height + 74) + ', width=' + (width + 24));
}

function getKey(e)
{
    return window.event ? window.event.keyCode : e.which;
}

function checkNumeric(e)
{
    var key = getKey(e);
    return (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27 || (key > 47 && key < 58));
}

function checkPhoneNumber(e)
{
    var key = getKey(e);
    return (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27 || key == 32 || key == 43 || key == 45 || (key > 47 && key < 58));
}

function trim(s)
{
    if(s.length == 0)
        return s;
    while(s.length > 0)
    {
        if(s.substr(0, 1) != " ")
            break;
        if(s.length == 1)
            return "";
        s = s.substr(1);
    }
    while(s.length > 0)
    {
        if(s.substr(s.length - 1, 1) != " ")
            break;
        s = s.substr(0, s.length - 1);
    }
    return s;
}

function isEmpty(fieldId)
{
    return (trim(document.getElementById(fieldId).value).length == 0);
}

function checkEmail(email)
{
    email = trim(email);
    if(email.length < 8)
        return false;
    var at = email.indexOf("@");
    if(at < 2)
        return false;
    var dot = email.lastIndexOf(".");
    if(dot < 0 || dot - at < 3 || dot > email.length - 3)
        return false;
    return true;
}
