// this is needed to decode unicode escaped characters
// passed via JSON
var NEED_JOBTITLE=1;

function decodeUTF(utftext) {
  var string = "";
  var i = 0;
  var c = c1 = c2 = 0;

  while ( i < utftext.length ) {

    c = utftext.charCodeAt(i);

    if (c < 128) {
      string += String.fromCharCode(c);
      i++;
    }
    else if((c > 191) && (c < 224)) {
      c2 = utftext.charCodeAt(i+1);
      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
      i += 2;
    }
    else {
      c2 = utftext.charCodeAt(i+1);
      c3 = utftext.charCodeAt(i+2);
      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }

  }

  return string;
}


function setSelected(sel,chosen) {
  for(i=0;i<sel.options.length;i++) {
    if(sel.options[i].value==chosen) {
      sel.selectedIndex=i;
    }
  }
}



// reinit a select 
function initSelect(sel,txtList,valList) {
  if(!sel){
    return;
  }
  sel.options.length=0; // zero existing
  if(txtList.length!=valList.length) {
    alert('initSelect with different length value and text lists:'+sel.name);
    return;
  }
  for(i=0;i<txtList.length;i++) {
    sel.options[i] = new Option(txtList[i],valList[i]);
  }  
  if(txtList.length==1) {
    sel.selectedIndex=0;
  }
}



function getXMLHTTP(){
  var A=null;
  try{
    A=new ActiveXObject("Msxml2.XMLHTTP")
      }catch(e){
    try{
      A=new ActiveXObject("Microsoft.XMLHTTP")
	} catch(oc){
      A=null
	}
  }
  if(!A && typeof XMLHttpRequest != "undefined") {
    A=new XMLHttpRequest()
      }
  return A
    }


//
// note you can pass a null xmlRequest and it will fill it in for you
// 
function makeRemoteRequest(url,xmlRequest,sendData){
  if(xmlRequest&&xmlRequest.readyState!=0){
    xmlRequest.abort(); // kill any existing request !
  }
  xmlRequest=getXMLHTTP();
  if(xmlRequest){
    //    alert('requesting:'+url);
    if(sendData!=null) {
      xmlRequest.open("POST",url,true);
    }else {
      xmlRequest.open("GET",url,true);
    }
    // Note that this function will ONLY be called when we get a complete
    // response back
    xmlRequest.onreadystatechange=function() {
      if(xmlRequest.readyState==4&&xmlRequest.responseText) {
        try{
	  eval(xmlRequest.responseText);
        }catch(e) {
	  alert("Got error:"+e+" when trying to evaluate:"+xmlRequest.responseText);
        }
      }
    };


    // DON'T TRY TO TALK WHEN WE'RE LOCAL...
    // Comment out when running from a local file...
    if(typeof xmlRequest.setRequestHeader != 'undefined') {
      xmlRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    }
    xmlRequest.send(sendData);
  }
}



// strip CR from string...
function stripCRFromString(va){
  for(var f=0,oa="",zb="\n\r"; f<va.length; f++) {
    if (zb.indexOf(va.charAt(f))==-1) {
      oa+=va.charAt(f);
    } else {
      oa+=" ";
    }
  }
  return oa
    }

// This is a result caching mechanism...
function cacheResults(is,cs,ds){
  _resultCache[is]=new Array(cs,ds)
    }


// Go read about encodeURIComponent here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthencodeuricomponent.asp
// Basically converts a string to a valid uri... (spaces become %20, etc, etc..)
function escapeURI(a){
  if(encodeURIComponent) {
    return encodeURIComponent(a);
  }
  if(escape) {
    return escape(a)
      }
}

function hide_div(dname) {
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(dname).style.visibility = 'hidden';
  }
}



function setSpan(id,value) {
  var n=document.getElementById(id);
  
  while(n.childNodes.length>0) {
    n.removeChild(n.childNodes[0]);
  }
  n.innerHTML=value;
}


function addOnLoadFunction(f){

  // incredibly funky onload add-event scripting, for all browsers

  if(typeof window.addEventListener != 'undefined') 
    {
      //.. gecko, safari, konqueror and standard
      window.addEventListener('load', f, false);
    }
  else if(typeof document.addEventListener != 'undefined')
    {
      //.. opera 7
      document.addEventListener('load', f, false);
    }
  else if(typeof window.attachEvent != 'undefined')
   {
     //.. win/ie
     window.attachEvent('onload', f);
   }

//** remove this condition to degrade older browsers
  else
    {
      //.. mac/ie5 and anything else that gets this far
      
      //if there's an existing onload function
      if(typeof window.onload == 'function')
       {
	 //store it
	 var existing = onload;

	 //add new onload handler
	 window.onload = function()
	   {
	     //call existing onload function
	     existing();

	     //call adsense_init onload function
	     f();
	   };
       }
     else
       {
	 //setup onload function
	 window.onload = f;
       }
   }
}



