// Microdirect JavaScript Util functions
// Version 1.0  - RFG Feb 2004
// Version 1.1  - RFG Apr 2004
// 				 			  AddToBasket, IncTxtBox and DecTxtBox Functions Added
//                Added referer to URL as IE6 has a problem with passing referer to aspx script??????
// Version 1.2  - Added New Accessories Layer

function popUpWindow(strURL, strName, intHeight, intWidth) {
	window.open(strURL,strName,'dependent=1,toolbar=0,titlebar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + intWidth + ',height=' + intHeight);
}

/*function getmeLogin(adminId,pWd)
{
    window.location.href='http://www.greenbeandirect.com/login.aspx?uid='+adminId+'&Pass='+pWd;
}*/

function AddToBasket(lngProductID) {
	//alert(lngProductID); 
	//alert('Quantity='+document.getElementById("txtQty").value);
	//alert(document.getElementById('cmbColour').value);
	if (document.getElementById('cmbColour').value=='N/A')
	{
	}
	else
	{
	    lngProductID=lngProductID+document.getElementById('cmbColour').value;
	}
	//alert(lngProductID);
	window.location.href='http://www.greenbeandirect.com/BasketFunction.aspx?StockCode='+ lngProductID + '&Quantity=' + document.getElementById('txtQty').value + '&Actn=Add';
}

function signup() {
	//alert(lngProductID); 
	//alert('Quantity='+document.getElementById("txtQty").value);
	//alert(document.getElementById('cmbColour').value);
	//alert(lngProductID);
	window.location.href='http://www.greenbeandirect.com/Signup.aspx?Email='+document.getElementById('txtEmailAddressCapture').value;
}

function QuickAddToBasket(lngProductID) {
	//alert(lngProductID);
	//alert(Qty);
  window.location.href='http://www.greenbeandirect.com/BasketFunction.aspx?StockCode='+ lngProductID + '&Quantity=1&Actn=Add';

}

function AddMediacode(Mediacode) {
	alert(Mediacode);
	//alert(Qty);
  //window.location.href='http://www.greenbeandirect.com/Mediacode.aspx?Mediacode='+Mediacode;

}


function RemoveFromBasket(lngProductID) {
	//Page=window.location.href;
	//alert(Page);
  	window.location.href='http://www.greenbeandirect.com/BasketFunction.aspx?StockCode=' + lngProductID+'&Actn=Del';
}

function IncTxtBox(objTxtBox) {
  var intValue=document.getElementById(objTxtBox).value;
	intValue++;
  document.getElementById(objTxtBox).value=intValue;
	return true;
}

function DecTxtBox(objTxtBox) {
  var intValue=document.getElementById(objTxtBox).value;
	if (intValue>=1) {
	  intValue--;
    document.getElementById(objTxtBox).value=intValue;
	}
	return true;
}

function isNumberPress() {
  if (((event.keyCode > 47 && event.keyCode < 58) ||
       (event.keyCode > 95 && event.keyCode < 106)) && 
       !event.shiftKey       || 
       (event.keyCode == 37) ||
	     (event.keyCode == 39) || 
       (event.keyCode == 46) || 
       (event.keyCode == 8)  || 
       (event.keyCode == 9))
  {
    return true;
  }
  else
    return false;
}

function intDown() {
  if (navigator.appVersion.indexOf("Mac")==-1) {
    if (isNumberPress())
		  event.returnValue = true;
	  else
	  {
      event.keyCode      = 0; // Set to zero so don't handle
      event.cancelBubble = true;
      event.returnValue  = false;
    }
  }
}

function intDownWithPeriod() {
  if (isNumberPress() || ((event.keyCode == 190 || event.keyCode == 110)&& event.srcElement.value.indexOf(".")==-1))    
    event.returnValue  = true;
	else
	{
    event.keyCode = 0; // Set to zero so don't handle
    event.cancelBubble = true;        
    event.returnValue  = false;
  }
}

function textCounter(field, maxlimit) { 
  if (field.value.length > maxlimit) // if too long...trim it! 
    field.value = field.value.substring(0, maxlimit); 
    // otherwise, update 'characters left' counter 
  else 
    field.title = maxlimit - field.value.length; 
} 


function NewWindow(mypage,myname,w,h,scroll,pos) {
  if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
  if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
  else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
  win=window.open(mypage,myname,settings);
}

function swapLayers(id, objThis) {	
	for (var intI=0; intI<arrLayers.length; intI++) {
	  if (arrLayers[intI]==id) { 
		  document.getElementById(id).style.display='block';
		}	else { 
		  document.getElementById(arrLayers[intI]).style.display='none';	
		}
	}
	return true;
}


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

