function toggle(container,panel,panel_class) {
	//var state = $(panel).getStyle('display');
	$$(container,panel_class).setStyle('display','none');
	$(panel).setStyle('display','block');
}

function show_hide(id,value) {
	if (value) {
		$(id).setStyle('display','block');
	} else {
		$(id).setStyle('display','none');
	}
}

function addRow(id, tipo) {
	var tbl = document.getElementById(id).tBodies[0];
	var newRow = tbl.insertRow(-1);
	var totRows = tbl.rows.length - 2;
	//alert('totRows='+totRows+' newRow='+newRow);
	switch (tipo) {
		case 1:	// educacion
			var newCell0 = newRow.insertCell(0);
			newCell0.innerHTML = '<a href="javascript:removeRow(\'teducacion\',3,\'tr'+totRows+'\');"><img src=\"themes/default/img/icons/cancel.png\" alt=\"B\" border=\"0\" /></a>';
			var newCell1 = newRow.insertCell(1);
			newCell1.innerHTML = '<select name="escolaridad[]" id="esc'+totRows+'" size="1"></select>';
			var newCell2 = newRow.insertCell(2);
			newCell2.innerHTML = '<input type="text" name="titulo[]" value="" size="35" />';
			var newCell3 = newRow.insertCell(3);
			newCell3.innerHTML = '<select name="situacion[]" id="sit'+totRows+'" size="1"></select>';
			addOption('esc'+totRows,edu_options,edu_ovals);
			addOption('sit'+totRows,sit_options,sit_ovals);
			newRow.id='tr'+totRows;
			break;
		case 2:	// idiomas
			var newCell0 = newRow.insertCell(0);
			newCell0.align = 'center';
			newCell0.innerHTML = '<a href="javascript:removeRow(\'tidiomas\',3,\'tr'+totRows+'\');"><img src=\"themes/default/img/icons/cancel.png\" alt=\"B\" border=\"0\" />';
			var newCell1 = newRow.insertCell(1);
			newCell1.innerHTML = '<select name="idioma[]" id="idi'+totRows+'" size="1"></select>';
			newCell1.align = 'center';
			var newCell2 = newRow.insertCell(2);
			newCell2.innerHTML = '<select name="oral[]" id="ora'+totRows+'"size="1"></select>';
			newCell2.align = 'center';
			var newCell3 = newRow.insertCell(3);
			newCell3.innerHTML = '<select name="lectura[]" id="lec'+totRows+'" size="1"></select>';
			newCell3.align = 'center';
			var newCell4 = newRow.insertCell(4);
			newCell4.innerHTML = '<select name="escrito[]" id="esc'+totRows+'" size="1"></select>';
			newCell4.align = 'center';
			var newCell5 = newRow.insertCell(5);
			newCell5.innerHTML = '<select name="negocio[]" id="neg'+totRows+'" size="1"></select>';
			newCell5.align = 'center';
			addOption('idi'+totRows,idi_options,idi_ovals);
			addOption('ora'+totRows,niv_options,niv_ovals);
			addOption('lec'+totRows,niv_options,niv_ovals);
			addOption('esc'+totRows,niv_options,niv_ovals);
			addOption('neg'+totRows,niv_options,niv_ovals);
			newRow.id='tr'+totRows;
			break;
		case 3:	// experiencia laboral
			var newCell0 = newRow.insertCell(0);
			newCell0.innerHTML = '<a href="javascript:removeRow(\'tlaboral\',3,\'tr'+totRows+'\');"><img src=\"themes/default/img/icons/cancel.png\" alt=\"B\" border=\"0\" /></a>';
			var newCell1 = newRow.insertCell(1);
			newCell1.innerHTML = '<input type="text" name="empresa[]" value="" size="12" />';
			var newCell2 = newRow.insertCell(2);
			newCell2.innerHTML = '<input type="text" name="fechaini[]" value="" size="6" />';
			var newCell3 = newRow.insertCell(3);
			newCell3.innerHTML = '<input type="text" name="fechafin[]" value="" size="6" />';
			var newCell4 = newRow.insertCell(4);
			newCell4.innerHTML = '<input type="text" name="puestoini[]" value="" size="12" />';
			var newCell5 = newRow.insertCell(5);
			newCell5.innerHTML = '<input type="text" name="puestofin[]" value="" size="12" />';
			var newCell6 = newRow.insertCell(6);
			newCell6.innerHTML = '<input type="text" name="baja[]" value="" size="10" />';
			newRow.id='tr'+totRows;
			break;
		case 4:	// areas de experiencia
			var newCell0 = newRow.insertCell(0);
			newCell0.innerHTML = '<a href="javascript:removeRow(\'texperiencia\',3,\'tr'+totRows+'\');"><img src=\"themes/default/img/icons/cancel.png\" alt=\"B\" border=\"0\" /></a>';
			var newCell1 = newRow.insertCell(1);
			newCell1.innerHTML = '<input type="text" name="area[]" value="" size="55" />';
			var newCell2 = newRow.insertCell(2);
			newCell2.innerHTML = '<input type="text" name="tiempo[]" value="" size="15" />';
			newRow.id='tr'+totRows;
			break;
	}
}

function removeRow(id, tipo, num) {
	var tbl = document.getElementById(id);
	var lastRow = tbl.rows.length;
	switch(tipo) {
		case 1:		// tabla sin encabezado
			if (lastRow > 1) tbl.deleteRow(lastRow - 1);
			break;
		case 2:		// tabla con encabezado
			if (lastRow > 2) tbl.deleteRow(lastRow - 1);
			break;
		case 3:		// un renglon especifico
			var r = document.getElementById(num).rowIndex;
			tbl.deleteRow(r);
			break;
		case 4:		// un renglon especifico y recalcula
			var r = document.getElementById(num).rowIndex;
			tbl.deleteRow(r);
			sumaVenta('partes');
			break;
	}
}

function addOption(id, option, value) {
	if (option.length == value.length) {
		for (var x=0; x < option.length; x++) {
			var objOption = new Option(option[x], value[x]);
			var sel = document.getElementById(id);
			sel.options.add(objOption);
		}
	} else {
		alert('Error: numero de opciones incorrecto. ('+option.length+' != '+value.length+')');
	}
}

//when the dom is ready
window.addEvent('load', function() {
	//store titles and text
	$$('#tipz').each(function(element,index) {
		var content = element.get('title').split('::');
		element.store('tip:title', content[0]);
		element.store('tip:text', content[1]);
	});
	//create the tooltips
	var tipz = new Tips('#tipz',{
		className: 'tipz',
		fixed: true,
		hideDelay: 50,
		showDelay: 50,
		offsets: {'x':0,'y':60}
	});
	
	if ($chk($('codigo_postal_envio'))) {
		$('codigo_postal_envio').addEvent('change',function(event) {
			event.stop();
			var cp = $('codigo_postal_envio').get('value');
			var cpLength = cp.length;
			if (cpLength == 5) {
				var req = new Request.JSON({
					method: 'post',
					url: 'direccionCP.php',
					data: {'codigo_postal':cp},
					onRequest: function() {$('loading').setStyle('display','inline');},
					onSuccess: function(envio) {
						if (envio.resultado == 'exito') {
							$('dir_estado').set('html',envio.estado);
							$('dir_estado_id').set('value',envio.estado_id);
							$('dir_municipio').set('html',envio.municipio);
							$('dir_municipio_id').set('value',envio.municipio_id);
							//$('dir_colonia').set('html',envio.colonias);
							$('colonia').empty();
							var objOption = new Option('Seleccione...', '');
							$('colonia').options.add(objOption);
							$A(envio.colonias).each(function(optionvalue){
								//alert(optionvalue);
								var objOption = new Option(optionvalue, optionvalue);
								$('colonia').options.add(objOption);
							});
						} else {
							alert(envio.texto);
							$('codigo_postal_envio').focus();
						}
						$('loading').setStyle('display','none');
					},
					onComplete: function(response) {$('loading').setStyle('display','none');}
				}).send();
			} else {
				alert ('Error: el código postal debe contener 5 números.');
				$('codigo_postal_envio').focus();
			}
		});
	}
	
	if ($chk($('envio'))) {
		var myFormValidation = new Validate('envio',{
			errorClass: 'red'
		});
	}
	
	if ($chk($('pagar'))) {
		var myFormValidation = new Validate('pagar',{
			errorClass: 'red'
		});
	}
});