/*--------LISTA DE PRECIOS------------------*/
function initLp(tipo){
	$$('.num').each(function(el){
    	el.addEvents({
		    'keyup': function(evt){
		    	if(!isInteger(el.value )){
		    		el.value = el.value.substr(0,el.value.length-1)
		    	}
		    }
		});

	});
	loadPedido(tipo)
	initPopUpMsg()
	initTooltip()
}

function initTooltip(){
	$$('.tt').each(function(el){
		el.tooltip ('<img src="'+el.name+'" alt="" />', { mode: 'auto', width: 160, style: 'alert'});
	})
		
}



function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}



var lastSearch=""
function search(value){
	value = value.toUpperCase();
	input = $('search-input');
	
	$$('.idArt').some(function(el){
	var content       = el.get('html').toUpperCase();
		var	contentSearch = content.substr(0,value.length).toUpperCase();
		if(contentSearch==value){
			if(lastSearch!="") lastSearch.removeClass("trFind");
			input.removeClass("searchNotFound");
			
			var ypos = el.getPosition().y - 30;
			window.scrollTo(0, ypos);

			lastSearch = el.getParent("tr");
			lastSearch.addClass("trFind");
			return true;
		}else{
			if(lastSearch!="") lastSearch.removeClass("trFind");
				
			input.addClass("searchNotFound");  			
		}
	});
	
}

function addItem(id, tipo, desc){
	if(!isInteger($('cant_'+id).value)){
		return 
	}
	comment =  $('comment_'+id).get("value")
	
	
	var ajax = new Request({
         method: 'post',
		 url:    "listaprecios/addItem.php",
		 data:   'idProd='+id+'&tipo='+tipo+'&cantidad='+$('cant_'+id).value+'&desc='+desc+'&comment='+comment,


		 onRequest: function(){},

         onSuccess: function(result){
			var data=result.split("|")
			
			$('bottom-menu-middle').set('html', "+ "+data[1]+"  ARTICULOS "+data[2]+" AGREGADOS +")
			$('top-totalCarrito').set('html', data[3])
			$('menu-bottom-totalCarrito').set('html', data[3])
			$('menu-bottom-cantidadCarrito').set('html', data[4])
			$('top-cantidadCarrito').set('html', data[4])
			
	 	},
		 
         onFailure: function(){alert('Error al agregar Articulo:'+id);}
		 
      }).send();  
}

function loadPedido(tipo){
	var request = new Request.JSON({
			url: 'carrito/json.items.php',
			data: 'tipo='+tipo,
			onComplete: function(jsonObj) {
				updatePedido(jsonObj);
			}
		}).send();
}

function updatePedido(data){
	for( var i in data){
		
		if($('cant_'+data[i].ID)!=null)
			$('cant_'+data[i].ID).set('value', data[i].CANTIDAD)
		
		if($('subtotal_'+data[i].ID)!=null)
			$('subtotal_'+data[i].ID).set('value', data[i].SUBTOTAL)
		
		
		if($('comment_'+data[i].ID)!=null)
			$('comment_'+data[i].ID).set('value', data[i].COMMENT)
			
		//alert(data[i].ID)
	}
}

function initPopUpMsg(){
	if($('popupMsg')!=null){
		$('popupMsg').addEvents({
		    'click': function(evt){
			$('popupMsg').dispose();
		    }
		});
	}
}

// COMMENTS
function showCommentArea(id, art){
	$('comment_id').set("value", id)
	$('comment_art').set("html", art)
	$('comment_desc').set("value", $('comment_'+id).get("value"))
	$('popupComment').setStyle("visibility", "visible")
}
function hideCommentArea(){
	$('popupComment').setStyle("visibility", "hidden")
}

function saveComment(){
	id = $('comment_id').get("value")
	$('comment_'+id).set("value", $('comment_desc').get("value"))
	hideCommentArea()
}


/*--------------------------*/

function equalHieght(){
	var height = 0
			var Element = $$('.cols div');
				Element.each(function(item){
					if(item.getStyle('height').toInt() > height){
						height = item.getStyle('height').toInt()
					}
				});
				Element.each(function(item){
					item.setStyle('height', height);
				});
	
}
/*-----------LOGIN TOP---------------*/

function onFocusHandler_p(fld){
	fld.style.display = 'none';
	var fld2 = (document.getElementById) ? document.getElementById('password') : document.all['password'];
	fld2.style.display = 'inline';
	fld2.focus();
	return true;
}
function onBlurHandler_p(fld){
	if(fld.value === ''){
		fld.style.display = 'none';
		var fld2 = (document.getElementById) ? document.getElementById('fauxPassword') : document.all['fauxPassword'];
		fld2.style.display = 'inline';
	}
	return true;
}

/*-------CARRITO----------*/
function removeItem(link){
	if(confirm("Esta seguro que desea quitar el artículo")) {
		window.location = link
	}
}

/*--------------------------*/

function hide(el2, el){
	el2.setStyle('display', 'none');
}
function show(el2, el){
	el2.setStyle('display', '');

}

var DropdownMenu = new Class({	
	initialize: function(element)
	{
		
		$A($(element).childNodes).each(function(el)
		{
			if(el.nodeName.toLowerCase() == 'li')
			{
				$A($(el).childNodes).each(function(el2)
				{
					if(el2.nodeName.toLowerCase() == 'ul')
					{
						
						hide($(el2));
						
						el.addEvent('mouseover', function()
						{
							show($(el2), el);
							return false;
						});

						el.addEvent('mouseout', function()
						{
							hide($(el2), el);
						});
						new DropdownMenu(el2);
					}
				});
			}
		});
		return this;
	}
});

window.addEvent('domready', function() {
	new DropdownMenu($('dropdownMenu'))
});
