function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	
	return true;
};


function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined")
    {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined")
    {
        target.attachEvent("on" + eventType, functionRef);
    }
    else
    {
        return false;
    }

    return true;
};


function clickButton(e, buttonid){ 
  var evt = e ? e : window.event;
  var bt = document.getElementById(buttonid);

  if (bt){ 
      if (evt.keyCode == 13){ 
         bt.click(); 
        return false; 
      } 
  } 
}


function addToFavorites(urlName, pageName) { 
    window.external.AddFavorite(urlName,pageName);
    return false;
}


function ShowAlternativeImage(imageName, clientID)    {
    
    document.getElementById(clientID).src = '/images/ecommerce/standard/' + imageName;
    //document['mainImage'].src = '/images/ecommerce/mini/' + imageName;
}


function copyAddress()  {
    //[RK] 27/03/2007.
    //Uses hidden fields to allow us to access ASP.NET controls from Javascript.
    
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']

    oForm.elements[prefix + '_DeliveryLine1TextBox'].value = oForm.elements[prefix + '_Line1TextBox'].value
    oForm.elements[prefix + '_DeliveryLine2TextBox'].value = oForm.elements[prefix + '_Line2TextBox'].value
    oForm.elements[prefix + '_DeliveryLine3TextBox'].value = oForm.elements[prefix + '_Line3TextBox'].value
    oForm.elements[prefix + '_DeliveryTownTextBox'].value = oForm.elements[prefix + '_TownTextBox'].value
    oForm.elements[prefix + '_DeliveryCountyTextBox'].value = oForm.elements[prefix + '_CountyTextBox'].value
    oForm.elements[prefix + '_DeliveryPostcodeTextBox'].value = oForm.elements[prefix + '_PostcodeTextBox'].value
    oForm.elements[prefix + '_DeliveryPhoneNumberTextBox'].value = oForm.elements[prefix + '_PhoneNumberTextBox'].value
    oForm.elements[prefix + '_DeliveryCountryDropDownList'].selectedIndex = oForm.elements[prefix + '_CountryDropDownList'].selectedIndex

}


function ViewProductSpecSheet(variantID, productID)    {
   // alert(productID);

	if (productID) {
		window.open('/Ecommerce/ProductSpecSheet.aspx?ID=' + variantID + '&productID=' + productID, 'SPECSHEET','width=450,height=700,scrollbars=yes,resize=no');
    }
}

function GlossaryTerm(term) {

	if (term) {
		window.open('/Glossary/Term.aspx?term=' + term, 'Glossary','width=450,height=350,scrollbars=yes,resize=no');
    }
}


function SubmitSecureIFrame() {
    document.forms['secureForm'].submit()
}


//Populates a hidden field with all the selected orders from the admin salessummary page.
function populateHiddenField(orderID, clientID, checkboxClientID)   {
    document.getElementById(clientID).value += '|' + orderID;

    //alert(document.getElementById(clientID).value)
}

//used on checkout.aspx
function textCounter(field, countfield, maxlimit) {
    if (document.getElementById(field).value.length > maxlimit) // if too long...trim it!
        document.getElementById(field).value = document.getElementById(field).value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else 
        //document.getElementById(countfield).value = maxlimit - document.getElementById(field).value.length;
        document.getElementById(countfield).innerHTML = maxlimit - document.getElementById(field).value.length;
}

function CheckMOQ(moq, qtyClientID) {

    var qty = document.getElementById(qtyClientID).value

    //alert('qty=' + qty + '   MOQ=' + moq);

    if (qty < moq) {
        alert('The minimum order quantity for this item is ' + moq);
        return false;
    }
    else {
        return true;
    }
}