var http = getXmlHttpObject();

function is_empty(str)
{
	return ( str==null || str==0 || str=="" || str.length < 1 );
} // is_empty

function validateRegister()
{
	var nombre = document.getElementById('nombre').value; 
	var telf = document.getElementById('telf').value; 
	var fax= document.getElementById('fax').value; 
	var mail = document.getElementById('mail').value; 
	var login = document.getElementById('login').value; 
	var passw = document.getElementById('passw').value; 
	var repassw = document.getElementById('repassw').value; 
	var direccion = document.getElementById('direccion').value; 
	var valid_mail = /[a-zA-Z0-9._-]+@+[a-zA-Z0-9._-]+\.+([a-zA-Z]{2})+/;
				
	if ((is_empty(nombre)==false)&&(is_empty(telf)==false)&&(is_empty(fax)==false)&&(is_empty(direccion)==false)&&(is_empty(mail)==false)&&(is_empty(login)==false)&&(is_empty(passw)==false)&&(is_empty(repassw)==false))
	{
		if (passw ==repassw)
		{
			is_valid = mail.match(valid_mail, "");	
			if (is_valid != null )
			{				
				return true;
			}
			else
			{
				alert("El formato del email es invalido !");	
				return false;			
			}			
		}
		else
		{
			alert("Los Passwords deben ser iguales! ");	
			return false;
		}
	}	
	else
	{
		alert("Debe llenar todos los campos! ");	
		return false;
	}
	
}

function check(producto)
{		
	if (isNaN(document.getElementById(producto).value)==false)
	{
		if (document.getElementById(producto).value > 0 )		
			var prod = parseFloat(document.getElementById(producto).value);		
		else
			document.getElementById(producto).value=0;	
	}
	else
		document.getElementById(producto).value=0;	
}

function editItem(item)
{	
	//var http = getXmlHttpObject();
		
	if (isNaN(document.getElementById(item).value)==false)
	{
		if (document.getElementById(item).value > 0 )		
		{
			var cantidad = parseFloat(document.getElementById(item).value);		
			if (cantidad > 0)
			{			
				http.open("GET", "edit.php?item="+item+"&cantidad="+cantidad, true);	
				http.send(null);									
				
			}//if	
			
		}			
		else
			document.getElementById(item).value=0;	
	}
	else
		document.getElementById(item).value=0;	
		
	
}



function addCart(producto)
{
	
	
	if ((document.getElementById(producto).value!=null)&&(document.getElementById(producto).value>0))
	{
		var prod = document.getElementById(producto).value;
		http.open("GET", "add.php?prod="+producto+"&cant="+prod, true);	
		http.onreadystatechange = handleHttpResponse;	
		http.send(null);	
		
		alert("Agregado al carrito ");
	
	}//if
	else
	{	
		document.getElementById(producto).value="0";
		
		document.getElementById('message').innerHTML= "<b>Por favor revise la cantidad</b>";
		alert("Debe digitar una cantidad mayor a 0 ");
	}
}


function infoProducto(producto)
{
	window.open('sendinfo.php?articulo='+producto,'Informacion','left=150,top=150,width=450,height=400,toolbar=0,resizable=0');		
	/*
	var http = getXmlHttpObject();
	
	if (producto!="")
	{		
		http.open("GET", "sendInfo.php?arculo="+item, true);	
		http.send(null);				
	}//if	
	*/
}



function deleteItem(item)
{	
	//var http = getXmlHttpObject();
	
	if (item > 0 )
	{		
		http.open("GET", "delete.php?item="+item, true);	
		http.send(null);	
	
		window.location.href ="checkout.php";
	}//if	
}

function cambioFamilia()
{
	var familia = document.getElementById('familias').value;
	
	if ((familia != null)&&(familia > 0))
	{
		window.location.href ="categorias.php?marca="+familia;
	}	
}//changeFamily


function cambiosubfamilia()
{
	var categoria = document.getElementById('categoria').value;
	
	if ((categoria != null)&&(categoria > 0))
	{
		window.location.href ="productos.php?lin="+categoria;
	}	
}//cambiosubfamilia

function buscaArticulo()
{
	var articulo = document.getElementById('producto').value;	
	
	//alert(producto);
	if ((articulo != null)&&(articulo != ""))
	{
		window.location.href ="busqueda.php?articulo="+articulo;
	}

}//buscaArticulo


function getXmlHttpObject(){
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e){
		try	{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
		catch (e){
		xmlhttp = false;
	}
	}
		@else
		xmlhttp = false;
		@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
	try{
	  xmlhttp = new XMLHttpRequest();
	}
	catch (e){
	  xmlhttp = false;
	}
	}
	return xmlhttp;
}//function 



function handleHttpResponse ()
{  		
	if (http.readyState == 1){			
		document.getElementById('message').innerHTML= "Cargando.....";
	}		
	
	if (http.readyState == 4)
	{
		document.getElementById('message').innerHTML= "<b>Articulo Agregado</b>";
	}//if
	
}//end handleHttpResponse	

function submitPedido()
{
	window.open('printcheckout.php','print','800,600');
	
	document.checkoutForm.submit();
	
}

