/* google maps related functions */

function unInitMap()
{
  if (typeof GUnload == 'function') GUnload();
}



/* editor related functions */


var beforetxt = '';
var cureditor = 0;
var cureditlink = 0;
var editinput = 0;
var editorPopup = 0;


function chkAddr()
{
  $('ajaxloader1').className = 'ajaxloader';
  var req = new Request.JSON({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=ChkAddr&adr='+$('locaddr').value.trim()+'&city='+$('loccity').value.trim()+'&country='+$('loccountry').value.trim(),
    onComplete: function(v){
      if (v.error=='')
      {       
       $('locmap').setStyle('display','block');
       $('locsubmbtn').setStyle('display','block');
       map2 = new GMap2(document.getElementById('locmap'));
       map2.addControl(new GSmallMapControl());
       map2.addOverlay(new GMarker( new GLatLng(v.lat,v.lng) ));
       var bounds = new GLatLngBounds();
       bounds.extend(new GLatLng(v.south, v.west));
       bounds.extend(new GLatLng(v.north, v.east));
       map2.setCenter(bounds.getCenter(), map2.getBoundsZoomLevel(bounds));       
      }
      else
      {
       $('locmap').set('html','<h1>Adres niet herkend</h1>');
       $('locmap').setStyle('display','block');
      }   
    }
  }).send();
}


function popupEditor( s, e, align, w )
{
  c = $(s.id).getCoordinates();
  var req = new Request.HTML({
  	  url:'scripts/jobcorner.lib.php',
  	  evalScripts: true,
  		data: 'action=GetEditor&c='+e+'&l='+c.left+'&t='+c.top,
  		method:'post',
  		onComplete: function(t,e,h,j){
  		  cleanupEditor();
        editorPopup = new Element('div').setProperty('id', 'tempeditor').injectInside(document.body);      
        editorPopup.setStyle('position','absolute');
        editorPopup.set('html',h);
        //editorPopup.setProperty('class','microform');
        if (w) editorPopup.setStyle('width', w );      
        editorPopup.addClass('microform');
        w = editorPopup.getStyle('width').replace('px','');
        if (!align) align='right';
        if (align=='right') z = (Number(c.left)-Number(w)+Number(c.width));
        else z = (c.left-20);        
        editorPopup.setStyle('left', z );
        editorPopup.setStyle('top',c.top-12);
        editorPopup.setStyle('display','block');
      }}).send();
}



function cleanupEditor()
{
  if (editorPopup!=0) editorPopup.dispose();
  editorPopup = 0;
}



function enbl(t)
{
  $a = t.getParent().getParent().getElements('a');
  $each($a,function(el){
    el.removeClass('current');
  })
  $(t).addClass('current');
}



function MakePostString( obj, post )
{    
  for (var i=0; i < obj.elements.length; i++){
    theNode = obj.elements[i];
    switch(theNode.nodeName.toLowerCase()){
      case "input":
      case "select":      
      case "textarea":
      case "checkbox":
        switch(theNode.type.toLowerCase()){
          case "radio":
          case "checkbox":
            if (theNode.checked) post += (theNode.id+"="+encodeURI(theNode.value)+ "&");             
            break;
          default:
            if (theNode.id.length>0) post += (theNode.id+"="+encodeURI(theNode.value)+ "&");
            break;
        }        
        break;
  }}
  return(post);
}   


function editzone( link, divid, act )
{
  if ($(divid).hasClass('editor'))
  {
    var v = '';
    $(link).set('text','busy...');
    $a = $(divid).getElements('input,select');
    $each( $a, function(el) {
			el.disabled=1;
			switch(el.type){
			  case 'radio':
			  case 'checkbox':
			    if (el.checked) v += el.id + '=' + encodeURI(el.value) + '&'; 
			    break;
			 default:
			   if (el.id.length>0) v += el.id + '=' + encodeURI(el.value) + '&'; 
			   break; 
		  }  
		});
		
    var req = new Request.HTML({
  	  url:'scripts/jobcorner.lib.php',
  		data: 'action='+act+'&attr='+divid+'&'+v,	
  		method:'post',
  		urlEncoded: true,
  		noCache: true,
  		onComplete: function(t,e,h,j){
        $(link).set('text','edit');
        $(divid).removeClass('editor');
      }}).send();      
  }
  else
  {
    $(link).set('text','save');
    $(divid).addClass('editor');
    $(divid).getElements('input,select').removeProperty('disabled');
  }
}



function editBox( link, divid, act )
{
  el = $(divid);
  if (el.hasClass('editor'))
  {
    $(link).set('text','busy...');
    el.set('readonly',true);
    var req = new Request.HTML({
  	  url:'scripts/jobcorner.lib.php',
  		data: 'action='+act+'&attr='+el.id+'&content='+encodeURIComponent(el.get('value').trim()),		
  		method:'post',
  		urlEncoded: true,
  		noCache: true,
  		onComplete: function(t,e,h,j){
        $(link).set('text','edit');
        el.removeClass('editor');
      }}).send();
  }
  else
  {
    $(link).set('text','save');
    beforetxt = el.get('html').trim();
    el.addClass('editor');
    el.set('readonly',false);
  }
}



function updatePerson(p,v)
{
  if (editorPopup) editorPopup.setStyle('display','none');
  var req = new Request.HTML({
    url:'scripts/jobcorner.lib.php',
  	data: 'action=UpdatePerson&attr='+p+'&content='+v,	
  	method:'post',
  	onComplete: function(t,e,h,j){
  	  if ($(p+'data')) $(p+'data').set('html',h);
      cleanupEditor();
    }}).send();
}



function updateJob(p,v)
{
  if (editorPopup) editorPopup.setStyle('display','none');
  if ( (p=='benefits') || (p=='facilities1') || (p=='facilities2') ){
    v=0;
    $$('input').each(function(el){ if (el.id.substr(0,4)=='icn_') if (el.checked) v += el.value.toInt(); });
  }
  else if (p=='publisheditems'){
    v=0;
    $$('input').each(function(el){ if (el.id.substr(0,3)=='cb_') if (el.checked) v += el.value.toInt(); });
  }
  else if (p=='location'){
    $('locmap').set('html','');
  }
  var req = new Request.HTML({
    url:'scripts/jobcorner.lib.php',
  	data: 'action=UpdateJob&attr='+p+'&content='+v,	
  	method:'post',
  	onComplete: function(t,e,h,j){
  	  if (p!='publisheditems'){
        $(p+'data').set('html',h);
        cleanupEditor();
      }
    }
  }).send();
}



function updateCandProf(p,v1,v2)
{
  if (editorPopup) editorPopup.setStyle('display','none');
  var req = new Request.HTML({
    url:'scripts/jobcorner.lib.php',
  	data: 'action=UpdateJob&attr='+p+'&exp='+encodeURIComponent(v2)+'&edulvl='+encodeURIComponent(v1),	
  	method:'post',
  	onComplete: function(t,e,h,j){
      $(p+'data').set('html',h);
      cleanupEditor();
    }}).send();
}




function updateAnonymousState(b)
{
  var req = new Request.HTML({
    url:'scripts/jobcorner.lib.php',
  	data: 'action=UpdateJob&attr=anonymous&content='+b,		
  	method:'post'
  }).send();
}
			
		
function saveJobTemplate()
{
  x = $('SaveJobTemplate').innerHTML;
  $('SaveJobTemplate').set('html','one moment...');
  var req = new Request.HTML({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=SaveJobTemplate&name='+$('JobTemplateName').get('value').trim(),
    onComplete: function(t,e,h,j){
      $('SaveJobTemplate').set('html',x);
      $('LoadFromTemplate').set('html',h);
    }
  }).send();
}



function saveJob(el)
{
  var tx = el.innerHTML;
  $(el).set('html','one moment...');
  var req = new Request.HTML({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=SaveJob',		
    onComplete: function(t,e,h,j){
      if (h=='ok')
      {
        $(el).set('html','save completed');
        window.location="/myjobs";
      }
      else 
      {
        $(el).set('html',tx);
        c = $(el).getCoordinates();
        editorPopup = new Element('div').setProperty('id', 'tempeditor').injectInside(document.body);      
        editorPopup.setStyle('position','absolute');
        editorPopup.set('html',h);
        editorPopup.setProperty('id','saveJobErrors');
        editorPopup.addClass('microform');
        w = editorPopup.getStyle('width').replace('px','');
        z = (Number(c.left)-Number(w)+Number(c.width));
        editorPopup.setStyle('left', z );
        editorPopup.setStyle('padding-left', '30px' );
        editorPopup.setStyle('background', 'url(../images/error.gif) no-repeat;' );
        editorPopup.setStyle('top',c.top-12);
        editorPopup.setStyle('display','block');
      }
    }
  }).send();
}



function toggleJob(e,i)
{
  $(e).set('html','one moment...');
  var req = new Request.HTML({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=ToggleJob&jobid='+i,		
    update: $('jobstable')
  }).send();
}



function delJob(e,i)
{
  $(e).set('html','one moment...');
  var req = new Request.HTML({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=DelJob&jobid='+i,		
    update: $('jobstable')
  }).send();
}



function jobHistory(e,i)
{
  $(e).set('html','one moment...');
  var req = new Request.HTML({
    method: 'post',
    url:'scripts/jobcorner.lib.php',
    data: 'action=JobHistory&jobid='+i,		
    update: $('jobstable')
  }).send();
}



function locMap2(lng,lat)
{
  //alert('ok');
  //if (GBrowserIsCompatible()) { 
    var map2 = new GMap2(document.getElementById('locmap'));
    map2.setCenter(new GLatLng(0,0), 13);
  //}
  alert('boe');
}



function updateCredits()
{
  var reqUpdateCredits = new Request.HTML({
    url: 'scripts/jobcorner.lib.php',
    method: 'post',
    data: 'action=UpdateCredits',
    onComplete:function(t,e,h,j){
      if ($('spnCredits'))
        $('spnCredits').innerHTML=h;
    }
  }).send();
}



/* mailbox related functions */


function showMailBox(s)
{
  $('mailbox').set('html','&nbsp;');
  var req = new Request.HTML({
    method: 'post',
    url: 'scripts/content.corner.communicate.php',
    data: 'mailbox='+s,
    update: $('mailbox')
  }).send();
}



function logonFirstMsg()
{
  TB_show('', '#TB_inline?height=200&width=350&inlineId=LogonFirstDiv', false);
  return(false);
}



function notAvailableYet()
{
  TB_show('', '#TB_inline?height=200&width=350&inlineId=NotAvailableYetDiv', false);
  return(false);
}



function updateSearch(attribute,value)
{
  $('updSearchAttribute').value=attribute;
  $('updSearchValue').value=value;
  $('updSearchForm').submit();
}



function showMicroEditor(f)
{
  c = $(f.id).getCoordinates();
  $(f.id+'Form').setStyle('left',c.left-$(f.id+'Form').width );
  $(f.id+'Form').setStyle('top',c.top-c.height-9);
  $(f.id+'Form').style.display = 'block';  
}



function cleanCities(s)
{
  var elSel = $('city');
  for (i = elSel.length - 1; i>=0; i--)
    elSel.remove(i);
  var elOptNew = document.createElement('option');
  elOptNew.text = s;
  elOptNew.value = 'none';
  var elOptOld = elSel.options[0];
  try {
    elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew, 0); // IE only
  }
  elSel.selectedIndex = 0;
}





function storeSearchParams( type )
{
  $('ajaxloader1').className = 'ajaxloader';
  var reqGetCountries = new Request.HTML({
    url: 'scripts/jobcorner.lib.php',
    method: 'post',
    data: 'action=SaveSearchParams&queryname='+$('queryname').value,
    onComplete:function(t,e,h,j){
      $('SaveSearchForm').innerHTML=h;
      $('ajaxloader1').className = '';
    }
  }).send();
}





function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse)
{
  var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y + 60))
  var slider = new Slider(scrollbar, handle, {	
    steps: steps,
	mode: (horizontal?'horizontal':'vertical'),
	onChange: function(step){
	  var x = (horizontal?step:20);
	  var y = (horizontal?20:step);
	  content.scrollTo(x,y);
	}
  }).set(0);
  if( !(ignoreMouse) ){
    $$(content, scrollbar).addEvent('mousewheel', function(e){	
	  e = new Event(e).stop();
	  var step = slider.step - e.wheel * 30;	
	  slider.set(step);					
	});
  }
  $(document.body).addEvent('mouseleave',function(){
    slider.drag.stop()
  });		
}



function getSubFunctions()
{
  /*if ($('functionl').value=='')
  {
    var elSel = $('subfunctionl');
    for (i = elSel.length - 1; i>=0; i--)
      elSel.remove(i);
    var elOptNew = document.createElement('option');
    elOptNew.text = '<?php echo addslashes($guistrings['gui_selectfunction']); ?>';
    elOptNew.value = 'none';
    var elOptOld = elSel.options[0];
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, 0); // IE only
    }
    elSel.selectedIndex = 0;
  }
  else
  {*/
  var req = new Request.HTML({
      url: 'scripts/jobcorner.lib.php',
      method: 'post',
      data: 'action=GetSubFunctions&function='+$('functionl').value,
      onComplete:function(t,e,h,j){
        $('divSubFunction').innerHTML=h;
      }
  }).send();
}





function getSubSectors()
{
  if ($('sector').value=='none')
  {
    cleanSubSectors();
  }
  else
  {
    $('ajaxloader1').className = 'ajaxloader';
    var req = new Request.HTML({
      url: 'scripts/jobcorner.lib.php',
      method: 'post',
      data: 'action=GetSubSectors&function='+$('sectorl').value,
      onComplete:function(t,e,h,j){
        $('divSubSector').innerHTML=h;
        $('ajaxloader1').className = '';
      }
    }).send();
  }
}

