function obtenNumero(objName) {
    var numero = "";
    var caracteres = "0123456789.";    
    var obj = document.getElementById(objName);
    if (obj) {
        var valor = obj.value;
        for (var i = 0; i < valor.length; i++) {
            var digito = String(valor.charAt(i));            
            if (caracteres.indexOf(digito) != -1) {                
                numero = numero.concat(digito);
            }         
        }                
        obj.value = numero;
    }
    return true;
}

function validaEmail(objName, name) {
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    var obj = document.getElementById(objName);
    if (obj) {
        var texto = obj.value;
        if (texto == "") { return true; }
        if(texto.match(emailExp)) {
            return true;
        } else {
            alert("Por favor ingrese correctamente el valor de " + name);
            obj.focus();
            return false;
        }
    } else {
        return false;
    }
}

function validaYear(objName, name) {
    var obj = document.getElementById(objName);
    if (obj) {
        var texto = obj.value;
	if (texto) {
        if(/^-?\d+$/.test(texto)) {
            if (texto.length == 4) {
                if (texto >= 1900) {
                    return true;
                } else {
                    alert(name + " no puede ser menor a 1900");
                    obj.focus();
                    return false;
                }
            } else {
                alert(name + " debe ser de cuatro digitos");
                obj.focus();
                return false;
            }
        } else {
            alert(name + " debe ser numerico");
            obj.focus();
            return false;
        }
	} else {
	    return true;
	}
    } else {
        return false;
    }
}

function validaFormato(objName, name, type) {
    var obj = document.getElementById(objName);
    var valido = false;
    if (obj) {
        switch (type) {
            case "email": valido = validaEmail(objName, name);
            break;
            case "year": valido = validaYear(objName, name);
            break;
            default: valido = false;
            break;
        }        
    } else {
        return false;
    }
    return valido;
}

function validaTextoRequerido(objName, name) {
	var obj = document.getElementById(objName);
	if (obj.value) {
		if (obj.value != "") {
			return true;
		}
	}
	alert(name + " es requerido.");
	obj.focus();
	return false;
}

function validaCheckRequerido(formName, objName, name) {
	var obj = eval("(document.getElementById('"+formName+"'))."+objName);
	if (obj.length != undefined) {
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked) {
				return true;
			}
		}
	} else {
		if (obj.checked) {
			return true;
		}
	}
	
	alert("Debe elegir al menos una opcion para " + name);
	return false;
}

function validaListaRequerida(objName, name) {
	var obj = document.getElementById(objName);
	if (obj) {
		if (obj.selectedIndex > -1) {
			return true;
		} else {
			alert("Debe elegir al menos un valor para " + name);
			obj.focus();
			return false;
		}
	}
	return false;
}

function validaRequerido(formName, objName, name, type) {
	var valido = false;
	switch (type) {
		case "texto": valido = validaTextoRequerido(objName, name);
		break;
		case "check": valido = validaCheckRequerido(formName, objName, name);
		break;	
		case "lista": valido = validaListaRequerida(objName, name);
		break;	
		default: valido = false;
		break;
	}
	
	if (valido) {
		return true;
	} else {
		return false;
	}
}

function validaPassword(obj1Name, obj2Name, name) {
	var obj1 = document.getElementById(obj1Name);
	var obj2 = document.getElementById(obj2Name);
	if (obj1 && obj2) {
		if (obj1.value == obj2.value) {
			return true;
		} else {
			alert("Debe introducir los mismos datos para " + name);
			obj1.focus();
			return false;
		}
	}
	return false;
}

function validaAgregarUsuario(formName) {
	if (validaRequerido(formName, "contacto","Contacto", "texto")) {

            if (validaFormato("email","Correo-e", "email")) {
                if (validaRequerido(formName, "tipo", "Tipo de Usuario", "check")) {
                    if (validaRequerido(formName, "status","Status de Usuario", "lista")) {
                        if (validaPassword("password", "password_ver", "Contraseña")) {
                            form = document.getElementById(formName);
                            form.submit();
                        }
                    }
                }
            }

	}
}

function validaAgregarLote(formName) {
	if (validaRequerido(formName, "nombre","Nombre", "texto")) {
		if (validaRequerido(formName, "descripcion","Descripcion", "texto")) {
			if (validaRequerido(formName, "estado", "Estado", "lista")) {
				form = document.getElementById(formName);
                form.enctype = "multipart/form-data";
                form.encoding = "multipart/form-data";
				form.submit();	
			}
		}
	}
}

function validaAgregarVehiculo(formName) {    
	if (validaRequerido(formName, "contacto","Contacto", "texto")) {
		if (validaRequerido(formName, "telefono","Teléfono", "texto")) {				
				if (validaFormato("email","Correo Electrónico", "email")) {
					if (validaRequerido(formName, "estado", "Estado", "lista")) {
						if (validaRequerido(formName, "marca", "Marca", "lista")) {
							if (validaRequerido(formName, "modelo", "Modelo", "lista")) {
                                                            if (validaRequerido(formName, "tipo_v", "Tipo", "lista")) {
                                                                if (validaRequerido(formName, "subtipo_v", "Subtipo", "lista")) {
                                                                    if (validaRequerido(formName, "transmision", "Transmisión", "lista")) {
									if (validaRequerido(formName, "motor", "Motor", "lista")) {
                                                                            
											if (validaRequerido(formName, "puerta", "Puerta", "lista")) {												
													                                                    if (validaFormato("anio", "Año", "year")) {														
															if (validaRequerido(formName, "status", "Status", "lista")) {
                                                                obtenNumero("kilometros");
                                                                obtenNumero("precio");
																form = document.getElementById(formName);
																form.enctype = "multipart/form-data";
                                                                form.encoding = "multipart/form-data";
																form.submit();
															}														
                                                    
												}
                                                                                            }
											}
                                                                                    }
										
                                                                        }
								}
							}
						}
					}
				}			
		}
	}
}

function validaAgregarCatalogo(formName) {
    if (validaRequerido(formName, "_value", "Valor Nuevo", "texto")) {
        form = document.getElementById(formName);
		form.submit();
    }
}

function validaRangoSelect(select1Name, select2Name, name) {
    var s1 = document.getElementById(select1Name);
    var s2 = document.getElementById(select2Name);
    if (s1 && s2) {
        var o1 = s1.options[s1.selectedIndex];
        var o2 = s2.options[s2.selectedIndex];
        if (o1 && o2) {
            var v1 = o1.value;
            var v2 = o2.value;
            if (v1 <= v2) {
                return true;
            } else {
                alert("El primer valor debe ser menor al segundo para " + name);
                s1.focus();
                return false;
            }
        }
    }
    return false;
}

function validaSeleccionTipo(formName, tipoId) {
    var form = document.getElementById(formName);
    var tipo = document.getElementById('tipo_v');
    var subtipo = document.getElementById('subtipo_v');
    tipo.value = tipoId;
    subtipo.value = '';
    form.action = 'index.php';
    form.submit();    
}

function validaSeleccionSubTipo(formName, tipoId, subtipoId) {
    var form = document.getElementById(formName);
    var tipo = document.getElementById('tipo_v');
    var subtipo = document.getElementById('subtipo_v');
    tipo.value = tipoId;
    subtipo.value = subtipoId;
    form.action = 'index.php';
    form.submit();
}

function validaBuscarEnTianguis(formName) {
    if (validaRangoSelect("a_desde", "a_hasta", "Año")) {
        //if (validaRangoSelect("k_desde", "k_hasta", "Kilometros")) {            
            var form = document.getElementById(formName);
            form.submit();
        //}
    }
}

function validaBusquedaAvanzada(formName) {
    if (validaRangoSelect("a_desde", "a_hasta", "Año")) {
        //if (validaRangoSelect("k_desde", "k_hasta", "Kilometros")) {
            if (validaRangoSelect("p_desde", "p_hasta", "Precio")) {
                var form = document.getElementById(formName);
                form.submit();
            }
        //}
    }
}

function validaBuscarEnLote(formName) {
    var id = document.getElementById("_loteId");
    var select = document.getElementById("lote");
    id.value = select.options[select.selectedIndex].value;    
    var form = document.getElementById(formName);
    form.submit();        
}

function validaContacto(formName) {
    if (validaRequerido(formName, "nombre", "Nombre", "texto")) {
        if (validaRequerido(formName, "direccion", "Direccion", "texto")) {
            if (validaRequerido(formName, "telefono", "Telefono", "texto")) {
                if (validaRequerido(formName, "estado", "Estado", "texto")) {
                    if (validaRequerido(formName, "ciudad", "Ciudad", "texto")) {
                        if (validaRequerido(formName, "to", "to", "lista")) {
                            if (validaRequerido(formName, "body", "Comentarios", "texto")) {
                                var form = document.getElementById(formName);
                                form.submit();
                            }
                        }
                    }
                }
            }
        }
    }
}
