//specific for WFS
var rollMode='none';            
var rollSubrequestSent=false;   
var rollAllDataReceived=false;

//assoc array containing all lang-dependent words
var rollWords=Array();
var activeRollStart='&nbsp;<SPAN STYLE="color:#000044">';
var activeRollEnd='</SPAN>';

//general roll downs
var rollRowCount = 10;
var rollLineHeight = 13;
var rollLineHeightV = 13;
var rollLineHShift = 1;
var showDiv=false;
var hideSelects=false;

version=0

if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
}
if (version>=5.5){
	showDiv=true;
}
var findOpera = new String(navigator.userAgent);
if (findOpera.search('Opera')!=-1){
	showDiv=false;
}
var d = document;

function GEByID(id) {
   return d.getElementById ? d.getElementById(id) : d.all[id];
}

function relPosXX(e) {
   	return 388;//	e.offsetLeft + (e.offsetParent != null && e.tagName != 'BODY' ? relPosXX(e.offsetParent) : 0);
}

function relPosYY(e) {
   return e.offsetTop + (e.offsetParent != null && e.tagName != 'BODY' ? relPosYY(e.offsetParent) : 0);
}

function rollDisplayMenu(n,x,y) {
   m = GEByID("menu"+n);
   m.style.visibility = 'visible';
   m.style.position   = 'absolute';
   m.style.left = (x+rollLineHShift)+'px';
   m.style.top = y + rollLineHeight+'px';
   showOverLayer("menu"+n);
}

function displayMenuV(n,x,y) {
   m = GEByID("menu"+n);
   m.style.visibility = 'visible';
   m.style.left = x;
   m.style.top = y + rollLineHeightV;
}

function rollShowMenu(n) {
   m = GEByID("menu"+n);
   m.style.visibility = 'visible';
   m.style.position   = 'absolute';
   showOverLayer("menu"+n);
}

function rollHideMenu(n) {
   m = GEByID("menu"+n);
   m.style.visibility = 'hidden';
}

function rollNewMenu(id){
   d.write('<div id=menu',id,' CLASS=menudiv style="position:absolute;top:-500px;" ><IMG SRC="images/blank.gif" WIDTH=1 HEIGHT=7><BR>');
   	d.write('<SELECT onclick="rollClicked(',id,')" onmouseover="rollMenuToHide=0" onMouseOut="rollStartHideMenu(',id,');" CLASS=roller SIZE=',rollRowCount,' ID=roll',id,' NAME=roll',id,'>');
}

function rollAddItem(id,item,href){
	d.write('<OPTION VALUE="',href,'">',item,'</OPTION>');
}
function rollEndMenu(){
   d.write('</SELECT>');
   //d.write('</TD></TR></TABLE>');
   d.write('</DIV>');
}

function showOverLayer(name) {
	return false;
	if (showDiv){
	   var DivRef = document.getElementById(name);
	   var IfrRef = document.getElementById('overlayer');
	    //DivRef.style.display = "block";
	    IfrRef.style.width = DivRef.offsetWidth;
	    IfrRef.style.height = DivRef.offsetHeight;
	    IfrRef.style.top =DivRef.style.top;
	    IfrRef.style.left = DivRef.style.left;
	    IfrRef.style.display = "block";
	    
	}
	if (hideSelects){
		displaySelects(false)
	}
}
function displaySelects(mode){
	allInputs=document.getElementsByTagName('select');
	for (var i=0;i < allInputs.length; i++){
		allInputs[i].style.display=mode? 'inline' : 'none';
	}
}
function hideOverlayer(name) {
	if (showDiv){
	   var IfrRef = document.getElementById('overlayer');
	   IfrRef.style.display = "none";
  }
  	if (hideSelects){
		displaySelects(true)
	}
}
function initOverLayer(){
	document.write(' <iframe id="overlayer" src="javascript:\'\'"  scrolling="no" frameborder="0" style="background-color: red;position:absolute; top:0px; left:0px; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); display:none;"></iframe>');
}

/////////////////////////////////////////////////
// IBW functions
/////////////////////////////////////////////////
function ibwSelectClear(obj){
        for (var i=obj.options.length-1;i>=0;i--){
                obj.options[i]=null;
        }
}
function ibwSelectAddOption(obj, value,title){
        a = new Option(title,value);
        obj.options[obj.options.length]=a
}
function ibwSelectGetOptionText(obj){
	a=obj.selectedIndex;
	if (a<0){
		return null;
	}
	return obj.options[obj.selectedIndex].text;
}
/////////////////////////////////////////////////
//  WaterFrontSite specific functions
/////////////////////////////////////////////////

var rbContinent=new Array();
var rbCountry=new Array();

var rbEuropeCountry=new Array();

//***Jon Warmington Modified here*****/
var rbOtherCountry=new Array();
//************END****************//

var rbTerritory=new Array();
var rbRegion=new Array();

/** pushes id,title pair to an array */
function rpAny(arrayLink,id,title,parent_id){

	var a=new Array();
	a['id']=id;
	a['title']=title;
	a['parent_id']=parent_id;
	arrayLink.push(a);
		
}
function rbFindById(arrayLink,id){
	for (var i in arrayLink){
		if (arrayLink[i]['id']==id){
			return arrayLink[i]['id'];
		}
	}
	return null;
}
function rollCheckTerrCount(parent_id){
	
	
	for (var i in rbTerritory){

		if (rbTerritory[i]['parent_id']==parent_id){
			return true;
		}
	}
	return false;
}
function rollBuildMenu(id,arrayLink,parent_id,addAll){
	obj=GEByID('roll'+id);
	flag=false;
	for (var i in arrayLink){

		if (arrayLink[i]['parent_id']==parent_id || parent_id < 0){
			ibwSelectAddOption(obj,arrayLink[i]['id'],arrayLink[i]['title']);
			flag=true;
		}
	}
	return flag;
}

/**
* rolldown push Continent
*/
function rpCont(id,title,parent_id){
	rpAny(rbContinent,id,title,parent_id);
}

/**
* rolldown push Contry
*/
function rpEuropeCoun(id,title,parent_id){
	rpAny(rbEuropeCountry,id,title,parent_id);
}

//***Jon Warmington Modified here*****/
/**
* rolldown push all other Contry
*/
function rbOtherCoun(id,title,parent_id){
	rpAny(rbOtherCountry,id,title,parent_id);
}
//************END****************//

/**
* rolldown push Contry
*/
function rpCoun(id,title,parent_id){
	rpAny(rbCountry,id,title,parent_id);
}
/**
* rolldown push Territory
*/
function rpTerr(id,title,parent_id){
	rpAny(rbTerritory,id,title,parent_id);
}
/**
* rolldown push Subregion
*/
function rpRegi(id,title,parent_id){
	rpAny(rbRegion,id,title,parent_id);
}
function rollMoveMenuRelative(id,obj){
	rollDisplayMenu(id,relPosXX(obj),relPosYY(obj));
	var m = GEByID("roll"+id);
	m.style.width = '200px';
	/*m.style.border = 'solid 5px black'*/
	m.style.fontSize = '12px';

}

function rollCheckData(){
	if (!rollSubrequestSent){
		if (LOADING_CONSTANT){
			GEByID('captionTerr').innerHTML=LOADING_CONSTANT;
			GEByID('captionRegi').innerHTML=LOADING_CONSTANT;
		}else {
			GEByID('captionTerr').innerHTML='Loading...';
			GEByID('captionRegi').innerHTML='Loading...';
		}
		GEByID('qsrequest').src='datafeed.php?name=qsdetails';
		rollSetCursors('wait');
		rollSubrequestSent=true;
	}
	return rollAllDataReceived;
}

function rollShowCountries(){
	rollCheckData();
	
	ibwSelectClear(GEByID('roll1'));
	rollBuildMenu(1,rbCountry,-1);
	rollMoveMenuRelative(1,GEByID('counMark'));
	rollShowMenu(1);
	rollMode='coun';
	return void(0);
}

function rollShowEuropeCountries(){
	if (!rollCheckData()){
		return void(0);
	}
	ibwSelectClear(GEByID('roll1'));
	rollBuildMenu(1,rbEuropeCountry,3);
	rollMoveMenuRelative(1,GEByID('terrMark'));
	rollShowMenu(1);
	rollMode='terr';
	return void(0);
}


function rollSouthAmericaCountries(){
	if (!rollCheckData()){
		return void(0);
	}
	ibwSelectClear(GEByID('roll1'));
	rollBuildMenu(1,rbEuropeCountry,2);
	rollMoveMenuRelative(1,GEByID('terrMark'));
	rollShowMenu(1);
	rollMode='terr';
	return void(0);
}

//***Jon Warmington Modified here*****/
function rollOtherCountries(id){
	if (!rollCheckData()){
		return void(0);
	}
	ibwSelectClear(GEByID('roll1'));
	rollBuildMenu(1,rbEuropeCountry,id);
	rollMoveMenuRelative(1,GEByID('terrMark'));
	rollShowMenu(1);
	rollMode='terr';
	return void(0);
}
//************END****************//

function rollShowContinents(){
	
	if (!rollCheckData()){
		
		return void(0);
	}

	ibwSelectClear(GEByID('roll1'));
	rollBuildMenu(1,rbContinent,0);
	rollMoveMenuRelative(1,GEByID('contMark'));
	rollShowMenu(1);
	rollMode='cont';
	return void(0);
}

function rollShowTerritories(){
	
	if (GEByID('rfCont').value == 3) {
		rollShowEuropeCountries()
		return void(0);	
	}
	if (GEByID('rfCont').value == 2) {
		rollSouthAmericaCountries()
		return void(0);	
	}
	
	//***Jon Warmington Modified here*****/
	//if(GEByID('rfCont').value == 12){
	//	rollOtherCountries(12)
	//	return void(0);	
	//}
	//************END****************//
	
	//***Jon Warmington Modified here*****/
	if (GEByID('rfCont').value != 1){
		rollOtherCountries(GEByID('rfCont').value)
		return void(0);	
	}
	//************END****************//
 	
	
	if ((!rollCheckData() || GEByID('rfCoun').value.length==0)){
		return void(0);
	}
	ibwSelectClear(GEByID('roll1'));
	if (rollBuildMenu(1,rbTerritory,GEByID('rfCoun').value)){
		rollMoveMenuRelative(1,GEByID('terrMark'));
		rollShowMenu(1);
		rollMode='terr';
	} else {
		rollHideMenu(1);
		rollMode='none';
	}
	
	return void(0);
}
function rollShowRegions(){
	if (!rollCheckData()  || GEByID('rfTerr').value.length==0){
		return void(0);
	}
	ibwSelectClear(GEByID('roll1'));
	ibwSelectAddOption(GEByID('roll1'),0,rollWords['all']);
	rollBuildMenu(1,rbRegion,GEByID('rfTerr').value);
	if (GEByID('roll1').length<2){
		ibwSelectAddOption(GEByID('roll1'),1,rollWords['north']);
		ibwSelectAddOption(GEByID('roll1'),2,rollWords['south']);
		ibwSelectAddOption(GEByID('roll1'),3,rollWords['east']);
		ibwSelectAddOption(GEByID('roll1'),4,rollWords['west']);
	}
	rollMoveMenuRelative(1,GEByID('regiMark'));
	rollShowMenu(1);
	rollMode='regi';
	return void(0);
}
function rollClicked(id){
	
	
	if (ibwSelectGetOptionText(GEByID('roll1'))==null){
		rollHideMenu(1);
		return;
	}
	
	switch (rollMode){
		case 'cont':

			GEByID('rfCont').value=GEByID('roll1').value;
			GEByID('captionCont').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
			rollHideMenu(1);
			rollDisable(2);
			setTimeout('rollShowCountries();rollMenuToHide=0',100);
			break;
		case 'coun':

			//***Jon Warmington Modified here*****/
			if (GEByID('roll1').value == 32 || GEByID('roll1').value == 190) {
				
				GEByID('rfCont').value = 1;
				GEByID('rfCoun').value = GEByID('roll1').value;
				
				GEByID('captionCoun').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
				rollHideMenu(1);
				if (rollAllDataReceived){
					rollDisable(3);
				}
				
				setTimeout('rollShowTerritories();rollMenuToHide=0',100);
								
			} else{
				
				if (GEByID('rfCont').value == 2){
					
					GEByID('rfCont').value = 2;
					GEByID('captionCoun').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
					rollHideMenu(1);
					rollDisable(3);
					setTimeout("rollSouthAmericaCountries();rollMenuToHide=0",100);	
				
				}
				else if (GEByID('rfCont').value == 3){
					
					GEByID('rfCont').value = GEByID('roll1').value;
					GEByID('captionCoun').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
					rollHideMenu(1);
					rollDisable(3);
					setTimeout("rollShowEuropeCountries();rollMenuToHide=0",100);
					
				} else{
					
					GEByID('rfCont').value = GEByID('roll1').value;
					GEByID('captionCoun').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
					rollHideMenu(1);
					rollDisable(3);
					setTimeout("rollOtherCountries(GEByID('roll1').value);rollMenuToHide=0",100);
					
				}
				
				
				
			}
			//************END****************//
			
			break;
		case 'terr':
		
			//***Jon Warmington Modified here*****/
			if (GEByID('rfCont').value == 1) {
				
				GEByID('rfTerr').value=GEByID('roll1').value;
								
			} else{
				
				GEByID('rfCoun').value=GEByID('roll1').value;
				GEByID('rfTerr').value=0;
				
			}	
			//************END****************//

			GEByID('captionTerr').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;
			rollHideMenu(1);

			rollDisable(4);
			setTimeout('rollShowRegions();rollMenuToHide=0',100);
			break;
		case 'regi':
			GEByID('rfRegi').value=GEByID('roll1').value;
			GEByID('captionRegi').innerHTML=activeRollStart+ibwSelectGetOptionText(GEByID('roll1'))+activeRollEnd;			
			document.quicksearch.submit();
			break;
		default:
			alert('Incorrect mode '+rollMode);
	}
	rollHideMenu(1);
}
function rollDoHide(){
	if (rollMenuToHide>0){
		rollHideMenu(rollMenuToHide);
	}
}
function rollStartHideMenu(id){
	rollMenuToHide=id;
	setTimeout('rollDoHide()',100);
}
function rollSetCursors(type){
	GEByID('counMark').style.cursor=type;
	GEByID('regiMark').style.cursor=type;
	GEByID('terrMark').style.cursor=type;
}

//no break; is not a bug, but a feature
function rollDisable(level){
	switch (level){
		case 2:
			if (GEByID('roll1').value != 3 && GEByID('roll1').value != 2) {
				GEByID('captionCoun').innerHTML=parent.rollWords['country'];
			} 
		case 3:
			GEByID('captionTerr').innerHTML=parent.rollWords['territory'];
		case 4: {
			GEByID('captionRegi').innerHTML=parent.rollWords['region'];
		}
	}
	rollClear(level);
}
function rollClear(level){
	switch (level){
		case 1:
			GEByID('rfCont').value="";
		case 2:
			if (GEByID('roll1').value != 3 && GEByID('roll1').value != 2) {
				GEByID('rfCoun').value="";
			} else {
				GEByID('rfCoun').value="";	
			}
		case 3:
			GEByID('rfTerr').value="";
		case 4: {
			GEByID('rfRegi').value="";
		}
	}
}
