var mat = new Object();

function verSector() {
	if (document.getElementById('skill_area').value == '00') {
		document.getElementById('sectorId').disabled = true;
	} else {
		document.getElementById('sectorId').disabled = false;
	}
}
function get_whatJobAreYouSeekingText(str){
	
	var temp= document.createElement("temporal");
	temp.innerHTML=str;	
	return temp.firstChild.nodeValue;
}

function validateCampos() {
	
	ok = false;
	ok = ok
			|| ((document.getElementById('LocationId') != null) && (document
					.getElementById('LocationId').value != '00'));
	ok = ok
			|| ((document.getElementById('skill_area') != null) && (document
					.getElementById('skill_area').value != '00'));
	ok = ok
			|| ((document.getElementById('sector') != null) && (document
					.getElementById('sector').value != '00'));
	ok = ok
			|| ((document.getElementById('jobTypeId') != null) && (document
					.getElementById('jobTypeId').value != '00'));
	
	var kwElement= document.getElementById('kw');
	
	ok = ok || (kwElement != null && kwElement.value.replace(/^\s*|\s*$/g, "") != '' &&
			kwElement.value!= get_whatJobAreYouSeekingText(whatJobAreYouSeekingText));
	
	//If validation OK then continue with .kw empty:
	if(ok && kwElement != null &&  kwElement.value== get_whatJobAreYouSeekingText(whatJobAreYouSeekingText)){
		document.getElementById('kw').value="";
	}	
	
	return ok;
}
function validateCamposInHeader() {
	
	ok = false;
	
	
	var kwElement= document.getElementById('kwHeader');
	
	ok = ok || (kwElement != null && kwElement.value.replace(/^\s*|\s*$/g, "") != '' &&
			kwElement.value!= get_whatJobAreYouSeekingText(whatJobAreYouSeekingText));
	
	//If validation OK then continue with .kw empty:
	if(ok && kwElement != null &&  kwElement.value== get_whatJobAreYouSeekingText(whatJobAreYouSeekingText)){
		document.getElementById('kwHeader').value="";
	}	
	
	return ok;
}

function cambioComboFecha() {
	if (!validateCampos()) {
		var boton = document.getElementById('sd');
		if (boton != null) {
			boton.disabled = true;
			boton.selectedIndex = 0;
		}
	} else {
		var boton = document.getElementById('sd');
		if (boton != null) {
			boton.disabled = false;
		}
	}
}

function cambioArea(text) {
	document.getElementById('sectorId').length = 0; // clear
	areaCombo = document.getElementById('skill_area');
	areaTXT = areaCombo.options[areaCombo.selectedIndex].value;
	if (areaTXT != '00') {
		lista = mat[areaTXT];
		for ( var i = 0; i < lista.length; i++) {
			document.getElementById('sectorId').options[i] = new Option(
					mat[areaTXT][i][1], mat[areaTXT][i][0]);
		}
	} else {
		document.getElementById('sectorId').options[0] = new Option(text, '');
	}
	verSector();
	cambioComboFecha();
}

function setMat(newMat) {
	mat = newMat;
}

function validateSearch(errorMessage) {
	if (!validateCampos()) {
		alert(errorMessage);
	}
	return ok;
}

function validateSearchInHeader(errorMessage) {
	if (!validateCamposInHeader()) {
		alert(errorMessage);
	}
	return ok;
}

function addHighlight(div) {
	if (div.id == 'aviso platinum') {
		div.style.backgroundPosition = 'bottom';
		div.style.border = "1px solid #FFA46C";
	}
	if (div.id == 'aviso destacado') {
		div.style.backgroundPosition = 'bottom';
		div.style.border = "1px solid #9DC1C0";
	}
}

function removeHighlight(div) {

	if (div.id == 'aviso platinum') {
		div.style.backgroundPosition = 'top';
		div.style.border = "1px solid #FFEFD7";
	}
	if (div.id == 'aviso destacado') {
		div.style.backgroundPosition = 'top';
		div.style.border = "1px solid #E6EFEF";
	}
}

function displayTransparentDiv(transparentDivId, loadingDivId) {
	var transparentDiv = document.getElementById(transparentDivId);
	transparentDiv.style.display = "block";
	modifDiv();

	if (loadingDivId) {
		var loadingDiv = document.getElementById(loadingDivId);
		loadingDiv.style.display = "block";
	}
}

function displayActionPopupDiv(actionPopupDivId) {
	var actionPopupDiv = document.getElementById(actionPopupDivId);
	centerElement(actionPopupDiv);
	actionPopupDiv.style.display = "block";
}

function displayPop(div, div2) {
	displayTransparentDiv(div2);
	displayActionPopupDiv(div);
}

function centerElement(element) {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentElement = element;
			var contentHeight = contentElement.offsetHeight;
			contentElement.style.position = 'absolute';
			if (windowHeight - contentHeight > 0) {
				contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
			}
		}
	}
}

function modifDiv() {
	var obj = document.getElementById('transparentDiv').style;
	if (obj.display == 'none')
		showSelect();
	else
		obj.height = document.getElementById('divMain').offsetHeight + 10 + 'px';
}

function showSelect() {
	var e = document.getElementsByTagName('select');
	for (i = 0; i < e.length; i++) {
		e[i].style.visibility = 'visible';
	}
}

function displayN2(div, div2) {
	document.getElementById(div).style.display = "none";
	document.getElementById(div2).style.display = "none";
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof (window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function displayJobOwner(username,firstname,lastname) {
	var jobOwnerPopUp = document.getElementById('jobOwnerPopUp');
	jobOwnerPopUp.innerHTML = '<b>Adquirido por: </b>' + '<br><b>Usuario: </b>' + username + '<br><b>Nombre: </b>' + firstname + '<br><b>Apellido: </b>' + lastname;
}

function displaySharedBy(username,firstname,lastname) {
	var sharedByPopUp = document.getElementById('sharedByPopUp');
	sharedByPopUp.innerHTML = '<b>Compartido por: </b>' + '<br><b>Usuario: </b>' + username + '<br><b>Nombre: </b>' + firstname + '<br><b>Apellido: </b>' + lastname;
}

function getSearchPostulationMetadata(params) {
	new Ajax.Request(params.controller, {
		method : 'get',
		parameters : {
			jobSeekerId : params.jobSeekerId,
			timestamp : new Date().getTime()
		},
		requestHeaders : {
			Accept : 'application/json'
		},
		onSuccess : function(transport) {
			onSearchPostulationMetadataRetrieved(params, transport);
		}
	});
}

function onSearchPostulationMetadataRetrieved(params, transport) {
	var metadata = transport.responseText.evalJSON();

	// if visible, enable jobseeker's name.
	if (metadata.visible) {
		$('jobSeekerNameSpan' + params.jobSeekerId).innerHTML = metadata.firstName
				.escapeHTML()
				+ '&nbsp;' + metadata.lastName.escapeHTML();
	}

	// if subscribed to selection process, display icon.
	if (metadata.subscribedToSelectionProcess) {
		var spid = 'jobSeekerSelectionProcessesHREF' + params.jobSeekerId; 
		if ($(spid)) {
			displayElement(spid, 'block');	
		}
	}

	// enable all jobseeker's custom labels.
	if (metadata.labels.length > 0) {
		for (counter = 0; counter < metadata.labels.length; counter++) {
			var label = metadata.labels[counter];
			onLabelApplied(params, params.jobSeekerId, label);
		}
	}
	
	// enable all employer's shared custom labels (binded with a jobseeker).
	if (metadata.sharedLabels.length > 0) {
		for (counter = 0; counter < metadata.sharedLabels.length; counter++) {
			onSharedLabelApplied(params, params.jobSeekerId, metadata.sharedLabels[counter]);
		}
	}
	
	
	// if acquired by other user from my company show info
	if (metadata.acquiredByUsername != '') {
		$('acquiredBySpan' + params.jobSeekerId).innerHTML = '<a><img href="#" onMouseOver="displayJobOwner(\'' +
															  metadata.acquiredByUsername.escapeHTML() + '\',\'' +
															  metadata.acquiredByFirstName.escapeHTML() + '\',\'' +
															  metadata.acquiredByLastName.escapeHTML() + '\'); positionClick(event,\'jobOwnerPopUp\'); displayBlock(\'jobOwnerPopUp\'); return false;" onMouseOut="displayN(\'jobOwnerPopUp\');return false" alt="" src="http://static.zonajobs.com.ar/imgs/others-postulations.jpg"></a>';
	}
	
	displayElement('jobSeekerLabelsLoadingDiv' + params.jobSeekerId, 'none');
}

function removeMessage(elemId){	
	var textbox= document.getElementById(elemId);		
	if(textbox.value.replace(/^\s*|\s*$/g, "")==get_whatJobAreYouSeekingText(whatJobAreYouSeekingText)){
		textbox.value="";
	}					
}

function restoreMessage(elemId){	
	var textbox= document.getElementById(elemId);	
	if(textbox.value.replace(/^\s*|\s*$/g, "")=="" || textbox.value.replace(/^\s*|\s*$/g, "")==get_whatJobAreYouSeekingText(whatJobAreYouSeekingText)){
		textbox.value=get_whatJobAreYouSeekingText(whatJobAreYouSeekingText);
	}					
}	
