/***** COSMOS CALC FUNCTIONS *****/

// Build the destination form
function getAllCosmosDest() {
	var CosmosDestArray = '';
	var CosmosAllDestSelect = '<select name="dropdest" id="dropdest" onchange="destSelect(this)">';
	
	for (x=0; x<CosmosDest.length; x++)  {
		CosmosDestArray = CosmosDest[x].split("|");
		if (CosmosDestArray[3]) {
			CosmosAllDestSelect += "<option value='"+CosmosDestArray[1]+"'>"+CosmosDestArray[3]+CosmosDestArray[0]+"</option>";
		} else {
			CosmosAllDestSelect += "<option value='"+CosmosDestArray[1]+"'>"+CosmosDestArray[0]+"</option>";			
		}
	}
	
		CosmosAllDestSelect += "</select>";
	
	document.getElementById('dropdestdiv').innerHTML = CosmosAllDestSelect;
}

// Pick the destination and calculate months
function destSelect(dest) {
	
	if (dest != 'alldest') {
	var destpicked = dest.selectedIndex;
	if (destpicked == -1)
		return;
	var vacaamount =  dest.options[destpicked].value;

	var CosmosDestArray = CosmosDest[destpicked].split("|");
	var DestSelectedName = CosmosDestArray[0];
	var DestSelectedLink = CosmosDestArray[2];	

	document.cosmos.vacaamount.value = vacaamount;
	} else {
		var DestSelectedName = 'alldest';
		var DestSelectedLink = ' ';	
	}

var totalamount = document.cosmos3.totalamount.value;
	
	getMonths(totalamount, 'yes', DestSelectedName, DestSelectedLink);		

}


// The following functions work together to get the amount of money spent each year based on inputs
function getMonths(totalamount, destCheck, DestSelectedName, DestSelectedLink) {

	if (destCheck == 'yes') {
		var total = totalamount/12;
	} else {
		var total = totalamount;
	}
	var vacamt = document.cosmos.vacaamount.value;
	var months = vacamt/(total);

	if (DestSelectedName == 'alldest') {
		var CosmosDestArray = '';
		var monthmessage = "<br />If you exchanged your chosen items for your travel dreams, you'll make...<br /> <ul style='margin-bottom:2px;'>";
		
		for (x=destStart; x<CosmosDest.length; x++)  {
			CosmosDestSplit = CosmosDest[x].split("|");
			var months = CosmosDestSplit[1]/(total);
			if (CosmosDestSplit[3]) {
				monthsForThisDest = Math.round(months);
				var strMonthDesc = "";
				var intMyMonths = 0+monthsForThisDest
				if (intMyMonths<2) {
					strMonthDesc = "month";
				} else {
					strMonthDesc = "months";
				} 
				monthmessage += "<li><a href='"+CosmosDestSplit[2]+"' target='_top'>"+CosmosDestSplit[0]+"</a> in "+monthsForThisDest+" "+strMonthDesc+"</li>";
			} else {
			//	monthmessage += "<li><a href='"+CosmosDestSplit[2]+"' target='_top'>"+CosmosDestSplit[0]+"</a> in "+monthsForThisDest+" months</li>";
			}
		}
		
		monthmessage += '</ul>&nbsp;';
	} else {
		if (Math.round(months) > 0 && Math.round(months) <= 1) {
				monthmessage = "<br />If you exchanged your chosen items for your travel dreams, you’ll make <a href='"+DestSelectedLink+"'>"+DestSelectedName+"</a> a reality in about <b>one month</b>.<br /><br /><a href='"+DestSelectedLink+"'><img src=\"view-vacations.jpg\" border=\"0\"></a><br />&nbsp;";
		} else if (Math.round(months) == 0 || Math.round(months) == 'Infinity' || isNaN(Math.round(months))) {
			monthmessage = '';
		} else {
				monthmessage = "<br />If you exchanged your chosen items for your travel dreams, you’ll make <a href='"+DestSelectedLink+"'>"+DestSelectedName+"</a> a reality in <b>" + Math.round(months) + " months</b>.<br /><br />So, select your Cosmos dream vacation now and start planning.<br /><br /><a href='"+DestSelectedLink+"'><img src=\"view-vacations.jpg\" border=\"0\"></a><br />&nbsp;";
		}	
	}
		document.getElementById('monthsdiv').innerHTML = monthmessage;	
		
		var totalyearly = total * 12;
		var totalmonthly = total;
	if(totalamount != '') {	
		document.getElementById('dreamsresults').style.display = '';
		document.getElementById('monthsdiv').style.display = '';			
//		document.getElementById('wordtotalyear').innerHTML = totalyearly;
//		document.getElementById('wordtotalmonth').innerHTML = totalmonthly;	
		document.cosmos3.totalamount.value = totalyearly;
	}
}

function toCurrency(num, showDollarSign) {

	if (showDollarSign) {
		return "$" + roundFloat(num, 2)
	} else {
		return roundFloat(num, 2)
	}

}

function roundFloat(num, decimalPlaces) {

	if (isNaN(num)) {
		parseFloat(num)
	}

	var temp = num * Math.pow(10, decimalPlaces)

	temp = Math.round(temp)	

	temp = temp.toString()

	while (temp.length <= decimalPlaces) {

		temp = "0" + temp

	}

	var decNdx = temp.length - decimalPlaces

	temp = temp.substring(0, decNdx) + "." + temp.substring(decNdx)

	return temp

}

function totalOrder(form) {

	var total = 0;

	for (i=1; i<form.length-1; i+=2) {
		amt = parseFloat(form.elements[i].value);

		if (isNaN(amt)) {
			amt = 0;
		}

		total += amt
	}

	var dest = document.cosmos.dropdest.options;
	var destpicked = dest.selectedIndex;
	var CosmosDestArray = CosmosDest[destpicked].split("|");
	var DestSelectedName = CosmosDestArray[0];
	var DestSelectedLink = CosmosDestArray[2];	
	
	if (DestSelectedName == 'Pick a destination') {
		DestSelectedName = 'alldest';	
	}

	getMonths(total, 'no', DestSelectedName, DestSelectedLink)
//	getMonths(total, 'no', '', '')
}



function extend(form, theitem, price) {
	var quan = form[theitem + "Qty"].value;
	
	if (isNaN(quan)) {
		quan = parseFloat(quan);
	} 
	
	if (isNaN(price)) {
		price = parseFloat(price);
	}
	
	var ext = quan * price;
	//form[theitem + "Qty"].value = toCurrency(quan, false)
	form[theitem + "Ext"].value = toCurrency(ext, false)
	totalOrder(form)
}

// Reset all the forms, divs and etc. back to null
function allReset() {
	document.cosmos3.reset();
	document.cosmos2.reset();	
	document.cosmos.reset();
	document.getElementById('dreamsresults').style.display = 'none';
	document.getElementById('monthsdiv').style.display = 'none';				
	document.getElementById('monthsdiv').innerHTML = '';
//  document.getElementById('wordtotalyear').innerHTML = '';
//	document.getElementById('wordtotalmonth').innerHTML = '';
	document.cosmos3.totalamount.value = '';
	getAllCosmosDest();
}	
