//** Tab Content script- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
//** Last updated: June 29th, 06

var enabletabpersistence=0 //enable tab persistence via session only cookies, so selected tab is remembered?

////NO NEED TO EDIT BELOW////////////////////////
var tabcontentIDs = new Object();
var tabValue=getTabVal();

function expandcontent(linkobj){    
    var ulid=linkobj.parentNode.parentNode.id //id of <OL id=mainTab> element is stored in var ulid
    
    var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of li inside ol maintab       
    for (var i=0; i<ullist.length; i++){
         
        ullist[i].className=""  //deselect all tabs by changing className = "" for all <li>
        
        if (typeof tabcontentIDs[ulid][i]!="undefined"){ //if tab content within this array index exists (exception: More tabs than there are tab contents)            
            document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents            
            }
        }
    linkobj.parentNode.className="active"  //highlight currently clicked on tab <li className="active">
    document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content
    saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))
    }

function savetabcontentids(ulid, relattribute){// save ids of tab content divs    
    if (typeof tabcontentIDs[ulid]=="undefined"){ //if this array doesn't exist yet        
            tabcontentIDs[ulid]=new Array()
        }        
        tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute               
}

function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie    
        
        for(var i=0; i<tabcontentIDs[ulid].length;i++){
            if(tabcontentIDs[ulid][i]!="undefined")
                if(tabcontentIDs[ulid][i] == selectedtabid){
                    tabValue=i;                    
                    }
        }
}

function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
    var ullist=document.getElementById(ulid).getElementsByTagName("li")
    for (var i=0; i<ullist.length; i++){
        if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
            return ullist[i].getElementsByTagName("a")[0]
            break
        }
    }
}

function initializetabcontent(){     
    for (var i=0; i<arguments.length; i++){ //loop through passed UL ids        
            var clickedontab=tabValue; //retrieve ID of last clicked on tab from cookie, if any             
            var ulobj=document.getElementById(arguments[i]); //retrieves OL with id mainTab
            
            //if (!(navigator.appVersion.indexOf("MSIE")>0)) {
		//ulobj.style.margin="0px 0px 0px -40px";		
            //}
            
            var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within OL mainTab
            
             //ulobj.style.display="block"; // turn the display to block for mainTab. Now users will be able to click on it.
            for (var x=0; x<ulist.length; x++){ //loop through each SPAN element
                var ulistlink=ulist[x].getElementsByTagName("a")[0]  //get <a> tag inside the li
                
                if (ulistlink.getAttribute("rel")){ //get rel="" value for the <a> tag inside span of <DIV id=mainTab>
                    savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs                                        
                    ulistlink.onclick=function(){                       
                        expandcontent(this)
                        return false
                        }
                    if(x == clickedontab){                         
                        expandcontent(ulistlink) //auto load currenly selected tab content
                        }
                    
                    }
                } //end inner for loop

                

            } //end outer for loop           
    }


function getCookie(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

function getTabVal(){
    var url = document.location + '';
    //alert(url);
    if(url.indexOf('?')== -1){
        return 0;
        }
    q=url.split("?");
    
    if (q[1]) {
        //Get all Name/Value pairs from the QueryString
        var pairs = q[1].split('&');
        for (i=0;i<pairs.length;i++) {

            //Get the Name from given Name/Value pair
            var keyval = pairs[i].split('=');

            if (keyval[0] == 'switchTab') {
            //Get the Value from given Name/Value pair and set to the return ID
            var tabNumber = keyval[1]; 
            
            return tabNumber;
            }
            
        }        
        return 0;
 }   
}

function setCookie(name, value){
    document.cookie = name+"="+value //cookie value is domain wide (path=/)
}

function selectSearchType(val){   
    
     var ulobj=document.getElementById(val);
     var ulist = ulobj.getElementsByTagName("option");     
     for(i=0;i<ulist.length;i++){
        
        var temp1 = ulobj.getElementsByTagName("option")[i];
        
    
        if(temp1.selected){
            var choice = temp1.getAttribute("value");
            //document.getElementById("container").style.width="600px";
            document.MapForm.action = document.MapForm.action+"&searchTheme="+choice;
            document.getElementById(val).style.display="none"; 
            
            if(choice == "Street" || choice == "StreetIntersection"){
                document.getElementById("City").style.display="block";                             
            }
            
            document.getElementById(choice).style.display="block";            
        }
    }
     //var ulist=ulobj.getElementsByTagName("option")[0];
     //alert(ulist.getAttribute("selected"));
     
            
}


function removeSearchTheme(){
    
    var url = document.MapForm.action;
    
    q=url.split("?");
    var newAction = q[0]+"?";
    if (q[1]) {
        //Get all Name/Value pairs from the QueryString
        var pairs = q[1].split('&');
        for (i=0;i<pairs.length;i++) {

            //Get the Name from given Name/Value pair
            var keyval = pairs[i].split('=');

            if (keyval[0] != 'searchTheme') {
            
                if(i==0){
                    newAction = newAction+pairs[i];
                }else{
                    newAction = newAction+"&"+pairs[i];
                }
            }
            
        }        
    }
    document.MapForm.action = newAction;
    
}
function backToSelectSearch(){
    //document.getElementById("container").style.width="600px";
    removeSearchTheme();
    document.getElementById('searchTab').style.display="block";
    document.getElementById('City').style.display="none";
    document.getElementById('Street').style.display="none";
    document.getElementById('StreetIntersection').style.display="none";
    document.getElementById('PostalCode').style.display="none";
    document.getElementById('Locality').style.display="none";
}

