//function to get the content of a specified cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

//function to list all apartments that have been viewed during the users session
function listApts() {
	if (getCookie("aptsviewed") != null) {
		document.write("<div style='margin:-10px 0px 10px;border:2px solid #4279AD;font-size:10px'><span style='font-weight:bold;display:block;background-color:#DEEFFF;padding:1px 3px'>You have recently viewed the following apartments:</span><span style='padding:1px 3px'><b>hint</b>: tick a checkbox to add that apartment to the enquiry form</span><span style='display:block;padding:1px 3px'>");
		var apts = getCookie("aptsviewed");
		var apt = apts.split("&");
			alert('1');
		for (var i=0;i<apt.length;i++) {			
			document.write("<input type='checkbox' style='position:relative;top:2px;padding-left:30px;margin-left:25px' onclick='addToForm(this)'>"+apt[i]);
		}
		document.write("</span></div>");
	}
}

//function to add checked apartments to the enquiry form
function addToForm(apt) {
	//get the name of the apartment
	var apartment = apt.nextSibling.nodeValue;
	var selects = document.getElementsByTagName("select");
	//the box was checked
	if (apt.checked == true) {
		//loop through all select menus
		for (var i=0;i<selects.length;i++) {
			//add the apartment to the first empty apartment dropdown
			if (selects[i].name == "Apartment") {
				if (selects[i].value == "") {
					selects[i].value = apartment;
					break;
				}
			}
			//if none are empty then add a new row with the apartment in it
			if (i+1 == selects.length) {
				var addbox = document.getElementById("addnewcheckbox");
				addRow(addbox);
			}
		}
	}
	//the box was unchecked so the apartment needs to be removed
	else {
		//loop through all selects
		for (var i=0;i<selects.length;i++) {
			//the current select has the same value as the unchecked apartment
			if (selects[i].value == apartment) {
				//the current select is the very first one
				if (selects[i-1].name == "year") {
					//set its value to empty
					selects[i].value = "";
					//replace all apartment selects with the one after it so there are no gaps if the removed apartment was in front of some
					//loop through all selected
					for (var j=0;j<selects.length;j++) {
						//check that there is more than 1 apartment select
						if (selects.length-3 > 1) {
							//the current select is not the last one
							if (j+1 < selects.length) {
								//set the value of it to the next select
								selects[j].value = selects[j+1].value
							}
							//the current select is the last one
							else {
								//so delete its row
								removeRow(selects[j])
							}
						}
					}
				}
				//the current select is not the very first one so just delete it
				else {
					removeRow(selects[i]);
				}
			}
		}
	}	
}