// JavaScript Document
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

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 getQueryString() {
  var args = new Object();
  // Get Query String
  var query = location.search.substring(1);
  // Split query at the comma
  var pairs = query.split("&");
  
  var counter = 0;
  
  // Begin loop through the querystring
  for(var i = 0; i < pairs.length; i++) {

    // Look for "name=value"
    var pos = pairs[i].indexOf('=');

    // if not found, skip to next
    if (pos == -1) continue;

    // Extract the name
    var argname = pairs[i].substring(0,pos);
    
    // Extract the value
    var value = pairs[i].substring(pos+1); 

    // Store as a property
    if (!args[argname]) {
      args[argname] = unescape(value);
    }
    else {
      args[argname] += ("&" + argname + "=" + unescape(value));
    }
  }
  
  return args; // Return the Object
}

function MM_showHideLayers() { //v6.0
  nd();
  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 IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function UpdateQty(item, current_image) {
	itemId = item.name;
	newQty = item.options[item.selectedIndex].text;
	document.location.href = 'catparts.php?action=update_item&id='+itemId+'&qty='+newQty+'&img_id='+current_image;
}
			
function UpdateQtyTextBox(item, current_image, old_qty) {
	itemId = item.name;
	newQty = item.value;
	if ( IsNumeric( newQty ) ) {
		if ( newQty < 0 ) { newQty = newQty * -1; }
//		alert( 'This will amend quantity to '+newQty );
		document.location.href = 'catparts.php?action=update_item&id='+itemId+'&qty='+newQty+'&img_id='+current_image;
	} else {
		alert( 'Sorry, positive numbers only please. Set to zero to remove the item.' );
		item.value = old_qty;
	}
}
			
function DelChk(myURL, x) {
       if ( confirm( x + "?") )
		window.open(myURL, '_self');
}

function Shop_AutoShowCart() {
	var myQueryString = getQueryString();
//	document.write(myQueryString['showcart']);
	if (myQueryString['showcart']=='1') { MM_showHideLayers('catparts_basket','','show'); }
}

function Shop_CheckCustomTags() {
	if (location.hash != '' ) {
		document.getElementById(location.hash).style.background = 'yellow'
	}
}

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=/modules/";
}