var rowCount = 1;
var clickArea = 0;
var bFlag = 0;
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&ia.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function setInputboxColours(id){
	if (rowCount > 1){
		document.getElementById('itemDesc' + eval(rowCount - 1)).style.border = 'solid rgb(165,172,178) 1px';
		document.getElementById('rate' + eval(rowCount - 1)).style.border = 'solid rgb(165,172,178) 1px';
		document.getElementById('value' + eval(rowCount - 1)).style.border = 'solid rgb(165,172,178) 1px';
		document.getElementById('qtyCount' + eval(rowCount - 1)).style.border = 'solid rgb(165,172,178) 1px';
		document.getElementById('date' + eval(rowCount - 1)).style.border = 'solid rgb(165,172,178) 1px';
	}
}

function DeleteTheRow(therow, rowID){
		var objRow = eval(therow);
		objRow.style.display = 'none'; 
		eval("document.taxFrm.itemID"+rowID+".value = -1");
		eval("document.taxFrm.value"+rowID+".value = -1");
		eval("document.taxFrm.rate"+rowID+".value = -1");
		eval("document.taxFrm.itemDesc"+rowID+".value = -1");
}

function secondSwap(thecell){

	var fieldsOK = true;
	var errorMsgText = "<ul class='errorList'>";
	var dots = '<li>';
	rowPos = rowCount - 1;
	
	if (isRequired(eval("document.taxFrm.rate"+ eval(rowPos)+".value"))){
		fieldsOK = false;
		errorMsgText += dots+"Please Enter the Decpreciation Rate for Item <B>"+ rowPos +"</B></li>";
		document.getElementById('rate' + eval(rowPos)).style.border = 'solid rgb(163,23,49) 2px';
	} else {
		document.getElementById('rate' + eval(rowPos)).style.border = 'solid rgb(165,172,178) 1px';
	}
	
	if (isRequired(eval("document.taxFrm.value"+ eval(rowPos)+".value"))){
		fieldsOK = false;
		errorMsgText += dots+"Please Enter the Original Cost for Item <B>"+ rowPos +"</B></li>";
		document.getElementById('value' + eval(rowPos)).style.border = 'solid rgb(163,23,49) 2px';
	} else {
		document.getElementById('value' + eval(rowPos)).style.border = 'solid rgb(165,172,178) 1px';
	}
	
	if (isRequired(eval("document.taxFrm.qtyCount"+ eval(rowPos)+".value"))){
		fieldsOK = false;
		errorMsgText += dots+"Please Enter the Quantity for Item <B>"+ rowPos +"</B></li>";
		document.getElementById('qtyCount' + eval(rowPos)).style.border = 'solid rgb(163,23,49) 2px';
	} else {
		document.getElementById('qtyCount' + eval(rowPos)).style.border = 'solid rgb(165,172,178) 1px';
	}
	
	document.getElementById('date' + eval(rowPos)).style.border = 'solid rgb(165,172,178) 1px';
	
	if (fieldsOK){
		if (rowCount > 1){
			eval("okabcFiller"+eval(rowCount - 1)).style.display = '';
			eval("okabc"+eval(rowCount - 1)).style.display = 'none';
		}
	
		ShowHideTheRow('selectFX');
		ShowHideTheRow('enterFX');
	} else {
		popSizedWin("error.php?error=" + errorMsgText, 500, 500);
	}
}

function firstSwap(){
	if (rowCount > 1){
		eval("okabcFiller"+eval(rowCount - 1)).style.display = '';
		eval("okabc"+eval(rowCount - 1)).style.display = 'none';
	}
	ShowHideTheRow('selectFX');
	ShowHideTheRow('enterFX');
}

function addNewRow(menuID) {

	if (getItemID(menuID) > 0){			// so users don't select "select an item below"
		setInputboxColours(0);

		// disable reset button
		firstSwap();
		document.taxFrm.resetBtn.disabled = true;
		checkMethod();
		document.getElementById('enterFX').style.display = " ";		
		newRowstoAdd = "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\"><tr ID=\"blahrow"+rowCount+"\" style=\"display: asdf\">"+generateRows(menuID)+"</tr><table>";
		taxFrm.SCounter.value++;	// count number of settle dates
		rowSection.innerHTML += newRowstoAdd;
		document.getElementById('enterFX').style.display= '';
		document.getElementById('selectFX').style.display= 'none';
		rowCount++;
		taxFrm.SPos.value = rowCount;		// position of last settle date value
		//window.scroll(0,20000);
		resetMenus();

	}
}


function addNewRow2(menuID, pos, itemID) {
		// disable reset button
		setInputboxColours(0);
		firstSwap();
		document.taxFrm.resetBtn.disabled = true;
		checkMethod();
		newRowstoAdd = "<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\"><tr ID=\"blahrow"+rowCount+"\" style=\"display: asdf\">"+generateRows2(menuID, pos, itemID)+"</tr><table>";
		taxFrm.SCounter.value++;	// count number of settle dates
		rowSection.innerHTML += newRowstoAdd;
		rowCount++;
		document.getElementById('enterFX').style.display= '';
		document.getElementById('selectFX').style.display= 'none';
		taxFrm.SPos.value = rowCount;		// position of last settle date value
		//window.scroll(0,20000);
		resetMenus();
}

function addNewOtherRow() {
	// disable reset button
	setInputboxColours(1);
	firstSwap();
	document.taxFrm.resetBtn.disabled = true;
	checkMethod();
	newRowstoAdd = "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\"><tr ID=\"blahrow"+rowCount+"\" style=\"display: asdf\">"+generateOtherRows()+"</tr><table>";
	taxFrm.BCounter.value++;	// count number of building dates
	rowSection.innerHTML += newRowstoAdd;
	rowCount++;
	document.getElementById('enterFX').style.display= '';
	document.getElementById('selectFX').style.display= 'none';
	taxFrm.BPos.value = rowCount;		// position of last build date value
	//window.scroll(0,20000);
	resetMenus();
}

function getItemID(menuID){ // return drop down menu item
	switch(menuID){
		case '1': 	return taxFrm.itemMenu1.value; break;
		case '2': 	return taxFrm.itemMenu2.value; break;
		case '3': 	return taxFrm.itemMenu3.value; break;
		case '4': 	return taxFrm.itemMenu4.value; break;
		case '5': 	return taxFrm.itemMenu5.value; break;
		case '6': 	return taxFrm.itemMenu6.value; break;
		case '7': 	return taxFrm.itemMenu7.value; break;
	}
}

function getItemID2(menuID, pos){ // return drop down menu item
	switch(menuID){
		case '1': 	return taxFrm.itemMenu1.options[pos].value; break;
		case '2': 	return taxFrm.itemMenu2.options[pos].value; break;
		case '3': 	return taxFrm.itemMenu3.options[pos].value; break;
		case '4': 	return taxFrm.itemMenu4.options[pos] .value; break;
		case '5': 	return taxFrm.itemMenu5.options[pos].value; break;
		case '6': 	return taxFrm.itemMenu6.options[pos].value; break;
		case '7': 	return taxFrm.itemMenu7.options[pos].value; break;
	}
}

function insertDesc(menuID){
	switch(menuID){
		case '1': return 	document.forms[0].itemMenu1.options[document.forms[0].itemMenu1.selectedIndex].text; break;
		case '2': return 	document.forms[0].itemMenu2.options[document.forms[0].itemMenu2.selectedIndex].text; break;
		case '3': return 	document.forms[0].itemMenu3.options[document.forms[0].itemMenu3.selectedIndex].text; break;
		case '4': return 	document.forms[0].itemMenu4.options[document.forms[0].itemMenu4.selectedIndex].text; break;
		case '5': return 	document.forms[0].itemMenu5.options[document.forms[0].itemMenu5.selectedIndex].text; break;
		case '6': return 	document.forms[0].itemMenu6.options[document.forms[0].itemMenu6.selectedIndex].text; break;
		case '7': return 	document.forms[0].itemMenu7.options[document.forms[0].itemMenu7.selectedIndex].text; break;
	}
}

function insertDesc2(menuID, pos){
	switch(menuID){
		case '1': return 	document.forms[0].itemMenu1.options[pos].text; break;
		case '2': return 	document.forms[0].itemMenu2.options[pos].text; break;
		case '3': return 	document.forms[0].itemMenu3.options[pos].text; break;
		case '4': return 	document.forms[0].itemMenu4.options[pos].text; break;
		case '5': return 	document.forms[0].itemMenu5.options[pos].text; break;
		case '6': return 	document.forms[0].itemMenu6.options[pos].text; break;
		case '7': return 	document.forms[0].itemMenu7.options[pos].text; break;
	}
}

function returnRate2(tempDate, menuID, theItem){
	// which method are they using?
	var method = "";
	if (document.taxFrm.rateMethod[0].checked){
		method = document.taxFrm.rateMethod[0].value;
	} else {
		method = document.taxFrm.rateMethod[1].value;
	}
	
	// convert string date into a date type for easier comparisons
	var aDate = new Date(tempDate.substring(6,10), tempDate.substring(3,5)-1, tempDate.substring(0,2)); // the date that changes
	var date01 = new Date("1991", "07", "01"); // YYYY,MM,DD <- date we compare to
	var date02 = new Date("1992", "02", "26"); // YYYY,MM,DD <- date we compare to
	var date03 = new Date("1999", "09", "21"); // YYYY,MM,DD <- date we compare to
	var date04 = new Date("2000", "06", "30"); // YYYY,MM,DD <- date we compare to
	var date05 = new Date("2004", "07", "01"); // YYYY,MM,DD <- date we compare to

	// use appropriate db
	var x = null;
	if (method == "Diminishing"){
		x = DimiArray;
	} else {
		x = PrimeArray;
	}
	
	// p is the index into the x array
	var p = 0;

	if (aDate <= date01){ // before 1st July 1991
		p = 1;
	} else if ((aDate > date01) && (aDate <= date02)){
		// greater than the 1st of July 1991 and less than 26th Feb 1992
		p = 2;
	} else if ((aDate > date02) && (aDate <= date03)){
		// greater than the 27th Feb 1992 and less than 21st Sep 1999
		p = 3;
	} else if ((aDate > date03) && (aDate <= date04)){
		// greater than the 22nd Sep 1999 and less than 30th June 2000
		p = 4;
	} else if ((aDate > date04) && (aDate < date05)){
		// after 1st of July 2000
		p = 5;
	} else if (aDate >= date05) {
		p = 6;
	}
	var itemID = getItemID(menuID);
	if (theItem >= 0){
		itemID = theItem;
	}
//alert("D " + itemID + " | " + menuID);
	if (itemID == 0){
		itemID = eval("document.taxFrm.itemID"+eval(rowCount - 1)+".value");
		//alert(eval("document.taxFrm.itemID"+eval(rowCount - 1)+".value"));
	}

//alert("D2 " + itemID + " | " + rowCount);	
	// first row in items displayed
	if (itemID == null){
		itemID = document.taxFrm.itemID1.value;
	}
	
//alert("D3 " + itemID + " | " + rowCount);
	var rate = 0;
	// search for the item and return the rate
	for (var i = 0;i<x.length;i++)
	{
	  if (x[i][0] == itemID){
		  	rate = x[i][p];
	  }
	}
	
	if ((itemID == 91) || (itemID == 92)){
		rate = "";
	}
	
	return rate;
}

function generateRows(menuID){
	saveNewDate(1);
	tempDate = taxFrm.settleDateValue.value;
	rate = returnRate2(tempDate, menuID, -1);

return "<input type=\"hidden\" name=\"itemID"+rowCount+"\" value=\"" + getItemID(menuID) + "\" class=\"BodyCopy10px\"><td width=\"16\"><img src=\"images\\delete.jpg\" width=\"10\" height=\"12\" border=\"0\" style=\"cursor:hand;\" title=\"Delete This Item\" onClick=\"if (confirm(\'Are you sure you want to delete this Item?\')){DeleteTheRow(blahrow"+rowCount+", "+rowCount+")}\"></td><td width=\"167\" class=\"BodyCopy10px\"><input type=\"hidden\" id=\"itemDesc"+rowCount+"\" name=\"itemDesc"+rowCount+"\" value=\'" + insertDesc(menuID) + "\' class=\"BodyCopy10px\" size=\"19\">"+ insertDesc(menuID) +"</td>" +
	"<td width=\"75\" class=\"BodyCopy10px\"><input id=rate" + rowCount +" name=rate" + rowCount +" type=\"text\" onKeyPress=numericValidation(); maxlength=\"5\" size=\"4\" class=\"BodyCopy10pxOrangeBorder\" value=\""+ rate +"\"> %</td>"+
	"<td width=\"119\" class=\"BodyCopy10px\">$ <input id=value" + rowCount +" name=value" + rowCount +" onKeyPress=\"numericValidation();\" type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"8\"></td>" +
	"<td width=\"100\"><input id=date" + rowCount +" name=date" + rowCount +" type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"11\" value=\"" + taxFrm.settleDateValue.value + "\" readonly onFocus=\"blur(); return showCalendar(\'date"+rowCount+"\', \'dd/mm/y\',\'\', \'\');\"></td><td id=\"okabc"+rowCount+"\" width=\"30\"><a href=\"javascript:;\" onClick=\"secondSwap(okabc"+rowCount+");\"><img src=\"images/ok.gif\" border=\"0\" width=\"23\" height=\"21\"></a></td><td width=\"30\" id=\"okabcFiller"+rowCount+"\" style=\"display: 'none'\" ><img src=\"images/spacer.gif\" width=\"30\"></td>";
}


function generateRows2(menuID, tpos, itemID){
	saveNewDate(1);
	tempDate = taxFrm.settleDateValue.value;
//alert("ID " + itemID);	
	rate = returnRate2(tempDate, menuID, itemID);
	
return "<input type=\"hidden\" name=\"itemID"+rowCount+"\" value=\"" + itemID + "\" class=\"BodyCopy10px\"><td width=\"12\"><img src=\"images\\delete.jpg\" width=\"10\" height=\"12\" border=\"0\" style=\"cursor:hand;\" title=\"Delete This Item\" onClick=\"if (confirm(\'Are you sure you want to delete this Item?\')){DeleteTheRow(blahrow"+rowCount+", "+rowCount+")}\"></td><td width=\"183\" class=\"BodyCopy10px\"><input type=\"hidden\" id=\"itemDesc"+rowCount+"\" name=\"itemDesc"+rowCount+"\" value=\'" + insertDesc2(menuID, tpos) + "\' class=\"BodyCopy10px\" size=\"19\">"+ insertDesc2(menuID, tpos) +"</td>" +
	"<td width=\"74\" class=\"BodyCopy10px\"><input id=\"rate" + rowCount +"\" name=rate" + rowCount +" type=\"text\" onKeyPress=numericValidation(); maxlength=\"5\" size=\"4\" class=\"BodyCopy10pxOrangeBorder\" value=\""+ rate +"\"> %</td>"+
	"<td width=\"119\" class=\"BodyCopy10px\">$ <input id=\"value" + rowCount +"\" name=value" + rowCount +" onKeyPress=\"numericValidation();\" type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"8\"></td>" +
	"<td width=\"15\" class=\"BodyCopy10px\"><input id=\"qtyCount" + rowCount +"\" name=qtyCount" + rowCount +" type=\"text\" onKeyPress=numericValidation(); maxlength=\"5\" size=\"4\" class=\"BodyCopy10pxOrangeBorder\" value=\"1\"></td>"+	
	"<td width=\"100\"><input id=\"date" + rowCount +"\" name=date" + rowCount +" type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"11\" value=\"" + taxFrm.settleDateValue.value + "\" readonly onFocus=\"blur(); return showCalendar(\'date"+rowCount+"\', \'dd/mm/y\',\'\', \'\');\"></td><td id=\"okabc"+rowCount+"\" width=\"30\"><a href=\"javascript:;\" onClick=\"secondSwap(okabc"+rowCount+");\"><img src=\"images/ok.gif\" border=\"0\" width=\"23\" height=\"21\"></a></td><td width=\"30\" id=\"okabcFiller"+rowCount+"\" style=\"display: 'none'\" ><img src=\"images/spacer.gif\" width=\"30\"></td>";
}

function roundNum(number,X) {
// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}



function generateOtherRows(){
	saveNewDate(2);
	return "<input type=\"hidden\" name=\"itemID"+rowCount+"\" value=\"0\" class=\"BodyCopy10px\"><td width=\"16\"><img src=\"images\\delete.jpg\" width=\"10\" height=\"12\" border=\"0\" style=\"cursor:hand;\" title=\"Delete This Item\" onClick=\"if (confirm(\'Are you sure you want to delete this Item?\')){DeleteTheRow(blahrow"+rowCount+", "+rowCount+")}\"></td><td width=\"155\"><input id=\"itemDesc"+rowCount+"\" type=\"text\" name=itemDesc"+rowCount+" class=\"BodyCopy10pxOrangeBorder\" size=\"19\"></td>" + 
	"<td width=\"75\" class=\"BodyCopy10px\"><input id=\"rate" + rowCount +"\" name=rate" + rowCount +" type=\"text\" onKeyPress=numericValidation(); maxlength=\"5\" size=\"4\" class=\"BodyCopy10pxOrangeBorder\"> %</td>"+
	"<td width=\"119\" class=\"BodyCopy10px\">$ <input id=\"value" + rowCount +"\" name=value" + rowCount +" onKeyPress=numericValidation(); type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"8\"></td>" +
	"<td width=\"75\" class=\"BodyCopy10px\"><input id=\"qtyCount" + rowCount +"\" name=qtyCount" + rowCount +" type=\"text\" onKeyPress=numericValidation(); maxlength=\"5\" size=\"4\" class=\"BodyCopy10pxOrangeBorder\" value=\"1\"></td>"+	
	"<td width=\"100\"><input id=\"date" + rowCount +"\" name=date" + rowCount +" type=\"text\" class=\"BodyCopy10pxOrangeBorder\" size=\"11\" value=\"" + taxFrm.buildingDateValue.value + "\" readonly onFocus=\"blur(); return showCalendar(\'date"+rowCount+"\', \'dd/mm/y\',\'\', \'\');\"></td><td id=\"okabc"+rowCount+"\" width=\"30\"><a href=\"javascript:;\" onClick=\"secondSwap(okabc"+rowCount+");\"><img src=\"images/ok.gif\" border=\"0\" width=\"23\" height=\"21\"></a></td><td width=\"30\" id=\"okabcFiller"+rowCount+"\" style=\"display: 'none'\" ><img src=\"images/spacer.gif\" width=\"30\"></td>";
}
