// JavaScript Document

function makeEditable(div, urlQuery)
{
	new Ajax.InPlaceEditor(div, file + urlQuery, 
						   		{
									externalControl: 'editResidence',
									formClassName: 'formEdit',
									highlightcolor: '#eeeeee',
									hoverClassName: 'overEdit',
									clickToEditText: 'Редактирай',
									rows:3
								}
						   );	
}

function editProfileSel(urlQuery)
{
	new Ajax.Request( file + urlQuery );
}

function subscribeNewsletter(chk, mail, username, id)
{
    if(chk.checked)
    {
        var subscribe = 1; 
    }
    else
    {
        var subscribe = 0;
    }
    
    new Ajax.Request( file + '?func=subscribeNewsletter&id=' + id + '&mail=' + mail + '&username=' + username + '&subscribe=' + subscribe, 
                     { 
                        onComplete: function (result) { s_alert(result.responseText); }
                     });
}

function inPlaceCollectionEditor(div, urlQuery, collection)
{
	new Ajax.InPlaceCollectionEditor(div, file + urlQuery, 
									 	{
  											collection: collection
										}
									);
}

function ajaxFileUpload(div, urlQuery)
{
	document.write('<form action="' + file + urlQuery + '" method="post" onsubmit="if($F(\'pic\')==\'\') return false; else return AIM.submit(this, {\'onStart\' : function() {}, \'onComplete\' : function(response){ $(\'' + div + '\').src = response; } } );" enctype="multipart/form-data">');
    document.write('<div><input type="file" id="pic" name="pic" />&nbsp;<input type="submit" value="upload" /></div>');
    document.write('</form>');	
}

/**
*
* AJAX IFRAME METHOD (AIM)
* http://www.webtoolkit.info/
*
**/

AIM = {
	frm : null,
    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
		AIM.frm = f;
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete((d.body.innerHTML.replace(/<pre>/, "")).replace(/<\/pre>/, ""));
			AIM.frm.reset();
        }
    }

}

