// JavaScript Document

function exi(form)
{
	if(!confirm('Esta seguro de querer agregar este producto'))
	{
		return false;
	}
	else
	{
		valcantidad(form.cantidad);
	}
}

function deleteP(posicion)
{
	if(confirm('¿Confirma que desea eliminar este producto?'))
	{ window.open('eliminar_producto.php?linea='+posicion+'','_self',''); }
}



/* validar usuario*/

function valLogin(form)
{

	if(form.usuario.value=="")
	{ alert("Ingrese su clave de usuario");  form.usuario.focus(); return false; }
	if(form.password.value=="")
	{ alert("Ingrese su contrasena"); form.password.focus(); 	return false; }		
	return true;
}


function valRegistro(form)
{

	if(form.nombre.value=="")
	{
		alert('Teclee el nombre de tarjetahabiente');
		form.nombre.focus();
		return false;
	}
	if(form.domicilio.value=="")
	{
		alert('Teclee su direccion');
			form1.domicilio.focus();
			return false;
	}		
	if(form.colonia.value=="")
	{	
		alert('Teclee su colonia');
		form.colonia.focus();
		return false;
	}
	if(form.ciudad.value=="")
	{
		alert('teclee su ciudad');
		form.ciudad.focus();
		return false;
	}
	if(form.estado.value=="")
	{
			alert('teclee su Estado');
			form.estado.focus();
			return false;
	}
	if(form.pais.value=="")
	{
		alert('teclee su Pais')
		form.pais.focus();
		return false;
	}
	if(form.telefono.value=="")
	{
		alert('teclee su Telefono');
		form.telefono.focus();
		return false;
	}
	if(form.email.value="")
	{
		alert('teclee su Mail');
		form.email.focus();
		return false;
	}		
	if(form1.cp.value=="")
	{
		alert('teclee su CP');
		form1.cp.focus();
		return false;
	}	
	if(form.usuario.value=="")
	{
		alert('teclee un uausario');
		form.usuario.focus();
		return false;
	}	
	if(form.password.value=="")
	{
		alert('teclee una contraseña');
		form.password.focus();
		return false;
	}	
}


function alterError(value) 
{
	if (value<=0.99)
	{
		newPounds = '0';
	}
	else 
	{
		newPounds = parseInt(value);
	}
	newPence = parseInt((value+.0008 - newPounds)* 100);
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
}


function showItems() 
{
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1) 
	{
    	countend = document.cookie.length;
    }
	fulllist = document.cookie.substring(countbegin, countend);
	totprice = 0;
	document.writeln('<form><table border="1" cellspacing="0" width="640" bgcolor="#E0E0E0" bordercolor="#FFFFFF" class="td">');
	document.writeln('<TR><TD width="250"><b>Productos</b></TD><TD width="80" align="right"><b>Cantidad</b></TD><TD width="120" align="right"><b>Costo x unidad</b></TD><td width="100" align="right"><b>Costo total</b><TD width="90">&nbsp;</TD></TR>');
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
		if (fulllist.substring(i,i+1) == '[') 
		{ itemstart = i+1; }
		else if (fulllist.substring(i,i+1) == ']') 
		{
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			var tax = itemtotal / 100 * (0 - 0);
    		tax = Math.floor(tax * 100)/100
			totprice = totprice + itemtotal + tax;
			itemlist=itemlist+1;
			document.writeln('<tr><td>'+theitem+'</td><td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td><td align=center><input TYPE="button" NAME="remove" VALUE="Quitar" onclick="javascript:removeItem('+itemlist+')"></td></tr>');
		}
		else if (fulllist.substring(i,i+1) == ',') 
		{
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '#') 
		{
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
	}
	document.writeln('<tr><td colspan=3><b>Total</b></td><td align=right>'+alterError(totprice)+'</td><td>&nbsp;</td></tr>');
	document.writeln('</TABLE>');
}

function removeItem(itemno) 
{
	newItemList = null;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
		if (fulllist.substring(i,i+1) == '[') 
		{ itemstart = i+1; }
		else if (fulllist.substring(i,i+1) == ']') 
		{
			itemend = i;
			theitem = fulllist.substring(itemstart, itemend);
			itemlist=itemlist+1;
			if (itemlist != itemno) 
			{ newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']'; }
		}
	}
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket="+newItemList;
	top.location = "orden.html";
}

function clearBasket() 
{
	if (confirm('¿Confirma que desea reestablecer el carrito?')) 
	{
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
		top.location = "orden.html";
	}
}

/*agrega a la secccion de pedidos en con precio de mayoreo*/


function valcantidad(campo)
{
	var er_cantidad= /^\d+$/;
	if(!er_cantidad.test(campo.value))
	{ alert("Tiene que ingresar un numero para la cantidad"); campo.focus(); }
}


function buyItem(newItem, newPrice, newQuantity)
{
	if (newQuantity <= 0) 
	{
		rc = alert('La cantidad ingresada es incorrecta');
		return false;
	}
	if (confirm('¿Agregar '+newQuantity+' '+newItem+' al carrito?')) 
	{
		index = document.cookie.indexOf("TheBasket");
		countbegin = (document.cookie.indexOf("=", index) + 1);
        countend = document.cookie.indexOf(";", index);
	    if (countend == -1) 
		{
        	countend = document.cookie.length;
        }
	    document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+","+newPrice+"#"+newQuantity+"]";
	}
	return true;
}

function resetShoppingBasket() 
{
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket=.";
}




/* ENVIO PEDIDO*/

function send(form)
{
var total=form.total.value;
var nota=form.nota.value
var cve_cliente=form.cve_cliente.value

window.open("realizar_compra.php?cve_cliente="+cve_cliente+"&total="+total+"&nota="+nota+"","_self","");
}

function prin(form)
{
window.print();
}




var newWindow
	function makeNewWindow(URL,w,h) {
		newWindow = window.open(URL,"","HEIGHT="+h+",WIDTH="+w+", resizable, scrollbars");
	}
	function WindowURL(URL,w,h){
		if(newWindow == null) makeNewWindow(URL,w,h);
		else if(newWindow.closed) makeNewWindow(URL,w,h);
		else if(newWindow) {
			newWindow.focus();
			newWindow.location.href = URL;
		}
	}





