function trim(string)
{
	return string.replace(/^\s+|\s+$/g,'');
}

function switchtab(pref,toshow,amount)
{
	for(var i=1;i<=amount;i++)
	{
		document.getElementById(pref+"_tab_"+i).className = ( i == toshow ? 'tab_selected' : 'tab' );
		document.getElementById(pref+"_tab_content_"+i).style.display = ( i == toshow ? 'block' : 'none' );
	}
}

function select_puesto(puesto_select_id,area_select_id,facturacion_select_id)
{
	if( $(facturacion_select_id).value == "" ) {
		$(area_select_id).disabled  = true;
		alert('Por favor, seleccione la facturación de su empresa');
	} else {
		$(area_select_id).disabled  = false;
	}
	
	var puestosel = $(puesto_select_id);
	
	var area_id = $(area_select_id).value;
	
	var facturacion_id = $(facturacion_select_id).value;
	
	while( puestosel.length > 0 )
		puestosel.remove(puestosel.length-1);
		
				
	if( area_id != '' )
	{
		opt_obj = document.createElement('option');
		opt_obj.text = _puestos_mensaje_lleno;
		opt_obj.value = "";
		try { puestosel.add(opt_obj,null); // standards compliant
		} catch(ex) { puestosel.add(opt_obj); // IE only 
		}
		
		var puestos_mostrados = 0;
		for(i=0;i<puestos[area_id].length;i++)
		{
			optdatos = puestos[area_id][i];
			
			if( fac_pue[optdatos.id] && fac_pue[optdatos.id][facturacion_id] )
			{
				puestos_mostrados++;
				
				opt_obj = document.createElement('option');
				opt_obj.text = optdatos.text;
				opt_obj.value = optdatos.id;
				
				try { puestosel.add(opt_obj,null); // standards compliant
				} catch(ex) { puestosel.add(opt_obj); // IE only
				}
			}
		}
		puestosel.disabled = false;
		
		
		if( puestos_mostrados == 0 )
		{
			while( puestosel.length > 0 )
				puestosel.remove(puestosel.length-1);
		
			opt_obj = document.createElement('option');
			opt_obj.text = 'Actualmente no hay datos.';
			opt_obj.value = "";
			puestosel.disabled = true;
			
			try { puestosel.add(opt_obj,null); // standards compliant
			} catch(ex) { puestosel.add(opt_obj); // IE only
			}
		}
	}
	else
	{
		opt_obj = document.createElement('option');
		opt_obj.text = _puestos_mensaje_vacio;
		opt_obj.value = "";
		try { puestosel.add(opt_obj,null); // standards compliant
		} catch(ex) { puestosel.add(opt_obj); // IE only
		}
		
		puestosel.disabled = true;
	}
}

function select_puesto2()
{
	var puestosel = $('puesto_id');
	var areasel = $('area_id');
	var facturacionsel = $('facturacion_id');
	var mercadosel = $('mercado_id');
	var industriasel = $('industria_id');
	
	var puesto_id = puestosel.value;
	var area_id = areasel.value;
	var facturacion_id = facturacionsel.value;
	var mercado_id = mercadosel.value;
	var industria_id = industriasel.value;
	
	
	
	while( puestosel.length > 0 )
		puestosel.remove(puestosel.length-1);
		
				
	if( areasel.value != '' )
	{
		opt_obj = document.createElement('option');
		opt_obj.text = _puestos_mensaje_lleno;
		opt_obj.value = "";
		try { puestosel.add(opt_obj,null); // standards compliant
		} catch(ex) { puestosel.add(opt_obj); // IE only 
		}
		
		
		
		var puestos_mostrados = 0;
		for(i=0;i<puestos[area_id].length;i++)
		{
			optdatos = puestos[area_id][i];
			var show = 0;
			if( facturacionsel.value != '' && mercadosel.value != '' && industriasel.value != '' ) {
				if( fac_pue[mercadosel.value] && 
					fac_pue[mercadosel.value][industriasel.value] && 
					fac_pue[mercadosel.value][industriasel.value][facturacionsel.value] && 
					fac_pue[mercadosel.value][industriasel.value][facturacionsel.value][optdatos.id]
					 )
					 show = 1;
			}
			else
			{
				show = 1;
			}
			
			if( show == 1 )
			{
				puestos_mostrados++;
				
				opt_obj = document.createElement('option');
				opt_obj.text = optdatos.text;
				opt_obj.value = optdatos.id;
				
				try { puestosel.add(opt_obj,null); // standards compliant
				} catch(ex) { puestosel.add(opt_obj); // IE only
				}
			}
		}
		puestosel.disabled = false;
		
		
		if( puestos_mostrados == 0 )
		{
			while( puestosel.length > 0 )
				puestosel.remove(puestosel.length-1);
		
			opt_obj = document.createElement('option');
			opt_obj.text = 'Actualmente no hay datos.';
			opt_obj.value = optdatos.id;
			puestosel.disabled = true;
			
			try { puestosel.add(opt_obj,null); // standards compliant
			} catch(ex) { puestosel.add(opt_obj); // IE only
			}
		}
	}
	else
	{
		opt_obj = document.createElement('option');
		opt_obj.text = _puestos_mensaje_vacio;
		opt_obj.value = "";
		try { puestosel.add(opt_obj,null); // standards compliant
		} catch(ex) { puestosel.add(opt_obj); // IE only
		}
		
		puestosel.disabled = true;
	}
}


var creditos_totales = 0;
function select_puesto3()
{
	creditos_totales = 0;
	$('puestos_hidden').value = '';
	
	var areasel = $('area_id');
	var facturacionsel = $('facturacion_id');
	var mercadosel = $('mercado_id');
	var industriasel = $('industria_id');
	
	var area_id = areasel.value;
	var facturacion_id = facturacionsel.value;
	var mercado_id = mercadosel.value;
	var industria_id = industriasel.value;
	
	var filas = '';
	var muestra = '';
	if( area_id != '' && facturacion_id != '' && mercado_id != '' && industria_id != '' )
	{
		var puestos_mostrados = 0;
		for(i=0;i<puestos[area_id].length;i++)
		{
			optdatos = puestos[area_id][i];
			var show = 0;
			if( facturacionsel.value != '' && mercadosel.value != '' && industriasel.value != '' ) {
				if( fac_pue[mercadosel.value] && 
					fac_pue[mercadosel.value][industriasel.value] && 
					fac_pue[mercadosel.value][industriasel.value][facturacionsel.value] && 
					fac_pue[mercadosel.value][industriasel.value][facturacionsel.value][optdatos.id]
					 )
					 show = 1;
			}
			else
			{
				show = 1;
			}
			
			if( show == 1 )
			{
				filas += '		<tr>'+
'			<td class="campo" style="width:auto;">'+
'				<label><input id="puesto_check_'+optdatos.id+'" type="checkbox" checked onclick="if($(\'puesto_check_'+optdatos.id+'\').checked){comprafamiliacuenta('+optdatos.creditos+','+optdatos.id+')}else{comprafamiliacuenta(-'+optdatos.creditos+','+optdatos.id+')} " class="cheq" id="puesto'+optdatos.id+'" />'+optdatos.text+'</label>'+
'			</td>'+
'			<td class="campo" style="width:60px;;"><span>'+optdatos.creditos+' crédito/s</span></td>'+
'		</tr>'+
'		<tr>'+
'			<td class="campo" colspan="2" style="width:auto; padding-top:0px; padding-bottom:2px; line-height:9px;">'+
'				<span>'+puestos_desc[optdatos.id].nombresalternativos.replace('<br />','')+'</span><br />'+
'				<a href="#" style="font-size:9px;" onclick="$(\'puesto_desc_'+optdatos.id+'\').show();return false;">Mostrar Descripción</a> / <a href="#" style="font-size:9px;" onclick="$(\'puesto_desc_'+optdatos.id+'\').hide();return false;">Ocultar Descripción</a><br />'+
'				<div id="puesto_desc_'+optdatos.id+'" class="seleccionado" style="display:none; margin:0px; padding:10px; height:80px; overflow:auto; overflow-x:hidden; font-weight:normal;">'+puestos_desc[optdatos.id].desc+'</div>'+
'			</td>'+
'		</tr>';
			
				comprafamiliacuenta(optdatos.creditos,optdatos.id);
				
				puestos_mostrados++;
			}
		}
		
		if( puestos_mostrados == 0 ) {
			muestra = 'Actualmente no hay datos.';
		} else {
			muestra = '<table class="registro">'+filas+'</table>';
		}
	}
	else
		muestra = 'Para visualizar el listado de puestos debe seleccionar todos los campos.';
		
		
	if( muestra == '' )
		muestra = 'No hay puestos para las opciones seleccionadas.';
	$('listado_puestos').innerHTML = muestra;
}

function comprafamiliacuenta(valor,id)
{
	if( valor > 0 )
		$('puestos_hidden').value = $('puestos_hidden').value + '-' + id + '-';
	else if( valor < 0 )
		$('puestos_hidden').value = $('puestos_hidden').value.replace('-'+id+'-','-');
	
	creditos_totales += parseInt(valor);
	
	$('total_creditos').update(creditos_totales+' crédito/s');
	$('total_restantes').update((creditos_activos-creditos_totales)+' crédito/s');
	
	if( (creditos_activos-creditos_totales) < 0 )
	{
		$('comprar_botton').style.visibility = 'hidden';
		$('comprar_mensaje').style.visibility = 'visible';
	}
	else
	{
		$('comprar_botton').style.visibility = 'visible';
		$('comprar_mensaje').style.visibility = 'hidden';
	}
}



var pasos = new Array(20,20,10,10,10,5,5,5,5,5,5,5,5,5,5,5,5,5);
var paso = 0;

function showhide(id)
{
	var smdiv = $('submenu_'+id);
	var smtable = $('table_submenu_'+id);
	$('submenu_li_'+id).show();
	
	tableheight = smtable.offsetHeight;
	divheight = parseInt(smdiv.style.height);
	
	paso = 0;
	if( divheight == 0 )
		moveTo(id,tableheight,tableheight);
	else
		moveTo(id,0,tableheight);
}

function moveTo(id,to,height)
{
	var smdiv = $('submenu_'+id);
	divheight = parseInt(smdiv.offsetHeight);
	
	if( to == 0 && divheight != to )
	{
		newheight = divheight - ( height / 100 * parseInt(pasos[paso]) )
		
		if( newheight < 0 ) newheight = 0;
		
		smdiv.style.height = newheight+'px';
		
		if( newheight != 0 )
			setTimeout("moveTo('"+id+"',"+to+","+height+")",100);
		else
			$('submenu_li_'+id).hide();
	}
	else if( to != 0 && divheight != to )
	{
		newheight = divheight + ( height / 100 * parseInt(pasos[paso]) )
		
		if( newheight > to ) newheight = to;
		
		smdiv.style.height = newheight+'px';
		
		if( newheight != to )
			setTimeout("moveTo('"+id+"',"+to+","+height+")",100);
		
	}
}
