///////////////////////////ADMIN - LOGIN - LOGOUT/////////////////////////////

//////////////////////////////////////////////////////////////////////////////

Namespace.register("cesvi.hr");
cesvi.hr = {};
cesvi.hr.isadmin = function(){var k = uno.cookie.read('hradmin');if (k){return true;}else{return false;}};
cesvi.hr.overs_url = '../uno/services/cesvi.hr.pos.aspx?jtype=overs';
cesvi.hr.italy_url = '../uno/services/cesvi.hr.pos.aspx?jtype=italy';
cesvi.hr.details_url = '../uno/services/cesvi.hr.pos.aspx?jtype=det&pid=';
cesvi.hr.registernl_url = '../uno/services/cesvi.nl.apply.aspx';
cesvi.hr.savepos_url = '../uno/services/hr-newpos.aspx';
cesvi.hr.rempos_url = '../uno/services/hr-rempos.aspx?id=';
cesvi.hr.app1load_url = '../uno/services/hr-app1.aspx?c=0&id=';
cesvi.hr.app1click_url = '../uno/services/hr-app1.aspx?c=1&id=';
cesvi.hr.app2click_upurl = '../uno/services/hr-app2.aspx?c=1&id=';
cesvi.hr.dlapp_url = '../uno/services/hr-app2.aspx?c=100&from=';
cesvi.hr.delapp_url = '../uno/services/hr-app2.aspx?c=101&from=';
cesvi.hr.adminpar = function() { if (cesvi.hr.isadmin()) { return '&all=true'; } else { return '';} };
cesvi.hr.doexport = function () {
    var dfrom = $('hr-datefrom').value;
    var dto = $('hr-dateto').value;
    if (dfrom != '') {
        if (!uno.validate.date(dfrom)) { uno.dialog.show('Errore', 'Il campo data "da" non è corretto.', 'warning', 4); }
    } else { dfrom = '01/01/2000'; }
    if (dto != '') {
        if (!uno.validate.date(dto)) { uno.dialog.show('Errore', 'Il campo data "a" non è corretto.', 'warning', 4); }
    } else {
        var today = new Date()
        dto = today.getDate() + '/' + (today.getMonth() + 1) + '/' + (today.getFullYear() + 1);
    }
    var url = cesvi.hr.dlapp_url + encodeURIComponent(dfrom) + '&to=' + encodeURIComponent(dto);
    location.href = url;
};
cesvi.hr.dodelete = function(){
    if(uno.validate.date($('hr-datefrom').value)){
        if(uno.validate.date($('hr-dateto').value)){
            uno.xml.asyncload(cesvi.hr.delapp_url+encodeURIComponent($('hr-datefrom').value)+'&to='+encodeURIComponent($('hr-dateto').value),false,uno.xml.presentok,false);
        }else{uno.dialog.show('Errore','Il campo data "a" non è corretto.','warning',4)}
    }else{uno.dialog.show('Errore','Il campo data "da" non è corretto.','warning',4)}
};

cesvi.ljtype = null;

cesvi.hr.gotodetails = function(id){location.href='details.htm?id='+id;};
cesvi.hr.loadetails = function(){
    var id = uno.querystring('id');
    if(id){
        uno.xml.asyncload(cesvi.hr.details_url+id,null,uno.xml.presentok,null);
    }else{
        if(cesvi.hr.isadmin()){cesvi.hr.renderposnew();}
}   };
cesvi.hr.registernl = function() {
    uno.xml.asyncloadpost(cesvi.hr.registernl_url, false, uno.xml.presentok, false, 'c-jobalert-form');
    setTimeout('location.href="index.htm"', 1500);
}
cesvi.hr.detclick = function(){
    var id = uno.querystring('id');
    if(id){
        if(cesvi.hr.isadmin()){//SAVE EDIT
            uno.xml.asyncloadpost(cesvi.hr.savepos_url+'?jtype='+cesvi.ljtype,null,uno.xml.presentok,null,'c-new-hrpos-form');
        }else{//APPLY
            location.href='apply01.htm?id='+id;}
    }else{if(cesvi.hr.isadmin())//SAVE NEW
            {uno.xml.asyncloadpost(cesvi.hr.savepos_url+'?jtype='+cesvi.ljtype,null,uno.xml.presentok,null,'c-new-hrpos-form');}}
};
cesvi.hr.loadapp1=function(){
    var id = uno.querystring('id');
    if(id){uno.xml.asyncload(cesvi.hr.app1load_url+id,null,uno.xml.presentok,null);}
    else{location.href='index.htm';}};  
cesvi.hr.app1click = function(){
    var id = uno.querystring('id');
    if(id){//SAVE App1
        if(cesvi.hr.validatef1()){      
        uno.xml.asyncloadpost(cesvi.hr.app1click_url+id,null,uno.xml.presentok,null,'c-myapp1-form');}
    }else{location.href='index.htm';}
};
cesvi.hr.app2click = function() {
    var id = uno.querystring('id');
    var filen = $('userfile').value;
    if (id) {//SAVE App2
        if ((filen != null) && (filen != '')) {
            uno.xml.asyncloadframe(cesvi.hr.app2click_upurl + id, 'c-myapp2-form', false, uno.xml.presentok);
        } else {
            alert('The curriculum file field is mandatory.');
        }
    } else { location.href = 'index.htm'; }
};
cesvi.hr.validatef1 = function(){
    var tform= $('c-myapp1-form');
    var ii=tform.getElementsByTagName('input');
    for (var i = 0;i<ii.length-1;i++){
        if (ii[i].value == null){alert('All the fields are mandatory.');return false;}
        if (ii[i].value == '') { alert('All the fields are mandatory.'); return false; }
   }
   return true;
};
cesvi.hr.rempos = function(id) { if (confirm('Delete the Vacancy?')) { uno.xml.asyncload(cesvi.hr.rempos_url + id, null, uno.xml.presentok, null); } }
/////////////////////////////////////HR INDEX////////////////////////////////////
cesvi.hr.myvisw = function(){
    if($('admin_exporter').style.display=='none'){
        $('admin_exporter').style.display='';
    }else{$('admin_exporter').style.display='none';}
};
cesvi.hr.loadjobs = function(){
//    var today = new Date()
//    var yyint = today.getFullYear();
//    var mmint = today.getMonth();
//    if (mmint == 1) {yyint=yyint-1;mmint=1;}
//    else{mmint = mmint-1;}
//    $('hr-dateto').value = today.toLocaleDateString();
//    $('hr-datefrom').value = '01/'+mmint+'/'+yyint;
    var jtype = uno.querystring('jtype');
    var img = $('aux_pagina').getElementsByTagName('img')[0];
    if (cesvi.hr.isadmin()){
        var aa = document.createElement('a');
        aa.href='details.htm?jtype=overs';aa.innerHTML = '<img src="image/nuovo_oversea.gif" alt="nuovo_oversea" />';
        $('nochng2_norem').appendChild(aa);
        aa = document.createElement('a');
        aa.href='details.htm?jtype=italy';aa.innerHTML = '<img src="image/nuovo_italy.gif" alt="nuovo_italy" />';
        $('nochng2_norem').appendChild(aa);
        aa = document.createElement('a');
        aa.href="javascript:cesvi.hr.myvisw();";aa.innerHTML = '<img src="image/esporta.gif" alt="download_appliants" />';
        $('nochng2_norem').appendChild(aa);
        aa = document.createElement('a');
        aa.href='javascript:cesvi.dologoutadmin(\'../hr.cesvi.org/index.htm\');';aa.innerHTML = '<img src="image/admin_logout.gif" alt="admin_logout" />';
        $('nochng2_norem').appendChild(aa);
        //GetTar
    }else{
        var aa = document.createElement('a');
        aa.href='javascript:cesvi.gologinadmin(\'../hr.cesvi.org/index.htm\');';aa.innerHTML = '  Admin Login';
        $('nochng2_norem').appendChild(aa);
        $('nochng2_norem').style.display = 'none';
    }
    if (!jtype){img.setAttribute('src','image/job_overseas.jpg');
        $('hr_oversea_top').innerHTML='<h2>Work overseas </h2>';$('hr_italy_top').innerHTML='<h2>Work in Italy </h2>';
        $('hr_common_content').style.display='';$('hr_oversea_content').style.display='';
        $('hr_italy_content').style.display='';
        uno.xml.asyncload(cesvi.hr.overs_url + cesvi.hr.adminpar(), null, uno.xml.presentok, null);
        uno.xml.asyncload(cesvi.hr.italy_url + cesvi.hr.adminpar(), null, uno.xml.presentok, null);
    }else if(jtype=='italy'){img.setAttribute('src','image/job_italy.jpg');
        $('hr_common_content').style.display='none';$('hr_oversea_content').style.display='none';
        $('hr_italy_content').style.display='';
        $('hr_italy_top').innerHTML='<h2>Work in Italy </h2>'+$('hr_common_top').innerHTML+'<br/>';
        uno.xml.asyncload(cesvi.hr.italy_url + cesvi.hr.adminpar(), null, uno.xml.presentok, null);
    }else if(jtype=='overs'){img.setAttribute('src','image/job_overseas.jpg');
        $('hr_common_content').style.display='none';$('hr_italy_content').style.display='none';
        $('hr_oversea_content').style.display='';
        $('hr_oversea_top').innerHTML='<h2>Work overseas </h2>'+$('hr_common_top').innerHTML+'<br/>';
        uno.xml.asyncload(cesvi.hr.overs_url + cesvi.hr.adminpar(), null, uno.xml.presentok, null);
}   };
/////////////////SERVER HANDLERS////////////////////////
cesvi.hr.addjobline_overs = function(node){$('hr_oversea_content').appendChild(cesvi.hr._addjobline(node));};
cesvi.hr.addjobline_italy = function(node){$('hr_italy_content').appendChild(cesvi.hr._addjobline(node));};
cesvi.hr._addjobline = function(node){
    var ctc = '<div class="aux_riga">\n';
    var pid = uno.xml.firstchildinnerxml(node,'id');
    ctc += '<div class="r01"><a href="javascript:cesvi.hr.gotodetails(' + pid + ')">' + uno.xml.firstchildinnerxml(node,'friendly') + '</a></div>\n';
    ctc += '<div class="r02">' + uno.xml.firstchildinnerxml(node,'location') + '</div>';
    ctc += '<div class="r03">' + uno.xml.firstchildinnerxml(node,'closedate') + '';
    if (cesvi.hr.isadmin()){ctc += '<br/><a href="javascript:cesvi.hr.rempos('+pid+');">Elimina</a></div>';}
    ctc+="</div></div>";
    var ndiv = document.createElement('div');
    ndiv.className = 'riga';
    ndiv.innerHTML = ctc;
    return ndiv;};
cesvi.hr.renderposdet = function(node){
    var img = $('aux_pagina').getElementsByTagName('img')[0];
    var jtype = uno.querystring('jtype');
    cesvi.ljtype = jtype;
    if(uno.xml.firstchildinnerxml(node,'loctype')=='italy'){img.setAttribute('src','image/job_italy.jpg');}
    else{img.setAttribute('src','image/job_overseas.jpg');}
    if (cesvi.hr.isadmin()){
        ppid = document.createElement('input');
        ppid.id= 'c-new-hrpos-id';
        ppid.name= 'c-new-hrpos-id';
        ppid.type= 'hidden';
        ppid.value = uno.querystring('id');
        $('hr_details_content').appendChild(ppid);
        cesvi.hr._appendrigaeditsmall('CODE',uno.xml.nodeinnerxml(uno.xml.getchild(node,'code')),'c-new-hrpos-code');
        cesvi.hr._appendrigaeditsmall('POSITION',uno.xml.nodeinnerxml(uno.xml.getchild(node,'friendly')),'c-new-hrpos-pos');
        cesvi.hr._appendrigaeditsmall('LOCATION',uno.xml.nodeinnerxml(uno.xml.getchild(node,'location')),'c-new-hrpos-loc');
        cesvi.hr._appendrigaeditsmall('CLOSING DATE',uno.xml.nodeinnerxml(uno.xml.getchild(node,'closedate')),'c-new-hrpos-clo');
        cesvi.hr.metacount = 0;
        var metas = node.getElementsByTagName('meta');
        for (var i=0;i<metas.length;i++){
            cesvi.hr.appendmeta('hr_details_content',uno.xml.nodeinnerxml(uno.xml.getchild(metas[i],'name')),uno.xml.nodeinnerxml(uno.xml.getchild(metas[i],'value')));
        }
        var aa = document.createElement('a');
        aa.href='javascript:cesvi.hr.appendmeta(\'hr_details_content\',\'\',\'\');';
        aa.innerHTML = '<img src="image/aggiungi_meta.gif" alt="aggiungi_meta" />';
        $('nochng2_norem').appendChild(aa);
    }else{
        cesvi.hr._appendriga('CODE',uno.xml.nodeinnerxml(uno.xml.getchild(node,'code')));
        cesvi.hr._appendriga('POSITION',uno.xml.nodeinnerxml(uno.xml.getchild(node,'friendly')));
        cesvi.hr._appendriga('LOCATION',uno.xml.nodeinnerxml(uno.xml.getchild(node,'location')));
        cesvi.hr._appendriga('CLOSING DATE',uno.xml.nodeinnerxml(uno.xml.getchild(node,'closedate')));
        var metas = node.getElementsByTagName('meta');
        for (var i=0;i<metas.length;i++){
            cesvi.hr._appendriga(uno.xml.nodeinnerxml(uno.xml.getchild(metas[i],'name')),uno.xml.nodeinnerxml(uno.xml.getchild(metas[i],'value')));
        }
    }
};
cesvi.hr._appendriga = function(name,value){
    var d1 = document.createElement('div');
    d1.className = 'sx_s02';
    d1.innerHTML = name;
    var d2 = document.createElement('div');
    d2.className = 'dx_s02';
    d2.innerHTML = value;
    var rdiv = document.createElement('div');
    rdiv.className='riga';
    rdiv.appendChild(d1);
    rdiv.appendChild(d2);
    $('hr_details_content').appendChild(rdiv);};
cesvi.hr.renderposnew = function(){
    var jtype = uno.querystring('jtype');if(!jtype){jtype='overs';}
    cesvi.ljtype = jtype;
    var img = $('aux_pagina').getElementsByTagName('img')[0];
    if(jtype=='italy'){img.setAttribute('src','image/job_italy.jpg');}
    else{img.setAttribute('src','image/job_overseas.jpg');}
    cesvi.hr._appendrigaeditsmall('CODE','','c-new-hrpos-code');
    cesvi.hr._appendrigaeditsmall('POSITION','','c-new-hrpos-pos');
    cesvi.hr._appendrigaeditsmall('LOCATION','','c-new-hrpos-loc');
    var d = new Date();
    var dd = d.getDay();var mm = d.getMonth();var YY = d.getFullYear();
    mm = mm+2;if(mm==13){mm=1};
    cesvi.hr._appendrigaeditsmall('CLOSING DATE','1/'+mm+'/'+YY,'c-new-hrpos-clo');
    cesvi.hr.metacount = 0;
    var aa = document.createElement('a');
    aa.href='javascript:cesvi.hr.appendmeta(\'hr_details_content\',\'\',\'\');';
    aa.innerHTML = '<img src="image/aggiungi_meta.gif" alt="aggiungi_meta" />';
    $('nochng2_norem').appendChild(aa);};
    cesvi.hr._appendrigaeditsmall = function(name, value, id, classname) {
        if (classname) { classname = ' class="' + classname + '"'; } else { classname = ''; }
        var d1 = document.createElement('div');
        d1.className = 'sx_s02';
        d1.innerHTML = name;
        var d2 = document.createElement('div');
        d2.className = 'dx_s02';
        d2.innerHTML = '<input type="text" id="' + id + '" name="' + id + '" value="' + value + '"' + classname + '/>';
        var rdiv = document.createElement('div');
        rdiv.className = 'riga';
        rdiv.appendChild(d1);
        rdiv.appendChild(d2);
        $('hr_details_content').appendChild(rdiv);
    };
cesvi.hr.metacount = 0;
cesvi.hr.mc = function(){cesvi.hr.metacount+=1;return cesvi.hr.metacount;};
cesvi.hr.appendmeta = function(parent,name,value){
    value=uno.replaceglobal(value,'[AND]','&');
     var d1 = document.createElement('div');
    d1.className = 'sx_s02';
    d1.innerHTML = '<input type="text" id="c-new-hrmetan-'+cesvi.hr.mc()+'" name="c-new-hrmetan-'+cesvi.hr.metacount+'" value="'+name+'" />';;
    var d2 = document.createElement('div');
    d2.className = 'dx_s02';
    value = uno.replaceglobal(value, '<br/>', '\n');
    d2.innerHTML = '<textarea id="c-new-hrmetav-'+cesvi.hr.metacount+'" name="c-new-hrmetav-'+cesvi.hr.metacount+'" type="text" style="width:320px;heigth:150px" cols="40" rows="8">'+value+'</textarea>';
    var rdiv = document.createElement('div');
    rdiv.className='riga';
    rdiv.appendChild(d1);
    rdiv.appendChild(d2);
    $(parent).appendChild(rdiv);};