//setup
//////////////////////
var xmlhttp=null;
var xmlhttpCart=null;
var cart=[];
var cartIndex=0;
var pView=null
var pIndex=-1;
var is_pt=false;

//recieve-display product list
//////////////////////
function xmlhttpReadyState(){
	if(xmlhttp.readyState==4){
		if(xmlhttp.responseText==""){
			products="<div>There are no products in this category</div>";
			pView.innerHTML=products;
			return;
		}
		pList=xmlhttp.responseText.split("\n");
		products="<table cellpadding=\"0\" cellspacing=\"0\" class=\"pTable\"><tr>";
		products+="<td class=\"h\">&nbsp;Code&nbsp;</td><td class=\"h\">UPC Code</td><td class=\"h\">Product Name</td><td class=\"h\">Size</td><td class=\"h\">Incl VAT</td>";
		products+="<td class=\"h\">Product Image and Nutritional Values</td><td class=\"h\">Cart</td>";
		products+="</tr>";
		pCount=pList.length;
		for(a=0;a<pCount;a++){
			p=pList[a].split("\t");
			price=p[4];
			if(is_pt)
				price=p[5];
			sPrice=price;
			if(price=='-1')
				sPrice='out of stock';
			products+="<tr><td>"+p[0]+"</td><td>"+p[1]+"</td><td>"+p[2]+"</td><td>"+p[3]+"</td><td>"+sPrice+"</td><td class=\"imageCenter\">";
			if(p[6]==1)
				products+="<a href=\"products/productimages/"+p[0]+".jpg\" target=\"_blank\"><img src=\"products/productimages/thumb_"+p[0]+".jpg\" border=\"0\" /></a>";
			else
				products+="&nbsp;";
			products+="</td>";//<td class=\"imageCenter\">";
			/*if(p[7]==1)
				products+="<a href=\"productinfo.php?cindex="+pIndex+"&index="+a+"&i\" target=\"_blank\"><img src=\"products/productimages/info/thumb_"+p[0]+".jpg\" border=\"0\" /></a>";
			else
				products+="&nbsp;";
			products+="</td>";*/
			
			if(price!='-1'){
				checked="";
				for(b=0;b<cart.length;b++)
				if(cart[b][0]==pIndex && cart[b][1]==a){
					checked="checked=\"checked\"";
					break;
				}
			}
			products+="<td>";
			if(price!='-1')
				products+="<input id=\"onCart_"+pIndex+"_"+a+"\" type=\"checkbox\" "+checked+" onclick=\"addToCart("+pIndex+","+a+",'"+p[0]+"','"+addslashes(p[2])+"')\" />";
			else
				products+="&nbsp;";
			products+="</td></tr>";
		}
		products+="</table>";
		pView.innerHTML=products;
	}
}

//update cart
//////////////////////
function updateCart(){	
	try{ 
		xmlhttpCart=new XMLHttpRequest();
		//xmlhttpCart.onreadystatechange=xmlhttpReadyState;
	}
	catch(e){
		xmlhttpCart=new ActiveXObject("Msxml2.XMLHTTP");
		//xmlhttpCart.onreadystatechange=xmlhttpReadyState;
	}
	
	cartS="";
	cCount=cart.length;
	for(a=0;a<cCount;a++){
		cartS+="cart[]="+cart[a][0]+"_"+cart[a][1]+"_"+cart[a][2];
		if(a<cCount-1)
			cartS+="&";
	}
	
	xmlhttpCart.open("POST", "products/update_cart.php", true);
	xmlhttpCart.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttpCart.send(cartS);
}

//request product list
//////////////////////
function pViewDisplay(index){
	pIndex=index;
	pView=document.getElementById('pView_'+pIndex);
	if(pView.style.display=='block'){
		pView.style.display='none';
		pView.innerHTML="Loading...";
	}
	else
		pView.style.display='block';
	
	//call product info
	if(pView.style.display=='block'){
		try{ 
			xmlhttp=new XMLHttpRequest();
			xmlhttp.onreadystatechange=xmlhttpReadyState;
		}
		catch(e){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
			xmlhttp.onreadystatechange=xmlhttpReadyState;
		}
		var date=new Date();
		xmlhttp.open("GET", "products/get_products.php?index="+pIndex+"&"+(date.getTime()), true);
		//xmlhttpCount.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(null);
	}
}

//cart
//////////////////////
function addToCart(cIndex,pIndex,code,name,ischecked,quantity){
	checked=false;
	if(ischecked==undefined)
		checked=document.getElementById("onCart_"+cIndex+"_"+pIndex).checked;	
	if(checked || ischecked!=undefined){
		frm=document.forms['cartContents'];
		cartIndex++;	
		//container
		try{cItem=document.createElement('<div class="cartItem" id="item_'+cIndex+'_'+pIndex+'">')}
		catch(e){cItem=document.createElement('div');cItem.setAttribute('class','cartItem');cItem.setAttribute('id','item_'+cIndex+'_'+pIndex);}
		//quantity holder
		try{qDiv=document.createElement('<span class="cartQuant">')}
		catch(e){qDiv=document.createElement('span');qDiv.setAttribute('class','cartQuant')}
		cItem.appendChild(qDiv);
		//quantity value
		qs="";
		if(quantity!=undefined){
			qs="value=\""+quantity+"\"";
		}
		try{quant=document.createElement('<input name="quant[]" id="quant_'+cIndex+'_'+pIndex+'" '+qs+' onkeyup="checkNumber('+cIndex+','+pIndex+')" class="" size="10">')}
		catch(e){
			quant=document.createElement('input');
			quant.setAttribute('name','quant[]');
			if(quantity!=undefined){
				quant.value=quantity;
			}
			quant.setAttribute('class','');
			quant.setAttribute('id','quant_'+cIndex+'_'+pIndex);
			quant.setAttribute('onkeyup','checkNumber('+cIndex+','+pIndex+')');
		}
		if(quantity!=undefined){
			quant.value=quantity;
			quant.setAttribute('value',quantity);
		}
		else{
			quant.setAttribute('value','1');
		}
		//remove button
		try{rem=document.createElement('<span onclick="removeFromCart('+cIndex+','+pIndex+')" style="cursor:pointer;margin-left:8px;float:none;">')}
		catch(e){
			rem=document.createElement('span');
			rem.setAttribute('onclick','removeFromCart('+cIndex+','+pIndex+')');
			rem.setAttribute('style','cursor:pointer;float:none;margin-left:8px;');
		}
		rem.innerHTML='Remove';
		//button
		try{mBut=document.createElement('<img onclick="minusCart('+cIndex+','+pIndex+')">')}
		catch(e){mBut=document.createElement('img');mBut.setAttribute('onclick',"minusCart("+cIndex+","+pIndex+")");}
		try{pBut=document.createElement('<img onclick="plusCart('+cIndex+','+pIndex+')">')}
		catch(e){pBut=document.createElement('img');pBut.setAttribute('onclick',"plusCart("+cIndex+","+pIndex+")");}
		mBut.src="Images/minus.png"; pBut.src="Images/plus.png";
		qDiv.appendChild(mBut);
		qDiv.appendChild(quant);
		qDiv.appendChild(pBut);
		qDiv.appendChild(rem);
		quant.style.textAlign='center';
		quant.setAttribute('size','3');
		quant.setAttribute('title','Quantity');
		var label=document.createElement('div');
		label.innerHTML=code+' - '+name+'<div style="clear:both;"></div>';
		cItem.appendChild(label);
		cItem.style.marginTop='10px';
		frm.insertBefore(cItem,frm.submit_checkout);
		q=1;
		if(quantity!=undefined)
			q=quantity;
		cart[cart.length]=[cIndex,pIndex,q];
		
		try{
			hidden=document.createElement('<input type="hidden" id="index_'+cIndex+'_'+pIndex+'" name="index[]" value="'+cIndex+'_'+pIndex+'">')
		}
		catch(e){
			hidden=document.createElement('input');
			hidden.setAttribute('type','hidden');
			hidden.setAttribute('id','index_'+cIndex+'_'+pIndex);
			hidden.setAttribute('name','index[]');
			hidden.setAttribute('value',cIndex+'_'+pIndex);
		}
		frm.appendChild(hidden);
		
		if(cart.length>0)
			frm.submit_checkout.style.display='block';
		else
			frm.submit_checkout.style.display='none';
		
		if(ischecked==undefined)
			updateCart();
	}
	else{
		removeFromCart(cIndex,pIndex);
	}
}

function removeFromCart(cIndex,pIndex){
	if(document.getElementById("onCart_"+cIndex+"_"+pIndex)!=null)
		document.getElementById("onCart_"+cIndex+"_"+pIndex).checked='';
	index=-1;
	for(a=0;a<cart.length;a++)
	if(cart[a][0]==cIndex && cart[a][1]==pIndex){	
		index=a;
		break;
	}
	cart.splice(a,1);
	d=document.getElementById('item_'+cIndex+'_'+pIndex);
	i=document.getElementById('index_'+cIndex+'_'+pIndex);
	frm=document.forms['cartContents'];
	frm.removeChild(d);
	frm.removeChild(i);
	
	if(cart.length>0)
		frm.submit_checkout.style.display='block';
	else
		frm.submit_checkout.style.display='none';
	
	updateCart();
}

function minusCart(cIndex,pIndex){
	i=document.getElementById('quant_'+cIndex+'_'+pIndex);
	
	if(isNaN(i.value))
		i.value=1;
	i.value--;
	if(i.value==0)
		i.value=1;
	
	for(a=0;a<cart.length;a++)
	if(cart[a][0]==cIndex && cart[a][1]==pIndex){	
		cart[a][2]=i.value;
		break;
	}
	updateCart();
}

function plusCart(cIndex,pIndex){
	i=document.getElementById('quant_'+cIndex+'_'+pIndex);
	if(isNaN(i.value))
		i.value=1;
	i.value++;
	
	for(a=0;a<cart.length;a++)
	if(cart[a][0]==cIndex && cart[a][1]==pIndex){	
		cart[a][2]=i.value;
		break;
	}
	updateCart();
}

function checkNumber(cIndex,pIndex){
	i=document.getElementById('quant_'+cIndex+'_'+pIndex);
	if(isNaN(i.value))
		i.value=1;
	
	for(a=0;a<cart.length;a++)
	if(cart[a][0]==cIndex && cart[a][1]==pIndex){	
		cart[a][2]=i.value;
		break;
	}
	
	updateCart();
}

function checkQuantities(){
	valid=true;
	for(a=0;a<cart.length;a++){
		i=document.getElementById('quant_'+cart[a][0]+'_'+cart[a][1]);
		if(isNaN(i.value)){
			valid=false;
			alert('All quantities must be numbers.');
			break;
		}
	}
	
	return valid;
}

function addslashes(str) {
	str=str.replace(/'/g,'\\\'');
	return str;
}

/*function format(number){
	decimal=number.indexOf('.');
	if(decimal==-1){
		return number+".00";
	}
	else{
		return Math.round(number*100)/100;
	}
}*/
