// Check the Number of Items, weight, width, height, thickness triggered by OnBlur
function check_number(p_num)
{
	if(p_num.value != "")
	{
	    if(isNaN(p_num.value))
		{
			alert('The input should be a numeric value');
			p_num.value='';
			p_num.select();
			return;
		}
	}
}

function Validate_OnClick() {

	var postalClass 	= document.frmCalculator.postalClass.value;
	var noOfItems		= document.frmCalculator.items.value;
	var itemWeight		= document.frmCalculator.itemWeight.value;
	var mailpieceShape	= document.frmCalculator.MailpieceShape.value;
	var itemWidth		= document.frmCalculator.itemWidth.value;
	var itemHeight		= document.frmCalculator.itemHeight.value;
	var itemThickness	= document.frmCalculator.itemThickness.value;
	var itemLength		= document.frmCalculator.itemLength.value;
	var itemDiameter	= document.frmCalculator.itemDiameter.value;
	var tariff			= document.frmCalculator.tariff.value;
	
	/* This is for comparaison */
	var numWidth = Number(itemWidth);
	var numHeight = Number(itemHeight);
	
	
	for (var i=0; i < document.frmCalculator.sustainable.length; i++)
   	{
   	if (document.frmCalculator.sustainable[i].checked)
	  {
	  var sustainable = document.frmCalculator.sustainable[i].value;
	  }
   	}

	if ( numHeight >= numWidth ) {
		// Width is always the greater value
		tempHeight = itemHeight;
		itemHeight = itemWidth;
		itemWidth = tempHeight;

		document.frmCalculator.itemWidth.value	= itemWidth;
		document.frmCalculator.itemHeight.value = itemHeight;
	}		

	var validationOK = true;

	if ( noOfItems == '' || noOfItems == 0 ) {
		validationOK = false;
		alert('Please enter a value greater than 0 in the number of items box.');
		document.frmCalculator.items.focus();
	} else if ( noOfItems == '' || noOfItems > 1000000000 ) {
		validationOK = false;
		alert('Please enter a value NOT greater than 1,000,000,000 in the number of items box.');
		document.frmCalculator.items.focus();
	} else if( itemWeight == '' || itemWeight == 0 ) {
		validationOK = false;
		alert('Please enter a value greater than 0 in the weight box.');
		document.frmCalculator.itemWeight.focus();
	}

	if ( mailpieceShape == 'Rectangular' ) {
		if( itemWidth == '' || itemWidth == 0 ) {
			validationOK = false;
			alert('Please enter a value greater than 0 in the width box.');
			document.frmCalculator.itemWidth.focus();
		} else if( itemHeight == '' || itemHeight == 0 ) {
			validationOK = false;
			alert('Please enter a value greater than 0 in the height box.');
			document.frmCalculator.itemHeight.focus();
		} else if( itemThickness == '' || itemThickness == 0 ) {
			validationOK = false;
			alert('Please enter a value greater than 0 in the thickness box.');
			document.frmCalculator.itemThickness.focus();
		}
	} else if ( mailpieceShape == 'Cylindrical' ) {
		if( itemLength == '' || itemLength == 0 ) {
			validationOK = false;
			alert('Please enter a value greater than 0 in the length box.');
			document.frmCalculator.itemLength.focus();
		} else if( itemDiameter == '' || itemDiameter == 0 ) {
			validationOK = false;
			alert('Please enter a value greater than 0 in the diameter box.');
			document.frmCalculator.itemDiameter.focus();
		}
	}

	if ( validationOK == true ) {

		var _PageName = 'results.cfm';
		var _Width    = 660;
		var _Height   = 930;
		var _WndName  = '';

		var scrWidth  = screen.width;
		var scrHeight = screen.height;

		var topLeft_X = Math.round( ( scrWidth  - _Width  ) / 2 );
		var topLeft_Y = Math.round( ( scrHeight - _Height ) / 11 );

		var feature = 'toolbar=0,' 	  +
					  'scrollbars=1,' +
					  'location=0,'   +
					  'status=0,'  +
					  'menubar=0,'    +
					  'resizable=1,'  +
					  'width='  + _Width    + ','   +
					  'height=' + _Height   + ','   +
					  'left='   + topLeft_X + ','   +
					  'top='    + topLeft_Y + '';

		resultsWindow = window.open( _PageName + "?PostalClass=" + postalClass + "&Items=" + noOfItems + "&ItemWeight=" + itemWeight + "&ItemWidth=" + itemWidth + "&ItemHeight=" + itemHeight + "&ItemThickness=" + itemThickness + "&ItemLength=" + itemLength + "&ItemDiameter=" + itemDiameter + "&MailpieceShape=" + mailpieceShape + "&tariff=" + tariff + "&sustainable=" + sustainable, _WndName, feature );
		resultsWindow.focus();
	}
}

function DimensionsArea_OnChange() {

	document.getElementById( 'CylindricalDimensions' ).style.display = 'none';

	if ( document.frmCalculator.MailpieceShape.value == 'Rectangular' ) {
		document.getElementById( 'RectangularDimensions' ).style.display = 'block';
		document.getElementById( 'CylindricalDimensions' ).style.display = 'none';
	} else if ( document.frmCalculator.MailpieceShape.value == 'Cylindrical' ) {
		document.getElementById( 'RectangularDimensions' ).style.display = 'none';
		document.getElementById( 'CylindricalDimensions' ).style.display = 'block';
	}
}

function launch_help()
{
	var w = window.open('help.html','help','width=880,height=700,left=40,top=30,status=no,resizable=yes,menubar=no,scrollbars=yes,toolbar=no');
}

