var id;
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function updateTimeOut(){
	alert("Error loading the page. Please press 'ok' and refresh the page");	
}

function showBasket(){
	if(document.getElementById("prod")!= "undefined"){
		runAjax("showBasket");
	}
}

function addProduct(id,itterator){
	var qty = document.getElementById("qty[" + id + "]").value;
	if(qty < 0 || qty > 20){
		alert("Please enter a quantity between 1 and 20");
		return 0;
	}
	var att = "";
	
		for(var i=0; i < itterator; i++){
			if(document.getElementById("attrib[" + id + "][" + i + "]")){
				att += document.getElementById("attrib[" + id + "][" + i + "]").options[document.getElementById("attrib[" + id + "][" + i + "]").selectedIndex].value + "|";
			}
		}

	runAjax("addProduct",id+","+att+","+qty);
}

function delProduct(id,uid){
	runAjax("delProduct",id+","+uid);
}

function loadBasket(){
		//setTimeout("showBasket()",100);
}

function switchPrice(dropdown,prod_id){
	var a = 0;
	var str;
	var varids="";
	while(str = document.getElementById("attrib["+prod_id+"]["+a+"]")){
		if(str){
			varids += str.options[str.selectedIndex].value + "|";
		}
		a++;
	}
	runAjax("getNewPrice",prod_id+","+varids);
}

// Admin

function loadFeature(featid){
	if(featid)
		id = featid;
	setTimeout("showFeature()",100);
}

function showFeature(){
	runAjax("getFeatures",id);
}

function addFeature(id){
	runAjax("addFeature",id+","+document.getElementById("featureadd").value);
	setTimeout("showFeature()",100);		
}

function delFeature(id){
	runAjax("delFeature",id);
	loadFeature();
}

function showAttributes(){
	runAjax("getAttribute",id);
}

function addAttribute(){
	var av = document.getElementById("attrib_value");
	var an = document.getElementById("attrib_name");
	var sa_div = document.getElementById("attributes");
	var price = document.getElementById("pricemod");
	runAjax("addAttribute",id+","+av.options[av.selectedIndex].value+","+price.value);
	setTimeout("showAttributes()",100);	
}

function move(dir,an_id,id){
	runAjax("moveAttribute",id+","+an_id+","+dir);
	setTimeout("showAttributes()",100);	
}

function delAttribute(id){
	runAjax("delAttribute",id);
	setTimeout("showAttributes()",100);
}

//

function runAjax(func,funcargs){
	if(funcargs==null){
		funcargs = "";	
	}
	
	//if(!readCookie("shopmg")){
	//	createCookie("shopmg","",60);
	//}
	
	var handlerFunc = function(t) {
    	switch(func){
			case "showBasket":
			case "addProduct":
			case "delProduct":
				var prod = document.getElementById("prod");
				prod.innerHTML = t.responseText;
			break;
			break;
			break;
			case "getFeatures":
				var feature = document.getElementById("addfeatures");
				feature.innerHTML = t.responseText;
			break;
			case "getAttribute":
				var cont = document.getElementById("attributes");
				cont.innerHTML = t.responseText;
			break;
			case "getNewPrice":
				var strloc = t.responseText;
				var strlocarr = strloc.split("|");
				var cont = document.getElementById("price"+strlocarr[0]);
				cont.innerHTML = "&pound;"+strlocarr[1];
			break;
		}
	}
	
    var options = {
		asynchronous : true,
        method : 'get',
		onSuccess : handlerFunc,
		onTimeout : updateTimeOut,
		encoding : 'iso-8859-1'
    };
	var i = Math.round(100000*Math.random());
    new Ajax.Request('/admin/shop/ajax.php?func=' + func + '&funcargs=' + funcargs + '&val=' + i, options);
	return handlerFunc;
}

window.onload=function(){ showBasket(); }
