// JavaScript Document

function valida_historia(action,id){

	

	var f1 = eval("document.f1"); 

	

	if(f1.nombre ==""){

		alert("Por favor ingrese el nombre del autor de la historia");

		f1.nombre.focus();

		return false;

	}else if(f1.apellidos ==""){

		alert("Por favor ingrese el apellido del autor de la historia");

		f1.apellidos.focus();

		return false;

	}else if(f1.email == ""){

		alert("Por favor ingrese el email del autor de la historia");

		f1.email.focus();

		return false;

	}else if(f1.modelo == ""){

		alert("Por favor ingrese el modelo ");

		f1.modelo.focus();

		return false;

	}else if(f1.axo_fabricacion == ""){

		alert("Por favor ingrese el año de fabricacion");

		f1.axo_fabricacion.focus();

		return false;

	}else if(f1.titulo == ""){

		alert("Por favor ingrese el titulo de la historia");

		f1.titulo.focus();

		return false;

	}else if(f1.contenido ==""){

		alert("Por favor ingrese el contenido de la historia");

		f1.contenido.focus();

		return false;

	}else{ 

		f1.action="cuenta_historia.php?action="+action+"&id="+id;

		f1.submit()

	}}



// JavaScript Document



function mantenimiento(url,id,opcion){

	if(opcion!="delete"){ 

		location.replace(url+'action='+opcion+'&id='+id);			

	}else if(opcion=="delete"){

		if(!confirm("Esta Seguro que desea Eliminar el Registro")){

			return false;	

		}else{

			location.replace(url+'action='+opcion+'&id='+id);			

		}		

	}}



/*

function validar_delete(url){	

	if(!confirm("Esta Seguro que desea Eliminar el Registro")){

		return false;	

	}

}

*/



function validar_delete(){

	if(!confirm("Esta Seguro que desea Eliminar el Registro")){

		return false;	

	}else{

		return true;	

	}	

}



/*

function valida_acceso(login, user){

	if(){

		

	}else if(){

		

	}	

}*/	

	

function validnum(e) { 

	tecla = (document.all) ? e.keyCode : e.which; 

	//alert(tecla)

    if (tecla == 8 || tecla == 46) return true; //Tecla de retroceso (para poder borrar) 

    // dejar la línea de patron que se necesite y borrar el resto 

    //patron =/[A-Za-z]/; // Solo acepta letras 

    patron = /\d/; // Solo acepta números

    //patron = /\w/; // Acepta números y letras 

    //patron = /\D/; // No acepta números 

    // patron = /[\d.-]/; numeros el punto y el signo -

    te = String.fromCharCode(tecla); 

    return patron.test(te);  

	// uso  onKeyPress="return validnum(event)"

}

function validar_categorias(opcion, id1, id){

	var name=document.f1.nombre;



	if(name.value==""){

		alert(" ERROR: Por favor ingrese el nombre de Categoria ");

		name.focus();

		return false

	}	

	if(document.f1.imagen.value=="" && opcion == "addc"){

		alert(" ERROR: Por favor ingrese la imagen de categoria ");

		document.f1.imagen.focus();

		return false	

	}else{

		document.f1.action='productos.php?action='+opcion+'&id1='+id1+'&id='+id

		document.f1.submit();

	}

}



var cantotal = 2

function crearCaracteristicas(){

	

	var cant = cantotal;

	if(typeof document.f1.tcar[0] == 'undefined'){

		type = document.f1.tcar.value;	

	}else if(document.f1.tcar[0].checked == 1){

		type = 0;

	}else {

		type = 1

	}

	if(type == 0){			

		//var contenedor = document.getElementById("carac");

		//var div = document.createElement("div");	 

		//div.id = "car" + cant; 

			

		$("#carac").append("<div id='car"+cant+"' class='divs'></div>");

		 		

			/* para el nombre*/

			$("#car" + cant).append("<label for='nombre_car" + cant+"'>Nombre :</label>"); 

			$("#car" + cant).append("<input type='text' name='nombre_car"+ cant +"' id='nombre_car"+ cant +"'  class='text'>"); 

			$("#car" + cant).append("<a href='#' onclick=\"removerDiv('car"+ cant +"')\" class='remove'> [ c- ] </a>");

		/*	var nombre = document.createElement("input");

			nombre.type = 'text';

			nombre.name = 'nombre_car' + cant;

			nombre.id   = 'nombre_car' + cant; 

			div.appendChild(nombre);	*/

			

			/* para el input file*/	

			$("#car" + cant).append("<label for='img_car" + cant+"'>Imagen :</label>");

			$("#car" + cant).append("<input type='file' name='img_car" + cant+"' id='img_car" + cant+"' class='text'>"); 	

			

		/*	var image = document.createElement("input");

			image.type = 'file';

			image.name = 'img_car' + cant;

			image.id   = 'img_car' + cant; 

			div.appendChild(image);*/

			

			/* para el input file*/	

			$("#car" + cant).append("<label for='desc_car" + cant + "'>Descripcion :</label>");	

			$("#car" + cant).append("<textarea name='desc_car" + cant + "' id='desc_car" + cant + "'> </textarea>");	

		/*	var desc = document.createElement("textarea");

			desc.name= 'desc_car' + cant;

			desc.id  = 'desc_car' + cant;	

			div.appendChild(desc);   */

		   $("#car" + cant).fadeIn('slow');

		   cantotal =  parseInt(cant) + 1;

		   $("#cantidad").val(cantotal); 

	}else{

		

		$("#carac").hide("slow");		

		$("#carac").empty();		

		$("#carac").append("<div  id='car"+cant+"' class='divs'></div>");

		$("#car" + cant).append("<label for='desc_car1'>Descripcion :</label>");	

		$("#car" + cant).append("<textarea name='desc_car1' id='desc_car1'> </textarea>");	

		$("#carac").fadeIn("slow");	

		$("#car" + cant).fadeIn('slow');

		

	}

}





function agregarSeccionFicha(){

	var numsec = $("#cft").val();

	var num = parseInt(numsec)+1;

	$("#sec_"+num).fadeIn('slow');

	$("#cft").val(num);

}



function quitarSeccionFicha(){

	var numsec = $("#cft").val();

	var num = parseInt(numsec)-1;

	$("#sec_"+numsec).fadeOut('slow',function() { $(this).remove();});

	$("#cft").val(num);

}

function quitarFicha(numsec){
	$("#sec_"+numsec).fadeOut('slow',function() { $(this).remove();});
	$("#cft").val(num);
}



function agregarAtributoFicha(num){	 

	var f1 = eval("document.f1")

	var atrinum =  'valor'+num+'[]' ;

	var tot_atrib = document.f1.elements[atrinum];	

	if(typeof tot_atrib == 'undefined'){ 

		var tota = 0;

	}else if(tot_atrib.value == ""){		 		

		var tota = 1; 		

	}else{		 		

		var tota = tot_atrib.length; 		

	};

	

	$("#sec_" + num).append("<div id='atrib" + num +"_"+ tota + "' class='divs_a'></div>");	

	$("#atrib" + num +"_"+ tota).load("carga_atributo.php?num=" + num);

	$("#atrib" + num +"_"+ tota).fadeIn('slow');}



function quitarAtributoFicha(num){

	var atrinum =  'valor'+num+'[]' ;

	var tot_atrib = document.f1.elements[atrinum];	

	

	if(typeof tot_atrib == 'undefined'){ 

		var tota = 0;

	}else if(tot_atrib.value == ""){		 		

		var tota = 0; 		

	}else {		 		

		var tota = parseInt(tot_atrib.length - 1); 

	}	

	var div = "atrib"+ num +"_"+ tota;

	removerDiv(div);

	//alert(div);

}



function removerDiv(HijoE){

	$("#"+HijoE).fadeOut('slow', function() { $(this).remove();}); 

}



function delete_caracteristica(HijoE, id){

	$("#"+HijoE).load("delete_caracteristica.php?id="+id);

	removerDiv(HijoE)

}



$(document).ready( function(){						   

	$("#images a").click( function(){

		var title = $(this).attr("title");

		$("#imgp").hide();

		$("#imgp").attr("src", title).fadeIn('slow'); 

	});	 

	

	$("input[@type=radio][@name=tcar]").click(

		function(){

			$("#carac").empty();	

			$("#cantidad").val(0);

		}														 

	)}); 



 

function delete_imagen(tipo){

	var f1 = eval("document.f1");

	$("#msg_delete").hide();

	if(f1.chkimag.length > 0){

		for(var i=0; i < f1.chkimag.length; i++){

			if(f1.chkimag[i].checked == 1){			

				var id = f1.chkimag[i].value;

				$(".imagen" + id).fadeOut('slow');

				$("#msg_delete").load("delete_imagen.php?id="+id+"&tipo="+tipo).fadeIn("slow");

			}

		}

	}else{

		if(f1.chkimag.checked == 1){			

			var id = f1.chkimag.value;

			$(".imagen" + id).fadeOut('slow');

			$("#msg_delete").load("delete_imagen.php?id="+id).fadeIn("slow");

		}	

	}	 			

}



function valida_imgs(tipo){

	 

	var f1 = eval("document.f1");

	

	if(tipo == "colores"){

		if(f1.color.value == ""){

			alert(" Seleccione un color ");

			f1.color.focus();

			return false;

		}else if(f1.image.value == ""){

			alert(" Ingrese la ruta de la imagen ");

			f1.image.focus();

			return false	

		}	

	}else if(tipo == "galeria"){

		if(f1.image.value == ""){

			alert(" Ingrese la ruta de la Imagen ");

			f1.image.focus();

			return false;	

		}			

	}	

	

	f1.submit();

}



function selecion_noticia(tip,objeto){

	var f1 = eval("document.f1");

	id=f1.elements[objeto].options[f1.elements[objeto].selectedIndex].value;

	titulo=f1.elements[objeto].options[f1.elements[objeto].selectedIndex].text;
	
	tipo=tip;

	if(f1.noticia_home[0].checked==true){

		f1.action="novedad_home.php?action=ver&id_primero="+id+"&titulo_primero="+titulo+"&tipo_primero="+tipo;

		f1.submit();

	}else if(f1.noticia_home[1].checked==true){

		f1.action="novedad_home.php?action=ver&id_segundo="+id+"&titulo_segundo="+titulo+"&tipo_segundo="+tipo;

		f1.submit();

	}else{

		alert("Error: por favor seleccione una opcion");	

	}

}



function validar_opciones(){

	var f1 = eval("document.f1");

	f1.action="novedad_home.php?action=add&valor=visible";

	f1.submit();	

}

function valida_img(){

	//var f1 = eval("document.f1");	

	//var img = f1.elements['image[]'];

	//if(img[0].value == "" || img[1].value == ""){

	//	alert("Error: por favor agregue imagenes !!");	

	//}else{}	

	f1.submit();

}



function seleccion_boletin(ti,obj){

	

	var f1 = eval("document.f1");

			id=f1.elements[obj].options[f1.elements[obj].selectedIndex].value;

			titulo=f1.elements[obj].options[f1.elements[obj].selectedIndex].text;

			tipo=ti;

		if(f1.noticia_home[0].checked==true){

			f1.action="boletines.php?action=edit&id_primero="+id+"&titulo_primero="+titulo+"&tipo_primero="+tipo;

			f1.submit();

		}else if(f1.noticia_home[1].checked==true){

			f1.action="boletines.php?action=edit&id_segundo="+id+"&titulo_segundo="+titulo+"&tipo_segundo="+tipo;

			f1.submit();

		}

		else if(f1.noticia_home[2].checked==true){

			f1.action="boletines.php?action=edit&id_tercero="+id+"&titulo_tercero="+titulo+"&tipo_tercero="+tipo;

			f1.submit();

		}

		else if(f1.noticia_home[3].checked==true){

			f1.action="boletines.php?action=edit&id_cuarto="+id+"&titulo_cuarto="+titulo+"&tipo_cuarto="+tipo;

			f1.submit();

		}

			else{

			alert("Error: por favor seleccione una opcion");	

		}

}



function validar_opciones_boletin(){

	var f1 = eval("document.f1");

	//f1.action="boletines.php?action=add&valor=visible";

	f1.action="boletines.php?action=add";



	f1.submit();	

}



function validar_opciones_boletin_UPDATE(){

	var f1 = eval("document.f1");

	f1.action="boletines.php?action=update";



	f1.submit();	

}



/* catalogo de partes */

num_cp = 0;

function agregarCParchivo(){	 

	var f1 = eval("document.f1") 

	var tot_atrib = document.f1.elements['archivo[]'];	

	

	if(typeof tot_atrib == 'undefined'){ 

		var tota = 0;

	}else if(tot_atrib.value == ""){		 		

		var tota = 1; 		

	}else{		 		

		var tota = tot_atrib.length; 		

	};

	

	$("#cp_archivos").append("<div id='cp_item_"+ tota + "' class='divs_a'></div>");	

	$("#cp_item_"+ tota).append("<label>archivo :</label><input type='file' name='archivo[]' style='width:300px' />"); 

	$("#cp_item_"+ tota).fadeIn("slow");

	

	num_cp ++;

}



function quitarCParchivo(){

	

	var tot_atrib = document.f1.elements['archivo[]'];	

	if(typeof tot_atrib == 'undefined'){ 

		var tota = 0;

	}else if(tot_atrib.value == ""){		 		

		var tota = 0; 		

	}else {		 		

		var tota = parseInt(tot_atrib.length - 1); 

	}	

	var div = "cp_item_"+ tota;

	removerDiv(div);

	num_cp--;

	//alert(div);

}



function delete_archivo_cp(id, div){

	$("#"+div).load("delete_imagen.php?archivo=true&id="+id).fadeIn('slow');	

	//removerDiv(div)

	

}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}