
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// From eplanner/www/js/common.js

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// End of code copied from eplanner/www/js/common.js

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function addtocart() {
	
	MM_swapImage('btn_add','','images/btn_add_anim.gif',1);
	var colour = MM_findObj('prod_colour').value;
	var size = MM_findObj('size').value;
	var quantity = MM_findObj('quantity').value;
	if (size > 0 && size !='#' && quantity > 0 && quantity != '#') {
		var product_id = MM_findObj('product_id').value;
		var qry = "cmd=add_to_cart&colour="+colour+"&product_id="+product_id+"&size="+size+"&quantity="+quantity;
		ajax_request(qry, 'update_cart');
		
		document.location.href='cart.php';
	} else {
		MM_swapImage('btn_add','','images/btn_addtobag.gif',1);
		alert('You must choose a size and quantity before adding this item to your cart.');
	}
	
	
}

function adduniformstocart() {
	
	MM_swapImage('btn_add','','images/btn_add_anim.gif',1);
	var colour = MM_findObj('prod_colour').value;
	var size = MM_findObj('size').value;
	var quantity = MM_findObj('quantity').value;
	if (size > "" && quantity > 0 && quantity != '#') {
		var product_id = MM_findObj('product_id').value;
		var qry = "cmd=add_to_cart&colour="+colour+"&product_id="+product_id+"&size="+size+"&quantity="+quantity;
		ajax_request(qry, 'update_cart');

		document.location.href='cart.php';		
	} else {
		MM_swapImage('btn_add','','images/btn_addtobag.gif',1);
		alert('You must choose a size and quantity before adding this item to your cart.');
	}
	
	
}

function checkout() {
	document.location.href="checkout.php";
	
}

function swap_image(url, id, colour, i, total, product_id) {
	var qry = "cmd=return_sizes&id="+id+"&product_id="+product_id+"&colour="+colour;
	ajax_request(qry, 'update_sizes');
	MM_swapImage('prod_img','',url,1);
	MM_findObj('colour_tag').innerHTML = colour;
	MM_findObj('prod_colour').value = colour;
	//item.value = colour;
	for(var j=0;j<total;j++) {
	
		eval("var swatch = MM_findObj('item"+j+"');");
		
		if (j == i) {
			swatch.className = "selected";
		} else {
			swatch.className = "";
		}
		
	}

	
}
function change_price(x)
{

		if(sprice[ x.value]=="" || sprice[ x.value]==0)
		{	//price=rrp
			document.getElementById('price_special').innerHTML="";
			document.getElementById('price_rrp').innerHTML="$"+ price[ x.value];
		}
		else
		{ //price=special
			document.getElementById('price_special').innerHTML="<strike>$"+ price[ x.value]+"</strike>";
			document.getElementById('price_rrp').innerHTML="$"+ sprice[ x.value];
		}
				
}

/**Ajax Request (Submits the form below through AJAX
 *               and then calls the ajax_response function)
 */
function ajax_request(qry, response) {
  var submitTo = 'ajax.php';
  eval("http('POST', submitTo, "+response+", qry);");
 
}

/**Ajax Response (Called when ajax data has been retrieved)
 *
 * @param   object  data   Javascript (JSON) data object received
 *                         through ajax call
 */

function update_sizes(data) {
  var sizes = MM_findObj('size');
  var lastsize = sizes.options[sizes.selectedIndex].value;
  
  selectedIndex = 0;
  sizes.length = 0;
  sizes.options[0] = new Option("Select Size", '', false, false);
  for (var i=0; i < data.count; i++) {
  	eval ("var size = data.size"+i+";");
  	if (size == lastsize) {
  		selectedIndex = i+1;
  	}
  	eval("sizes.options["+sizes.length+"] = new Option(data.size"+i+",data.size"+i+", false, false);");
  }
  sizes.selectedIndex = selectedIndex;
  
  full_size = data.full_size;
}

function update_cart(data) {
	var total = data.items * data.quantity;
	var cart_alert = MM_findObj('cart_alert');
	cart_alert.innerHTML = '<a href="cart.php">My Bag</a> Contains '+data.items+' items, $'+data.total+' | <a href="checkout.php" class="lbOn">Checkout Now</a>';
	if (data.error == 1) {
		var alert_text = '<h3>There was an error adding this item to your bag</h3>';
	} else {
		var alert_text = '<h3>THIS ITEM HAS BEEN ADDED TO YOUR BAG</h3><img src="'+data.thumb+'" width="35" height="45" /><strong>'+data.qty+' x '+data.description+'</strong><br />'+data.colour+', Size '+data.size+', <em>$'+data.price+'</em><p><a href="cart.php">View bag</a> | <a href="checkout.php">Checkout Now</a></p>';
	}
	var cart_popup = MM_findObj('item_box');

	cart_popup.innerHTML = alert_text;
	
	MM_findObj('size').style.visibility = "hidden";
	MM_showHideLayers('item_box_add','','show');
	MM_swapImage('btn_add','','images/btn_addtobag.gif',1);
	setTimeout("MM_findObj('size').style.visibility = 'visible';MM_showHideLayers('item_box_add','','hide');", 6000);
}