function b() {

if (window.getSelection)

var x = window.getSelection();

else if (document.getSelection)

var x = document.getSelection();

else if (document.selection)

var x = document.selection.createRange().text;

else

var x = '';

return x;

}



function a(antes,despues,ta){

txt =new Array();

// MODIFICAR ESTO

var antes = antes;

var despues = despues;

// Y ESTO

var tx = ta;

txt[1] = b();

tx.focus();

               var sel = document.selection;

               range = sel.createRange();

               range.colapse;



               var sel_before = range.duplicate();

               var sel_after  = range.duplicate();

               range.moveToElementText(tx);

               sel_before.setEndPoint("StartToStart",range);

               var text_start = sel_before.text;

               var len_start = text_start.length;

               sel_after.setEndPoint("EndToEnd",range);

               var text_end = sel_after.text;

               var len_ende = text_end.length;

               var slen = txt[1].length;			   

               if(slen > 0) {

                  txt[0] = text_start.substring(0, len_start - slen);

                  txt[2] = text_end.substring( slen , len_ende);

               } else {

                  txt[0] = text_start;

                  txt[2] = text_end;

               }

			   tx.value = txt[0] + antes + txt[1] + despues + txt[2];



}

function max(txarea,total) 

{ 

    tam = txarea.value.length; 

    str=""; 

    str=str+tam; 

    Digito.innerHTML = str; 

    Resta.innerHTML = total - str; 



    if (tam > total){ 

        aux = txarea.value; 

        txarea.value = aux.substring(0,total); 

        Digito.innerHTML = total 

        Resta.innerHTML = 0 

    } 

} 

function max2(txarea,total) 

{ 

    tam = txarea.value.length; 

    str=""; 

    str=str+tam; 

    Digito2.innerHTML = str; 

    Resta2.innerHTML = total - str; 



    if (tam > total){ 

        aux = txarea.value; 

        txarea.value = aux.substring(0,total); 

        Digito2.innerHTML = total 

        Resta2.innerHTML = 0 

    } 

} 

stat=new Array(); 

function changeMain(id){



objPadre=document.getElementById(id);

 objHijo=document.getElementById('iDMain'+id);

  if (stat[id]!=1) {

	  stat[id]=1;

	  objHijo.style.display='block';

  } else {

  	if(stat[id]==1){

		stat[id]=0;

		objHijo.style.display='none';

	}

  } 

} 

function buyItem(newItem, newPrice, newQuantity) {

	if (newQuantity <= 0){

		rc = alert('La cantidad ingresada es incorrecta');

		return false;

	}

	newQuantity = parseInt(newQuantity);

/*	if (confirm('¿Agregar '+newQuantity+' '+newItem+' al carrito?')) {*/

	if (confirm('¿Agregar '+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+"]";

	}

	window.location.reload();

	return true;

}



function resetShoppingBasket() {

	index = document.cookie.indexOf("TheBasket");

	document.cookie="TheBasket=.";

}

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(fulllist.length);

	if (fulllist.length > 1 ){

	document.writeln('<form><table class="contenido_cont" cellspacing="2" width="100%" bgcolor="#f6f6f6" bordercolor="#cccccc" class="td">');

	document.writeln('<TR><TD colspan=5>Carrito de compras</TD></TR>');

	document.writeln('<TR><TD width="250"><b>Producto</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="Sacar" 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;

	window.location.reload();

}



function clearBasket() {

	if (confirm('¿Confirma que desea reestablecer el carrito?')) {

		index = document.cookie.indexOf("TheBasket");

		document.cookie="TheBasket=.";

		window.location.reload();

	}

}
