String.prototype.getPrefix = function() { return this.substr(0, this.indexOf('_'));};
String.prototype.getSuffix = function() { return this.substr(this.lastIndexOf('_')+1);};
//String.prototype.wordCount = function () {return this.split(/\b[\s,\.-:;]*/).length;};
String.prototype.wordCount = function () {
    return this.replace(/'/g, '').
        replace(/^\s+/g, '').
        replace(/^\W+/g, '').
        replace(/\s+$/g, '').
        replace(/\W$/g, '').
        split(/\b[\s'",\.-:;\?\!]*/).length;
};
function limit_length(field, max_length) {
    Event.observe($(field), 'keypress', function(e) {
        key = e.which || e.keyCode;
        allowed_keys = [
            Event.KEY_BACKSPACE, Event.KEY_TAB, Event.KEY_ESC, Event.KEY_LEFT,
            Event.KEY_UP, Event.KEY_RIGHT, Event.KEY_DOWN, Event.KEY_DELETE
        ];
        if($F(field).wordCount()>=max_length && !allowed_keys.include(key)) {
            Event.stop(e)
        }
    });
}
//Simple static object to keep data returned from ajax requests
//The idea is to minimize ajax requests to server(and also make page faster)
var Cache = {
    contents: {},
    max_size: 15, //Shield for memory leaks
    set: function(key, value, overwrite) {
        overwrite = overwrite || false;
        if(!overwrite && Cache.contains(key)) {
            throw "Cannot overwrite value for " + key;
        }
        if(Cache.contents.length==Cache.max_size) {
            throw "Cache overflow";
        }
        this.contents[key] = value;
    },

    contains: function(key) {
        return typeof Cache.contents[key] != "undefined";
    },
    get: function(key) {
        if(!Cache.contains(key)) {
            throw "Unknow Cache key " + key;
        }
        return Cache.contents[key];
    },
    update: function(key, value) {
        if(!Cache.contains(key)) {
            throw "Cannot update " + key + ": key doesnt exists.";
        }
        Cache.set(key, value, 1);
    }
}
function reload_towns(select) {
    county = $F(select);
    if(Cache.contains(county)) {
        $('towns').update(Cache.get(county));
        return;
    }
    new Ajax.Request('/sources/ajax.php', {
        method: "GET",
        parameters: $H({
            "do": "get_towns_by_county",
            "county": county
        }).toQueryString(),
        onComplete: function(r) {
            response = r.responseText;
            Cache.set(county, response);
            $('towns').update(response);
        }
    });
}

function swap_img(link) {
    old_big = $('big_img').src;
    $('big_img').src = $(link).down('img').src.replace('/thumbs/', '/big/');
    $(link).down('img').src = old_big.replace('/big/', '/thumbs/');
}
function reload_spam_trap(img) {
    img = $(img);
    img.src = '/etc/verification/image.php?' + Math.random();
    return false;
}

function reload_areas(select) {
    country_id = $F(select);
    new Ajax.Request('/inc/ajax.php', {
        parameters: $H({
            'get': 'areas',
            'parent': country_id
        }).toQueryString(),
        'asynchronous': false, //This should complete before starting next request.a..
        onComplete: function(response) {
            //IE cannot udpate select box ... 
            if(navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) {
                $('select_areas').update(response.responseText);
            } else {
                $('select_areas').outerHTML = '<select id="select_areas" onchange="reload_counties(this)" class="field">' + 
                                                response.responseText + '</select>';
            }
        }
    });
}
function reload_counties(select) {
    area_id = $F(select);
    new Ajax.Request('/inc/ajax.php', {
        parameters: $H({
            'get': 'counties',
            'parent': area_id
        }).toQueryString(),
        onComplete: function(response) {
            if(navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) {
                $('select_counties').update(response.responseText);
            } else { 
               $('select_counties').outerHTML = '<select id="select_counties" class="field" name="county">' + 
                                                response.responseText + '</select>';
            }
        }
    });
}
function syncsubmitbtn(amount) {
    if(amount) {
        $('paybtn').show();
        $('activatebtn').hide();
    } else {
        $('paybtn').hide();
        $('activatebtn').show();
    }
}
function choose_amount(amount, id) {
    $('amount').value = amount;
    $('returnurl').value = $('returnurl').value.replace(/method=\w.*?/, 'method=' + id);
    //alert($('returnurl').value);
}