var webDb;
var cssTableDsp='table';

function setWebDb(var1)
// Call this from a pass-through script tag on a form/page, using
// computed text to provide the web db path.
{
	webDb='/'+var1+'/';
}

function replaceURL(var1)
{
	var url;
	if (var1.indexOf('://')<0) {
		url=(var1.charAt(0)=='/')?webDb+var1.substring(1):webDb+var1;
	}
	window.location.replace(url);
	return false;
}

// Get the default display for tables from the first non-hidden table in the document.
function getCSSTableDisplay()
{
	var ta=document.getElementsByTagName('table');
	if (!ta) return;
	for (var i=0; i<ta.length; i++) {
		var table=ta[i];
		if (!table) return;
		var tds=table.style.display;
		if (tds=='none') ;
		else if (tds=='') /*alert('Table display is blank.')*/;
		else {
			cssTableDsp=tds;
			alert('Default table display: '+tds);
			return;
		}
	}
}

function expView(id)
{
	var img=document.getElementById('veimg'+id);
	var table=document.getElementById('vtable'+id);
	var isrc;
	if (!table) return false;
	if (table.style.display=='none') {
		table.style.display=cssTableDsp;
		isrc='/icons/collapse.gif';
	} else {
		table.style.display='none';
		isrc='/icons/expand.gif';
	}
	if (img) img.src=isrc;
	return false;
}

// Display the callback request form in a new window.
function popupCallbackRequest(insType)
{
	if (!insType) insType='generic';
	var width=(screen.width<725)?screen.width:725;
	window.open(webDb+'f-callbackReq?OpenForm&InsType='+insType,'IFCallbackRequest',
	'width='+width+',height=245,location=1,status=1,titlebar=1,toolbar=1',true)
}

function callbackShowDIIFields(radioName,blockId)
// Show or hide fields for disability income insurance on other callback
// forms. radioName names a radio button that must have the "Yes" option
// checked to show these fields.
{
	var radio=document.getElementsByName(radioName);
	if (!radio) return;
	var block=document.getElementById(blockId);
	if (!block) return;
	var rVal='';
	for (var i=0; i<radio.length; i++) {
		if (radio[i].checked) { rVal=radio[i].value; break; }
	}
	block.style.display=(rVal=='Yes')?'block':'none';
}

function frameAlert(msg,onlyCurrent)
{
	if (window.parent && !onlyCurrent)
		window.parent.alert(msg);
	else window.alert(msg);
}

function validateFieldValue(fName,msg)
{
	if (!fName) return true;
	if (fName=='') return true;
	var field=document.getElementsByName(fName);
	if (!field) return true;
	if (!field[0]) return true;
	if ((field[0].value=='') || !(field[0].value)) {
		frameAlert(msg);
		return false;
	}
	return true;
}

function validateSelectSingle(fName,minIndex,badIndexArray,msg)
{
	if (!fName) return true;
	if (fName=='') return true;
	var field=document.getElementsByName(fName);
	if (!field) return true;
	if (!field[0]) return true;
	var idx=field[0].selectedIndex;
	if (!minIndex) minIndex=0;
	if (idx<minIndex) {
		frameAlert(msg);
		return false;
	}
	if (badIndexArray) {
		// TODO: allow caller to specify an array of indexes which should not be selected.
	}
	return true;
}

function validateRadio(fName,msg)
{
	if (!fName) return true;
	if (fName=='') return true;
	var field=document.getElementsByName(fName);
	if (!field) return true;
	if (!field[0]) return true;
	for (var i=0; i<field.length; i++) {
		if (field[i].checked) return true;
	}
	frameAlert(msg);
	return false;
}

// IE hover and PNG behaviours are
// relative to document rather than stylesheet
function behaviour_links () {
	// find out path of document
	fullpath = window.location.pathname.substring(0, location.pathname.lastIndexOf("/") + 1);

	// Get depth
	danumber = fullpath.split("/").length - 2;

	// Extra directory level on dev2
	if ((window.location.hostname.indexOf("dev2.digitalink.com.au")) > -1) {
		danumber = danumber - 1 
	}

	var daPath = ""

	// Go up for each dir level
	for (var i=0; i<danumber;i++) {
		daPath += "../"
	}
	
	// return html
	var bh = "<style type=\"text/css\" media=\"all\"> img, #area-of-need ul li { behavior: url(" + daPath + "css/iepngfix.htc); } "
	bh += "body { behavior: url(" + daPath + "css/csshover.htc); } </style>"
	
	return bh
}


/////////////////////
//
// jQuery functions
//
/////////////////////


$(document).ready(function(){

	// add menu hover fade class		
	$("#main-menu li").addClass("hover-sub");
			
	// Main menu fade
	$("#main-menu li.hover-sub").not(["ul li"]).hover(function() {		
		$(this).children("ul").fadeIn("fast");
	},
	function(){			
		$(this).children("ul").fadeOut("fast"); 
	});
				   
});

