function getURLVar(urlVarName) {

	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');

	var urlVarValue = '';

	if(urlHalves[1]){

		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');

		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){

			if(urlVars[i]){

				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');

				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {

					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];

				}

			}

		}

	}

	return urlVarValue;   

}

var tab_locator = getURLVar('locator') + '_' + getURLVar('PMDbProgramId');

/*==================================================
Cookie functions
==================================================*/
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


/* Tab controller */
TabControl = function(tabId, options) {
	/* JW: First, see if a tab cookie already exists for this locator. */
	/* If so, store it so we show that tab. If not, default to a_page */
	
	var stored_cookie = getCookie('tab_' + tab_locator);
	
	if (!stored_cookie) { stored_cookie = 'a_page'; }

	/* Get the tab we're starting on */
	var id = "#" + tabId;

	var tabList = $$(id + ' div.tabs ul li a');
	
	/* Loop through and find if the default tab we expect to exist does */
	var tabExists = false;
	
	tabList.each(function(a) {
	
		var page = a.getAttribute('href').match(/[-_\w]+$/i)[0];

		if (page == stored_cookie) { tabExists = true; }
		
	});
	
	if (!tabExists) { stored_cookie = 'a_page'; }

	

	/* For each link */
	tabList.each(function(a) {
			
		// Match the value of the href attribute that consistes of any number (not zero) of -_ or characters and then the end of string.
		var page = a.getAttribute('href').match(/[-_\w]+$/i)[0];

		// JW: if current tab matches cookied (or default) tab, show content and tab highlight
		if (page == stored_cookie) {
			$(page).show();
			$(a.parentNode).addClassName('on');
		} else {
			$(a.parentNode).removeClassName('on');
			$(page).hide();
		}

		/*
		if (page != options['current']) {
			$(page).hide()
		} else {
			$(a.parentNode).addClassName('on')
		}
		*/
		
		/* $(options).show() */

		/* Listen for a click on the link... */
		
		Event.observe(a, 'click', function(element) {

			/* Remove any active items */
			$$(id + ' div.tabs ul li.on').each(function(e) {
				e.removeClassName('on');
			})

			/* Everything that isn't our page hide */
			$$(id + ' .PS_tab_page[id!='+page+']').each(function(e) { e.hide() });

			/* Add active to our boy */
			$(a.parentNode).addClassName('on');

			/* JW: Set the cookie for this tab selection */
			setCookie('tab_' + tab_locator, page);

			/* Show the active */
			
			/* alert('ERE' + page); */
			
			$(page).show();

			Event.stop(element);

			});
		});

	
	}
	
