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_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_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;
}

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_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function selecta(formname,selectname) {
	var popup = MM_findObj(selectname, document);
	var topdown = popup.options[popup.selectedIndex].value
//	alert(topdown);
	window.location.href = topdown;
}

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);
	enlargement = window.open(url,"enlargement","menubar=no,directories=no,width=630,height=540,location=no,scrollbars=yes,status=no,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,variant2,quant,price)
{
	//alert('here');
	if(!variant) variant='null';
	
	var var_arr = variant.split(",");
	variant_code = var_arr[0];
	variant_name = var_arr[1];
	if(variant2) 
	{
		var var_arr = variant2.split(",");
		variant_code += ", "+var_arr[0];
		variant_name += ", "+var_arr[1];
	}

	var stockString = id+'/'+variant_code;

	//alert(out_of_stock_arr.search(stockString));

	if(arr_search(stockString, out_of_stock_arr))
	{
		alert('Sorry, Item out of Stock');
		return;
	}

	var text = document.cartForm.cart.value;
	var split_arr = text.split('",');
	var temp_string ='';
	var check = 0; //has item been added already? 0=no, 1=yes

	if(document.cartForm.cart.value.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))
			{
				alert('Item already added to your cart');
				check = 1;
			}
		}
		if(check == 0)
		{
			document.cartForm.cart.value += ',"'+id+'/'+desc+'/'+variant_code+'/'+1+'/'+price+'"';
			//alert_basket(desc, variant_name);
		}
	}
	else
	{
		document.cartForm.cart.value += '"'+id+'/'+desc+'/'+variant_code+'/'+1+'/'+price+'"';
		//alert_basket(desc, variant_name);	
	}
	if(check == 0)
		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()
{	
	//alert('check '+document.cartForm.cart.value);
	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;
}


















