var _xmlHttp;
var _div_id;
var _data;

var _orderBy;
var _orderBy2;
var limit;
limit = 25;

var y;
var w;

var FIRST_AD_LINE;
FIRST_AD_LINE = 5;
var SECOND_AD_LINE;
SECOND_AD_LINE = 18;

_orderBy = "nseen";
_orderBy2 = "";


function getElementTag(name){
    if (document.all){
        return document.all[name];
    } else return document.getElementById(name);
}



/**
 AJAX QUERIES :
**/
function submitMainJobSearch(){

	getElementTag('actionPhrase').innerHTML='';
	
	var form = document.forms['mainForm'];
	
	query('results',form.keywords.value,form.jobtype.value,form.profession.value,form.country.value,form.company.value,'1','',form.location.value,form.sectorCode.value,form.companyIdInput.value);
	getElementTag('companyIdInput').value = "";
	
	return false;
}


function submitQuickSearch(geoloc){
	
	var form = document.forms['quickSearchForm'];
	
	query('results',form.keywordsQuery.value,'A','',geoloc,'','1','','','',''); 
	
	getElementTag('actionPhrase').innerHTML='';
	return false;
}


//COMPANY SELECT QUERY
function companySelectQuery(div_id,companyNameIncomplete,event) {
  _div_id=div_id;
  
  if (event.keyCode == 13){
	return submitMainJobSearch();
  }else{
  
  	if (companyNameIncomplete == ""){
  		getElementTag(_div_id).style.visibility = "hidden";
  		getElementTag(_div_id).style.zIndex = -1;
  	}
  	else {
	    getElementTag(_div_id).style.visibility = "visible";
	    getElementTag(_div_id).style.zIndex = 180;
	    makeRemoteRequest("ajaxStructure.php?ajax=companySelect&a=" + companyNameIncomplete, _xmlHttp, null);
	}
  }
}

function localQuery(div_id,name) {
  _div_id=div_id;
    getElementTag(_div_id).style.visibility = "visible";
    makeRemoteRequest(name, _xmlHttp, null);    

}
//COMPANY LSO QUERY
function companyLSO(div_id,name) {
 _div_id=div_id;
 show(_div_id);
 makeRemoteRequest("ajaxStructure.php?ajax=companyLSO&name=" + name, _xmlHttp, null);

}

//JOBS QUERY

function query(div_id,keywords,jobtype,profession,country,company,page,order_by,location,sectorCode,advertiser) {
  var orderBy;
	if(company.length == 1){
		alert("The company keyword must be at least 2 characters long.");
		return;
	}
  _div_id=div_id;
  
  if(order_by != ""){
  	orderBy=order_by;
  }
  else{
  	orderBy=_orderBy;
  }
  
  var s;
  s = "";
  s+="limit=" + limit;			
  s+="&page=" + page;
  s+="&jobtype=" + jobtype;
  s+="&keywords=" + keywords;
  s+="&orderBy=" + orderBy;
  s+="&country=" + country;
  s+="&advertiser=" + advertiser;
  s+="&sectorCode=" + sectorCode;
  s+="&location=" + location;
  s+="&profession=" + profession;
  s+="&company=" + company;

	var u;
	u = "";
	if(keywords != ""){  u+="&keywords=" + keywords;}
	if(profession != ""){  u+="&profession=" + profession;}
	if(country != ""){  u+="&country=" + country;}
	if(company != ""){  u+="&company=" + company;}
	if(location != ""){  u+="&location=" + location;}
	if(sectorCode != ""){  u+="&sectorCode=" + sectorCode;}
	if(advertiser != ""){  u+="&advertiser=" + advertiser;}
	if(orderBy != "") {  u+="&orderBy=" + orderBy;}	
	
	w = keywords+","+profession+","+country+","+company+","+location+","+sectorCode+","+advertiser+",,"+orderBy+".htm";
	
	document.forms['reloadForm'].keywords.value = keywords;
	document.forms['reloadForm'].profession.value = profession;
	document.forms['reloadForm'].country.value = country;
	document.forms['reloadForm'].company.value = company;
	document.forms['reloadForm'].location.value = location;
	document.forms['reloadForm'].sectorCode.value = sectorCode;
	document.forms['reloadForm'].advertiser.value = advertiser;
	document.forms['reloadForm'].orderBy.value = orderBy;
	document.forms['reloadForm'].reload.value = 1;
	
	y = "saveSearch=yes" + u;
	  
  getElementTag(_div_id).innerHTML = "<div align='center' class='loading'><img src='http://www.lifescience-online.com/commonimages/icons/loading.gif'/> Loading data, please wait.</div>";
  makeRemoteRequest("ajaxStructure.php?ajax=jobs&"+s, _xmlHttp, null);  
}

/**
 AJAX :
**/

function setData(data){
  _data=data;
       
  getElementTag(_div_id).innerHTML = data;
}

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) {

        setData(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);

  }
}

/**
 FORM OUTPUT :
**/

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) {
  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;
  }
}


/**
 OTHERS :
**/

//sets the "orderby" variable
function orderby(content,choice){
	_orderBy = content;
	getElementTag('order').value = _orderBy;
	getElementTag('ordering').selectedIndex = choice;
}

//Job description opening and closing

function moreOrLess(id,falseid){

	var div;
	div = "div" + id;

	if(getElementTag(div).style.visibility == "visible"){
		less(id,falseid);
	}
	else if(getElementTag(div).style.visibility == "hidden"){
		more(id);
	}

}

function more(id){

	var div;
	var tableTr;
	
	tableTr = "tr[" + id + "]";
	div = "div" + id;

	getElementTag(div).style.visibility = "visible";
	getElementTag(div).style.position = "relative";
	getElementTag(tableTr).style.background = '#F1E9B8';
	getElementTag(div).style.background = '#F1E9B8';
}

function less(id,falseid){
	
	var div;
	var tableTr;
	
	tableTr = "tr[" + id + "]";
	div = "div" + id;
	companyDiv = "companyLinks[" + id + "]";
	
	if(getElementTag(div)){
		getElementTag(div).style.visibility = "hidden";
		getElementTag(div).style.position = "absolute";
		
		getElementTag(tableTr).style.background = '#FFFFFF';
		getElementTag(div).style.background = '#FFFFFF';
		
		if ((falseid % 2) == 0){
			getElementTag(tableTr).style.background = '#E8ECE4';
			getElementTag(div).style.background = '#E8ECE4';
		}
	}
	
	closeCompanySearch(companyDiv,'divTemp');
}

// To hide the company query
function closeCompanySearch(company_div,company_more_div){

	if (getElementTag(company_more_div)){
		if (getElementTag(company_more_div).style.visibility != "hidden"){
			getElementTag(company_more_div).style.left="0";
			getElementTag(company_more_div).style.top="0";
			getElementTag(company_more_div).style.visibility = "hidden";
		}
	}
	if (getElementTag(company_div)){
		if (getElementTag(company_div).style.visibility != "hidden"){
			getElementTag(company_div).style.left="0";
			getElementTag(company_div).style.top="0";
			getElementTag(company_div).style.visibility = "hidden";
		}
	}
}

function lessAll(){
	for (g=0;g<limit;g++){
		less(g);
	}
}

function lessAllOther(id){
	for (g=0;g<limit;g++){
		if(g != id && g < FIRST_AD_LINE){
			less(g,g);
		}
		if(g != id && g >= FIRST_AD_LINE && g < SECOND_AD_LINE){
			less(g,g+1);
		}
		if(g != id && g >= SECOND_AD_LINE){
			less(g,g+2);
		}
	}
}

//move cursor

function placeCursorToLogin(){
	
	document.forms["loginFormToFocus"].name.focus();
} 

//RSS QUERY C

function rssQuery(){
	if(w == null){
		alert('Please start a new search !');
		return false;
	}
	else{
 		window.open('rss/' + w,'external');
 	}
}
function vibesQuery(){
	if(w == null){
		alert('Please start a new search !');
		return false;
	}
	else{

		ww = 'http://jobs.lifescience-online.com/rss/' + w;
		ww = encodeURIComponent(ww);

		getElementTag("vibeForm").action = "http://www.lifescience-online.com/coplets/users/pharmaVibes/addChannel.html";
		document.forms['vibeForm'].channelURL.value =  ww;

 		//window.open('http://www.lifescience-online.com/coplets/users/pharmaVibes/addChannel.html?channelURL=' + ww,'external');
 	}
}

function vibesQuery2(id,keywords,profession,country,company,location,sectorCode,advertiser,orderBy){
	
	
	w = keywords+","+profession+","+country+","+company+","+location+","+sectorCode+","+advertiser+",,"+orderBy+".htm";

	vibeForm = "vibeForm" + id;

	ww = 'http://jobs.lifescience-online.com/rss/' + w;
	ww = encodeURIComponent(ww);

	getElementTag(vibeForm).action = "http://www.lifescience-online.com/coplets/users/pharmaVibes/addChannel.html";
	document.forms[vibeForm].channelURL.value =  ww;

	//window.open('http://www.lifescience-online.com/coplets/users/pharmaVibes/addChannel.html?channelURL=' + ww,'external');
}

function defineQuery(keywords,jobtype,profession,country,company,page,order_by,location,sectorCode,advertiser){

  var orderBy;

  if(order_by != ""){
  	orderBy=order_by;
  }
  else{
  	orderBy=_orderBy;
  }
  
	var u;
	u = "";
	if(keywords != ""){  u+="&keywords=" + keywords;}
	if(profession != ""){  u+="&profession=" + profession;}
	if(country != ""){  u+="&country=" + country;}
	if(company != ""){  u+="&company=" + company;}
	if(location != ""){  u+="&location=" + location;}
	if(sectorCode != ""){  u+="&sectorCode=" + sectorCode;}
	if(advertiser != ""){  u+="&advertiser=" + advertiser;}
	if(orderBy != "") {  u+="&orderBy=" + orderBy;}	
	
	w = keywords+","+profession+","+country+","+company+","+location+","+sectorCode+","+advertiser+",,"+orderBy+".htm";
	
	y = "saveSearch=yes" + u;
	  
	return false;
}

function saveSearch(searchName){

	if(y == null){
		alert('Please start a new search !');
		return false;
	}
	else{
 		parent.location.href='index.php?' + y + '&reload=1&searchName=' + searchName;
 		return false;
 	}
}

function jobMail(searchName){

	if(y == null){
		alert('Please start a new search !');
		return false;
	}
	else{
 		parent.location.href='index.php?' + y + '&mailer=1&reload=1&searchName=' + searchName;
 		return false;
 	}
}

function setSelectedCountry(geoloc){
	var i;
	for(i=0;i<document.mainForm.country.options.length;i++) {
    	if(document.mainForm.country.options[i].value == geoloc){
    		document.mainForm.country.selectedIndex=i;
    	}
  	}
}


/**
 DIV's :
**/

function showhideWell(place){

	if(getElementTag(place).style.visibility != 'visible'){
		showWell(place);
	}
	else if(getElementTag(place).style.visibility != 'hidden'){
		hideWell(place);
	}
}

function showWell(place){

	getElementTag(place).style.visibility = 'visible';
	getElementTag(place).style.zIndex = 180;
}

function hideWell(place){

	getElementTag(place).style.visibility = 'hidden';
	getElementTag(place).style.zIndex = -1;
}

function hide(place){
	if(getElementTag(place)){
		getElementTag(place).style.visibility = 'hidden';
	}
}

function show(place){
	getElementTag(place).style.visibility = 'visible';
}


function showHide(place){
	
	if(getElementTag(place).style.visibility != 'visible'){
		show(place)
	}
	else if(getElementTag(place).style.visibility != 'hidden'){
		hide(place)
	}
}

// For mouse position
function setMousePosition(event,popup,xModificator,yModificator){
	if (!xModificator){
		xModificator = 0;
	}
	if (!yModificator){
		yModificator = 0;
	}
   var ev = event || window.event;
   var element = getElementTag(popup);
    //alert("document.all +"+document.all+"+\nev.PageX = "+ev.pageX+"\n ev.clientX "+ev.clientX+"\n element.style.left "+element.style.left+"\n element.style.offsetLeft "+element.style.offsetLeft);
    if (ev.pageX || ev.pageY){   
        
        finalY = ev.pageY + 15 + yModificator;
        
        if (finalY < 5){
        	finalY = 5
        }
        element.style.left  = (ev.pageX + 30 + xModificator)+"px";
        element.style.top  = finalY+"px";
    } else if (document.all){ //MSIE               
        try{
        
           	finalY = document.body.scrollTop + ev.clientY + 15 + yModificator;
           	
           	if (finalY < 5){
	        	finalY = 5
	        }
           
            element.style.left = (document.body.scrollLeft + ev.clientX + 30 + xModificator);
            element.style.top = finalY;//element.style.offsetTop  = (ev.clientY + 5);
        }catch(e){
           
        	finalY = document.body.scrollTop + ev.clientY + 15 + yModificator;
           	
           	if (finalY < 5){
	        	finalY = 5
	        }
           
            element.style.offsetLeft  = (document.body.scrollLeft + ev.clientX + 30 + xModificator);
            element.style.offsetTop  = finalY;     
        }
    }   
}

var bookmarkList = new Array();
// For mouse position
function setMousePosition2(event,popup,yModificator,id){
	
	var userX;
	var x;
	
	x = 202;
	if (document.body.clientWidth > 1000 && document.body.clientWidth < 1264){
		x = 334 - (1264 - document.body.clientWidth)/2;
	}else if(document.body.clientWidth >= 1264){
		x = 334;
	}
	
	
	if (!yModificator){
		yModificator = 0;
	}
	
   var ev = event || window.event;
   var element = getElementTag(popup);
    //alert("document.all +"+document.all+"+\nev.PageX = "+ev.pageX+"\n ev.clientX "+ev.clientX+"\n element.style.left "+element.style.left+"\n element.style.offsetLeft "+element.style.offsetLeft);
    
    if (ev.pageX || ev.pageY){
    	
    	if(!bookmarkList[id]){
			bookmarkList[id] = ev.pageY;
			y = ev.pageY;
		}else{
			if(bookmarkList[id] < ev.pageY && (ev.pageY - bookmarkList[id]) < 250){
				y = bookmarkList[id];
			}else{
				y = ev.pageY;
				bookmarkList[id] = ev.pageY;
			}
		}
    
        element.style.left  = x+"px";
        element.style.top  = yModificator + y +"px";
        
    } else if (document.all){ //MSIE               
        try{
            if(!bookmarkList[id]){
				bookmarkList[id] = ev.clientY;
				y = ev.clientY;
			}else{
				if(bookmarkList[id] < ev.clientY && (ev.clientY - bookmarkList[id]) < 250){
					y = bookmarkList[id];
				}else{
					y = ev.pageY;
					bookmarkList[id] = ev.clientY;
				}
			}
			
            element.style.left = x;
            element.style.top = yModificator + document.body.scrollTop + y;//element.style.offsetTop  = (ev.clientY + 5);
        }catch(e){

            element.style.offsetLeft  = x;
            element.style.offsetTop  = yModificator + document.body.scrollTop + y;       
        }
    }   
}


//Show details of bookmarked search
function showDetails(div_id,keywords,jobtype,profession,country,company,orderBy,location,sectorCode,advertiser,date,mailer,dateToMail,id,searchName){
	//Comment out "return" for other layout
	return;
	var details_div;
	details_div = div_id;
	
	show(details_div);
	
	var z = "";
	
	z+="<p style='margin-top:0px;' align='center'>" + date + " - <b>" + searchName + "</b> (click to edit)</b></p>";
	z+="All jobs";
    if(profession != ""){z+=" as <b>" + profession + "</b><br>";}
    if(country != ""){z+=" in <b>" + country + "</b><br>";}
    if(location != ""){z+=" in <b>" + location + "</b><br>";}
    if(company != ""){z+=" in company <b>" + company + "</b><br>";}
	if(sectorCode != ""){z+=" in sector <b>" + sectorCode + "</b><br>";}
	if(keywords != ""){z+=" with keyword(s) <b>" + keywords + "</b><br>";}
	z+=" ordered by <b>" + orderBy + "</b>.<br><br>";
	
	if(mailer == 1){
		z+="<form id='mailerForm' action='./' method='post'>I want to <a onClick=\"document.forms['mailerForm'].submit();\">Cancel Subscription</a> to my Mailer.<input type='hidden' name='editMailer' value='0'><input type='hidden' name='id' value='" + id + "'><input type='hidden' name='dateToMail' value='" + dateToMail + "'></form>";
	}
	else{z+="<form id='mailerForm' action='./' method='post'>I want to <a onClick=\"document.forms['mailerForm'].submit();\">get this search's results by mail once a week</a><input type='hidden' name='editMailer' value='1'><input type='hidden' name='id' value='" + id + "'><input type='hidden' name='dateToMail' value='" + dateToMail + "'></form>";}
	
	getElementTag(details_div).innerHTML = z;
}

//Show details of bookmarked jobs
function showDetailsJobs(div_id,profession,company,location,date,sectorCode,country,salary,jobType,comment,id){
	
	var uu;
	var details_div;
	details_div = div_id;
	
	if(comment == ""){
		comment = "No personal comment (click to edit)";
	}
	
	show(details_div);

	
		uu = "<table width='400' id='detailsTable' border='0' cellspacing='0' cellpadding='5'>";
			uu += "<tr style='background-image: url(images/table_header_bg.jpg);background-repeat:repeat-x;background-color:#000'>";
				uu += "<td title='' class='headerCol' width=''>Profession</td>";
				uu += "<td title='' class='headerCol' width=''>Company</td>";
				uu += "<td title='' class='headerCol' width=''>Location</td>";
				uu += "<td title='' class='headerCol' width=''>Date</td>";
			uu += "</tr>";
			uu += "<tr valign='top'>";
				uu += "<td>"+ profession +"</td>";
				uu += "<td>" + company + "</td>";
				uu += "<td>" + location + "</td>";
				uu += "<td>" + date + "</td>";
			uu += "</tr>";
			uu += "<br>";
			uu += "<tr valign='top'>";
				uu += "<td colspan='4'>";
					uu += "<ul>";
						
						uu += "<li>";
							uu += "<span class='boldBlue'>Sector</span> : " + sectorCode;
						uu += "</li>";
						
						uu += "<li>";
							uu += "<span class='boldBlue'>Country</span> : " + country;
						uu += "</li>";
						
						uu += "<li>";
							uu += "<span class='boldBlue'>Salary</span> : " + salary;
						uu += "</li>";
						
						uu += "<li>";
							uu += "<span class='boldBlue'>Job Type</span> : " + jobType;
						uu += "</li>";
						
					uu += "</ul>";
				uu += "</td>";
		
			uu += "</tr>";
		uu += "</table>";
		
	uu += "<form id='detailsForm' action='./' method='post'><span style='cursor:default;position:absolute;' id='commentSpan' onClick=\"show('commentTextbox');show('commentSubmit');hide('commentSpan');document.forms['detailsForm'].comment.select();\"><i>\"";
    uu += comment;
    uu += "\"</i></span>";
    uu += " <input type='hidden' name='id' value='" + id + "'><input id='commentTextbox' type='textbox' name='comment' size='35' length='255' style='visibility:hidden;' value='"+ comment +"'> <input id='commentSubmit' type='button' onClick=\"document.forms['detailsForm'].submit();\" value='ok' style='visibility:hidden;'></form>";
	
	
	getElementTag(details_div).innerHTML = uu;
	
	
}


function changeImage(place){
	getElementTag(place).sry = 'images/arrow.gif';
	
	getElementTag(place).sry = 'images/arrow.gif';
}

function changeCountryFlag(countryISO,countryTitle){
	
	getElementTag('countryTitle').innerHTML = countryTitle;
	getElementTag('countryTitle2').innerHTML = countryTitle;
	
	var numSec;
	
	for(numSec=0;numSec<8;numSec++){
	
		if(getElementTag('sectorListIntro'+ numSec)){
			line = getElementTag('sectorListIntro'+ numSec).href;
			while(line.substring(line.length -1,line.length) != ","){
				line = line.substring(0,line.length -1);
			}
			line += countryTitle;
			line += ".htm";
			
			getElementTag('sectorListIntro'+ numSec).href = line;
		}
	}
	document.forms['quickSearchForm'].changedGeoloc.value = countryISO;
	
}


function setValue(value,id){
	getElementTag('companyTextBoxInput').value = value;
	getElementTag('companyIdInput').value = id;
}



