function initialize()
{
	var temp;
	initMatrix("form_contents0");
	fillMatrix();

	if(complete_report!="")
		reloadTable(complete_report);
	setPersonal();
	setHomeCurrencyIndex();
	getTodaysDate();
}

var num_of_load_menu_entries=10;
var num_of_params=10;
var max_columns = 7;
var row_number=1;
var num_of_rows= 3*2;
var form_width = 800;
var matrix_division = 100;
var matrix_repetition = 1;
var max_repetitions = 1;
var max_rows = matrix_division*max_repetitions+1;

var setWidth = 130;
var table_sub_rows = new Array();
var loadMenu = new Array(num_of_load_menu_entries);
var submitArray = new Array();
var selected_currencies_array;
var selected_currencies_index;

var load_menu = true;
var user_name = "";

var session = false;
var deleteMenuOffset = 0;
var file_view_choice = new Array();
var file_list = new Array();
// keeps track of the row which is uploading an image
var row = null;

function initMatrix(target)
{
	var limit = matrix_repetition*matrix_division + 1;
	var text;
	var code = '<table id="main_table'+matrix_repetition+'" width="'+form_width+'" cellspacing="0" cellpadding="0" width="100%" border="1" bordercolor="#CCCCCC">';
	var start = 0;
	setWidth = 130;
	if(matrix_repetition>1) {
		start = 1+matrix_division*(matrix_repetition-1);
		setWidth = 140;
	}

	for(var i=start; i<limit; i++) {
		if(i==0){
			code += '<tr id="row'+i+'" bgcolor="#DFDFDF" align="center">';
		} else {
			code += '<tr style="display:none;" id="row'+i+'" align="center">';
		}

		if(i!=0 && (1-i%2)) {
			var temp = max_columns-1;
			text = "row"+i+"column0";
			code += '<td bgcolor="#DFDFDF" id='+text+'></td>';
			text = "row"+i+"column1";
			code += '<td colspan="'+temp+'" id="'+text+'"></td>';
		} else {
			for(var j=0; j< max_columns ; j++) {
				text = "row"+i+"column"+j;
				if(j==0){
					code +='<td bgcolor="#DFDFDF" id="'+text+'" width="'+setWidth+'"></td>';
				}else{
					code +='<td id="'+text+'"></td>';
				}
			}
		}
		code += '</tr>';
	}
	code += '</table>';

	document.getElementById(target).innerHTML=code;
}

function fillMatrix()
{
	createMenu();
	while(row_number<=num_of_rows) {
		createRow(row_number);
		file_view_choice[(row_number-1)/2] = 0;
		row_number+=2;
	}
}

// populates headers
function createMenu()
{
	var ref;

	ref = document.getElementById("row0column0");
	ref.innerHTML = 'No.';
	ref = document.getElementById("row0column1");
	ref.innerHTML = 'Expense Type';
	ref = document.getElementById("row0column2");
	ref.innerHTML = '<a href="#" onClick="return openWindow(\'description.html#date\',\'description\',\'scrollbars=yes,resizable=yes,width=300,height=300\')">Date&nbsp;of&nbsp;Purchase<br/>(mm/dd/yyyy)</a>';
	ref = document.getElementById("row0column3");
	ref.innerHTML = '<a href="#" onClick="return openWindow(\'description.html#amount\',\'description\',\'scrollbars=yes,resizable=yes,width=300,height=300\')">Amount</a>';
	ref = document.getElementById("row0column4");
	ref.innerHTML = 'Currency of Purchase';
	ref = document.getElementById("row0column5");
	ref.innerHTML = '<a href="#" onClick="return openWindow(\'description.html#payment\',\'description\',\'scrollbars=yes,resizable=yes,width=300,height=300\')">Payment Method and Rate</a>';
	ref = document.getElementById("row0column6");
	ref.innerHTML = 'Exchange Rate';
}

/**
* Fills a row with the appropriate fields
*/
function createRow(number)
{
	var code;
	var ref;

	ref = document.getElementById("row"+number+"column0");
document.getElementById("row"+number).style.display = "";

//the value for the area object is increased by one to signify that the sub row
//is in the next row of the matrix; the index for the main row should be odd.
	ref.width=setWidth;
	ref.innerHTML = '<b>'+(number+1)/2+'</b><img border="0" align=right id="image'+number+'" src="arrowDown.gif" width="7" height="10" class="cursorChange" usemap="#arrow'+number+'"/>';
	ref.innerHTML += '<map id="arrow'+number+'" name="arrow'+number+'"><area coords="1,1,7,10" onClick="deploySubRow('+(number+1)+')"></map>';

	//expense type : INPUT FIELD IS expen_field_#
	ref = document.getElementById("row"+number+"column1");
	ref.width = "160";
	code = '<select id="'+'expen_field_'+number+'" onBlur="this.selectedIndex=checkExpenseType(this.selectedIndex);"><option value=-1 selected>Choose expense type...</option><option value=-1>====TRAVEL====</option><option value="Air Travel">Air Travel</option><option value="Ground Transport">Ground Transport</option><option value="Rental Car">Rental Car</option><option value="Parking/Gas">Parking/Gas</option><option value="Lodging">Lodging</option><option value="Mileage">Mileage</option><option value=-1>====MEALS====</option><option value="Breakfast">Breakfast</option><option value="Lunch">Lunch</option>';
	code += '<option value="Dinner">Dinner</option><option value=-1>====OTHER====</option><option value="Books/magazines">Books/magazines</option><option value="Dues/Subscription">Dues/Subscriptions</option><option value="Copies">Copies</option><option value="Computer hardware">Computer hardware</option><option value="Computer software">Computer software</option><option value="Employee development">Employee development</option><option value="Entertainment">Entertainment</option><option value="Office supplies">Office supplies</option>';
	code += '<option value="Postage/shipping">Postage/shipping</option><option value="Seminar/conference">Seminar/conference</option><option value="Telecom">Telecom</option><option value="Cash Advance">Cash Advance</option><option value="Miscellaneous">Miscellaneous</option></select>';
	ref.innerHTML = code;

	//date of purchase : INPUT FIELDS ARE date_field_#, date_field1_#, and date_field2_#
	ref = document.getElementById("row"+number+"column2");
	ref.innerHTML = '<input size=13 id="'+'date_field_'+number+'" type="text" maxLength="10" onBlur="this.value=checkDate(this.value);">';

	//amount
	ref = document.getElementById("row"+number+"column3");
	ref.innerHTML = '<input size=10 id="'+'am_field_'+number+'" type="text" name="Amount_rf" maxLength="255" onBlur= "this.value = checkNumber(this.value, this.name);">';

	//currency of purchase : INPUT FIELD IS curr_field_#
	code = '<select id="'+'curr_field_'+number+'"><option value=-1 selected>Currency used';
code+='<option value="USD">US Dollar . USD<option value="AFA">Afghanistan Afghani . AFA<option value="ALL">Albanian Lek . ALL<option value="DZD">Algerian Dinar . DZD<option value="ADF">Andorran Franc . ADF<option value="ADP">Andorran Peseta . ADP<option value="AOA">Angolan Kwanza . AOA<option value="AON">Angolan New Kwanza . AON<option value="ARS">Argentine Peso . ARS<option value="AMD">Armenian Dram . AMD<option value="AWG">Aruban Florin . AWG<option value="AUD">Australian Dollar . AUD';
code+='<option value="ATS">Austrian Schilling . ATS<option value="AZM">Azerbaijan Manat . AZM<option value="AZN">Azerbaijan New Manat . AZN<option value="BSD">Bahamian Dollar . BSD<option value="BHD">Bahraini Dinar . BHD<option value="BDT">Bangladeshi Taka . BDT<option value="BBD">Barbados Dollar . BBD<option value="BYR">Belarusian Ruble . BYR<option value="BEF">Belgian Franc . BEF<option value="BZD">Belize Dollar . BZD<option value="BMD">Bermudian Dollar . BMD<option value="BTN">Bhutan Ngultrum . BTN';
code+='<option value="BOB">Bolivian Boliviano . BOB<option value="BAM">Bosnia and Herzegovina Convertible Mark . BAM<option value="BWP">Botswana Pula . BWP<option value="BRL">Brazilian Real . BRL<option value="GBP">British Pound . GBP<option value="BND">Brunei Dollar . BND<option value="BGN">Bulgarian Lev . BGN<option value="BIF">Burundi Franc . BIF<option value="XOF">CFA Franc BCEAO . XOF<option value="XAF">CFA Franc BEAC . XAF<option value="XPF">CFP Franc . XPF<option value="KHR">Cambodian Riel . KHR';
code+='<option value="CAD">Canadian Dollar . CAD<option value="CVE">Cape Verde Escudo . CVE<option value="KYD">Cayman Islands Dollar . KYD<option value="CLP">Chilean Peso . CLP<option value="CNY">Chinese Yuan Renminbi . CNY<option value="COP">Colombian Peso . COP<option value="KMF">Comoros Franc . KMF<option value="CDF">Congolese Franc . CDF<option value="CRC">Costa Rican Colon . CRC<option value="HRK">Croatian Kuna . HRK<option value="CUC">Cuban Convertible Peso . CUC<option value="CUP">Cuban Peso . CUP';
code+='<option value="CYP">Cyprus Pound . CYP<option value="CZK">Czech Koruna . CZK<option value="DKK">Danish Krone . DKK<option value="DJF">Djibouti Franc . DJF<option value="DOP">Dominican R. Peso . DOP<option value="NLG">Dutch Guilder . NLG<option value="XEU">ECU . XEU<option value="XCD">East Caribbean Dollar . XCD<option value="ECS">Ecuador Sucre . ECS<option value="EGP">Egyptian Pound . EGP<option value="SVC">El Salvador Colon . SVC<option value="EEK">Estonian Kroon . EEK';
code+='<option value="ETB">Ethiopian Birr . ETB<option value="EUR">Euro . EUR<option value="FKP">Falkland Islands Pound . FKP<option value="FJD">Fiji Dollar . FJD<option value="FIM">Finnish Markka . FIM<option value="FRF">French Franc . FRF<option value="GMD">Gambian Dalasi . GMD<option value="GEL">Georgian Lari . GEL<option value="DEM">German Mark . DEM<option value="GHC">Ghanaian Cedi . GHC<option value="GIP">Gibraltar Pound . GIP<option value="XAU">Gold (oz.) . XAU<option value="GRD">Greek Drachma . GRD';
code+='<option value="GTQ">Guatemalan Quetzal . GTQ<option value="GNF">Guinea Franc . GNF<option value="GYD">Guyanese Dollar . GYD<option value="HTG">Haitian Gourde . HTG<option value="HNL">Honduran Lempira . HNL<option value="HKD">Hong Kong Dollar . HKD<option value="HUF">Hungarian Forint . HUF<option value="ISK">Iceland Krona . ISK<option value="INR">Indian Rupee . INR<option value="IDR">Indonesian Rupiah . IDR<option value="IRR">Iranian Rial . IRR<option value="IQD">Iraqi Dinar . IQD';
code+='<option value="IEP">Irish Punt . IEP<option value="ILS">Israeli New Shekel . ILS<option value="ITL">Italian Lira . ITL<option value="JMD">Jamaican Dollar . JMD<option value="JPY">Japanese Yen . JPY<option value="JOD">Jordanian Dinar . JOD<option value="KZT">Kazakhstan Tenge . KZT<option value="KES">Kenyan Shilling . KES<option value="KWD">Kuwaiti Dinar . KWD<option value="KGS">Kyrgyzstanian Som . KGS<option value="LAK">Lao Kip . LAK<option value="LVL">Latvian Lats . LVL';
code+='<option value="LBP">Lebanese Pound . LBP<option value="LSL">Lesotho Loti . LSL<option value="LRD">Liberian Dollar . LRD<option value="LYD">Libyan Dinar . LYD<option value="LTL">Lithuanian Litas . LTL<option value="LUF">Luxembourg Franc . LUF<option value="MOP">Macau Pataca . MOP<option value="MKD">Macedonian Denar . MKD<option value="MGA">Malagasy Ariary . MGA<option value="MGF">Malagasy Franc . MGF<option value="MWK">Malawi Kwacha . MWK<option value="MYR">Malaysian Ringgit . MYR';
code+='<option value="MVR">Maldive Rufiyaa . MVR<option value="MTL">Maltese Lira . MTL<option value="MRO">Mauritanian Ouguiya . MRO<option value="MUR">Mauritius Rupee . MUR<option value="MXN">Mexican Peso . MXN<option value="MDL">Moldovan Leu . MDL<option value="MNT">Mongolian Tugrik . MNT<option value="MAD">Moroccan Dirham . MAD<option value="MZM">Mozambique Metical . MZM<option value="MMK">Myanmar Kyat . MMK<option value="ANG">NL Antillian Guilder . ANG<option value="NAD">Namibia Dollar . NAD';
code+='<option value="NPR">Nepalese Rupee . NPR<option value="NZD">New Zealand Dollar . NZD<option value="NIO">Nicaraguan Cordoba Oro . NIO<option value="NGN">Nigerian Naira . NGN<option value="KPW">North Korean Won . KPW<option value="NOK">Norwegian Kroner . NOK<option value="OMR">Omani Rial . OMR<option value="PKR">Pakistan Rupee . PKR<option value="XPD">Palladium (oz.) . XPD<option value="PAB">Panamanian Balboa . PAB<option value="PGK">Papua New Guinea Kina . PGK<option value="PYG">Paraguay Guarani . PYG';
code+='<option value="PEN">Peruvian Nuevo Sol . PEN<option value="PHP">Philippine Peso . PHP<option value="XPT">Platinum (oz.) . XPT<option value="PLN">Polish Zloty . PLN<option value="PTE">Portuguese Escudo . PTE<option value="QAR">Qatari Rial . QAR<option value="ROL">Romanian Lei . ROL<option value="RON">Romanian New Lei . RON<option value="RUB">Russian Rouble . RUB<option value="RWF">Rwandan Franc . RWF<option value="WST">Samoan Tala . WST<option value="STD">Sao Tome/Principe Dobra . STD';
code+='<option value="SAR">Saudi Riyal . SAR<option value="RSD">Serbian Dinar . RSD<option value="SCR">Seychelles Rupee . SCR<option value="SLL">Sierra Leone Leone . SLL<option value="XAG">Silver (oz.) . XAG<option value="SGD">Singapore Dollar . SGD<option value="SKK">Slovak Koruna . SKK<option value="SIT">Slovenian Tolar . SIT<option value="SBD">Solomon Islands Dollar . SBD<option value="SOS">Somali Shilling . SOS<option value="ZAR">South African Rand . ZAR<option value="KRW">South-Korean Won . KRW';
code+='<option value="ESP">Spanish Peseta . ESP<option value="LKR">Sri Lanka Rupee . LKR<option value="SHP">St. Helena Pound . SHP<option value="SDD">Sudanese Dinar . SDD<option value="SDP">Sudanese Pound . SDP<option value="SRD">Suriname Dollar . SRD<option value="SRG">Suriname Guilder . SRG<option value="SZL">Swaziland Lilangeni . SZL<option value="SEK">Swedish Krona . SEK<option value="CHF">Swiss Franc . CHF<option value="SYP">Syrian Pound . SYP<option value="TWD">Taiwan Dollar . TWD';
code+='<option value="TZS">Tanzanian Shilling . TZS<option value="THB">Thai Baht . THB<option value="TOP">Tonga Pa\'anga . TOP<option value="TTD">Trinidad/Tobago Dollar . TTD<option value="TND">Tunisian Dinar . TND<option value="TRL">Turkish Lira . TRL<option value="TRY">Turkish New Lira . TRY<option value="TMM">Turkmenistan Manat . TMM<option value="UGX">Uganda Shilling . UGX<option value="UAH">Ukraine Hryvnia . UAH<option value="UYU">Uruguayan Peso . UYU<option value="AED">Utd. Arab Emir. Dirham . AED';
code+='<option value="VUV">Vanuatu Vatu . VUV<option value="VEB">Venezuelan Bolivar . VEB<option value="VEF">Venezuelan Bolivar Fuerte . VEF<option value="VND">Vietnamese Dong . VND<option value="YER">Yemeni Rial . YER<option value="YUN">Yugoslav Dinar . YUN<option value="ZMK">Zambian Kwacha . ZMK<option value="ZWD">Zimbabwe Dollar . ZWD';
	code += '</select>';
	ref = document.getElementById("row"+number+"column4");
	ref.innerHTML = code;

	//payment method : INPUT FIELD IS pay_field_#
	code = '<td width="150"><select id="'+'pay_field_'+number+'"><option value=-1 selected>Payment Type</option><option value="Cash / Check">Cash / Check</option><option value="Personal Credit Card">Personal CreditCard</option>';
	code += '<option value="Corporate Card">Corporate Card</option><option value="Debit Card">Debit Card </option><option value="Trav. Check">Trav. Check</option></select></td>';
	ref = document.getElementById("row"+number+"column5");
	ref.innerHTML = code;

	//exchange rate : INPUT FIELD IS rate_field_#
	ref = document.getElementById("row"+number+"column6");
	ref.innerHTML = '<input size=8 id="'+'rate_field_'+number+'" type="text" name="Rate" maxLength="255" onBlur="this.value = checkRate(this.value, this.name);">';

	//sub row variables
	var numer = (number-1)/2;
	table_sub_rows[numer] = new Array('','','');
}

function eliminateRow(number)
{
	var temp;
	var limit = max_columns - ((max_columns - 2)*(1-number%2));
	for(var i=0; i< limit; i++) {
		temp = "row" + number + "column" + i;
		document.getElementById(temp).width = null;
		document.getElementById(temp).innerHTML = "";
	}
	document.getElementById('row'+number).style.display = 'none';
}

/**
* sets up and fills in the "sub-row"
* 	the "sub-row" contains the fields to enter the expense comments, location, and image
*/
function deploySubRow(number)
{
	var code;
	// removes the display style (should've been "none" to hide the row)
	document.getElementById("row"+number).style.display = '';

	//number is the row index on the matrix that can be filled by this sub row, the variable
	//does not represent the entry number. number/2 is the entry number

	// changes the arrow to correspond with the subrow being deployed (ie. down arrow removed and up arrow shown)
	// removes down arrow
	document.getElementById("row"+(number-1)+"column0").innerHTML = '<b>'+(number)/2+'</b>';

	// adds up arrow
	ref = document.getElementById("row"+number+"column0");
	ref.innerHTML = '<p width="15"></p><img border="0" align=left id="image'+number+'" src="arrowUp.gif" width="7" height="10" class="cursorChange" usemap="#arrow'+number+'"/>';
	ref.innerHTML += '<map id="arrow'+number+'" name="arrow'+number+'"><area borderwidth="0" coords="1,1,7,10" onClick="destroySubRow('+(number-1)+')"></map>';

	// builds the HTML for the fields for images, comments and location
	var index = number/2 - 1;
	ref = document.getElementById("row"+number+"column1");
	code = '<table width="*" border="0"><tr>';

	//comments
	code += '</tr><tr ><td valign="top" colspan="2">Comments :';
	code += '<textarea cols=40 rows=4 id="'+'com_field_'+(number-1)+'" wrap=soft onblur="this.value=checkLength(this.value,65535); setSubRow('+index+',1,this.value);">'+table_sub_rows[index][1]+'</textarea>';

	//location
	code += 'Location :';
	code +='<input size=15 id="loc_field_'+(number-1)+'" type="text" maxLength="255" onBlur="setSubRow('+index+',0,this.value)"></td>';

	code += '</tr></table>';

	// applies HTML code to the screen
	ref.innerHTML = code;
	var loc_field = document.getElementById('loc_field_'+(number-1));
	loc_field.value = table_sub_rows[index][0];

	// sets selected index to the image currently selected by user for this expense
	for(var i=0; i<image_field.length; i++){
		if(image_field.options[i].value == table_sub_rows[(number-2)/2][2]){
			image_field.selectedIndex = i;
		}
	}
}

/**
* Clears and hides the "subRow"
*	-clears the values stored for the subRow, and hides the subRow (ie. style display:none;)
*/
function destroySubRow(row_num)
{
	var ref;
	document.getElementById("row"+(row_num+1)).style.display = 'none';

	ref = document.getElementById("row"+row_num+"column0");
	ref.width=setWidth; ref.innerHTML = '<b>'+(row_num+1)/2+'</b><img border="0" align=right id="image'+row_num+'" src="arrowDown.gif" width="7" height="10" class="cursorChange" usemap="#arrow'+row_num+'"/>';
	ref.innerHTML += '<map id="arrow'+row_num+'" name="arrow'+row_num+'"><area coords="1,1,7,10" onClick="deploySubRow('+(row_num+1)+')"></map>';
	var temp=row_num + 1;
	document.getElementById("row"+temp).size = null;
	var number = (temp/2);

	//clears the elements of information
	for(var i=0; i<2; i++) {
		document.getElementById("row"+temp+"column"+i).width=null;
		document.getElementById("row"+temp+"column"+i).innerHTML = '';
	}
}

/**
* updates the array with the new information entered in a SubRow
*	indexes:
* 	0 - location
*	1 - comments
*  	2 - images
*/
function setSubRow(index, i, value)
{
	table_sub_rows[index][i] = value;
}

/**
* If number of rows is less than max rows, a new row is added, otherwise the
*   user is alerted they have the maximum number of rows already.
*/
function createNewRow()
{
	if (num_of_rows+2 < max_rows) {
		if(row_number > matrix_repetition*matrix_division) {
			matrix_repetition++;
			initMatrix("form_contents"+(matrix_repetition-1));
		}
		createRow(row_number);
		file_view_choice[(row_number-1)/2] = 0;
		num_of_rows += 2;
		row_number += 2;
	} else {
		alert("Cannot create any more rows");
	}
}

function createNewRow_check()
{
	createNewRow();
}

/**
* Deletes the last row in the table
*/
function deleteRow()
{
	if (num_of_rows!=0){
		row_number-=2;
		num_of_rows-=2;
		eliminateRow(row_number);
		eliminateRow(row_number+1);
	}
	table_sub_rows[(row_number-1)/2] = null;

	if((row_number-2) < (matrix_repetition-1)*matrix_division && matrix_repetition>1) {
		matrix_repetition--;
		document.getElementById("form_contents"+matrix_repetition).innerHTML = '';
	}
}

function deleteRow_check(){
	deleteRow();
}

function clearReport_check(){
	if(confirm('This action will erase the current form.\nDo you wish to continue?')){
		freshReport();
	}
}

/**
* Creates a new report
*	- deletes all rows
*	- clears the report description
*	- creates three fresh rows
*/
function freshReport(){
	while(num_of_rows>0){
		deleteRow();
	}
	for(var i=0; i<3; i++){
		createNewRow();
	}
	document.getElementById('report_desc').value = "";
}

/**
* Returns a string containing all the 'profile information'
* includes full name, company, email, home currency, and report description.
*/
function getGeneralInfo()
{
	var text="";
	var o;

	text += "full_name=" + document.getElementById("report_person_name").value + "&";
	text += "company=" + document.getElementById("report_comp_name").value + "&";
	text += "email=" + document.getElementById("report_email").value + "&";
	o = document.getElementById("homecurrency");
	text += "curr=" + o.options[o.selectedIndex].value + "&";
	text += "description=" + document.getElementById("report_desc").value + "&";

	return text;
}

/**
*  Checks each row to see if there is any information recorded. if there is
*  it's data is appended to a string containing all the information
*
* 	Returns a string containing each expense.
*/
function verifyForm(save_name)
{
	var postMsg = "";
	var empty = true;
	for(var i=1; i<=num_of_rows;) {
		if (checkRow(i)){
			postMsg+=submitRow(i);
			empty = false;
		}
		i+=2;
	}
	if(empty){
		alert("The report is empty.");
		return "";
	} else {
		return postMsg;
	}
}

/**
* Checks the row specified if it contains any information.
*
*	Returns true (contains information) / false (contains no information)
*/
function checkRow(index)
{
	temp1 = document.getElementById('expen_field_'+index);
	temp2 = document.getElementById('curr_field_'+index);
	temp3 = document.getElementById('pay_field_'+index);

	if(temp1.options[temp1.selectedIndex].value + "" != "-1" ||
	document.getElementById('date_field_'+index).value + "" != "" ||
	document.getElementById('am_field_'+index).value + "" != "" ||
	temp2.options[temp2.selectedIndex].value + "" != "-1" ||
	temp3.options[temp3.selectedIndex].value + "" != "-1" ||
	document.getElementById('rate_field_'+index).value + "" != "" ||
	table_sub_rows[(index-1)/2][0] + "" != "" ||
	table_sub_rows[(index-1)/2][1] + "" != "" ||
	table_sub_rows[(index-1)/2][2] + "" != "") {
		return true;
	} else {
		return false;
	}
}

/**
* Returns a string containing the information from the row specified
* 	Formatting will depend on the global variable "session"
*	"session" is true for generating a report and false for all other calls.
*/
function submitRow(index)
{
	var No, Expense_Type, Date_of_Purchase, Amount, Home_Currency;
	var Purchase_Currency, Payment_Method, Exchange_Rate;
	var Location, Comments, Image;
	var oldindex=index;
	var o;

	No = "" + ((index+1)/2);

	//expen_field_#
	Expense_Type = document.getElementById('expen_field_'+index).selectedIndex;

	Date_of_Purchase = document.getElementById('date_field_'+index).value;

	//am_field_#
	Amount = document.getElementById('am_field_'+index).value + "";

	//homecurrency
	o = document.getElementById('homecurrency');
	Home_Currency = o.options[o.selectedIndex].value;

	//curr_field_#
	o = document.getElementById('curr_field_'+index);
	Purchase_Currency = o.options[o.selectedIndex].value;
	if (Purchase_Currency == -1) { Purchase_Currency = Home_Currency; }

	//pay_field_#
	Payment_Method = document.getElementById('pay_field_'+index).selectedIndex;

	//rate_field_#
	Exchange_Rate = document.getElementById('rate_field_'+index).value + "";

	//loc_field_#
	index = (index+1)/2 - 1;
	Location = escape(table_sub_rows[index][0]);//document.getElementById('loc_field_'+index).value + "";

	Comments = escape(table_sub_rows[index][1]);//document.getElementById('com_field_'+index).value + "";

	Image = escape(table_sub_rows[index][2]);//document.getElementById('image_field_'+index).value + "";


	if(!session){
	//{No, Expen, Date, Am, Home_curr, Sell_curr, Pay, Rate, Loc, Com, Image};
		var msg = "No=" + No + "&Expen=" + Expense_Type + "&Date=" + Date_of_Purchase + "&Am=" + Amount;
		msg += "&Curr=" + Purchase_Currency + "&Pay=" + Payment_Method + "&Rate=" + Exchange_Rate;
		msg += "&Loc=" + Location + "&Com=" + Comments+ "&Image=" + Image + "&?";
	}
	else {
		var msg = No + "&" + Expense_Type + "&" + Date_of_Purchase + "&" + Amount;
		msg += "&" + Purchase_Currency + "&" + Payment_Method + "&" + Exchange_Rate;
		msg += "&" + Location + "&" + Comments+ "&" + Image + "&?";

		var o = document.getElementById('curr_field_'+oldindex);
		var p = o.options[o.selectedIndex].value;
		if (p == -1) { p = Home_Currency; }
		selected_currencies_array[selected_currencies_index++] = p + ":" + Date_of_Purchase;
	}

	return msg;
}

/**
* Gets the correct XML object depending on the users browser. Used in the xmlHTTP calls.
*/
function getXmlObject()
{
	var xmlhttp;
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp=false;
			}
		}
	}
	if (!xmlhttp) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

/**
* Sends a message through xmlHTTP to the server and returns the response text.
*/
function requestQuery(msg){
	var text;
	var xmlhttp = getXmlObject();
	if(!xmlhttp) {
		alert("Failed to initialize XMLHTTP object.  Please update your browser version.");
	} else {
		xmlhttp.open("POST", reportajaxURL, false);
		try {
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		} catch (e) {}
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState == 4) {
				text = xmlhttp.responseText;
				if(text.indexOf('?')<0 && text.length>4) {
					alert(text);
					if(text == "You need to be logged in to use this option.\r\n") {
						location.href = loginURL;
					}
				}
				return text;
			}
		}
		xmlhttp.send(msg);
	}
	if (xmlhttp.readyState == 4) {
		return xmlhttp.responseText;
	}
}


var refreshed_rows;//this index is used to erase the data of the rows
		//that are not overwritten by the loading process
function reloadTable(text)
{
var temp=0;
refreshed_rows = new Array(num_of_rows/2);
for(var i=0; i<(num_of_rows/2); i++)
	refreshed_rows[i]=false;

while((temp=text.indexOf("?"))>0) {
	reloadRow(text.substring(0, temp));
	text = text.substring((temp+1));
}

for(var i=0; i<refreshed_rows.length; i++) {
	if(refreshed_rows[i]==false)
		clearRow(2*i+1);
}
}

function reloadGeneralInfo(text)
{
var values = new Array(5);
for(var i=0; i<5 && (end=text.indexOf("&"))>-1; i++) {
	values[i] = text.substring(0, end);
	text = text.substring((end+1));
}

if(i!=5) {
		alert("Incomplete load from server.\n Load action failed.");
} else {
	document.getElementById("report_person_name").value = values[0];
	document.getElementById("report_comp_name").value = values[1];
	document.getElementById("report_email").value = values[2];
		var ref = document.getElementById("homecurrency");
		for(var i=0; i<ref.length; i++) {
			if(ref.options[i].value == values[3]) {
				ref.selectedIndex = i;
			}
		}
	document.getElementById("report_desc").value = values[4];
}
}

function reloadRow(text)
{
	var end=0;
	var values = new Array(num_of_params);
	for(var i=0; i<num_of_params && (end=text.indexOf("&"))>-1; i++) {
		values[i] = text.substring(0, end);
		text = text.substring((end+1));
	}

	if (i!=num_of_params)
		alert("Incomplete load from server.\n Load action failed.");
	else {
	//the following code is dependant on the way the table is coded
	//any changes to the table should be reflected here as well
	//num_of_params=10
	var info_row_num = parseInt(values[0]);

	if ((info_row_num-1) < refreshed_rows.length)
		refreshed_rows[info_row_num-1] = true;

	while(info_row_num > num_of_rows/2)
		createNewRow();

	var index=(2*info_row_num-1);
	var number=index;
	document.getElementById('expen_field_'+index).selectedIndex = values[1];
	document.getElementById('date_field_'+index).value = values[2];
	document.getElementById('am_field_'+index).value = values[3];
		var ref = document.getElementById('curr_field_'+index);
		for(var i=0; i<ref.length; i++) {
			if(ref.options[i].value == values[4]) {
				ref.selectedIndex = i;
			}
		}
	document.getElementById('pay_field_'+index).selectedIndex = values[5];
	document.getElementById('rate_field_'+index).value = values[6];
	//document.getElementById('image_field_'+index).value = unescape(values[9]);

	index = (index-1)/2;
	table_sub_rows[index][0] = unescape(values[7]);
	table_sub_rows[index][1] = unescape(values[8]);
	table_sub_rows[index][2].selectIndex = 3;
	table_sub_rows[index][2] = unescape(values[9]);

	var ref = document.getElementById("row"+number+"column0");
	ref.width=setWidth; ref.innerHTML = '<b>'+(number+1)/2+'</b><img border="0" align=right id="image'+number+'" src="arrowDown.gif" width="7" height="10" class="cursorChange" usemap="#arrow'+number+'"/>';
	ref.innerHTML += '<map id="arrow'+number+'" name="arrow'+number+'"><area coords="1,1,7,10" onClick="deploySubRow('+(number+1)+')"></map>';
	number++;
	for(var i=0; i<2; i++) {
			// document.getElementById("row"+number+"column"+i).width=null;
		document.getElementById("row"+number+"column"+i).innerHTML = '';
	}
	}
}

function clearRow(index)
{
	document.getElementById('expen_field_'+index).selectedIndex = 0;
	document.getElementById('date_field_'+index).value = "";
	document.getElementById('am_field_'+index).value = "";
	document.getElementById('curr_field_'+index).selectedIndex = 0;
	document.getElementById('pay_field_'+index).selectedIndex = 0;
	document.getElementById('rate_field_'+index).value = "";
	index = (index-1)/2;
	table_sub_rows[index][0] = "";
	table_sub_rows[index][1] = "";
	table_sub_rows[index][2] = "";
}

/**
 * Generates the string containing the expense report, sends it to the server to generate the report.
 */
function generateReport()
{
	var info = "G";
	var o;
	info += escape(document.getElementById("report_person_name").value) + "&";
	info += escape(document.getElementById("report_comp_name").value) + "&";
	info += escape(document.getElementById("report_desc").value) + "&";
	o = document.getElementById("homecurrency");
	info += o.options[o.selectedIndex].value + "&";
	info += escape(document.getElementById("report_email").value) + "&";
	info += o.options[o.selectedIndex].value + "&?";
	session = true;
	selected_currencies_array = new Array();
	selected_currencies_index = 0;
	var msg = verifyForm("blank");

	for(var i=0; i<selected_currencies_array.length; i++){
		info += selected_currencies_array[i] + "&";
	}
	info += "?";
	session = false;
	if(msg != "") {
		requestQuery(info+msg);
		window.location.href = expensereportURL;
	}
}

function generateSpreadSheet() {
	var info = "G";
	var o;
	info += escape(document.getElementById("report_person_name").value) + "&";
	info += escape(document.getElementById("report_comp_name").value) + "&";
	info += escape(document.getElementById("report_desc").value) + "&";
	o = document.getElementById("homecurrency");
	info += o.options[o.selectedIndex].value + "&";
	info += escape(document.getElementById("report_email").value) + "&";
	o = document.getElementById("homecurrency");
	info += o.options[o.selectedIndex].value + "&?";
	session = true;
	selected_currencies_array = new Array();
	selected_currencies_index = 0;
	var msg = verifyForm("blank");

	for(var i=0; i<selected_currencies_array.length; i++){
		info += selected_currencies_array[i] + "&";
	}
	info += "?";
	session = false;
	if(msg != "") {
		requestQuery(info+msg);
		window.location.href = reportcsvURL;
	}
}

function newWindow(rowNum)
{
row = rowNum;
//    var text = requestQuery("U");
//    if(text != "You need to be logged in to use this option.\r\n"){
var my_window;
if (!my_window || my_window.closed)
	my_window = window.open("","subWindow","height=200,width=550,resizable");
my_window.focus();
my_window.location.href = imagesaverURL;
//   }
}

function checkStatus(func){
	if(readCookie('JSESSIONID')!=null){
		eval(func);
	} else {
		alert("I'm sorry, you are not currently signed in; You are now being redirected to the login page.");
		location.href = loginURL;
	}
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0;i<ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ)==0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function showmouseover(x) { window.status = x; return true; }
function showmouseout() { window.status = null; return true; }
function openWindow(url,wind,feat) { window.open(url,wind,feat); return false; }
