// JavaScript Document

// Side menu open/shut
obj = 0;
oldid = 0;

function showhide(id){ 
	if (obj && oldid != id) {obj.style.display = "none";}
	if (document.getElementById){ 
	obj = document.getElementById(id);
	oldid = id;
		if (obj.style.display == "none"){ 
			obj.style.display = ""; 
		} else { 
		obj.style.display = "none"; 
		}
	}
	return false;
} 

function colapse($id){
	if (document.getElementById){ 
	obj = document.getElementById(id);
	obj.style.display == "none";
	}
}

// Product popup
function popup(mylink)
    {
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
       href=mylink;
    else
       href=mylink.href;
    window.open(href, 'PopUpWindow', 'width=560,height=400,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0');
	return false;
}

// Repetitive AJAX Calls

// Opens a category in the main view
function goTo(cat1,cat2,cartid){
	return ajaxFunction('body','list.php?cat1='+cat1+'&cat2='+cat2+'&cartid='+cartid, true);
}

// Return to default products page
function goHome(cartid){
	return ajaxFunction('body','default.php', true);
}

// Calls the mini cart to add an item to the database
function addCart(cartid,cid){
	ran = Math.floor(Math.random()*101);
	return ajaxFunction('minicart','minicart.php?cartid='+cartid+'&add='+cid+'&ran='+ran);
}

// Used to refresh the minicart when the normal cart has finished loading
function refreshMiniCart(cartid){
	ran = Math.floor(Math.random()*101);
	return ajaxFunction('minicart','minicart.php?cartid='+cartid+'&ran='+ran);
}

// Opens the cart in the main view
function viewCart(cartid){
	ran = Math.floor(Math.random()*101);
	return ajaxFunction('body','cart.php?cartid='+cartid+'&ran='+ran, true)
}

// Will eventually open the checkout, this may be redudant if checkout is on a seperate page
//function checkout(cartid){
//	alert('Online checkout is not yet available.');
//	return viewCart(cartid);
//}

// Updates the Cart from the cart page
function updateCart(cartid){
	var form = document.forms['cartform'].elements;
	var l = form.length;
	var params = '';
	var x;
	for(x=0;x<l;x++) {
		params += '&'+form.item(x).name+'='+form.item(x).value;
	}
	ajaxFunction('body','cart.php?cartid='+cartid+'&update=2'+params);
	refreshMiniCart(cartid);
	//return document.forms['cartform'].submit();
	return false;
}

// Calls the mini cart spoofed to think its empty, then the cart which performs the action
function clearCart(cartid){
	var conf = confirm("Are you sure you want to clear your cart?");
	if(conf){
		ajaxFunction('minicart','minicart.php?cartid='+cartid+'&clear=true');
		return ajaxFunction('body','cart.php?cartid='+cartid+'&clear=true');
	} else {
		return false;
	}
}

// Useful Stuff
function findChecked(obj){
  for (var x = 0;x<obj.length;x+=1){
    if (obj[x].checked == true){
      return obj[x];
    }
  }
  return false;
}

// AJAX
function ajaxFunction(id, url, loader){
	
	var elem = document.getElementById(id);
	
	if(loader==true){
		elem.innerHTML = '<h1>Loading</h1><div class="loading"><img src="/img/load.gif"> Loading, Please Wait...</div>';	
	}
	
	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("Your browser does not support AJAX!");
				return true; // Failed, do anchor link
			}
		}
	}

	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			elem.innerHTML = xmlHttp.responseText;
		}
	}
	
	if (!elem) {
	alert('The element with the passed ID doesn\'t exists in your page');
	return true; // Failed, do anchor link
	}
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return false; // Worked, don't do anchor link
}	