/* Scripts by d.went@brand-architects.co.uk */

/* Show / hide VIEWING DATE form element */

window.onload = function() {
	prepViewingDate();
}

function prepViewingDate() {
	var $viewingDate = document.getElementById('viewingDate');
	if ($viewingDate!=null) {
		$viewingDate.className='hideItem';
		var $viewingDateComment = document.getElementById('viewingDateComment');
		$viewingDateComment.className='hideItem';
	}
}

function revealViewingDate() {
	var $viewingDate = document.getElementById('viewingDate');
	if ($viewingDate!=null) {
		$viewingDate.className='showItem';
	}
}

/* END --------------------------------------------------------------------------- */





/* Open the map */
function openMap(ID,theX,theY)
{window.open('http://www.streetmap.co.uk/em/flexioffices.srf?account=flexioffices&arrow=y&x='+theX+'0&y='+theY+'0',ID,'resizable=yes,width=600,height=450,top=40,left=40');}
function openMap2(ID,thePostcode)
{window.open('http://www.streetmap.co.uk/em/flexioffices.srf?account=flexioffices&arrow=y&postcode='+thePostcode,ID,'resizable=yes,width=600,height=450,top=40,left=40');}

/* END --------------------------------------------------------------------------- */







// This next script checks whether cookies are enabled (and workable via javascript).
var cookieEnabled=(navigator.cookieEnabled)? true : false
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ //if not IE4+ nor NS6+
	document.cookie="testcookie"
	cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}


/* AddToReport and AddToReportCompleted explained:

AddToReport:
This function first of all checks if cookies are enabled (via the check above). If not, a js warning is given. Otherwise, the rest of the function carries out.
For each type of add (add / request viewing / request quote now) the office is added to that part of the cookie via the iframe. If the rest of the script 
doesn't work, at least it should be in the cookie!

AddToReportCompleted:
Once the ASP page that loads in the iframe has added the office ID to the cookie, it then fires this function. This ensures that only once added, does the image change, 
and the list get added to.
First of all, it checks to see if its in the list already. If not, then the item is added. Then, the images are swapped according the the add type. Then the alertMe function
is called if its the first add. */


function addToReport(theID,searchedLocation,officeTitle,typeOfAdd) {
	// Check if cookies are enabled on client's browser
	if (!cookieEnabled) { 
		alert("In order to create a report, flexioffices.co.uk needs you to have cookies enabled on your browser. If this isn't possible, please give us a call on 0800 197 1127 and one of our trained agents will create your search report for you.");
	}
	
	else { // Cookies are enabled, so go for it
	
		document.images['add'+theID].src='../imgs/but_add2.gif'; // Switch to the ADDING image
		if (typeOfAdd=='add') {
			window["addRep"].location.href='addToReport.asp?ID='+theID+'&SL='+searchedLocation+'&OT='+officeTitle; // Add to cookie via the iFrame
			document.images['add'+theID].blur(); // Remove the focus from it
		}
	
		if (typeOfAdd=='quote') {
			window["addRep"].location.href='addToReport.asp?ID='+theID+'&typ=quote&SL='+searchedLocation+'&OT='+officeTitle; // Add to cookie via the iFrame
			document.images['quote'+theID].blur(); // Remove the focus from it
		}
	
		if (typeOfAdd=='viewing') {
			window["addRep"].location.href='addToReport.asp?ID='+theID+'&typ=viewing&SL='+searchedLocation+'&OT='+officeTitle; // Add to cookie via the iFrame
			document.images['viewing'+theID].blur(); // Remove the focus from it
		}
	}
}

function addToReportCompleted(theID,searchedLocation,officeTitle,typeOfAdd) {
	if (checkListForID(theID)==false) {
		// Add the officeTitle to the display list
		var $listItem = document.createElement('li'); // create the li
		$listItem.setAttribute('id','listItem'+theID); // Give the li an id to allow for removing again
		var $listItemText = document.createTextNode(officeTitle+' '); // write the list item text
		$listItem.appendChild($listItemText); // Put the text into the li
		var $listItemLink = document.createElement('a'); // Create the a link
		$listItemLink.setAttribute('href','javascript:;'); // give it a href
		$listItemLink.onclick = function(){removeFromReport(theID);};
		var $listItemLinkText = document.createTextNode('remove'); // create the link text
		$listItemLink.appendChild($listItemLinkText); // append the link text to the link
		$listItem.appendChild($listItemLink); // append the link to the li text
		// Now put it into the list
		var $officeList = document.getElementById('officeList'); // Get the reference to the ul
		$officeList.appendChild($listItem); // put the link into the document
	}

	// Switch the images on the page
	document.images['add'+theID].src='../imgs/but_added.gif'; // Switch the Add to Report image
	document.images['add'+theID].blur(); // Remove the focus from it


	if (typeOfAdd=='quote') {
		document.images['quote'+theID].src='../imgs/but_quote_requested.gif'; // Switch the Add to Report image
		document.images['quote'+theID].blur(); // Remove the focus from it
	 }

	if (typeOfAdd=='viewing') {
		document.images['viewing'+theID].src='../imgs/but_viewing_requested.gif'; // Switch the Add to Report image
		document.images['viewing'+theID].blur(); // Remove the focus from it
	 }

	// Alert if its the first office to be added
//	alertMe();

}

function removeFromReport(theID) {
	// Remove the ID from all the parts of the cookie
	window["addRep"].location.href='removeFromReport.asp?ID='+theID; // Add to cookie via the iFrame

	// Switch the images on the page back to original state
	if (document.images['add'+theID] != undefined) { // If the listing is on this page, then swap the images back
		document.images['add'+theID].src='../imgs/but_add_greenquotemeadd.gif';
	}
	if (document.images['quote'+theID] != undefined) { // If the listing is on this page, then swap the images back
		document.images['quote'+theID].src='../imgs/but_quote.jpg';
	}
	if (document.images['viewing'+theID] != undefined) { // If the listing is on this page, then swap the images back
		document.images['viewing'+theID].src='../imgs/but_viewing.jpg';
	}

	// Remove from the display list
	var $liToRemove = document.getElementById('listItem'+theID);
	var $officeList = document.getElementById('officeList');
	$officeList.removeChild($liToRemove);

}


function checkListForID(theID) {
	if (document.getElementById('listItem'+theID) == null) {
		return false;
	}
	else { return true; }
}
