var $counter = 2;

function createAddLink(){
	var $link = document.createElement("p");
	$link.className = "contact-link";
	var $linkText = document.createTextNode("add another transaction");
	var $linkNode = document.createElement("a");
	$linkNode.onclick = addBox;
	$linkNode.appendChild($linkText);
	$link.appendChild($linkNode);
	return $link;
}

function createRemoveLink(){
	var $link = document.createElement("p");
	$link.setAttribute("id", "removeLink");
	$link.className = "contact-link";
	var $linkText = document.createTextNode("remove last transaction");
	var $linkAttribute = "removeBox();";
	var $linkNode = document.createElement("a");
//	$linkNode.setAttribute("onclick", $linkAttribute);
	$linkNode.onclick = removeBox;
	$linkNode.appendChild($linkText);
	$link.appendChild($linkNode);
	return $link;
}

function createOption($value, $name){
	var $optionText = document.createTextNode($name);
	var $option = document.createElement("option");
	$option.setAttribute("value", $value);
	$option.appendChild($optionText);
	return $option;
}

function createSelect($id){
	var $select = document.createElement("select");
	$select.setAttribute("id", $id);
	$select.setAttribute("name", $id);
	return $select;
}

function createLabel($for, $name){
	var $labelText = document.createTextNode($name);
	var $label = document.createElement("label");
	$label.setAttribute("for", $for);
	$label.appendChild($labelText);
	return $label;
}

function addBox(){
	if(document.getElementById("removeLink")){
		document.getElementById("transactions").removeChild(document.getElementById("removeLink"));
	}
	var $fieldset = document.createElement("fieldset");
	$fieldset.setAttribute("id", "tr-"+$counter);
	var $legend = document.createElement("legend");
	var $legendText = document.createTextNode("Transaction "+$counter+" (optional)");
	$legend.appendChild($legendText);
	$fieldset.appendChild($legend);
	var $trLabel = createLabel("tr-type"+$counter, "Transaction Type:");
	$fieldset.appendChild($trLabel);
	var $trSelect = createSelect("tr-type"+$counter);
	$trSelect.appendChild(createOption("", "Select..."));
	$trSelect.appendChild(createOption("Sale", "Sale"));
	$trSelect.appendChild(createOption("Purchase", "Purchase"));
	$trSelect.appendChild(createOption("Remortgage", "Remortgage"));
	$trSelect.appendChild(createOption("Equity-Transfer", "Equity Transfer"));
	$trSelect.appendChild(createOption("Right-To-Buy", "Right To Buy"));
	$fieldset.appendChild($trSelect);
	$fieldset.appendChild(document.createElement("br"));
	var $tenureLabel = createLabel("tenure"+$counter, "Tenure:");
	$fieldset.appendChild($tenureLabel);
	var $tenureSelect = createSelect("tenure"+$counter);
	$tenureSelect.appendChild(createOption("", "Select..."));
	$tenureSelect.appendChild(createOption("Freehold", "Freehold"));
	$tenureSelect.appendChild(createOption("Leasehold", "Leasehold"));
	$tenureSelect.appendChild(createOption("Unregistered", "Unregistered"));
	$tenureSelect.appendChild(createOption("Unknown", "Unknown"));
	$fieldset.appendChild($tenureSelect);
	$fieldset.appendChild(document.createElement("br"));
	var $addressLabel = createLabel("tr-address"+$counter, "Property Address (inc. postcode):");
	$fieldset.appendChild($addressLabel);
	var $textarea = document.createElement("textarea");
	$textarea.setAttribute("id", "tr-address"+$counter);
	$textarea.setAttribute("name", "tr-address"+$counter);
	$textarea.setAttribute("rows", "4");
	$fieldset.appendChild($textarea);
	$fieldset.appendChild(document.createElement("br"));
	var $valueLabel = createLabel("tr-value"+$counter, "Property/Remortgage Value:");
	$fieldset.appendChild($valueLabel);
	var $input = document.createElement("input");
	$input.setAttribute("type", "text");
	$input.setAttribute("id", "tr-value"+$counter);
	$input.setAttribute("name", "tr-value"+$counter);
	$fieldset.appendChild($input);
	$fieldset.appendChild(document.createElement("br"));
	document.getElementById("transactions").appendChild($fieldset);
	document.getElementById("transactions").appendChild(createRemoveLink($counter));
	$counter++;
	return $counter;
}

function removeBox(){
	var $transactions = document.getElementById("transactions");
	var $element = "tr-"+($counter-1);
	var $remove = document.getElementById($element);
	$transactions.removeChild($remove);
	if(!document.getElementById("tr-2")){
		document.getElementById("transactions").removeChild(document.getElementById("removeLink"));
	}
	$counter--;
	return $counter;
}
