// ********************************************************************************************
// * File: ups.js
// * Project: 48HrBooks.com
// * Revision History:
// * 23MAR2006 - KMC - Initial Development
// * 08DEC2006 - KMC - Calculation and Performance Updates
// ********************************************************************************************

var UPS_LBSPERPACKAGE = 60;

function UPS_SendRequest( objRemainder, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3, receiverpostalcode, receivercountrycode, packagingtypecode, shipmentweight, rateshop, svccode )
{
	var xml;
	var	xmlHttp;
	var xmlHttp2;
	var pkgs = 0;
	
	xmlHttp = AJAX_GetXMLHttp();
	if ( !xmlHttp )
	{
		// Could not create XMLHTTP object
		return 0;
	}
	
	// Calcualte the number of FULL packages
	pkgs = Math.floor( shipmentweight / UPS_LBSPERPACKAGE );
	if ( pkgs == 0 )
	{
		pkgs++;
	}
	
	if ( ( shipmentweight % UPS_LBSPERPACKAGE != 0 ) && ( shipmentweight > UPS_LBSPERPACKAGE ) )
	{
		// if we're sending multiple packages -- get the weight of the final package
		// and calculate its cost
		remainderWeight = shipmentweight - ( pkgs * UPS_LBSPERPACKAGE );
		xml = UPS_BuildXMLString( receiverpostalcode, receivercountrycode, packagingtypecode, remainderWeight, rateshop, svccode );
		UPS_Remainder( xmlHttp, xml, objRemainder, shipmentweight, pkgs, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3, receiverpostalcode, receivercountrycode, packagingtypecode, rateshop, svccode );
	}
	else
	{
		xml = UPS_BuildXMLString( receiverpostalcode, receivercountrycode, packagingtypecode, shipmentweight, rateshop, svccode );
		UPS_SendXML( xmlHttp, xml, shipmentweight, pkgs, objRemainder, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3 );
	}
}

function UPS_SendXML( xmlHttp, xml, shipmentweight, pkgs, objRemainder, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3 )
{
	var ret;
	
	// Send an asynchronous http request
	xmlHttp.open( 'POST', 'upsXML.asp', true );
	xmlHttp.onreadystatechange = function()
	{
		if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 )
		{
			// if we're sending multiple packages, multiply the full package shipping cost
			// by the number of full packages -- and if there is a remainder package, add its cost
			ret = UPS_ParseXML( xmlHttp.responseText );
			ret *= pkgs;
			
			if ( parseFloat( objRemainder.value ) > 0 )
			{
				ret += parseFloat( objRemainder.value );
			}
			ret = Math.round( ret * 100 ) / 100;
			ret = ret.toFixed( 2 );
			obj1.value = ret;
			obj2.value = ret;
			obj3.value = ret;
		}
	}
	xml = escape( xml );
	xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	xmlHttp.send( 'xml=' + xml );
}

function UPS_Remainder( xmlHttp, xml, objRemainder, shipmentweight, pkgs, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3, receiverpostalcode, receivercountrycode, packagingtypecode, rateshop, svccode )
{
	var ret;

	// Get the remainder package cost
	xmlHttp.open( 'POST', 'upsXML.asp', true );
	xmlHttp.onreadystatechange = function()
	{
		if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 )
		{
			// Save remainder value
			ret = UPS_ParseXML( xmlHttp.responseText )
			objRemainder.value = ret;
			
			// Get the rest
			xml = UPS_BuildXMLString( receiverpostalcode, receivercountrycode, packagingtypecode, shipmentweight, rateshop, svccode );
			UPS_SendXML( xmlHttp, xml, shipmentweight, pkgs, objRemainder, obj1, obj2, obj3, objPrice1, objPrice2, objPrice3 );
		}
	}
	xml = escape( xml );
	xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	xmlHttp.send( 'xml=' + xml );
}

function UPS_ParseXML( xml )
{
	var xmlDoc;
	var ups_result;
	var numNodes;
	var amt;
	var nl;
	var n;

	// Instantiate the XML parser
	xmlDoc = AJAX_GetXMLParser();
	if ( xmlDoc == null )
	{
		alert( "Error: Could not instantiate XML parser." );
		return 0;
	}
	
	// Load the XML string
	xmlDoc = AJAX_LoadXML( xmlDoc, xml );
	if ( xmlDoc == null )
	{
		alert( "Error: Could not parse XML data." );
		return
	}
	
	n = xmlDoc.getElementsByTagName( "ResponseStatusCode" );
	ups_result = parseInt( n[0].firstChild.nodeValue );
	if ( ups_result != 1 )
	{
		// UPS Error
		return 0;
	}
	
	n = xmlDoc.getElementsByTagName( "TotalCharges" );
	return parseFloat( n[0].childNodes[1].firstChild.nodeValue );
}

function UPS_CalcWeight( bksID, shippingConstant, shippingMultiplier, pgs, qty )
{
	switch( bksID )
	{
		case 1:
			w = 5.375;
			h = 8.375;
			break;
		case 2:
			w = 5.875;
			h = 8.875;
			break;
		case 3:
			w = 8.375;
			h = 10.875;
			break;
		case 4:
			w = 8.875;
			h = 11.875;
			break;
		case 5:
			w = 4.125;
			h = 6.875;
			break;
		default:
			return -1;
	}
	
	if ( w == 0 || h == 0 || pgs == 0 || qty == 0 )
	{
		return -1;
	}

	return shippingConstant + ( w * h * pgs * qty * shippingMultiplier );
}

function UPS_BuildXMLString( receiverpostalcode, receivercountrycode, packagingtypecode, shipmentweight, rateshop, svccode )
{
	var xml;
	
	// Build the XML Request
	xml = "<?xml version='1.0'?>";
	xml = xml + "<AccessRequest xml:lang='en-US'>";
	xml = xml + "<AccessLicenseNumber>" + UPS_XMLKEY + "</AccessLicenseNumber>";
	xml = xml + "<UserId>" + UPS_LOGIN + "</UserId>";
	xml = xml + "<Password>" + UPS_PASSWORD + "</Password>";
	xml = xml + "</AccessRequest>";
	xml = xml + "<?xml version='1.0'?>";
	xml = xml + "<RatingServiceSelectionRequest xml:lang='en-US'>";
	xml = xml + "<Request>";
	xml = xml + "<TransactionReference>";
	xml = xml + "<CustomerContext>Bare Bones Rate Request</CustomerContext>";
	xml = xml + "<XpciVersion>1.0001</XpciVersion>";
	xml = xml + "</TransactionReference>";
	xml = xml + "<RequestAction>Rate</RequestAction>";
	xml = xml + "<RequestOption>" + rateshop + "</RequestOption>";
	xml = xml + "</Request>";
	xml = xml + "<PickupType><Code>01</Code></PickupType>";
	xml = xml + "<CustomerClassification><Code>01</Code></CustomerClassification>";
	xml = xml + "<Shipment><Shipper>";
	xml = xml + "<Address>";
	xml = xml + "<PostalCode>" + PS_ZIP + "</PostalCode>";
	xml = xml + "<CountryCode>" + PS_COUNTRY + "</CountryCode>";
	xml = xml + "</Address>";
	xml = xml + "</Shipper>";
	xml = xml + "<ShipTo><Address>";
	xml = xml + "<PostalCode>" + receiverpostalcode + "</PostalCode>";
	xml = xml + "<CountryCode>" + receivercountrycode + "</CountryCode>";
	xml = xml + "<ResidentialAddressIndicator></ResidentialAddressIndicator>";
	xml = xml + "</Address></ShipTo>";
	xml = xml + "<Service><Code>" + svccode + "</Code></Service>";
	xml = xml + "<Package><PackagingType><Code>" + packagingtypecode + "</Code>";
	xml = xml + "<Description>Package</Description></PackagingType>";
	xml = xml + "<Description>Rate Shopping</Description>";
	if ( shipmentweight > UPS_LBSPERPACKAGE )
	{
		xml = xml + "<PackageWeight><Weight>" + UPS_LBSPERPACKAGE + "</Weight></PackageWeight>";
	}
	else
	{
		xml = xml + "<PackageWeight><Weight>" + shipmentweight + "</Weight></PackageWeight>";
	}
	xml = xml + "</Package>";
	xml = xml + "</Shipment></RatingServiceSelectionRequest>";
	
	return xml;
}