function numberformat(num,dec)
{
  mul=Math.pow(10,dec);
  num=num*mul;
  num=Math.round(num);
  num = num/mul;
  var numstr=String(num);
  if(numstr.indexOf(".") == -1)
  {
    numstr = numstr + ".";
    for(nfi=0;nfi<dec;nfi++) numstr = numstr + "0";
  }
  decpl = numstr.length - numstr.indexOf(".");
  decpl = decpl - 1;
  if (decpl < dec) { for(nfi=decpl;nfi<dec;nfi++) numstr = numstr + "0"; }

  return (numstr);
}

function numbersonly(myfield, e, dec)
{
  var key;
  var keychar;

  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;

  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) ||
      (key==9) || (key==13) || (key==27) )
  {
    return true;
  }
  else if ((("0123456789").indexOf(keychar) > -1)) // numbers
  {
    return true;
  }
  else if (keychar == ".") // decimal point
  {
    return true;
  }
  else
  {
    return false;
  }
}

function textonly(myfield, e, dec)
{
  var key;
  var keychar;

  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;

  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) ||
      (key==9) || (key==13) || (key==27) )
  {
    return true;
  }
  else if ((("0123456789").indexOf(keychar) > -1)) // numbers
  {
    return false;
  }
  else
  {
    return true;
  }
}

function isDate (day,month,year)
{
  if (month == 2)
  {
    if((year % 4) == 0 && (year != 1900))
    {
      if (day > 29) { return false; }
      else { return true; }
    }
    else
    {
      if (day > 28) { return false; }
      else { return true; }
    }
  }
  else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
  { if (day > 30) { return false; } else { return true; } }
  else { if (day > 31) { return false; } else {return true; } }
}

function clearText(myInput)
{
  myInput.value = "";
}

function showStatus(sMsg)
{
  window.status = sMsg;
  return true;
}

function viewLarge()
{
  imgPopup = window.open(sDir+'popup/viewimage.php?imageID='+document.getElementById("txtImageID").value,'imgPopup','width=495,height=700,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { imgPopup.focus(); }
}

function viewLargeSample(id)
{
  imgPopup = window.open(sDir+'popup/viewsample.php?imageID='+id,'imgPopup','width=10,height=10,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { imgPopup.focus(); }
}

function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if (strHref.indexOf("?") > -1)
  {
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
    {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
      {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

function getRedirect()
{
  var currentPage = window.location.href;
  var txtAdd = currentPage.indexOf("add")-1;
  var txtUpdate = currentPage.indexOf("update")-1;
  var txtDelItem = currentPage.indexOf("deleteitem")-1;
  var txtDel = currentPage.indexOf("empty")-1;
  var txtHash = currentPage.indexOf("#");
  if (txtAdd > -1) { currentPage = currentPage.substr(0,txtAdd); }
  if (txtUpdate > -1) { currentPage = currentPage.substr(0,txtUpdate); }
  if (txtDelItem > -1) { currentPage = currentPage.substr(0,txtDelItem); }
  if (txtDel > -1) { currentPage = currentPage.substr(0,txtDel); }
  if (txtHash > -1) { currentPage = currentPage.substr(0,txtHash); }

  return currentPage;
}

function addToBasket(id,qty,radSmall,minOrder)
{
  var extensions = "";
  var currentPage = getRedirect();
  var small=0;
  if (currentPage.indexOf("?") > -1) { extensions = "&"; } else { extensions = "?"; }
  extensions += "add=1";

  if (qty >= minOrder)
  {
    if(radSmall[0]) { if (radSmall[0].checked) { small=1; } }
    document.getElementById("txtHiddenID").value = id;
    document.getElementById("txtHiddenQty").value = qty;
    document.getElementById("txtSmall").value = small;
    document.frmBasket.action = currentPage+extensions+"#"+id;
    document.frmBasket.submit();
  }
  else { alert("Please note that this item has a minimum order of "+minOrder+" and cannot be purchase in smaller quantities"); }
}

function deleteItem(id,name)
{
  if (confirm("Are you sure you want to delete "+name+" from your basket?"))
  {
    var extensions = "";
    var currentPage = getRedirect();
    if (currentPage.indexOf("?") > -1) { extensions = "&"; } else { extensions = "?"; }
    extensions += "deleteitem=1";

    document.getElementById("txtHiddenID").value = id;
    document.frmBasket.action = currentPage+extensions+"#"+id;
    document.frmBasket.submit();
  }
}

function updateBasket()
{
  document.frmBasketPage.action = tDir+"index.php?page=basket&update=1";
  document.frmBasketPage.submit();
}

function emptyBasket()
{
  var extensions = "";
  var currentPage = getRedirect();
  if (currentPage.indexOf("?") > -1) { extensions = "&"; } else { extensions = "?"; }
  extensions += "empty=1";
  window.location.href=currentPage+extensions;
}

function showBasket()
{
  window.location.href = tDir+"index.php?page=basket";
}

function tellFriend()
{
  friendPopup = window.open(tDir+'popup/friend.php','friendPopup','width=320,height=250,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { friendPopup.focus(); }
}

function newsletter()
{
  newsletterPopup = window.open(tDir+'popup/newsletter.php','newsletterPopup','width=320,height=135,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { newsletterPopup.focus(); }
}

function feedback(productid,custid)
{
  feedbackPopup = window.open(tDir+'popup/feedback.php?productid='+productid+'&custid='+custid,'feedbackPopup','width=320,height=250,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { feedbackPopup.focus(); }
}

function favourite(productid,custid)
{
  favouritePopup = window.open(tDir+'popup/favourite.php?productid='+productid+'&custid='+custid,'favouritePopup','width=320,height=150,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0');
  if (window.focus) { favouritePopup.focus(); }
}

var logoTimer=null;
var logoDirection='d';
function scrollClear() { clearTimeout(logoTimer); logoTimer=0; /*document.getElementById("spnDWBCheck").innerHTML='cleared - '+logoTimer;*/ }
function scrollAuto(divName,amount,delay)
{
	scrollClear();
	logoTimer = setTimeout('scrollAuto(\''+divName+'\','+amount+','+delay+')',delay);
	mydiv = document.getElementById(divName);
	if ((mydiv.scrollTop+378) >= mydiv.scrollHeight && logoDirection == 'd') { logoDirection='u'; }
	else if (mydiv.scrollTop <= 0 && logoDirection == 'u') { logoDirection='d'; }
	//document.getElementById("spnDWBCheck").innerHTML = mydiv.scrollHeight+" - "+mydiv.scrollTop+" - "+(mydiv.scrollTop+378)+" - "+logoTimer;
	scrollTo(divName,amount,logoDirection);
}
function scrollTo(divName,divX,direction)
{	mydiv = document.getElementById(divName); if (direction == 'u') { mydiv.scrollTop = mydiv.scrollTop - divX; } else { mydiv.scrollTop = mydiv.scrollTop + divX; } }
function scrollToBottomOfdiv(divName) { mydiv = document.getElementById(divName); mydiv.scrollTop = mydiv.scrollHeight; }