function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Changing Buy/Rent
function checkBuyRent() {
	var intSector = document.getElementById('fSector').value;
	showSector(intSector);
}

// Show/Hide an area ------------------------------------------------- //
function showSector(id) {
	whichdiv = document.getElementById(id);
	var thearray= new Array("listS0","listS1","listS2","listS3","listS4","listS5","listS6","listS7","listS8","listS9","listSC","listL0","listL1","listL2","listL3","listL4","listL5","listL6","listL7","listL8","listL9","listLC"); 
	for(i=0; i<thearray.length; i++) {
		var intBuy = document.getElementById('fTypeBuy').checked;
		var intRent = document.getElementById('fTypeRent').checked;

		if (intBuy == true)
		{
			var strCheck = 'S';
		}
		if (intRent == true)
		{
			var strCheck = 'L';
		}

		if(thearray[i] == 'list' + strCheck + id){
			if (id == 'C')
			{
				document.getElementById('cvty-zoom').className="show";
				document.getElementById('list'+strCheck+'0').className="ghost";
			}
			else
			{
				document.getElementById(thearray[i]).className="show";
				document.getElementById('fSector').value = id;
			}
		} else {
			if (thearray[i] != 'listSC' && thearray[i] != 'listLC') {
				document.getElementById(thearray[i]).className="hide"; 
			}
		} 
	} 
}

function showMapBehaviour() {
	if (!document.getElementById("map")) return false;
	var Links = document.getElementById("map");
	var link = Links.getElementsByTagName("a");
	for(var i=0; i<link.length; i++){ 
		link[i].onclick = function() {
			var strLink = this.href.substring((this.href.length)-11,(this.href.length)-10);
			if (document.getElementById("lnk_list" + strLink)) {
				showSector(strLink);
				return false;
			}
		}
	}
	return false;
}
addLoadEvent(showMapBehaviour);

function showMapBehaviourCV() {
	if (!document.getElementById("cvtyzoom")) return false;
	var Links = document.getElementById("cvtyzoom");
	var link = Links.getElementsByTagName("area");
	for(var i=0; i<link.length; i++){ 
		link[i].onclick = function() {
			var strLink = this.href.substring((this.href.length)-11,(this.href.length)-10);
			if (document.getElementById("lnk_list" + strLink)) {
				showSector(strLink);
				return false;
			}
		}
	}
	return false;
}
addLoadEvent(showMapBehaviourCV);


// Select All/None -------------------------------------------------- //
function selectAll(iSector) {
	var intBuy = document.getElementById('fTypeBuy').checked;
	var intRent = document.getElementById('fTypeRent').checked;

	if (intBuy == true)
	{
		var strCheck = 'S';
	}
	if (intRent == true)
	{
		var strCheck = 'L';
	}

	var chk = document.getElementById('fAll' + strCheck + iSector).checked;

	var j = document.getElementById('fSectorCount'+ strCheck + iSector).value;
	for ( i=0; i<j; i++) {
		document.getElementById('fSector'+strCheck+iSector+"_"+i).checked = chk
	}
	checkTicked()
}

//	Check how many areas have been ticked ---------------------------- //
function checkTicked() {
	var j;
	var iCheck = 0;
	var iCheckSector = 0;
	var iCheckCV = 0;

	var intBuy = document.getElementById('fTypeBuy').checked;
	var intRent = document.getElementById('fTypeRent').checked;

	if (intBuy == true)
	{
		var strCheck = 'S';
	}
	if (intRent == true)
	{
		var strCheck = 'L';
	}

	for ( i=0; i<=9; i++) {
		j = document.getElementById('fSectorCount'+ strCheck + i).value;
		iCheckSector = 0;
		for ( k=0; k<j; k++)
		{
			if (j > 0)
			{
				if (document.getElementById('fSector'+strCheck+i+"_"+k).checked == true)
				{
					iCheckSector++;
					iCheck++;
					if ((i == 4 || i == 5 || i == 6 || i == 7 || i == 8) && iCheckSector > 0) {
						iCheckCV++;
					}
				}
			}
		}
		//Normal Sectors.
		if (iCheckSector >= 1)
		{
			highlightMap(i);
		}
		else
		{
			dehighlightMap(i);
		}
		//Highlight CV on the main map too.
		if (iCheckCV >= 1)
		{
			highlightMap('CV');
		}
		else
		{
			dehighlightMap('CV');
		}
	}
}

function highlightMap(iSector)
{
	document.getElementById('mapSector'+iSector+'Chk').className="checked";
}

function dehighlightMap(iSector)
{
	document.getElementById('mapSector'+iSector+'Chk').className="hide";
}

//Hide the CV section of the map
function cvHide()
{
	document.getElementById('cvty-zoom').className = "hide";
};