var thmbMaxW = 140;
var thmbMaxH = 110;

function FitThumbnail(Image)
{
    var Dimensions = Image.getDimensions();
    if(Dimensions.height>thmbMaxH) Image.setStyle({'height':thmbMaxH});
    else if(Dimensions.width>thmbMaxW) Image.setStyle({'width':thmbMaxW});
}

function WaitFor(wait)
{
    var i=0;
    setInterval(function() { i++; }, 1);
}

function LoadImage(id, size)
{
    var Kep = new Image();
    Kep.src = 'image.php?id='+id+'&size='+size;

    $(id).onload = null;
    $(id).src = Kep.src;
    

}


function WindowCreate( mod, args )
{
    new Ajax.Request('factory.php', { method: 'get', parameters: {
                                    'mod': mod,
                                    'args': args },
                                    onSuccess: function(transport) {
                                       $('body').insert(transport.responseText);
                                       $('body').setStyle({
                                           'overflow': 'hidden'});

                                       Animate('AjaxWindow');
                                       }
                                    });
                                    
}

function WindowUpdate(Element, url, args)
{
    new Ajax.Updater(Element, url, { method: 'post', parameters: args });
}

function AjaxForm(Name)
{
    return Name.serialize(true);
}

function Animate(Element)
{
    var Dimensions = $(Element).getDimensions();
    if(Dimensions.width<100) Dimensions.width = document.getElementById(Element).offsetWidth;
    
    var maxW = 800;
    var maxH = 500;

    if(Dimensions.width>maxW || Dimensions.width<100)
    {
        $(Element).setStyle({'width':maxW});
        Dimensions.width=maxW;

    }
    else $(Element).setStyle({'width':Dimensions.width});

    if(Dimensions.height>maxH || Dimensions.height<100)
    {
        $(Element).setStyle({'height':maxH});
        Dimensions.height=maxH;

    }
    else $(Element).setStyle({'height':Dimensions.height});

    var scrTop = document.body.scrollTop;

    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }

        if (navigator.appName.indexOf("Microsoft")!=-1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }

    winW = Math.round(winW/2);
    winH = Math.round(winH/2);

    var wIndex = winW-Math.round(Dimensions.width/2);
    var hIndex = winH-Math.round(Dimensions.height/2);

    $('AjaxWindowContainer').setStyle({ 'top': hIndex+scrTop, 'left':wIndex });
}

function WindowClose()
{
    $('AjaxWindowContainer').remove();
    $('AjaxBackground').remove();
    $('body').setStyle({ 'overflow': 'auto' });
}

function PostForm(form, response) {
    form = $(form);
    form.request( { method: 'post', onSuccess: function(transport) { $(response).update(transport.responseText); form.reset();}});
}

function ToggleDisabled(Type)
{
    if($(Type+'name').disabled) {
        $(Type+'name').enable();
        $(Type+'zip').enable();
        $(Type+'city').enable();
        $(Type+'street').enable();
        $(Type+'house').enable();
    } else {
        $(Type+'name').disable();
        $(Type+'zip').disable();
        $(Type+'city').disable();
        $(Type+'street').disable();
        $(Type+'house').disable();
    }
}