
function newColourSelected(colCaption, colId, prodid, lid)
{
	document.getElementById('selectedColour').value=colId;
	document.getElementById('selectedColourCaption').value=colCaption;
	document.getElementById('selectedcolourSPAN').innerHTML=colCaption;

	if (lid==1)
		document.getElementById('availabilitySPAN').innerHTML='έλεγχος . . .';
	else
		document.getElementById('availabilitySPAN').innerHTML='checking . . .';
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
	// Internet Explorer
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					//alert("Ο browser σας δεν υποστηρίζει AJAX!");
				return false;
				}
			}
		}
	
	xmlHttp.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp.readyState==4)
		{
			document.getElementById('availabilitySPAN').innerHTML=readAvailability(xmlHttp.responseText);
		}
	}
	var rand = Math.floor(Math.random()*11);
	xmlHttp.open("GET","http://www.kannabishop.com/wmt/getAvailability.php?rand="+rand+"&lid="+lid+"&prodid="+prodid+"&colid="+colId, true);
	xmlHttp.send(null);

	function readAvailability(text)
	{	
		if (window.DOMParser)
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(text,"text/xml");
		  }
		else // Internet Explorer
		  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  xmlDoc.async="false";
			  xmlDoc.loadXML(text);
		  }
		return xmlDoc.getElementsByTagName("availability")[0].childNodes[0].nodeValue;	
	} //end function readAvailability
} //end function newColourSelected

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function addItemToCart(prodid, lid)
{
	var colid = document.getElementById('selectedColour').value;
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
	// Internet Explorer
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					//alert("Ο browser σας δεν υποστηρίζει AJAX!");
				return false;
				}
			}
		}
	
	xmlHttp.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp.readyState==4)
		{
			readBasket(xmlHttp.responseText);
			newColourSelected(document.getElementById('selectedColourCaption').value, colid, prodid, lid);
		}
	}

	if (IsNumeric(colid))
	{
		var rand = Math.floor(Math.random()*11);
		xmlHttp.open("GET","http://www.kannabishop.com/wmt/addItemToCart.php?rand="+rand+"&prodid="+prodid+"&colid="+colid+"&lid="+lid, true);
		xmlHttp.send(null);
	} //end if 
	else 
	{
		if (lid==1) alert('Δεν έχετε επιλέξει χρώμα'); else alert('You must select colour first');
	} //end else 

	function readBasket(text)
	{	
		if (window.DOMParser)
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(text,"text/xml");
		  }
		else // Internet Explorer
		  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  xmlDoc.async="false";
			  xmlDoc.loadXML(text);
		  }
		document.getElementById("leftBasketDIV").innerHTML = xmlDoc.getElementsByTagName("numOfItems")[0].childNodes[0].nodeValue + '<br/>' + xmlDoc.getElementsByTagName("totalAmount")[0].childNodes[0].nodeValue;
		var sAlert = xmlDoc.getElementsByTagName("alert")[0].childNodes[0].nodeValue;
		if (sAlert.length>0) alert(sAlert);
	} //end function readBasket
} //end function addItemToCart

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function getBasket(lid)
{
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
	// Internet Explorer
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					//alert("Ο browser σας δεν υποστηρίζει AJAX!");
				return false;
				}
			}
		}
	
	xmlHttp.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp.readyState==4)
		{
			readBasket(xmlHttp.responseText);
		}
	}

	var rand = Math.floor(Math.random()*11);
	xmlHttp.open("GET","http://www.kannabishop.com/wmt/wmt_getBasket.php?rand="+rand+"&lid="+lid, true);
	xmlHttp.send(null);

	function readBasket(text)
	{	
		if (window.DOMParser)
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(text,"text/xml");
		  }
		else // Internet Explorer
		  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  xmlDoc.async="false";
			  xmlDoc.loadXML(text);
		  }
		document.getElementById("leftBasketDIV").innerHTML = xmlDoc.getElementsByTagName("numOfItems")[0].childNodes[0].nodeValue + '<br/>' + xmlDoc.getElementsByTagName("totalAmount")[0].childNodes[0].nodeValue;
	} //end function readBasket
} //end function getBasket


///////////////////////////////////////////////////////////////////////////////////////////////////////////

function changeQuantity(lid, bid, obj)
{
	document.body.style.cursor = "progress";
	if (lid==1) document.getElementById('basketMessageDIV').innerHTML='παρακαλώ περιμένετε ... έλεγχος διαθεσιμότητας'; else document.getElementById('basketMessageDIV').innerHTML='please wait ... checking availability';
	var xmlHttp, xmlHttp2;
	try {
		// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
			xmlHttp2=new XMLHttpRequest();
		} catch (e) {
	// Internet Explorer
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					//alert("Ο browser σας δεν υποστηρίζει AJAX!");
				return false;
				}
			}
		}

	var rand = Math.floor(Math.random()*11);
	var re = new RegExp("quantity_([0-9]*)");
	var m = re.exec(obj.name);
	itemid = m[1];
	items = parseInt(obj.value); 
	oldItems = parseInt(document.getElementById('quantity_'+bid+'_init').value);

	xmlHttp.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp.readyState==4)
		{
			var k = parseInt(readQuantity(xmlHttp.responseText));
			if ((items-oldItems)<=k && k>0)
			{
				if (lid==1) document.getElementById('basketMessageDIV').innerHTML='παρακαλώ περιμένετε ... ενημέρωση του καλαθιού σας'; else document.getElementById('basketMessageDIV').innerHTML='please wait ... refreshing your shopping basket';
				doChangeQuantity(xmlHttp2, lid, rand, itemid, items);
			} //end if 
			else
			{
				if (lid==1) alert('Δεν υπάρχουν τόσα διαθέσιμα προϊόντα'); else alert('Current stock cannot cover so many items for this product');
				obj.value=document.getElementById(obj.name+'_init').value;
				document.getElementById('basketMessageDIV').innerHTML='';
				document.body.style.cursor = "default";
			} //end else
		}
	}

xmlHttp2.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp2.readyState==4)
		{
			document.location=document.location.href;
		}
	}
	
	if (document.getElementById("quantity_"+bid+"_init").value<document.getElementById("quantity_"+bid).value)
	{
		xmlHttp.open("GET","http://www.kannabishop.com/wmt/getAvailability.php?rand="+rand+"&lid="+lid+"&itemid="+itemid, true);
		xmlHttp.send(null);
	} //end if 
	else
	{
		doChangeQuantity(xmlHttp2, lid, rand, itemid,items);
	} //end else 

 function readQuantity(text)
	{	
		if (window.DOMParser)
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(text,"text/xml");
		  }
		else // Internet Explorer
		  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  xmlDoc.async="false";
			  xmlDoc.loadXML(text);
		  }
		var el = xmlDoc.getElementsByTagName("availability"); 
		return el.item(0).getAttribute("number"); 
	} //end function readQuantity
	

} //end function changeQuantity

//////////////////////////////////////////////////////////////////////////////////////
function deleteFromBasket(lid, itemid)
{
	document.body.style.cursor = "progress";
	if (lid==1) document.getElementById('basketMessageDIV').innerHTML='παρακαλώ περιμένετε ... ενημέρωση του καλαθιού σας'; else document.getElementById('basketMessageDIV').innerHTML='please wait ... refreshing your shopping basket';
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
	// Internet Explorer
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					//alert("Ο browser σας δεν υποστηρίζει AJAX!");
				return false;
				}
			}
		}
	xmlHttp.onreadystatechange=function()
	{
		//0	The request is not initialized
		//1	The request has been set up
		//2	The request has been sent
		//3	The request is in process
		//4	The request is complete
		if(xmlHttp.readyState==4)
		{ 
			document.location=document.location.href.replace("#", "");
		} //end if 
	} //end function 
	var rand = Math.floor(Math.random()*11);
	doChangeQuantity(xmlHttp, rand, lid, itemid, '0')
} //end function

//////////////////////////////////////////////////////////////////////////////////////
function doChangeQuantity(xmlHttp2, rand, lid, itemid, items)
{
	xmlHttp2.open("GET","http://www.kannabishop.com/wmt/changequantity.php?rand="+rand+"&lid="+lid+"&item="+itemid+"&items="+items, true);
	xmlHttp2.send(null);
} //end function doChangeQuantity

//////////////////////////////////////////////////////////////////////////////////////
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   if (sText.length==0) IsNumber=false;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
} //end function IsNumeric

//////////////////////////////////////////////////////////////////////////////////////
function Checkout(lid, transactionType)
{
	if (confirm("Είστε σίγουροι για την ολοκλήρωση της παραγγελίας;"))
	{
		document.body.style.cursor = "progress";
		document.getElementById('BuyButton').disabled='disabled';
		if (lid==1) document.getElementById('basketMessageDIV').innerHTML='παρακαλώ περιμένετε ... ολοκληρώνουμε την παραγγελία σας'; else document.getElementById('basketMessageDIV').innerHTML='please wait ... while we are processing your order';

		var rand = Math.floor(Math.random()*11);
		if (transactionType=='1') 
		{
			location.href="http://www.kannabishop.com/wmt/checkout.php?rand="+rand+"&lid="+lid+"&type="+transactionType;
			return false;
		} //end if  - cc payment redirect
		
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			} catch (e) {
		// Internet Explorer
			try {
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				try {
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						//alert("Ο browser σας δεν υποστηρίζει AJAX!");
					return false;
					}
				}
			}
		xmlHttp.onreadystatechange=function()
		{
			//0	The request is not initialized
			//1	The request has been set up
			//2	The request has been sent
			//3	The request is in process
			//4	The request is complete
			if(xmlHttp.readyState==4)
			{ 
				if (xmlHttp.responseText=='1')
				{
					if (lid==1) alert('Η παραγγελία σας ολοκλήρώθηκε.\nΘα λάβετε ένα email με της πληροφορίες της παραγγελίας σας.\n\nΤο καλάθι σας θα αδειάσει.'); else alert('Your order is completed.\nYou will receive an email with your order details.\n\nYour shopping basket will now empty.');
					document.location=document.location.href.replace("#", "");
					/*
					document.getElementById('param1').value=xmlHttp.responseText;
					CCPaymentForm.submit();
					*/
				} //end if 
				else if (xmlHttp.responseText=='-1')
				{
					if (lid==1) alert('ΔΕΝ μπορέσαμε να ολοκληρώσουμε την παραγγελία σας!'); else alert('Your order WAS NOT completed!');
					document.location=document.location.href.replace("#", "");
				} //end else 
			} //end if 
		} //end function 

		xmlHttp.open("GET","http://www.kannabishop.com/wmt/checkout.php?rand="+rand+"&lid="+lid+"&type="+transactionType, true);
		xmlHttp.send(null);
	} //end if - confirm
	else return false;
} //end function Checkout

/////////////////////////////////////////////////////////////
function ChangePaymentMethod(lid, type)
{
		document.body.style.cursor = "progress";
		document.getElementById('BuyButton').disabled='disabled';
		if (lid==1) document.getElementById('basketMessageDIV').innerHTML='παρακαλώ περιμένετε ... υπολογισμός μεταφορικών'; else document.getElementById('basketMessageDIV').innerHTML='please wait ... while we re-calculating shipping cost';
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			} catch (e) {
		// Internet Explorer
			try {
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				try {
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						//alert("Ο browser σας δεν υποστηρίζει AJAX!");
					return false;
					}
				}
			}
		xmlHttp.onreadystatechange=function()
		{
			//0	The request is not initialized
			//1	The request has been set up
			//2	The request has been sent
			//3	The request is in process
			//4	The request is complete
			if(xmlHttp.readyState==4)
			{ 
				if (xmlHttp.responseText!='-1')
				{
					document.location=document.location.href.replace("#", "");
				} //end if 
			} //end if 
		} //end function 
		var rand = Math.floor(Math.random()*11);
		xmlHttp.open("GET","http://www.kannabishop.com/wmt/changepaymentmethod.php?rand="+rand+"&lid="+lid+"&type="+type, true);
		xmlHttp.send(null);
} //end function Checkout

