function GetValueFromElement(name)
{
    var documentElements = document.documentElement.getElementsByTagName("input");
    var nameValue = "";       

    for (var i=0; i<documentElements.length; i++)
    {
        var nameID = documentElements[i].id.split("_");
        
        if ((nameID[nameID.length-1] != "undefined") && (nameID[nameID.length-1] == name))
        {
            nameValue = documentElements[i].value;
        }
    }
    return nameValue;
}

function showTab(tabId) 
{
	var oriString = GetValueFromElement("CaptionsArray");
	var captions = oriString.split("|!| ");
	
	for(var i=0; i<captions.length; i++) {
		document.getElementById('tab' + i + 'Top').style.display = 'none';
	
		if(i == tabId) {
			document.getElementById('tab' + i).style.display = 'block';
		}
		else {
			document.getElementById('tab' + i).style.display = 'none';
		}
	}
	
    CookieTime(tabId);

	document.getElementById('tabs').innerHTML = generateTabs(tabId);
}

function showTabs()
{
	var currentUrl = window.location.href.split("?");
    var qs = null;
    var tabId = 0;
    var cookieValue = null;
 
 	var currentKey = currentUrl[0].replace("http://","").replace("https://","");
    cookieValue = readCookie(currentKey);
    
    if (cookieValue != null)
    {
        tabId = cookieValue;
    }
    else if (currentUrl.length > 1)
    {
        qs = new Querystring();
        tabId = qs.get("tabindex");
    }
    
    showTab(tabId);
}

function CookieTime(tabId)
{
	var currentUrl = window.location.href.split("?");
	var currentKey = currentUrl[0].replace("http://","").replace("https://","");
	
    createCookie(currentKey,tabId,1);
}

function RemoveCookie(currentUrl)
{
	eraseCookie(currentUrl);
}
	
function generateTabs(tabId) {
	var bottomImage = 'tabSectionTop';
	var tabCode = '';
	var buttonCode = '';
	var imagePath = '/images/tabs/';
	var classStyle = '';
	var tabImage = '';
	var selected = '';
	var i = 0;
	var oriString = GetValueFromElement("CaptionsArray");
	var captions = oriString.split("|!| ");
	
	if(captions.length > 4 ) {
		buttonCode = '<div id="noTabs">View Policy <select class="tab-select" onChange="showTab(this.value);">';
		for(i=0; i<captions.length; i++) {
			selected = '';
			if(i == tabId)  {
				selected = 'selected="true"';
			}
			buttonCode = buttonCode + '<option ' + selected + ' value="' + i + '">' + captions[i] + '</option>';
		}
		buttonCode = buttonCode + '</select></div>';
		tabImage = 'noTabs';
		bottomImage = bottomImage + 'First';
		
		tabCode = '<div id="tabButtons" style="background-image: url(' + imagePath + tabImage + '.jpg);">' + buttonCode + '</div>';
		tabCode = tabCode + '<div><img src="' + imagePath + bottomImage + '.gif"/></div>';
	}
	else if(captions.length == 4 ) {
		for(i=0; i<captions.length; i++) {
			classStyle = '';
			if(i==tabId) {
				classStyle = 'class="currentTab"';
			}
			buttonCode = buttonCode + '<a ' + classStyle + ' href="javascript:showTab(' + i + ');">' + captions[i] + '</a>';
		}
		
		tabImage = 'tabs' + tabId;
		
		if(tabId==0) {
			bottomImage = bottomImage + 'First';
		}
		
		tabCode = '<div id="tabButtons" style="background-image: url(' + imagePath + '4/' + tabImage + '.jpg);">' + buttonCode + '</div>';
		tabCode = tabCode + '<div><img src="' + imagePath + bottomImage + '.gif"/></div>';
		
	}
	else if(captions.length == 3 ) {
		for(i=0; i<captions.length; i++) {
			classStyle = '';
			if(i==tabId) {
				classStyle = 'class="currentTab"';
			}
			buttonCode = buttonCode + '<a ' + classStyle + ' href="javascript:showTab(' + i + ');">' + captions[i] + '</a>';
		}
		
		tabImage = 'tabs' + tabId;
		
		if(tabId==0) {
			bottomImage = bottomImage + 'First';
		}
		
		tabCode = '<div id="tabButtons" style="background-image: url(' + imagePath + '3/' + tabImage + '.jpg);">' + buttonCode + '</div>';
		tabCode = tabCode + '<div><img src="' + imagePath + bottomImage + '.gif"/></div>';
		
	}
	else if(captions.length == 2 ) {
		for(i=0; i<captions.length; i++) {
			classStyle = '';
			if(i==tabId) {
				classStyle = 'class="currentTab"';
			}
			buttonCode = buttonCode + '<a ' + classStyle + ' href="javascript:showTab(' + i + ');">' + captions[i] + '</a>';
		}
		
		tabImage = 'tabs' + tabId;
		
		if(tabId==0) {
			bottomImage = bottomImage + 'First';
		}
		
		tabCode = '<div id="tabButtons" style="background-image: url(' + imagePath + '2/' + tabImage + '.jpg);">' + buttonCode + '</div>';
		tabCode = tabCode + '<div><img src="' + imagePath + bottomImage + '.gif"/></div>';
		
	}
	else if(captions.length == 1 ) {
		for(i=0; i<captions.length; i++) {
			classStyle = '';
			if(i==tabId) {
				classStyle = 'class="currentTab"';
			}
			buttonCode = buttonCode + '<a ' + classStyle + ' href="javascript:showTab(' + i + ');">' + captions[i] + '</a>';
		}
		
		tabImage = 'tabs' + tabId;
		
		if(tabId==0) {
			bottomImage = bottomImage + 'First';
		}
		
		tabCode = '<div id="tabButtons" style="background-image: url(' + imagePath + '1/' + tabImage + '.jpg);">' + buttonCode + '</div>';
		tabCode = tabCode + '<div><img src="' + imagePath + bottomImage + '.gif"/></div>';
		
	}			
	
	
	return tabCode;
}