// FILE: initialize.js
// GOAL: To define functions to initialize support for Taipan Trader game

// Begin

// Setup global variables
var g_destination = new Array();
g_destination[0] =  'Pacific_Ocean';
g_destination[1] =  'Tokyo'; 
g_destination[2] =  'Yokohama'; 
g_destination[3] =  'Kyota';
g_destination[4] =  'Pusan';
g_destination[5] =  'Shanghai';
g_destination[6] =  'Taipei'; 
g_destination[7] =  'Hong_Kong'; 
g_destination[8] =  'Batavia'; 
g_destination[9] =  'Manila';
g_destination[10] = 'Bangkok';

// Default location
var g_locus = 7;
var g_last_locus=6;
var g_location = 'Hong Kong';
var g_last_location = 'Pacific Ocean';
var g_place = 'Hong Kong';

// Market Prices
var g_jade_price = 5000.00;
var g_silk_price = 500.00;
var g_arm_price = 100.00;
var g_rice_price = 50.00;
var g_gen_price = 10.00;

var g_min_jade_price = 1000.00;
var g_min_silk_price = 100.00;
var g_min_arm_price = 10.00;
var g_min_rice_price = 5.00;
var g_min_gen_price = 1.00;

var g_max_jade_price = 9000.00;
var g_max_silk_price = 900.00;
var g_max_arm_price = 90.00;
var g_max_rice_price = 40.00;
var g_max_gen_price = 9.00;

// Inventory variables
var g_inventory = 0.00;
var g_currency = 1; // USA = 1, UK = 2, ...
var g_currency_abbreviation = "USD";
var g_currency_lead = '$';
var g_currency_follow = ' ';

var g_ship = 1;
var g_gun = 5;
var g_cargo = 0;
var g_tonnage = 50;

// Customer inventory initially
var c_jade_price = g_jade_price;
var c_silk_price = g_silk_price;
var c_arm_price = g_arm_price;
var c_rice_price = g_rice_price;
var c_gen_price = g_gen_price;
var p_jade_quant = 0;
var p_silk_quant = 0;
var p_arm_quant = 0;
var p_rice_quant = 0;
var p_gen_quant = 0;

// Ocean Status
var g_ocean_state = new Array();
g_ocean_state[0] =  'calm seas';
g_ocean_state[1] =  'rough seas';
g_ocean_state[2] =  'heavy swell';
g_ocean_state[3] =  'slight winds';
g_ocean_state[4] =  'steady winds';
g_ocean_state[5] =  'cyclone';

// Controls for pirate action
var g_pirate_proximity = 0.50;
var g_pirate_hostility = 0.50;
var g_battlereadiness = 3;


// Setup initial data for Customer Variable Data
var c_cash = g_typical_loan;	// Typically same as borrowed amount
var c_debt = g_typical_loan;	// Typically 5000.00
var c_payment = g_payment_counter_start; // Typically 10 percent or 500.00;

var c_hong="My Hong";
var c_locus = g_locus;
var c_last_locus = g_last_locus;
var c_place=g_place;
var c_ship = g_ship;
var c_damage = 1.00;
var c_gun = g_gun;
var c_cargo = g_cargo;
var c_tonnage = g_tonnage;
var c_jade_quant = 0;
var c_silk_quant = 0;
var c_arm_quant = 0;
var c_rice_quant = 0;
var c_gen_quant = 0;

var c_selection = 1;
var c_price = 10.00;
var c_quant = 1;

// Also update the cargo hold
c_cargo = c_jade_quant + c_silk_quant + c_arm_quant + c_rice_quant + c_gen_quant;

// And calculate total value at this point
var c_total = c_cash - c_debt;


////////////////////////////////////////   BEGIN INITIALIZATION CODE    ////////////////////////////////


// Functions to create new cookies ----------

// FUNCTION: addToCookieExpiring
// PURPOSE:  To add a name=value pair to the cookie with an expiration date
function addToCookieExpiring(name, value) {
	// Set expiration date for today + 90 days
	var exp = new Date();
	exp.setTime(exp.getTime() + 90*24*60*60*1000);	// 90 days
	var expDate = exp.toGMTString();

	// Now set the cookie value
	document.cookie = name + "=" + value + ";expires=" + expDate + "; ";
	return true;
}


// FUNCTION: addToCookie
// PURPOSE:  To add a name=value pair to the cookie
function addToCookie(name,value) {
	// Set expiration date for today + 90 days
	var exp = new Date();
	exp.setTime(exp.getTime() + 90*24*60*60*1000);	// 90 days
	var expDate = exp.toGMTString();

	document.cookie = name + "=" + value + ";expires=" + expDate + "; ";
	return true;
}


// FUNCTION: getCookieValue
// PURPOSE: To get value to the specific cookie value
function getCookieValue(name)  {
	var start=0;
	var end=0;
	cookieyes=false;
	var cookietext= document.cookie;
	var i=0;
	templen = name.length

	while ( i<= cookietext.length)
	{
		start = i;
		end  = start + templen;
		if (cookietext.substring(start,end)==name) 
		{
			cookieyes = true;
			break;
		}
		
		// Increment along the cookie text 
		i++
	}
	
	if (cookieyes==true)
	{
		start = end + 1;
		end=document.cookie.indexOf(";",start);
		if (end < start)
			end=document.cookie.length;
		result = document.cookie.substring(start,end);
	}
	else
	{
		result = "NA";
	}
}


// Initialize cookie for the first time
function initcookie()
{
	// Setup initial data
	addToCookieExpiring("game", "taipan");
	
	addToCookie("c_hong", c_hong);
	addToCookie("c_place", c_place);
	addToCookie("c_locus", c_locus);
	addToCookie("c_last_locus", c_last_locus);
	addToCookie("c_ship", c_ship);
	addToCookie("c_damage", c_damage);
	addToCookie("c_gun", c_gun);
	addToCookie("c_cargo", c_cargo);
	addToCookie("c_tonnage", c_tonnage);
	addToCookie("c_jade_quant", c_jade_quant);
	addToCookie("c_silk_quant", c_silk_quant);
	addToCookie("c_arm_quant", c_arm_quant);
	addToCookie("c_rice_quant", c_rice_quant);
	addToCookie("c_gen_quant", c_gen_quant);

	addToCookie("c_cash", c_cash);
	addToCookie("c_debt", c_debt);
	addToCookie("c_payment", c_payment);
}

// FUNCTION: initialize_screen
// PURPOSE:  To initialize the screen input fields
function initialize_screen()
{
	document.holding.i_cash.value=c_cash;
	document.holding.i_debt.value=c_debt;
	document.holding.i_payment.value=c_payment;
	document.holding.i_hong.value=c_hong;
	document.holding.i_locus.value=c_locus;
	document.holding.i_last_locus.value=c_last_locus;
	document.holding.i_place.value=c_place;
	document.holding.i_ship.value=c_ship;
	document.holding.i_damage.value=c_damage;
	document.holding.i_gun.value=c_gun;
	document.holding.i_cargo.value=c_cargo;
	document.holding.i_tonnage.value=c_tonnage;
	document.holding.i_jade_quant.value=c_jade_quant;
	document.holding.i_silk_quant.value=c_silk_quant;
	document.holding.i_arm_quant.value=c_arm_quant;
	document.holding.i_rice_quant.value=c_rice_quant;
	document.holding.i_gen_quant.value=c_gen_quant;
	document.holding.i_selection.value=c_selection;
	document.holding.i_price.value=c_price;
	document.holding.i_quant.value=c_quant;
}


////////////////////////////////////////////////   FINAL CODE    ///////////////////////////////////////

// FUNCTION: setupUser
// PURPOSE:  To Setup User for the first time
function setupUser()
{
	initcookie();
	initialize_screen();
	
	// addToCookieExpiring("userName", "darby");
	bSetupComplete=true;
	
}



// FUNCTION: borrowFund
// PURPOSE:  To enable the user to borrow money from Wu
function borrowFund()
{
	// Capture the amount requested as cash
	c_debt=parseFloat(document.holding.i_debt.value);
	var max_to_borrow = stringCurrency(g_max_wu_loan);

	// If we are asking for too much, warn the user.
	if (c_debt > g_max_wu_loan)
	{
		alert('Honorable Wu is not to be robbed! He will only loan you up to ' + max_to_borrow + ' for starting your business!');
	}
	else
	{
		// Borrow this much money
		c_cash=c_debt;
		document.holding.i_cash.value=c_cash;
	
		// Save the user information
		saveUser();

		// Now go to the home window
		window.location = "home.htm";
	}
	
}


// FUNCTION: saveUser
// PURPOSE:  To save the user settings
function saveUser()
{
	// Capture current data in the cookie
	c_cash=parseFloat(document.holding.i_cash.value);
	c_debt=parseFloat(document.holding.i_debt.value);
	c_payment=parseFloat(document.holding.i_payment.value);
	c_hong=document.holding.i_hong.value;
	c_locus=parseInt(document.holding.i_locus.value);
	c_last_locus=parseInt(document.holding.i_last_locus.value);
	c_place=document.holding.i_place.value;
	c_ship=parseInt(document.holding.i_ship.value);
	c_damage=parseFloat(document.holding.i_damage.value);
	c_gun=parseInt(document.holding.i_gun.value);
	c_cargo=parseInt(document.holding.i_cargo.value);
	c_tonnage=parseInt(document.holding.i_tonnage.value);
	c_jade_quant=parseInt(document.holding.i_jade_quant.value);
	c_silk_quant=parseInt(document.holding.i_silk_quant.value);
	c_arm_quant=parseInt(document.holding.i_arm_quant.value);
	c_rice_quant=parseInt(document.holding.i_rice_quant.value);
	c_gen_quant=parseInt(document.holding.i_gen_quant.value);
	c_selection=parseInt(document.holding.i_selection.value);
	c_price=parseFloat(document.holding.i_price.value);
	c_quant=parseInt(document.holding.i_quant.value);
	
	// Recreate the cookie
	addToCookieExpiring("game", "taipan");
	
	addToCookie("c_cash", c_cash);
	addToCookie("c_debt", c_debt);
	addToCookie("c_payment", c_payment);

	addToCookie("c_hong", c_hong);
	addToCookie("c_locus", c_locus);
	addToCookie("c_last_locus", c_last_locus);
	addToCookie("c_place", c_place);
	addToCookie("c_ship", c_ship);
	addToCookie("c_damage", c_damage);
	addToCookie("c_gun", c_gun);
	addToCookie("c_cargo", c_cargo);
	addToCookie("c_tonnage", c_tonnage);
	addToCookie("c_jade_quant", c_jade_quant);
	addToCookie("c_silk_quant", c_silk_quant);
	addToCookie("c_arm_quant", c_arm_quant);
	addToCookie("c_rice_quant", c_rice_quant);
	addToCookie("c_gen_quant", c_gen_quant);

	addToCookie("c_selection", c_selection);
	addToCookie("c_price", c_price);
	addToCookie("c_quant", c_quant);
}


///////////////////////////////////////////   END INITIALIZATION CODE    ////////////////////////////////


//  End


