
function check_tipocuenta() {
	var src = document.getElementById('tipocuenta');
	if (!src) {
		return;
	}
	var dst = document.getElementById('edit-rangoinversion-wrapper');
	if (!dst) {
		return;
	}
	if (src.options[src.selectedIndex].value == 'proveedor') {
		if (dst.style) {
			dst.style.display = 'none';
		} else {
			dst.display = 'none';
		}
	} else {
		if (dst.style) {
			dst.style.display = 'inherit';
		} else {
			dst.display = 'inherit';
		}
	}
}

/** 
 * Redondea especificando un numero de decimales
 *
 * @param	float	num		Numero a redondear
 * @param	int		dec		Numero de decimales
 *
 * @return	float			Numero redondeado
 */
function roundFloat(num, dec) { 
	return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}

function update_valoracion() {
	var ov1  = document.getElementById('valoracion_1');
	var ov2  = document.getElementById('valoracion_2');
	var ov3  = document.getElementById('valoracion_3');
	var ov4  = document.getElementById('valoracion_4');
	var ov5  = document.getElementById('valoracion_5');
	var ov6  = document.getElementById('valoracion_6');
	var ov7  = document.getElementById('valoracion_7');
	var ov8  = document.getElementById('valoracion_8');
	var ov9  = document.getElementById('valoracion_9');
	var ov10 = document.getElementById('valoracion_10');
	var ov	 = document.getElementById('valoracion');

	if (!ov1 || !ov2 || !ov3 || !ov4 || !ov5 || !ov6 || !ov7 || !ov8 || !ov9 || !ov10 || !ov) {
		return false;
	}
	var v1  = parseInt(ov1.options[ov1.selectedIndex].value);
	var v2  = parseInt(ov2.options[ov2.selectedIndex].value);
	var v3  = parseInt(ov3.options[ov3.selectedIndex].value);
	var v4  = parseInt(ov4.options[ov4.selectedIndex].value);
	var v5  = parseInt(ov5.options[ov5.selectedIndex].value);
	var v6  = parseInt(ov6.options[ov6.selectedIndex].value);
	var v7  = parseInt(ov7.options[ov7.selectedIndex].value);
	var v8  = parseInt(ov8.options[ov8.selectedIndex].value);
	var v9  = parseInt(ov9.options[ov9.selectedIndex].value);
	var v10 = parseInt(ov10.options[ov10.selectedIndex].value);

	ov.value = roundFloat((v1+v2+v3+v4+v5+v6+v7+v8+v9+v10)/10,2);
}

function refresh_divisa(obj) {
	if (!obj) {
		return false;
	}
	var id = obj.options[obj.selectedIndex].value;
	location = '?divisa='+id;
}

function check_required(id) {
	if (!id) {
		return false;
	}
	var obj = document.getElementById(id);
	if (!obj) {
		return false;
	}
	var tid = "_"+id;
	var tobj = document.getElementById(tid);
	if (!tobj) {
		return false;
	}
	var hid = id+"_hidden";
	var hobj = document.getElementById(hid);
	if (hobj) {
		if (formValObj && formValObj.__validateForm) {
			formValObj.__validateForm();
		}
		if (!obj.value || !hobj.value) {
			tobj.innerHTML = '<div style="height: 27px;" class="DHTMLSuite_validationImage DHTMLSuite_invalidInputImage"></div>';
			if (disableSubmit && !document.getElementById('mySubmit').disabled) {
				disableSubmit();
			}
		} else {
			tobj.innerHTML = '<div style="height: 27px;" class="DHTMLSuite_validationImage DHTMLSuite_validInputImage"></div>';
		}
	} else {
		if (formValObj && formValObj.__validateForm) {
			formValObj.__validateForm();
		}
		if (!obj.value) {
			tobj.innerHTML = '<div style="height: 27px;" class="DHTMLSuite_validationImage DHTMLSuite_invalidInputImage"></div>';
			if (disableSubmit && !document.getElementById('mySubmit').disabled) {
				disableSubmit();
			}
		} else {
			tobj.innerHTML = '<div style="height: 27px;" class="DHTMLSuite_validationImage DHTMLSuite_validInputImage"></div>';
		}
	}
}

function initPestanas(mainid) {
    if (!mainid) {
        alert('Debe especificar un ID');
        return false;
    }
    var mod = document.getElementById(mainid);
    if (!mod) {
        alert('No se ha podido encontrar el modulo');
        return false;
    }

    var objs = mod.getElementsByTagName('span');
    if (objs && objs.length) {
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className == 'pestana') {
                objs[i].onclick = function() {
                    activaPestana(mainid, this.id);
                }
                var id  = objs[i].id.substring(1);
                var obj = document.getElementById(id);
                if (obj && obj.style) {
                    obj.style.display = 'none';
                }
            } else {
                objs[i].onclick = '';
                var id  = objs[i].id.substring(1);
                var obj = document.getElementById(id);
                if (obj && obj.style) {
                    obj.style.display = 'block';
                }
            }
        }
    }
}

function activaPestana(mainid, id) {
    if (!mainid || !id) {
        return false;
    }
	var mod = document.getElementById(mainid);
    if (!mod) {
        alert('No se ha podido encontrar el modulo');
        return false;
    }
    var objs = mod.getElementsByTagName('span');
    if (objs && objs.length) {

        // Verificamos que el DIV destino existe
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className=='pestana' || objs[i].className=='pestana sel') {
                var idcontent = objs[i].id.substring(1);
                var obj       = document.getElementById(idcontent);
                if (objs[i].id == id && !obj) {
                    alert('No se ha encontrado el DIV del contenido');
                    return false;
                }
            }
        }

        // Todo OK
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className=='pestana' || objs[i].className=='pestana sel') {
                var idcontent = objs[i].id.substring(1);
                var obj       = document.getElementById(idcontent);
                if (objs[i].id == id) {
                    if (obj && obj.style) {
                        obj.style.display = 'block';
                    }
                    objs[i].className = 'pestana sel';
                    objs[i].onclick = null;
                } else {
                    if (obj && obj.style) {
                        obj.style.display = 'none';
                    }
                    objs[i].className = 'pestana';
                    objs[i].onclick = function() {
                        activaPestana(mainid, this.id);
                    }
                }
            }
        }
    }
}

var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad(str, len, pad, dir) {

    if (typeof(len) == "undefined") { var len = 0; }
    if (typeof(pad) == "undefined") { var pad = ' '; }
    if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }

	str = ""+str+""

    if (len + 1 >= str.length) {
        switch (dir){
            case STR_PAD_LEFT:
                str = Array(len + 1 - str.length).join(pad) + str;
            	break;

            case STR_PAD_BOTH:
                var right = Math.ceil((padlen = len - str.length) / 2);
                var left = padlen - right;
                str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
            	break;

            default:
                str = str + Array(len + 1 - str.length).join(pad);
            	break;
        }
    }
    return str;
}

function confirmar(url) {
	if (!confirm('Seguro que desea realizar esta operación?')) {
		return;
	}
	location = url;
}

function check_form_alta(formid) {
	var form = document.getElementById(formid);
	if (!form) {
		alert('No se ha detectado el formulario '+formid);
		return false;
	}
	if (!form.nombre.value || form.nombre.value == 'Nombre') {
		alert('Debe indicar el nombre');
		form.nombre.focus();
		return false;
	}
	if (!form.apellidos.value || form.apellidos.value == 'Apellidos') {
		alert('Debe indicar los apellidos');
		form.apellidos.focus();
		return false;
	}
	if (!form.emailcontacto.value || form.emailcontacto.value == 'emailcontacto') {
		alert('Debe indicar el email');
		form.emailcontacto.focus();
		return false;
	}
	if (!form.nacion.value || form.nacion.value == 'nacion') {
		alert('Debe indicar su pais');
		form.nacion.focus();
		return false;
	}
	if (!form.provincia.value || form.provincia.value == 'provincia') {
		alert('Debe indicar su provincia');
		form.provincia.focus();
		return false;
	}
	if (!form.login.value || form.login.value == 'usuario') {
		alert('Debe indicar su login');
		form.login.focus();
		return false;
	}
	if (!form.pass.value || form.pass.value == 'contraseña') {
		alert('Debe indicar su contraseña');
		form.pass.focus();
		return false;
	}
	if (!form.DeseoInfoCarta2.checked) {
		alert('Debe aceptar el acuerdo de confidencialidad y protección de datos');
		form.pass.focus();
		return false;
	}

	return true;
}
