$(document).ready(function() {
	handleAutoTabs();
});

function handleAutoTabs() {
	if(document.getElementById('Tab1Text').innerHTML=="&nbsp;"||document.getElementById('Tab1Text').innerHTML=="\n								&nbsp;")
	{
	    document.getElementById('TabZone1').style.display='none';

	}
	if(document.getElementById('Tab2Text').innerHTML=="&nbsp;"||document.getElementById('Tab2Text').innerHTML=="\n								&nbsp;")
	{
	    document.getElementById('TabZone2').style.display='none';

	}
	if(document.getElementById('Tab3Text').innerHTML=="&nbsp;"||document.getElementById('Tab3Text').innerHTML=="\n								&nbsp;")
	{
	    document.getElementById('TabZone3').style.display='none';

	}

	var LocId;
	var tabParam_pattern = /tab=([^&#]+)/;
	var queryString = document.location.search;
	var tabHash_pattern = /#([^&#?]+)/;
	var hashString = document.location.hash;
	var tab_parts = [];
	if (tabHash_pattern.test(hashString) == true) {//look for #whatever in hash
		tab_parts = tabHash_pattern.exec(hashString);
		LocId = tab_parts[1].replace(/-/g,' ');//convert hyphens to spaces
		LocId = LocId.replace(/\%20/g,' ');//convert %20 to spaces
	} else if (tabParam_pattern.test(queryString) == true) {//look for tab=whatever in query string
		tab_parts = tabParam_pattern.exec(queryString);
		LocId = tab_parts[1].replace(/-/g,' ');//convert hyphens to spaces
		LocId = LocId.replace(/\%20/g,' ');//convert %20 to spaces
	}

	if(LocId!=null)
	{
	    var i=1;
	    for(i=1;i<4;i++)
	    {
	        var Id=document.getElementById('Tab'+i+'Text').innerHTML.split('&nbsp;')[0];
	        var mId;
	        if(Id.split('\n')[1]!=undefined)
	        {
	            mId=Id.split('\n')[1].split('&nbsp;')[0].substring(8);
	        }

	        if(Id==LocId||mId==LocId)
	        {
		        document.getElementById('Tab'+i+'RightCorner').removeAttribute('class');
		        document.getElementById('Tab'+i+'RightCorner').setAttribute('className','right_corner_auto');
		        document.getElementById('Tab'+i+'RightCorner').setAttribute('class','right_corner_auto');

            	document.getElementById('Tab'+i+'Text').removeAttribute('class');
		        document.getElementById('Tab'+i+'Text').setAttribute('className','tabtext_style_auto');
		        document.getElementById('Tab'+i+'Text').setAttribute('class','tabtext_style_auto');

		        document.getElementById('Tab'+i).removeAttribute('class');
		        document.getElementById('Tab'+i).setAttribute('className','tab_text_auto');
		        document.getElementById('Tab'+i).setAttribute('class','tab_text_auto');

		        document.getElementById('Tab'+i+'LeftCorner').removeAttribute('class');
		        document.getElementById('Tab'+i+'LeftCorner').setAttribute('className','left_corner_auto');
		        document.getElementById('Tab'+i+'LeftCorner').setAttribute('class','left_corner_auto');

		        document.getElementById('Tab'+i+'Sel').removeAttribute('class');
		        document.getElementById('Tab'+i+'Sel').setAttribute('className','tabs_control_auto');
		        document.getElementById('Tab'+i+'Sel').setAttribute('class','tabs_control_auto');
		        document.getElementById('Tab'+i+'ContentZone').style.display='block';
                break;
            }

        }

        var SelTabId;
        SelTabId=i;
        if(i!=4)
        {
            var TabCount=1;
		    for(TabCount=1;TabCount<4;TabCount++)
		    {
		        if(TabCount!=SelTabId)
		        {
		            document.getElementById('Tab'+TabCount+'ContentZone').style.display='none';

		            document.getElementById('Tab'+TabCount+'RightCorner').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'RightCorner').setAttribute('className','noright_corner_auto');
		            document.getElementById('Tab'+TabCount+'RightCorner').setAttribute('class','noright_corner_auto');

		            document.getElementById('Tab'+TabCount+'Text').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'Text').setAttribute('className','non_selectedtab_text_auto');
		            document.getElementById('Tab'+TabCount+'Text').setAttribute('class','non_selectedtab_text_auto');

		            document.getElementById('Tab'+TabCount).removeAttribute('class');
		            document.getElementById('Tab'+TabCount).setAttribute('className','non_selectedtab_auto');
		            document.getElementById('Tab'+TabCount).setAttribute('class','non_selectedtab_auto');

		            document.getElementById('Tab'+TabCount+'LeftCorner').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'LeftCorner').setAttribute('className','noleft_corner_auto');
		            document.getElementById('Tab'+TabCount+'LeftCorner').setAttribute('class','noleft_corner_auto');

		            document.getElementById('Tab'+TabCount+'Sel').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'Sel').setAttribute('className','non_selected_auto');
		            document.getElementById('Tab'+TabCount+'Sel').setAttribute('class','non_selected_auto');
		        }
		    }
		}
		else
		{
		    var TabCount=1;
	        for(TabCount=1;TabCount<4;TabCount++)
	        {
	            if(document.getElementById('Tab'+TabCount+'Text').innerHTML!="&nbsp;"||document.getElementById('Tab'+TabCount+'Text').innerHTML!="\n								&nbsp;")
	            {


        	        document.getElementById('Tab'+TabCount+'RightCorner').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'RightCorner').setAttribute('className','right_corner_auto');
		            document.getElementById('Tab'+TabCount+'RightCorner').setAttribute('class','right_corner_auto');

		            document.getElementById('Tab'+TabCount+'Text').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'Text').setAttribute('className','tabtext_style_auto');
		            document.getElementById('Tab'+TabCount+'Text').setAttribute('class','tabtext_style_auto');

		            document.getElementById('Tab'+TabCount).removeAttribute('class');
		            document.getElementById('Tab'+TabCount).setAttribute('className','tab_text_auto');
		            document.getElementById('Tab'+TabCount).setAttribute('class','tab_text_auto');

		            document.getElementById('Tab'+TabCount+'LeftCorner').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'LeftCorner').setAttribute('className','left_corner_auto');
		            document.getElementById('Tab'+TabCount+'LeftCorner').setAttribute('class','left_corner_auto');

		            document.getElementById('Tab'+TabCount+'Sel').removeAttribute('class');
		            document.getElementById('Tab'+TabCount+'Sel').setAttribute('className','tabs_control_auto');
		            document.getElementById('Tab'+TabCount+'Sel').setAttribute('class','tabs_control_auto');
        		    document.getElementById('Tab'+TabCount+'ContentZone').style.display='block';
	                break;
	            }
	        }
		}
	}
	else
	{
		document.getElementById('Tab1ContentZone').style.display='block';
		document.getElementById('Tab1Sel').className = 'tab_text_auto';
		document.getElementById('Tab1LeftCorner').className = 'left_corner_auto';
		document.getElementById('Tab1').className = 'tab_text_auto';
		document.getElementById('Tab1Text').className = 'tabtext_style_auto';
		document.getElementById('Tab1RightCorner').className = 'right_corner_auto';
	}
}

function changeUrl (tabid)
{
	var curUrl = document.location.href;

	curUrl = curUrl.replace(/#.*$/,'');//remove existing hash

	var tabText = document.getElementById('Tab' + tabid + 'Text').innerHTML.split('&nbsp;')[0];
	tabText = tabText.replace(/^\s+|\s+$/g,'')//trim whitespace (left and right)
	tabText = tabText.replace(/\s/g,'-');//convert spaces to hyphen

     document.location = curUrl + '#'+tabText;
     handleAutoTabs();
}

//script for tab hiding/showing
function ShowTab1()
{
		changeUrl (1);
}
function ShowTab2()
{		
		changeUrl (2);		       
}

function ShowTab3()
{
		changeUrl (3);       
}
