// Last Edited 25 Jan 2010
// Sets ccokie to remember tab on home page and onClick event to change tab displayed

function set_cookie ( name, value, expires_year, expires_month, expires_day, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( expires_year )
  {
    var expires = new Date ( expires_year, expires_month, expires_day );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
	cookie_string += "; path=" + escape ( path );

  if ( domain )
	cookie_string += "; domain=" + escape ( domain );

  if ( secure )
	cookie_string += "; secure";

  document.cookie = cookie_string;

}


function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

    var tabLinks = new Array();
    var contentDivs = new Array();
	
    function init() {
	


      // Grab the tab links and content divs from the page
      var tabListItems = document.getElementById('tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }
	  
	  var t = 4;
	  
	   if ( ! get_cookie ( "tabname" ) )
		{
		  var t = 4;
		}
		else
		{
		
		var tabNameValue = get_cookie ("tabname");
		
		//alert (tabNameValue);
		
			switch(tabNameValue)
			{
			case "paytab" : var t = 0; break;
			case "applytab" : var t = 1; break;
			case "reporttab" : var t = 2; break;
			case "locatetab" : var t = 3; break;
			case "latestab" : var t = 4; break;
			default : var t = 4;
			} 
		
		
		//var t = 2; //use cookie tab value
		} 

      // Assign onclick events to the tab links, and
      // highlight the first tab
      var i = 0;
	  // var t = 4; // 4 is latest tab

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == t ) tabLinks[id].className = 'selected'; // 4 is latest tab
        i++;
      }

      // Hide all content divs except the first
      var i = 0; 

      for ( var id in contentDivs ) {
        if ( i != t ) contentDivs[id].className = 'tabContent hide'; // 4 is latest tab
        i++;
      }
    }

    function showTab() {
	
	/*if ( ! get_cookie ( "tabname" ) )
	{
	  var selectedId = getHash( this.getAttribute('href') );
	}
	else
	{
	  var selectedId = get_cookie ( "tabname" );
	}*/
	 	
	 var selectedId = getHash( this.getAttribute('href') );
	  
	// if ( ! getHash( this.getAttribute('href') ) )
	//{
	 // var selectedId = get_cookie ( "tabname" );
	 // document.write (selectedId);
	//}
	
	
	
      //var selectedId = getHash( this.getAttribute('href') );
	  //document.write (selectedId);
	
	// record tab user is on	
	var tabname = selectedId;  //"hello";
    var current_date = new Date;
    var cookie_year = current_date.getFullYear ( );
    var cookie_month = current_date.getMonth ( );
    var cookie_day = current_date.getDate ( ) + 1; // expires after 1 day
    set_cookie ( "tabname", tabname, cookie_year, cookie_month, cookie_day );
	
			
      // Highlight the selected tab, and dim all others.
      // Also show the selected content div, and hide all others.
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          contentDivs[id].className = 'tabContent';
        } else {
          tabLinks[id].className = '';
          contentDivs[id].className = 'tabContent hide';
        }
      }
       // Stop the browser following the link
      return false;
    }

    function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }

    function getHash( url ) {
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }
