/*
// $RCSfile: euroantennas.js,v $
// $Source: /cvs/euroantennasweb-live/euroantennas.js,v $, $Revision: 1.4 $, $Date: 2009/07/14 21:07:06 $, $State: Exp $ 
*/
// EXPAND AND COLLAPSE TABLES

window.onload=function(){
	initExpandableTables(""); // no default open
}

// set global variables
var thisTable = "";
var thisBody = "";
var arrRows = "";

var expandAllText = "Expand All";
var expandAllClass = "expandAll";
var hideAllText = "Hide All";
var hideAllClass = "hideAll";

function initExpandableTables(defaultOpen)
{
	if(!document.getElementById) return false;
	if(!document.getElementById("expandingTable")) return false;
	thisTable = document.getElementById("expandingTable");
	
	// Get and save current table class
	var savedTableClass = thisTable.className;
	
	//temporarily hide the table
	thisTable.setAttribute("class","hide");
	thisTable.className = "hide"; //For IE - stupid browser!
	
	// Do row manipulation
	thisBody = thisTable.tBodies[0];
	arrRows = thisBody.rows;

	var currentSection = "";
	//arrRows = thisBody.getElementsByTagName("tr");
	
	for(i=0; i < arrRows.length; i++){
		if(arrRows[i].getAttribute("id")){
			currentSection = arrRows[i].getAttribute("id");
			// Create "a" element
			var aTag = document.createElement("a");
			aTag.setAttribute("href","#");
			aTag.setAttribute("id",currentSection+"A");
			aTag.setAttribute("title",currentSection);
			aTag.setAttribute("class","expand");
			aTag.className = "expand"; //For IE - stupid browser!
			
			// Create "span" element
			var spanTag = document.createElement("span");
			spanTagText = document.createTextNode("Expand");
			spanTag.appendChild(spanTagText);
			
			spanTag.setAttribute("class","hide");
			spanTag.className = "hide"; //For IE - stupid browser!
			
			aTag.appendChild(spanTag);

			// Set onClick event
			aTag.onclick = function(){
				showHideChildren(this);
				return false;
			}
			
			// Place new "a" element as first child of first TD
			firstTdTag = arrRows[i].getElementsByTagName("td")[0];
			firstTdTag.insertBefore(aTag,firstTdTag.firstChild);
		}
		else {
			if(!arrRows[i].getAttribute("title")){
				arrRows[i].setAttribute("title",currentSection);
			}
			arrRows[i].setAttribute("class", "hide");
			arrRows[i].className = "hide"; //For IE - stupid browser!
		}
	}
	
	// If there is a default open value set then open it
	if(defaultOpen){
		currentSection = defaultOpen;
		for(i=0; i < arrRows.length; i++){
			var thisRowTitle = arrRows[i].getAttribute("title");
			if(thisRowTitle == currentSection) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!	
			}
		}
		headerTr = document.getElementById(defaultOpen+"A");
		headerTr.setAttribute("class","contract");
		headerTr.className = "contract"; //For IE - stupid browser!
	}
	
	
	// Create and Add Expand/Hide all link
	var aTagExpandAll = document.createElement("a");
	aTagExpandAll.setAttribute("href","#");
	aTagExpandAll.setAttribute("class",expandAllClass);
	aTagExpandAll.className = expandAllClass; //For IE - stupid browser!
	aTagExpandAllText = document.createTextNode(expandAllText);
	aTagExpandAll.appendChild(aTagExpandAllText);
	
	var aTagHideAll = document.createElement("a");
	aTagHideAll.setAttribute("href","#");
	aTagHideAll.setAttribute("class",hideAllClass);
	aTagHideAll.className = hideAllClass; //For IE - stupid browser!
	aTagHideAllText = document.createTextNode(hideAllText);
	aTagHideAll.appendChild(aTagHideAllText);
	
	//Set onclick events 
	aTagExpandAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	aTagHideAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	
	// Get table head row
	firstThTag = thisTable.tHead.rows[0].cells[0]; 

	// Place the new "a" elements in the first TH
	firstThTag.insertBefore(aTagExpandAll,firstThTag.firstChild);
	firstThTag.insertBefore(aTagHideAll,firstThTag.firstChild);
	
	//replace saved table class
	thisTable.setAttribute("class",savedTableClass);
	thisTable.className = savedTableClass; //For IE - stupid browser!
	
	setSecondLevelLinks();
}

function showHideChildren(thisA){

	currentSection = thisA.getAttribute('title');
	for (var i=0; i<arrRows.length; i++) {         
		var thisRowTitle = arrRows[i].getAttribute("title");
		if(thisRowTitle == currentSection) {
			
			if(arrRows[i].getAttribute("class") == "hide"){
				if(thisRowTitle != "secondlevel"){
					arrRows[i].setAttribute("class","");
					arrRows[i].className = ""; //For IE - stupid browser!
					thisA.setAttribute("class","contract");
					thisA.className = "contract"; //For IE - stupid browser!
				}
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!
				thisA.setAttribute("class","expand");
				thisA.className = "expand"; //For IE - stupid browser!
			}
		}
	}
}

function showHideAll(thisA){
	action = thisA.firstChild.nodeValue;
	
	for (var i=0; i<arrRows.length; i++) {
		if(arrRows[i].getAttribute("title")){
			if(action == expandAllText) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!	
			}				 
		} else {
			if(action == "Expand All"){
				arrRows[i].getElementsByTagName("a")[0].className = "contract";
			} else {
				arrRows[i].getElementsByTagName("a")[0].className = "expand";
			}
		}
	}
}

function showHideSecondLevel(thisA){
	//Get Node to show or hide
	secondLevel = thisA.parentNode.parentNode.nextSibling;
	if(secondLevel.className == "") {
		secondLevel.className = "hide";
	} else {
		secondLevel.className = "";
	}
	
	if(thisA.className == "expand level2"){
		thisA.className = "contract level2";
	} else {
		thisA.className = "expand level2";	
	}
}

function setSecondLevelLinks(){
	//Get all a's within expandable table
	thisTable = document.getElementById("expandingTable");
	allAs = thisTable.getElementsByTagName("a");
	
	for (var i=0; i<allAs.length; i++) {
		if(allAs[i].getAttribute("rel") == "secondLevelLink"){
			allAs[i].className = "expand level2";
			allAs[i].onclick = function(){
				showHideSecondLevel(this);
			}
		}
	}
}

// END EXPANDING TABLE

// BEGIN IE FLICKER FLICKER FLICKER FLICKER

(function(){

	/*Use Object Detection to detect IE6*/
	var  m = document.uniqueID /*IE*/
	&& document.compatMode  /*>=IE6*/
	&& !window.XMLHttpRequest /*<=IE6*/
	&& document.execCommand ;
	
	try{
		if(!!m){
			m("BackgroundImageCache", false, true) /* = IE6 only */ 
		}
		
	}catch(oh){};
	})();

// END IE FLICKER FLICKER FLICKER FLICKER




/***********FLASH MAP STUFF***************/

var http = createRequestObject();

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function closedupPop(){
    var popup = document.getElementById('dupPop');
    popup.innerHTML = '';
    popup.style.display = 'none';
}

function dupPop(passeddata){
    var poptext = '<span class="closepop" onclick="closedupPop();">close (X)</span><h4>Please select a location:</h4><ul>';
    var passeddata2 = passeddata;
    var passeddata3 = passeddata2.split("-");
    var num = passeddata3.length;
    for(i=0;i<num;i++){
        var items = passeddata3[i].split("|");
        var poptext = poptext + '<li><a href="javascript:sendCountryRequest(\'' + items[0] + '\')">' + items[1] + '</a></li>';
    }
    var poptext = poptext + '</ul>';
    var popup = document.getElementById('dupPop');
    popup.innerHTML = poptext;
    popup.style.display = 'block';
}

function setCountry(country){
    var countryitems = country;
    flashProxy.call('zoomTo', countryitems);
}

var currentCountry;

function sendCountryRequest(country) {

    var popup = document.getElementById('dupPop');
    popup.innerHTML = '';
    popup.style.display = 'none';
    var officeDetailsContent = document.getElementById('officeDetails').innerHTML;
    document.getElementById('theloader').style.display = 'block';
	var url = "getcontact.php?agent=" + country + "&timeStamp=" + new Date().getTime();
	http.open('get', url );
    currentCountry = country;
	http.onreadystatechange = handleCountryResponse;
	http.send(null);
}

function handleCountryResponse() {
	if(http.readyState == 4) {
		var content = http.responseText;
        /*Select the country in the DD*/
        //var mapRef = xmldoc.getElementsByTagName("mapRef").item(0).firstChild.nodeValue;
        /*
        var countries_dd = document.getElementById("countries_dd");
        var length = countries_dd.options.length;
        for(i=0; i<=length-1; i++){
            theval = countries_dd.options[i].value;
            var newRef = countryId + '-' + mapRef;
            if(theval==newRef){
                countries_dd.options[i].selected = 'selected';
            }else{
                //countries_dd.options[i].selected = '';
            }
        }
        */
        
        var anchor = '<a id="' + currentCountry + '"></a>';
		document.getElementById('officeDetails').innerHTML = anchor + content;
        document.location.href="#" + currentCountry;
        document.getElementById('theloader').style.display = 'none';

	} else if (http.readyState == 1) {
        /* DO A LOADER */
    }
}