/**
*Procedimientos y funciones de uso general
*
*/
var nav4 = window.event ? true : false;
var tipmage = "";

$( document ).ready(function(){

	
	//solo numeros
	$( "input.onlynumber" ).keypress(function( evt ){
		var key = nav4 ? evt.keyCode : evt.which;
		return /[\d]/.test( String.fromCharCode( key ) );
	});
	
	//monetario
	$( "input.money" ).keyup(function(){
		this.value = number_format( this.value );
	});
	
	//solo letras
	$( "input.onlyword" ).keypress(function(){
		var key = nav4 ? evt.keyCode : evt.which;
		return /[\w]/.test( String.fromCharCode( key ) );
	});
	
	$( "form.formvalida" ).submit(function(){  return EvaluaReg( this ) });
	
	/*
	$( ".menu ul li a" ).click(function(){  
		$( ".subnav2" ).hide();
		$( "." + $(this).attr("rel") ).show();
	});
	*/
	$(".menu").webwidget_vertical_menu({
		/*
		menu_width: '160',
		menu_height: '25',
		menu_margin: '2',
		menu_text_size: '12',
		menu_text_color: '#CCC',
		menu_background_color: '#666',
		menu_border_size: '2',
		menu_border_color: '#000',
		menu_border_style: 'solid',
		menu_background_hover_color: '#999',
		*/
		directory: 'images'
	});


	$( ".btnGaleria" ).click(function(){  
		$( "#imgPrincipal" ).attr( "src", $(this).attr("rev") );
		$( "#imgPrincipal" ).parent().attr( "href", $(this).attr("rev") );
		$( "#cloud-zoom-big" ).css( "background-image", "url(" + $(this).attr("rev") + ")" );
		
	});
	
	$( ".addCart" ).click(function(){ 
		var cuero = $( "#idcuero" ).val();
		var talla = $( "#idtalla" ).val();
		var color = $( "#idcolor" ).val();
		var categoria = $( "#idcategoria" ).val();
		var cantidad = $( "#Cantidad" ).val();
		var tipo = $( "#Tipo" ).val();
		var producto = $( "#idproducto" ).val();
		var IDPuntoVentaAlmacenes = $( "#IDPuntoVentaAlmacenes" ).val();
		agregarCarro( $(this).attr("rel"), cuero, talla, color, categoria, cantidad ,tipo, producto,IDPuntoVentaAlmacenes);
	});
	
	verificacarro();
	
	
	$( ".btnViewCart" ).click(function(){  
		location.href = "viewcart.php";
		return false;
	});
	
});


function addNotify()
{
	if( !window.contactonotify )
	{
		window.contactonotify = $( '<div id="volatilnotif" class="info_notify">Obteniendo datos...</div>' )
					.css({ 
							"position":"absolute",
							"top" : "1200px",
							"left" : "650px",
							"width" : "400px",
							"height" : "50px",
							"z-index" : "10"
				}).get();
				
		$( document.body ).append( window.contactonotify );
	}
	else
		$( window.contactonotify ).show();
		
	setTimeout( function(){$( "#volatilnotif" ).fadeOut( "slow" )} , 600 );	
}

function agregarCarro( idproducto , cuero , talla, color, categoria ,cantidad,tipo,producto,IDPuntoVentaAlmacenes)
{
	jQuery.ajax( {
		"type" : "POST",
		"data" : { "IDProducto" :idproducto , "IDCuero" : cuero , "IDTalla" : talla, "IDColor": color, "Cantidad": cantidad, "IDPuntoVentaAlmacenes": IDPuntoVentaAlmacenes },
		"dataType" : "json",
		"url" : "addcart.php" ,
		"beforeSend" : function(){
			
			$( "#contentBotones" ).html( "<img src='img/progress_bar.gif' >" );
			
		},
		"success" : function( data ){
			data = data.column;
		
			if( data.carrook )
			{
				if(tipo == 'Proveedor')
					location.href = "detalle.php?idc="+categoria+"&id="+producto+"";
				else
					$( "#contentBotones" ).html( "Producto Agregado Correctamente!  <a href='viewcart.php?idc="+categoria+"'><img src='img/btn_detalle_carrito.jpg' /></a>"  );
			}
			else
			{
				if(tipo == 'Proveedor')
					location.href = "detalle.php?idc="+categoria+"&id="+producto+"";
				else
					$( "#contentBotones" ).html( "Hubo un error y el producto no pudo ser agregado!" );
			}
			
		},	
		"complete" : function(xhr, status) {
			//alert( 'complete' );
			if(tipo == 'Proveedor')
				location.href = "detalle.php?idc="+categoria+"&id="+producto+"";
			else
				$( "#contentBotones" ).html( "Producto Agregado Correctamente!  <a href='viewcart.php?idc="+categoria+"'><img src='img/btn_detalle_carrito.jpg' /></a>"  );
			
			
			verificacarro();
		}

	});
	
	return false;
}//en function

function verificacarro(  )
{
	jQuery.ajax({
	   type: "POST",
	   url: "verifycart.php",		   
	   success: function(data){
		$( "#cartValue" ).val( data );
	   },
	   error : function( xhr, status ){
			$( "#cartValue" ).val( status );
			console.info( xhr );
	   }
	});
	
	return false;
}


