function Apply(id_ad,id_user,notlogged) {

        var url = '/jobseekers/apply.php';
	var pars = 'id_ad='+id_ad+'&id_user='+id_user;

	if (notlogged == 1) { pars = pars+'&notlogged=1'; }

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
            //alert(o);
                if (o != '') {
                        document.getElementById('apply').style.display = 'none';
                        document.getElementById('next_step').style.display = 'none';
                        document.getElementById('application_step').style.display = '';
                        document.getElementById('application_step').innerHTML = '';
			appdiv = document.createElement('div');
			appdiv.innerHTML = o;
                        document.getElementById('application_step').appendChild(appdiv);
                }
        }
        });

}

function Registration(id_ad,ref,ip) {

        var url = '/jobseekers/registration.php';
	var pars = 'id_ad='+id_ad+'&referer='+ref+'&ip='+ip;

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
            //alert(o);
                if (o != '') {
                        document.getElementById('apply').style.display = 'none';
                        document.getElementById('next_step').style.display = 'none';
                        document.getElementById('application_step').style.display = '';
                        document.getElementById('application_step').innerHTML = '';
			regdiv = document.createElement('div');
			regdiv.innerHTML = o;
                        document.getElementById('application_step').appendChild(regdiv);
                }
        }
        });

}

function checkUser(id_ad) {

        var url = '/do/checkuser.php';
	var pars = 'id_ad='+id_ad;

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
            //alert(o);
                if (o != 0) {
			if ( o == -1) {
				alert('Attenzione: ti sei gia\' candidato a questa offerta');
			} else {
				Apply(id_ad,o,'');
			}
                } else {
                        document.getElementById('apply').style.display = 'none';
                        document.getElementById('next_step').style.display = '';
                }
        }
        });

}

function access() {
        document.getElementById('next_step').style.display = 'none';
        document.getElementById('access').style.display = '';
}

function applyLogin(id_ad) {

        //document.getElementById('loading').style.display = '';
        email = document.getElementById('email').value;
        pwd = document.getElementById('password').value;
        rememberme = document.getElementById('rememberme').value;
        var url = '/jobseekers/login.php';
        var pars = 'email='+email+'&password='+pwd+'&rememberme='+rememberme;

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
             //alert(o);
                if (o) {
			checkUser(id_ad);
                } else {
                        document.getElementById('login_error').style.display = '';
                        document.getElementById('password').value = '';
                }
        }
        })

}


function moderate(id_ad) {

        var url = '/do/moderation.php';
	var pars = 'id_ad='+id_ad;

	el = document.getElementById('moderation');
        scrollXY = getPageScroll();
        x = findPosX(el) - scrollXY[0];
        y = findPosY(el) - scrollXY[1];
        $("#dialog").dialog({ zIndex: 3999 });
        //$('#dialog').dialog('option','position',[findPosX(el),findPosY(el)]);
        $('#dialog').dialog('option','position',[x,y + 20]);
        $("#dialog").dialog({ width: 550, height: 300, show: 'slide' });
        $("#dialog").dialog({ title: 'Annuncio non conforme' });
        $('#dialog').dialog('open');
        document.getElementById('dialog').innerHTML = '<iframe width="520" height="250" frameborder="0" name="moderation_dialog" src="'+url+'?'+pars+'"></iframe>';

}

function Bookmark2(id_ad) {

        var url = '/do/bookmark.php';
        var pars = 'id_ad='+id_ad;

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
             //alert(o);
                if (o) {
                        showPanel('Salva questa offerta',o,'bkmark');
                }
        }
        })

}

function Bookmark(id_ad) {

        var url = '/do/bookmark.php';
        var pars = 'id_ad='+id_ad;
        showPanel('Aggiungi offerta ai preferiti','<iframe frameborder="0" name="bmark'+id_ad+'" src="'+url+'?'+pars+'"></iframe>','bookmark'+id_ad);

}

function showPanel(title,text,el) {

                        //scrollXY = getPageScroll();
                        //x = findPosX(el) - scrollXY[0];
                        //y = findPosY(el) - scrollXY[1] + 20;
                        //$('#dialog').dialog('option','position',[findPosX(el),findPosY(el)]);
                        //$('#dialog').dialog('option','position',[x,y]);
			if (title == 'Annuncio non conforme') {
                        	$("#dialog").dialog({ width: 520, show: 'slide' });
			} else {
                        	$("#dialog").dialog({ width: 320, show: 'slide' });
			}
                        $("#dialog").dialog({ title: title });
                        $('#dialog').dialog('open');
                        document.getElementById('dialog').innerHTML = text;

}

function getPassword(el) {

        var url = '/jobseekers/password.php';

        scrollXY = getPageScroll();
        x = findPosX(el) - scrollXY[0];
        y = findPosY(el) - scrollXY[1];
        $("#dialog").dialog({ zIndex: 3999 });
        //$('#dialog').dialog('option','position',[findPosX(el),findPosY(el)]);
        $('#dialog').dialog('option','position',[x - 40,y + 20]);
        $("#dialog").dialog({ width: 340, show: 'slide' });
        $("#dialog").dialog({ title: 'Password dimenticata' });
        $('#dialog').dialog('open');
        document.getElementById('dialog').innerHTML = '<iframe frameborder="0" name="quickpassword_dialog" src="'+url+'"></iframe>';

}


