function MM_findObj(n, d) { //v3.0
  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); return x;
}

/*
function alert_basket(item, variant)
{
	if(!variant)
	{
		var mss = '\''+item+'\'';
	}
	else
	{
		var mss = '\''+item+'\' ['+variant+']';
	}
	
	alert('You have added: '+mss+' to your shopping cart');
}
*/
/*
function storeNav(rellink)
{
//	alert(link.href);
//	cartForm.action = "<? echo $url."/"; ?>"+link.href+"?hello";
	document.cartForm.url.value = rellink + '?style=4';
	//alert(document.cartForm.url.value);
	document.cartForm.action = document.cartForm.url.value;
	//alert(document.cartForm.action);
//	link.href  = "<? echo basename($PHP_SELF) ?>";
//  link.href  = "javascript:document.cartForm.submit()";
//	alert(cartForm.url.value);
//	alert(cartForm.action);
	document.cartForm.submit();
	return true;
}
*/
/*
function doPopUp(url) // opens enlarged img in new window
{
	//alert(url);
	if((navigator.platform=="MacPPC") && (navigator.appName == "Microsoft Internet Explorer"))
		loadPopUp(url,430,195);
	else
		loadPopUp(url,250,305);
	//autoResizePopUp();
	//enlargement = window.open(url,"enlargement","menubar=no,directories=no,width=470,height=600,location=no,scrollbars=yes,status=yes,resizable=yes");
}

function doPopUpImg(url) // opens enlarged img in new window
{
	//alert(url);
	url = "/htdocs/popupImage.php?image="+url;
	if((navigator.platform=="MacPPC") && (navigator.appName == "Microsoft Internet Explorer"))
		loadPopUp(url,660,290);
	else
		loadPopUp(url,250,305);
	//autoResizePopUp();
	//enlargement = window.open(url,"enlargement","menubar=no,directories=no,width=470,height=600,location=no,scrollbars=yes,status=yes,resizable=yes");
}
*/
/*function change_variant(id, size)
{
	var text = document.cartForm.cart.value;
	var split_arr = text.split(',');
	var temp_string ='';

	if(document.cartForm.cart.value.length > 0)
	{
		for(var x = 0; x < split_arr.length; x++)
		{
			var vals = split_arr[x].split('/');
			if(vals[0] == id)
			{
				vals[2] = size;
			}
			
			temp_string += ','+vals[0]+'/'+vals[1]+'/'+vals[2]+'/'+vals[3]+'/'+vals[4];
		}
		document.cartForm.cart.value = temp_string;
		
		if(document.cartForm.cart.value.charAt(0) == ',')
		{
			document.cartForm.cart.value = document.cartForm.cart.value.substring(1,document.cartForm.cart.value.length);
		}
	}
	//alert (document.cartForm.cart.value);
}*/
/*
function arr_search(needle, stack)
{
	//alert(stack.length);
	if(!needle)
	{
		return 0;
	}
//	alert(needle);
	for(var x = 0;x < stack.length; x++)
	{
//		alert('is '+needle+' equal to '+stack[x])
		if(stack[x] == needle)
		{
			return 1;
		}
	}
	return 0;
}
*/
function ToggleCart(id,desc,variant,quant,price,itemid)
{
	var variant_code = "";
	var variant_name = "";
	var valid_clearance = true;

	if (variant)
	{
		var var_arr = variant.split(",");
		variant_code = var_arr[0];
		variant_name = var_arr[1];
	}

	var cart = document.cartForm.cart.value;
	if (typeof this.decodeURIComponent != 'function')
	{
		if (typeof this.unescape == 'function') 
			cart = unescape(cart);
	}
	else
		cart = decodeURIComponent(cart);
	var split_arr = cart.split('",');
	var temp_string ='';
	var check = 0; //has item been added already? 0=no, 1=yes

	if (cart.length != 0)
	{
		for(var x = 0; x < split_arr.length; x++)
		{
			var cartitem = split_arr[x].substr(1);
			//alert(cartitem);
			var vals = cartitem.split('/');
			//alert(' id '+vals[0]+'  size '+vals[2]);
			if((vals[0]) == id && (vals[2] == variant_code))
			{
				// vals[3]=vals[3]+1;
				alert('This item is already in your shopping basket.\n\nYou can add, remove and adjust the item quantities \nby following "View Basket" link at the top of the screen.');
				check = 1;
			}
		}
	}
	if(check == 0 && valid_clearance == true)
	{
		if (cart.length > 0)
			cart += ',';
		cart += '"'+id+'/'+desc+'/'+variant_code+'/'+quant+'/'+price+'/'+itemid+'"';
		document.cartForm.cart.value = cart;
		document.cartForm.submit();	
	}
}
/*	
function switch_page(page)
{
	document.exchangeForm.action = page;
	
	//alert(document.exchangeForm.action);
	
	document.exchangeForm.cart.value = document.cartForm.cart.value;
	document.exchangeForm.submit();
}

function set_boxes()
{
	
	var text = document.cartForm.cart.value;
	
	//alert('Text - '+text);
	var split_arr = text.split(',');
	
	if(text.search('/') != -1)
	{
		
		for(var x = 0; x < split_arr.length; x++)
		{
			//alert('loop counter: '+x);
			var vals = split_arr[x].split('/');
			var id = vals[0];
			var id1 = vals[2];
			//alert(id);alert(id1);
			var object_id=MM_findObj(id);
//			alert(object_id);
			//first check if the id exists in the page
			if(object_id) 
			{ 
				//the checkbox
				object_id.checked=true;
			}
			//alert(id1);
			if(document.id && id1 != null)
			{
				var object_name = "cart_form_"+id; 
				var formName = MM_findObj(object_name);
	//			alert(formName);
				formName.variant0.value=id1; 
			}
						
		}
	}
}
*/
function do_checkout()
{	
	document.cartForm.submit();	
}

function inp_alert(iobj,msg)
{
	var obg=iobj.style.backgroundColor;
	iobj.style.backgroundColor="#FFA0A0";
	iobj.focus();
	alert(msg);
	iobj.style.backgroundColor=obg;
}
function emailCheck(oField)
{
	if (!oField) alert ("Invalid field: "+oField);

	var regEmail = /^[_\.0-9a-z-]+\@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/i;
		
	if(!regEmail.test(oField.value))
	{
		inp_alert(oField,"Please enter a valid email address.");
		return false;
	}
	else
		return true;
}
function pcCheckUK(oField)
{
	if (!oField) alert ("Invalid field: "+oField);

	var regPC = /^[A-Z]{1,2}[1-9][0-9]?[0-9][A-Z]{2}$/i;
	if(!regPC.test(oField.value))
	{
		inp_alert(oField,"Please enter a valid UK PostCode.");
		return false;
	}
	else
		return true;
}
function phoneCheck(oField)
{
	if (!oField) alert ("Invalid field: "+oField);

	var regPhone = /^((([+]|00)?[ -]?44)|0)[ -]?([ -]?[0-9]){7,25}$/;
	if(!regPhone.test(oField.value))
	{
		inp_alert(oField,"Please enter a valid UK Phone Number.");
		return false;
	}
	else
		return true;
}