// rateLookup-common.js

function calculateRates(form)
{

	var zipText = form.zipCode.value;
	
	if (zipText.length < 5 || !isInteger(zipText)) {
		alert("Please enter 5 digit zip code")
		return;
	}
	
	// get 3 digit zip code
	var partialZip = zipText.substring(0,3);
	
	
	if (rates == null) {
		initializeRateArray();
	}
	if (mrates == null) {
		initializemRateArray();
	}
	
	var rate = rates[partialZip];
	var mrate = mrates[partialZip];
	
	if (rate == null) {
		alert("Invalid zip code")
		return;
	} else {
	
		// populate other zip code field
		document.form2.findZip.value = zipText;
			
		// rates
		form.standard1.value = "$"+rates[partialZip].s1;
		form.standard2.value = "$"+rates[partialZip].s2;
		form.standard3.value = "$"+rates[partialZip].s3;
		form.high1.value = "$"+rates[partialZip].h1;
		form.high2.value = "$"+rates[partialZip].h2;
		form.high3.value = "$"+rates[partialZip].h3;
		form.mstandard1.value = "$"+mrates[partialZip].s1;
		form.mstandard2.value = "$"+mrates[partialZip].s2;
		form.mstandard3.value = "$"+mrates[partialZip].s3;
		form.mhigh1.value = "$"+mrates[partialZip].h1;
		form.mhigh2.value = "$"+mrates[partialZip].h2;
		form.mhigh3.value = "$"+mrates[partialZip].h3;
		
	}
	
	// set focus to a different field
	form.calculateButton.focus();
	
}


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function clearRates(form)
{

	if (form.standard1.value != "") {

		document.form2.findZip.value = "";
	
		form.standard1.value = "";
		form.standard2.value = "";
		form.standard3.value = "";
			
		form.high1.value = "";
		form.high2.value = "";
		form.high3.value = "";
		
		form.mstandard1.value = "";
		form.mstandard2.value = "";
		form.mstandard3.value = "";
			
		form.mhigh1.value = "";
		form.mhigh2.value = "";
		form.mhigh3.value = "";
	}
		
}


