var ReadVal = '';

function MM_findObj(n, d){
	var p,i,x;
	if(!d)
	d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length){
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.

// CONSTANTS
var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

if (number - 0 != number) return null;  // if number is NaN return null
var useSeparator = format.indexOf(separator) != -1;  // use separators in number
var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
var useCurrency = format.indexOf(currency) != -1;  // use currency format
var isNegative = (number < 0);
number = Math.abs (number);
if (usePercent) number *= 100;
format = strip(format, separator + percent + currency);  // remove key characters
number = "" + number;  // convert number input to string

// split input value into LHS and RHS using decpoint as divider
var dec = number.indexOf(decpoint) != -1;
var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

// split format string into LHS and RHS using decpoint as divider
dec = format.indexOf(decpoint) != -1;
var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

// adjust decimal places by cropping or adding zeros to LHS of number
if (srightEnd.length < nrightEnd.length) {
var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
nrightEnd = nrightEnd.substring(0, srightEnd.length);
if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

// patch provided by Patti Marcoux 1999/08/06
while (srightEnd.length > nrightEnd.length) {
nrightEnd = "0" + nrightEnd;
}

if (srightEnd.length < nrightEnd.length) {
nrightEnd = nrightEnd.substring(1);
nleftEnd = (nleftEnd - 0) + 1;
}
} else {
for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
else break;
}
}

// adjust leading zeros
sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
while (sleftEnd.length > nleftEnd.length) {
nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
}

if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
if (isNegative) {
// patch suggested by Tom Denn 25/4/2001
output = (useCurrency) ? "(" + output + ")" : "-" + output;
}
return output;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
var output = "";  // initialise output string
for (var i=0; i < input.length; i++)
if (chars.indexOf(input.charAt(i)) == -1)
output += input.charAt(i);
return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
input = "" + input;
var output = "";  // initialise output string
for (var i=0; i < input.length; i++) {
if (i != 0 && (input.length - i) % 3 == 0) output += separator;
output += input.charAt(i);
}
return output;
}

function fmtPrice(value)
{
result = formatNumber(value, "$0.00");
return result;
}

function getDollarValue(pointsAmount){
var dollarValue;
if (pointsAmount <= 250){
	dollarValue = 0;
}else if(pointsAmount <= 1500){
	dollarValue = 1;
}else if(pointsAmount <= 2000){
	dollarValue = 1.25;
}else if(pointsAmount <= 3000){
	dollarValue = 1.75;
}else if(pointsAmount > 3000){
	dollarValue = 2;
}
return dollarValue;
}	

function UpdateCost(type){
	var totalPoints,varValue,pointValue,totalPointValue,tripTypePoints,tripTypeObj,i;
	totalPointValue = 0;
	if(type == 'manager'){
		var fieldArray = new Array('internationalDirect', 'internationalIndirect', 'cruiseDirect', 'cruiseIndirect', 'domesticDirect','domesticIndirect', 'landAirDirect', 'landAirIndirect');
		var pointsArray = new Array('30','10','15','5','10','4','10','4');
	}else{
		var fieldArray = new Array('internationalDirect', 'cruiseDirect', 'domesticDirect', 'landAirDirect');
		var pointsArray = new Array('30','15','10','10');
	}
	var tripTypePointsArray = new Array('170','245','480');

	tripTypeObj=MM_findObj('tripType'); 
	this.tripTypePoints = MM_findObj('tripTypePoints'); 

	this.tripTypePoints.value = tripTypePointsArray[tripTypeObj.value];
	for (i=0; i <fieldArray.length; i++) {
		varValue=MM_findObj(fieldArray[i]);
		pointValue=varValue.value * pointsArray[i];
		totalPointValue += pointValue;	
	}

	if (this.tripTypePoints.value < 250){
	 this.tripTypePoints.value = 250;
	}

	allPointValue=totalPointValue;
	totalPointValue=totalPointValue - this.tripTypePoints.value;

	if (totalPointValue < 0){
		totalPointValue = 0;
	}
	this.allPoints=MM_findObj('allPoints');
	this.allPoints.value = allPointValue;
	//this.pointValue1=MM_findObj('pointValue1');
	//this.pointValue2=MM_findObj('pointValue2');
	this.pointValue3=MM_findObj('pointValue3');
	this.pointsRedeemed1=MM_findObj('pointsRedeemed1');
	//this.pointsRedeemed2=MM_findObj('pointsRedeemed2');
	//this.pointValue1.value=getDollarValue(this.pointsRedeemed1.value);
	//this.pointValue2.value=getDollarValue(this.pointsRedeemed2.value);
	this.commission1=MM_findObj('commission1');
	//this.commission1.value = this.pointsRedeemed1.value*this.pointValue1.value;
	//this.commission2=MM_findObj('commission2');
	//this.commission2.value = this.pointsRedeemed2.value*this.pointValue2.value;
	//this.retroActiveCommission=MM_findObj('retroActiveCommission');
	this.totalPoints = MM_findObj('totalPoints');
	this.totalPoints.value = totalPointValue;
	this.totalCommission = MM_findObj('totalCommission');
	this.pointValue3.value = getDollarValue(allPointValue);
	if(totalPointValue < this.pointsRedeemed1.value){ 
		this.pointsRedeemed1.value = totalPointValue; // If they ask to redeem more than they have available, set it to the max redeem value
	}
	this.totalCommission.value = this.pointsRedeemed1.value * this.pointValue3.value;
	//this.totalCommission.value = (totalPointValue) * this.pointValue3.value;
	//this.retroActiveCommission.value = fmtPrice(this.totalCommission.value - this.commission2.value - this.commission1.value);
	//this.commission1.value = fmtPrice(this.commission1.value);
	//this.commission2.value = fmtPrice(this.commission2.value);
	//this.pointValue1.value = fmtPrice(this.pointValue1.value);
	//this.pointValue2.value = fmtPrice(this.pointValue2.value);
	this.pointValue3.value = fmtPrice(this.pointValue3.value);
	this.totalCommission.value = fmtPrice(this.totalCommission.value);
}