function calcula_total_unit(id){

	var nombre_campo='cantidad_'+id;

	eval("var cantidad=document.frm.cantidad_"+id+".value;");
	eval("var coste_unit=document.frm.coste_unit_"+id+".value;");
	eval("var total_unit=document.frm.total_unit_"+id+";");

	if (es_numero(nombre_campo)){

		total_unit.value=cantidad*coste_unit;


	} else {

		total_unit.value='';

	}

	calcular_totales();

}

function es_numero(campo){


	eval("var numero=document.frm."+campo+".value;");

	if (isNaN(numero)){

		alert('Error: debe introducir un numero válido. Decimales con un punto');

		eval("document.frm."+campo+".value='';");

		return false;

	} else {

		return true;

	}

}

function suma_total_unit(){

	var suma=new Number(0);

	for(i=0;ele=document.frm.elements[i];i++){

		var pos=ele.id.indexOf('total_unit_');

		if(pos=='0'){

	     	 suma=suma+Number(ele.value);
		}
	}

	return suma;

}

function fijar_pax(campo){

	if (es_numero(campo)){

		eval("var pax=document.frm."+campo+".value;");

		document.frm.cantidad_1.value=pax;
		calcula_total_unit('1');
		document.frm.total_costes_pax.value=pax;
		document.frm.coste_pp_pax.value=pax;
		document.frm.pvp_pax.value=pax;
		document.frm.margen_pax.value=pax;

		calcular_totales();


	}


}

function calcular_totales(){

	document.frm.total_costes_unit.value	= suma_total_unit();
	document.frm.coste_pp_total_unit.value	= document.frm.total_costes_unit.value/document.frm.pax.value;
	document.frm.coste_pp_total.value		= document.frm.coste_pp_total_unit.value*document.frm.coste_pp_pax.value;
	document.frm.pvp_total_unit.value		= Number(document.frm.coste_pp_total_unit.value)+Number(document.frm.margen_total_unit.value);
	document.frm.pvp_total.value			= document.frm.pvp_total_unit.value*document.frm.pvp_pax.value;
	document.frm.margen_total.value			= document.frm.pvp_total.value-document.frm.coste_pp_total.value;


	var val1=new String(document.frm.coste_pp_total_unit.value);
	var pos_punto=val1.indexOf('.','0');
	var num_float=pos_punto + 1+ 2;
	document.frm.coste_pp_total_unit.value = val1.substr(0,num_float);

	var val2=new String(document.frm.coste_pp_total.value);
	var pos_punto2=val2.indexOf('.','0');
	if (pos_punto2!=-1){
		var num_float2=pos_punto2 + 1+ 2;
		document.frm.coste_pp_total.value = val2.substr(0,num_float2);
	}

	var val3=new String(document.frm.pvp_total_unit.value);
	var pos_punto3=val3.indexOf('.','0');
	if (pos_punto3!=-1){
		var num_float3=pos_punto3 + 1+ 2;
		document.frm.pvp_total_unit.value = val3.substr(0,num_float3);
	}

	var val4=new String(document.frm.pvp_total.value);
	var pos_punto4=val4.indexOf('.','0');
	if (pos_punto4!=-1){
		var num_float4=pos_punto4 + 1+ 2;
		document.frm.pvp_total.value = val4.substr(0,num_float4);
	}

	var val5=new String(document.frm.margen_total.value);
	var pos_punto5=val5.indexOf('.','0');
	if (pos_punto5!=-1){
		var num_float5=pos_punto5 + 1+ 2;
		document.frm.margen_total.value = val5.substr(0,num_float5);
	}

}

function paso1_tabla_costes(){

	if ((es_numero('num_pax')) && (document.frm.actividades_id.value!=0)&& (document.frm.tipo_agencia.value!=0)&& (document.frm.num_pax.value!='')){


		cargar_tabla_costes(document.frm.num_pax.value,document.frm.tipo_agencia.value,document.frm.actividades_id.value);
		/*
		document.frm.tipo_agencia.disabled=true;
		document.frm.num_pax.disabled=true; */

	}else {

		alert('Rellene los campos correctamente');

	}
}

function paso1_tabla_presupuestos(alerta){

	if ((document.frm.actividades_id.value!=0) && (document.frm.localizaciones_id.value!=0)){

		cargar_tabla_presupuestos(document.frm.proyectos_id.value,document.frm.actividades_id.value,document.frm.presupuestos_id.value);

	}else {

		alert(alerta);

	}
}

function paso1_tabla_cantidades(){

	if ((es_numero('num_pax')) && (document.frm.actividades_id.value!=0) && (document.frm.num_pax.value!='')){


		cargar_tabla_costes(document.frm.num_pax.value,document.frm.tipo_agencia.value,document.frm.actividades_id.value);
		/*
		document.frm.tipo_agencia.disabled=true;
		document.frm.num_pax.disabled=true; */

	}else {

		alert('Rellene los campos correctamente');

	}
}


function insertar_extra(){

	document.frm.contador_extra.value++;
	var v=document.frm.contador_extra.value;
	var nombre='fila_extra_'+v;
	var cadenaa="<tr id='"+nombre+"'><td><a href='javascript:void(null)' onclick=\'Element.remove(\""+nombre+"\");\'>hola "+v+"<\/a><\/td><\/tr>";

	var cadena="<tr class='linea_concepto' id='fila_extra_"+v+"'>";
	cadena +="<td align='right'><input type='text' name='nombre_extra_"+v+"' class='caja_cantidad_larga' tabindex='"+v+"001'  /></td>";
	cadena +="<td align='right'><input type='text' name='cantidad_extra_"+v+"' class='caja_cantidad' onchange='calcula_total_unit(\"extra_"+v+"\");es_numero(\"cantidad_extra_"+v+"\")' tabindex='"+v+"002' /></td>";
	cadena +="<td align='right'><input type='text' name='coste_unit_extra_"+v+"' class='caja_cantidad' onchange='calcula_total_unit(\"extra_"+v+"\");es_numero(\"coste_unit_extra_"+v+"\")' tabindex='"+v+"003' /></td>";
	cadena +="<td align='right'><input type='text' name='total_unit_extra_"+v+"' id='total_unit_extra_"+v+"' class='caja_trans' readonly='true' /></td>";
	cadena +="<td align='left' colspan='2'><a class='negrita' href='javascript:void(null)' onclick=\'Element.remove(\""+nombre+"\");calcular_totales();\'>Eliminar</a></td>";
	cadena +="<td align='right'><input type='text' name='notas_extra_"+v+"' class='caja_cantidad_larga' tabindex='"+v+"004'  /></td>";
	cadena +="</tr>";


	new Insertion.After('titulo_conceptos_extra',cadena);
	return false;


}

function sendFormPresupuesto(){

//aqui, comprobare los datos, q... creo q solo es los extras vacios

document.frm.submit();


}


function cargarContenido(){
	var comentarios;
	var mensaje;

	comentarios = document.getElementById('comentarios');
	mensaje = document.getElementById('mensaje').value;
	req = new ajax();
	req.open('POST', 'guarda_comentarios.php',true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	req.send('mensaje='+mensaje);

	document.getElementById('mensaje').value='';

	req.onreadystatechange=function() {
		if (req.readyState==4) {
			comentarios.innerHTML = req.responseText;

		}
	}
}

function mostrarContenido(){
	comentarios = document.getElementById('comentarios');
	req = new ajax();
	req.open('POST', 'guarda_comentarios.php', true);
	req.setRequestHeader('Content-Type', 'text/plain');
	req.send('leer=true');
	req.onreadystatechange=function() {
		if (req.readyState==4) {
			comentarios.innerHTML = req.responseText;
		}
	}
}


//this function will check password security based on a
//points system
//less than 6 characters = -1 point
//all numerals or all digits = -1 point
//no special characters = -1 point


function check_pass_security(password, updatefield, img_base, img_activa)
{
        document.getElementById('seguridad').style.display='inline';
        var pass=password;
        var score = 4;
        var result;

        if(pass.length < 6){
                score = score - 1;
        }

        if(!pass.match(/[a-z_]/i) || !pass.match(/[0-9]/)){
                score = score - 1;
        }

        if(!pass.match(/\W/)){
                score = score - 1;
        }

        if (pass.length==0){

        	score=0;
        }

        if(score == 4){
                result ="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";

        } else if(score == 3){
                result = "<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";

        } else if(score == 2){
                result = "<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";

        } else if(score == 1){
                result = "<img src='"+img_activa+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";

        } else if(score == 0){
                result = "<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";
                result +="<img src='"+img_base+"' alt='' />";

        }

        document.getElementById(updatefield).innerHTML = result;

        return;
}

