function onloadSELTT(){

	for(var item=1;item<15;item++)
	{
		var objID='H_'+item;var obj=new tableObj(objID);
		if (obj &&obj.valid)tableObjList[objID]=obj; 
		objID='R_'+item;obj=new tableObj(objID);
		if (obj &&obj.valid)tableObjList[objID]=obj;
	}
	document.all ? handleIE() : handleGecko();
    
    // trigger onclick on the first radio button which isn't disabled
    var i, len, 
        radios = jQuery('input[type=radio]'), 
        first_enabled = null;
        
    for (i=0, len=radios.length; i<len; i+=1) {
        if (radios[i].disabled!==true) {
            first_enabled = radios[i];
            break;
        }
    }
    if (first_enabled!==null && first_enabled.onclick) {
        first_enabled.onclick();
        first_enabled.checked=true;
    }  
}


var tableObjList = new Array();

function tableObj(pos){

	this.headerDiv  = document.getElementById('ttbH_' + pos);
	//this.headerDiv2  = document.getElementById('ttbH_' + pos);
	
	this.marginDiv	= document.getElementById('ttbM_' + pos);
	this.marginDiv2	= document.getElementById('ttbCo_' + pos);
	
	this.contentDiv	= document.getElementById('ttbCo_' + pos);
	this.contentDiv2	= document.getElementById('ttbM_' + pos);

	if (this.headerDiv && this.marginDiv && this.contentDiv && this.marginDiv2 && this.contentDiv2)
		this.valid = true;
	else
		this.valid = false;
}

function handleIE()
{
	for(var item=1;item<15;item++)
	{		
		var objID='H_'+item;
		
		if (tableObjList[objID] && tableObjList[objID].valid)
		{
			addScrollSynchronization(tableObjList[objID].marginDiv, tableObjList[objID].contentDiv, "vertical");
			addScrollSynchronization(tableObjList[objID].headerDiv, tableObjList[objID].contentDiv, "horizontal");
		}
		
		if (tableObjList[objID] && tableObjList[objID].valid)
		{
			addScrollSynchronization(tableObjList[objID].marginDiv2, tableObjList[objID].contentDiv2, "vertical");
			//addScrollSynchronization(tableObjList[objID].headerDiv2, tableObjList[objID].contentDiv2, "horizontal");
		}
		
		objID='R_'+item;
		if (tableObjList[objID] && tableObjList[objID].valid)
		{
			addScrollSynchronization(tableObjList[objID].marginDiv, tableObjList[objID].contentDiv, "vertical");
			addScrollSynchronization(tableObjList[objID].headerDiv, tableObjList[objID].contentDiv, "horizontal");
		}		
		
		if (tableObjList[objID] && tableObjList[objID].valid)
		{
			addScrollSynchronization(tableObjList[objID].marginDiv2, tableObjList[objID].contentDiv2, "vertical");
		//	addScrollSynchronization(tableObjList[objID].headerDiv2, tableObjList[objID].contentDiv2, "horizontal");
		}
		
	}
	
}

function handleGecko(){

	for(var item=1;item<15;item++)
	{		
		var objID='H_'+item;
		if (tableObjList[objID] && tableObjList[objID].valid)
		{tableObjList[objID].contentDiv.onscroll = onScrollGeckoHandler;}
		objID='R_'+item;
		if (tableObjList[objID] && tableObjList[objID].valid)
		{tableObjList[objID].contentDiv.onscroll = onScrollGeckoHandler;}		
	}
}

function onScrollGeckoHandler(event){

	if (event)
	{
		var pos = event.target.id.lastIndexOf('_');
		if(pos>0)
		{
			var objID = event.target.id.substring(pos-1);			
			var obj=tableObjList[objID];
			if (obj)
			{
				// Vertical
				obj.headerDiv.scrollLeft = event.target.scrollLeft;
				obj.marginDiv.scrollTop = event.target.scrollTop;
			}
		}
	}
}

// This is a function that returns a function that is used
// in the event listener
function getOnScrollFunction(oElement) {

	return function () {
		if (oElement._scrollSyncDirection == "horizontal" || oElement._scrollSyncDirection == "both")
			oElement.scrollLeft = event.srcElement.scrollLeft;
		if (oElement._scrollSyncDirection == "vertical" || oElement._scrollSyncDirection == "both")
			oElement.scrollTop = event.srcElement.scrollTop;
	};

}
// This function adds scroll syncronization for the fromElement to the toElement
// this means that the fromElement will be updated when the toElement is scrolled
function addScrollSynchronization(fromElement, toElement, direction) {
	removeScrollSynchronization(fromElement);

	fromElement._syncScroll = getOnScrollFunction(fromElement);
	fromElement._scrollSyncDirection = direction;
	fromElement._syncTo = toElement;
	toElement.attachEvent("onscroll", fromElement._syncScroll);
}

// removes the scroll synchronization for an element
function removeScrollSynchronization(fromElement) {
	if (fromElement._syncTo != null)
		fromElement._syncTo.detachEvent("onscroll", fromElement._syncScroll);

	fromElement._syncTo = null;;
	fromElement._syncScroll = null;
	fromElement._scrollSyncDirection = null;
}

function lineSelectionChange(){
	var selForm = document.getElementById('lineSelectionForm');
	if (selForm) selForm.submit();
}

function tableExpand(objID){
	var obj=document.getElementById(objID);
	if (obj){obj.style.display='block';}
	
	var colButton=document.getElementById('col_'+objID);
	if (colButton){colButton.style.display='inline';}
	
	
	var expButton=document.getElementById('exp_'+objID);
	if (expButton){expButton.style.display='none';}
}

function tableCollaps(objID){
	var obj=document.getElementById(objID);
	if (obj){obj.style.display='none';}
	
	var colButton=document.getElementById('col_'+objID);
	if (colButton){colButton.style.display='none';}
	
	var expButton=document.getElementById('exp_'+objID);
	if (expButton){expButton.style.display='inline';}	
}

function Pos100 (dir, pos) {
	  window.scrollTo(0, 0);
}

function disableEnable(val, dir, pos, tid) {
            
	 if (val == 1){
	 
        jQuery("body").removeClass("allStops");
        jQuery("body").addClass("mainStops");
     
		//$('.back3').css('backgroundColor', '#eeeeee')
		//$('.noT4').css('backgroundColor', '#c8c8c8')	
        //$('tr.tiT').addClass('ttDN');
        //$('.tiT').css('display', 'none');
         $('.tiTOn').addClass('tiT');
         $('.tiT').removeClass('tiTOn');
        
			
		if(document.getElementById('ttbCo_'+ dir +'_' + pos)){
			$('#ttbCo_'+ dir +'_' + pos).get(0).focus();
		}
			
		Pos100(dir, pos); 
			 
	 	for (i =1; i <= listH; i++){
			if(document.getElementById('allStops_' + i +'_H')){
                $('#allStops_' + i +'_H').css('display', '');
                $('#loadPDFURLAllStops_' + i +'_H').css('display', '');
			}
			if(document.getElementById('timing_' + i +'_H')){
               $('#timing_' + i +'_H').css('display', 'none');
               $('#loadPDFURLTimingPoints_' + i +'_H').css('display', 'none');
			}
		}
				
		for (i =1; i <= listR; i++){
			if(document.getElementById('allStops_' + i +'_R')){
                $('#allStops_' + i +'_R').css('display', '');
                $('#loadPDFURLAllStops_' + i +'_R').css('display', '');	
				
				}
				if(document.getElementById('timing_' + i +'_R')){
                    $('#timing_' + i +'_R').css('display', 'none');
                    $('#loadPDFURLTimingPoints_' + i +'_R').css('display', 'none');
				} 							
			}					
	}
	else{
         jQuery("body").removeClass("mainStops");
         jQuery("body").addClass("allStops");
		//$('.back3').css('backgroundColor', '#c8c8c8'); 		
		//$('.noT4').css('backgroundColor', '#eeeeee')	;	
         //$('tr.tiT').removeClass('ttDN');
         $('.tiT').addClass('tiTOn');
         $('.tiTOn').removeClass('tiT');
        
		if(document.getElementById('ttbCo_'+ dir +'_' + pos)){
			document.getElementById('ttbCo_'+ dir +'_' + pos).focus();
		}
		
		Pos100(dir, pos);
			
		for (i =1; i <= listH; i++){	
            jQuery('#allStops_' + i +'_H').css('display', 'none');
            jQuery('#timing_' + i +'_H').css('display', '');
            jQuery('#loadPDFURLAllStops_' + i +'_H').css('display', 'none');
            jQuery('#loadPDFURLTimingPoints_' + i +'_H').css('display', '');	
		}
		
		for (i =1; i <= listR; i++){	
            jQuery('#allStops_' + i +'_R').css('display', 'none');
            jQuery('#timing_' + i +'_R').css('display', '');			
            jQuery('#loadPDFURLAllStops_' + i +'_R').css('display', 'none');
            jQuery('#loadPDFURLTimingPoints_' + i +'_R').css('display', '');
		}				
	} 
}
			
			
function mouse(wait) {
	var myBody = document.body;
	if (wait == 'wait') {  
	 myBody.style.cursor = "wait";
	 window.setTimeout("mouse('out')", 2000);
	} else {
	 myBody.style.cursor = "default";
	}
}

function RadioButton(val, wait){

    mouse('wait'); 

    var pos = val.substring(3, val.indexOf('H'));
    var direction = val.substring(val.indexOf('H'),val.indexOf('H')+1);
    
    var list = countH;
    var listH = countH;
    var listR = listR1;	
    
    if(val.indexOf('R') > 0){
        pos = val.substring(3,val.indexOf('R'));
        direction = val.substring(val.indexOf('R'),val.indexOf('R') +1);
    
    	list = countR;
        listR = countR;
        listH = listH1;							
    }
    
    if(direction == 'R'){
        if(document.getElementById('headerR')){
            jQuery('#headerR').css('display', '');
        }
        if(document.getElementById('headerH')){
             jQuery('#headerH').css('display', 'none');
        }
    }
    else{
        if(document.getElementById('headerH')){
            jQuery('#headerH').css('display', '');
        }
        if(document.getElementById('headerR')){
           jQuery('#headerR').css('display', 'none');
        }
    }
    
    for (i =1; i <= list; i++){
                
        if(i == pos){
            if(val.indexOf('H') > 0){
            
                jQuery('#ttbTable_' + direction +'_' + pos).css('display', 'block');
                jQuery('#ttbTable_' + direction +'day_' + pos).css('display', 'block');
                
                if(listR > 0){	
                    jQuery('#ttbTable_R_' + pos).css('display', 'none');	
                    jQuery('#ttbTable_Rday_' + pos).css('display', 'none');                    
                }
                
                document.getElementById('ttbCo_'+ direction +'_' + pos).focus();
                
                Pos100();
                
                if(document.getElementById(val)){
                    jQuery('#' +val).css('display', 'block');
                }
                
            }
            else{										
                   	
                $('#ttbTable_' + direction +'_' + pos).css('display', 'block');   	
                $('#ttbTable_' + direction +'day_' + pos).css('display', 'block');   
                
                
                if(listH > 0){		
                    $('#ttbTable_H_' + pos).css('display', 'none');  
                    $('#ttbTable_Hday_' + pos).css('display', 'none'); 
                }		

                document.getElementById('ttbCo_'+ direction +'_' + pos).focus();
                Pos100();
                
                jQuery('#' + val).css('display', 'block'); 
                
            }
        }
        else{
            if(listH > 0 && listR > 0){
                jQuery('#ttbTable_' + 'H' +'_' + i).css('display', 'none'); 
                jQuery('#ttbTable_' + 'R' +'_' + i).css('display', 'none'); 
                
                jQuery('#ttbTable_' + 'H' +'day_' + i).css('display', 'none'); 
                jQuery('#ttbTable_' + 'R' +'day_' + i).css('display', 'none'); 																	
            }
        
            if(listH > 0){
                jQuery('#ttbTable_' + 'H' +'_' + i).css('display', 'none'); 
                jQuery('#ttbTable_' + 'H' +'day_' + i).css('display', 'none'); 
            }
                
            if(listR > 0){
                jQuery('#ttbTable_' + 'R' +'_' + i).css('display', 'none'); 
                jQuery('#ttbTable_' + 'R' +'day_' + i).css('display', 'none'); 
            }
        } 
    }						
}

jQuery(document).ready(function() {

    if(document.getElementById('htmlTT')){
        onloadSELTT();
        Pos100();
    }
    
}); 

