// JavaScript Document
function $get(el) {

	var obj = document.getElementById(el);
	if (obj) {
		return obj;		
	} else {
		return null;
	}
} 
function CreateBookmarkLink(t,u) {	
	var title = t; 	
	var url = u;
	
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite(url, title); }
	else if (window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function initJobList() {
	var con = $get('content');
	var list = con.getElementsByTagName('dl');
	var links;
	var labels;
	var parentAdd;
	var parentRemove;
	var labelAnchor;
	var labelText;
	var addClass;
	var iLength;
	
	for (i=0;i<=(list.length-1);i++) {
		if (list[i].className=='results') {
			links = list[i].getElementsByTagName('a');
			
			for (j=0;j<=(links.length-1);j++) {
				if (links[j].className=='title') {
					$(links[j].id).addEvent('mouseover', function(){
						var parent = this.parentNode;
						var theId = parent.id;
						theId = theId.replace('row-title','');
						var dd = $get('row-definition' + theId);
						var dt = $get('row-title' + theId);
						
						dt.className = 'on';
						dd.className = 'on';
								   });
					$(links[j].id).addEvent('mouseout', function() {
						var parent = this.parentNode;
						var theId = parent.id;
						theId = theId.replace('row-title','');
						var dd = $get('row-definition' + theId);
						var dt = $get('row-title' + theId);
					
						dt.className = '';
						dd.className = '';							   
								   });
				}
			}
			inputs = list[i].getElementsByTagName('input');	
			iLength = inputs.length;
			labels = list[i].getElementsByTagName('label');	
			for (l=0;l<=(labels.length-1);l++) {
				labelText = getText(labels[l]);
				parentAdd = labels[l].parentNode;
				addClass = parentAdd.className;
				labelAnchor = document.createElement('a');
				labelAnchor.setAttribute('href','#');
				labelAnchor.setAttribute('title',labelText);
				labelAnchor.setAttribute('id',labels[l].id.replace('label-','link-'));
				parentAdd.appendChild(labelAnchor);
				setText(labelAnchor,labelText);
				labelAnchor.className = 'add';
				if (addClass.indexOf('added')>-1) {
					parentAdd.className = 'add';
					labelAnchor.className = 'added';
					setText(labelAnchor,'Added to my shortlist jobs');
				}
				$(labelAnchor.id).addEvent('click', function(e){	
					var e = new Event(e);										 
					var theId = this.id;
					theId = theId.replace('link-','');
					addShortList(this,theId);
					e.stop();
				});
				$get('add-to-shortlist-btn').style.display='none';
			}
			for (d=0;d<=(iLength-1);d++) {
				parentRemove = labels[0].parentNode;
				parentRemove.removeChild(labels[0]);
				parentRemove.removeChild(inputs[0]);
			}
		}
	}
}
function initShortList() {
	var con = $get('content');
	var list = con.getElementsByTagName('dl');
	var links;
	
	for (i=0;i<=(list.length-1);i++) {
		if (list[i].className=='results') {
			links = list[i].getElementsByTagName('a');
			
			for (j=0;j<=(links.length-1);j++) {
				if (links[j].className=='title') {
					$(links[j].id).addEvent('mouseover', function(){
						var parent = this.parentNode;
						var theId = parent.id;
						theId = theId.replace('row-title','');
						var dd = $get('row-definition' + theId);
						var dt = $get('row-title' + theId);
						
						dt.className = 'on';
						dd.className = 'on';
								   });
					$(links[j].id).addEvent('mouseout', function() {
						var parent = this.parentNode;
						var theId = parent.id;
						theId = theId.replace('row-title','');
						var dd = $get('row-definition' + theId);
						var dt = $get('row-title' + theId);
					
						dt.className = '';
						dd.className = '';							   
								   });
				}
			}
		}
	}
}
function initJob(jobRef) {
	var obj = $get('add-job');
	var links = obj.getElementsByTagName('a');
	
	if (obj!=null) {		
		labelAnchor = document.createElement('a');
		labelAnchor.setAttribute('href','#');
		labelAnchor.setAttribute('title','Add to my shortlist');
		labelAnchor.setAttribute('id','link-' + jobRef);
		obj.appendChild(labelAnchor);
		setText(labelAnchor,'Add to my shortlist');
		labelAnchor.className = 'add';
		if (obj.className.indexOf('added')>-1) {
			obj.className = 'add';
			labelAnchor.className = 'added';
			setText(labelAnchor,'Added to my shortlist jobs');
			labelAnchor.setAttribute('title','Added to my shortlist jobs');
		}
		$(labelAnchor.id).addEvent('click', function(e){	
			var e = new Event(e);										 
			var theId = this.id;
			theId = theId.replace('link-','');
			addShortList(this,theId);
			e.stop();
		});
	}
	obj.removeChild(links[0]);
		
}
function listingEffect(obj) {
	var dt = obj.parentNode;
	var dd = $get(dt.id.replace('row-title','row-definition'));
								
	if (dt.className=='on') {
		dt.className = '';
		dd.className = '';		
	} else  {
		dt.className = 'on';
		dd.className = 'on';
	}
}
function addShortList(obj,theId) {
	var req;
	
	if(typeof XMLHttpRequest!="undefined") {
		req= new XMLHttpRequest();
	} else {
		try  {		
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(oc) {
				req=null;
			}
		}
	}
	
	if (obj.className=='add') {
		var add_url = '/job-finder/my-shortlisted-jobs/add-ajax?job=' + theId;
		
		if(req!=null) {
			obj.className = 'adding';
			setText(obj,'Adding to my shortlist jobs');
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK" 				
					if (req.status == 200) {
						if(req.responseText.indexOf("update=true")>-1) {
							setText(obj,'Added to my shortlist jobs');	
							obj.className = 'added';
						} else {
							alert('Unexpected response text');
						}
					} else {
						alert(req.status);
					}
				}
			}
			req.open("GET",add_url, true);
			req.send(null);
		}
	} else if (obj.className=='added') {
		var remove_url = '/job-finder/my-shortlisted-jobs/remove-ajax?job=' + theId;
		if(req!=null) {
			obj.className = 'removing';
			setText(obj,'Removing from my shortlisted jobs');
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK" 				
					if (req.status == 200) {
						if(req.responseText.indexOf('update=true')>-1) {
							setText(obj,'Add to my shortlist jobs');	
							obj.className = 'add';
						} else {
							alert('Unexpected response text');
						}
					} else {
						alert(req.status);
					}
				}
			}
		req.open("GET", remove_url, true);
		req.send(null);
		}
	} else {
		alert('Please wait until the current process has completed');	
	}
}

function getText(el) {
	if ('string' == typeof el.textContent) {
		return  el.textContent;
	} else if ('string' == typeof el.innerText) {
		return el.innerText;
	}
}
function setText(el,str) {
	if ('string' == typeof el.textContent) {
		 el.textContent = str;
	} else if ('string' == typeof el.innerText) {
		el.innerText = str;
	}
}

function addLoadEvent(func) {  
var oldonload = window.onload;  
if (typeof window.onload != 'function') {  
           window.onload = func;  
} else {  
   window.onload = function() {  
       if (oldonload) {  
         oldonload();  
       }  
       func();  
      }  
  }  
}  

/* CAPTCHA image swapper */
function captchaImgRefresh() {
if(document.getElementById) {
	if(document.getElementById('refresh-captcha')) {
		document.getElementById('refresh-captcha').onclick = function() { 
			var rndm = Math.floor(Math.random()*1001); // prevent caching
			document.getElementById('captcha-img').src += '&rand='+rndm;
		}
		}
	}
}
