// Account Selector Javascript

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// initialize
function as_init()
{


	//var step = parseInt(document.as_form.step.value);
	// set step visibility
	//alert('step ' + step);

	// first page load
	//if (step == 0)
	//{
	//	step = 1;
	//}

	as_setStep(1);



	// set the visited step state
	//alert('onload');
	//expandcollapse(document.getElementById('as_ccard,1,yn'));

	// HACK: initialize the conditional fields
	//var e = document.getElementsByName('as_travel,1,yn');
	//var f = e[0];
	//if (e[0].value == '0') f = e[1];

	//as_setConditionalFields(f);

	e = document.getElementsByName('as_channel_atm,1');
	as_setConditionalFields(e[0]);
	e = document.getElementsByName('as_channel_branch,1');
	as_setConditionalFields(e[0]);
	e = document.getElementsByName('as_channel_online,1');
	as_setConditionalFields(e[0]);

	// initialize the expand collapse elements
	var collection;
	collection = document.getElementsByName('as_ccard,1,yn');

	for ( i = 0;  collection && i  < collection.length; i++)
	{
		//alert(collection[i].name + ' ' + collection[i].value  + ' chk: ' + collection[i].checked);
		if (collection[i].checked)
			expandcollapse(collection[i]);
	}


	// set the debug flag
	var a = document.URL.indexOf("debug");
	if (a != -1)
	{
		b = document.URL.indexOf("]",a);

		if (b > -1)
		{
			dbgs = document.URL.substring(a+7,b);
			//alert(" debug: " + dbgs);
			document.as_form.debug.value = dbgs;

			var ff = document.getElementById('actionurl');
			//alert(" ff.value: " + dbgs);
			ff.value = document.as_form.action;
			ff.style.display = 'block';

		}
	}

	// set the olb flag
	// so that user opens accounts in OLB and not public if account is available in OLB
	var olbvalue = getURLParam('olb');
	//alert("OLB VALUE: " + olbvalue);
	if (olbvalue == 1)
	{
		document.as_form.olb.value = 1;
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function as_submitform()
{
	// only allow submit if reached step 3

	//alert(stepsVisited+" steps to recoment "+stepsToRecommend);
	if (stepsVisited >= stepsToRecommend)
	{
		var recommendbt = document.getElementById('as_recommendbtn');

		// HACK: to make the 2 + 1 steps work
		if (stepsVisited < 3 && stepsToRecommend == 2)
		{
			as_setStep(stepsVisited + 1);
		}

		if (document.as_form.debug.value != "")
		{
			var ff = document.getElementById('actionurl');
			document.as_form.action=ff.value;
		}

		//Append OLB Parameter to action -- For Onlinbanking selector
		//appendOLBParam(document.as_form);

		// handy to have some debug switch in here, this is used by the perl cgi
		if (document.as_form.debug.value != "")
		{
			document.as_form.target = '_AS_DEBUG';
			//alert('submit action: ' + document.as_form.action);
		}

		document.as_form.submit();

		// go to named anchor at top
		window.location.href='#as_top';

		stepsVisited = stepsToRecommend + 1;
	}
	// otherwise go to next step
	else
	{
		as_setStep(stepsVisited + 1);
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function as_setStep(s)
{

	//alert ('newstep: ' + s + ' currentStep: ' + currentStep + ' stepsVisited:' + stepsVisited);

	if (s > stepsVisited + 1) return;

	for (var i = 1; i < 4 ; i++)
	{
		var stepPanel = document.getElementById('as_step' + i);
		var stepTab = document.getElementById('as_step' + i + 'tab');
		var msg = stepTab.getElementsByTagName('div')[1];	// HACK
		//alert(msg.innerHTML);
		var icon = stepTab.getElementsByTagName('img')[0];
		//alert (msg.innerText);

		// current step
		if (i == s)
		{
			stepPanel.style.display = 'block';
			stepTab.className = 'stepTabOn';
			msg.innerHTML = '';
		}
		// step is accessible
		else if (i < stepsVisited + 1 || i == s + 1)
		{
			stepPanel.style.display = 'none';
			stepTab.className = 'stepTab';
			if (i < stepsVisited + 1)
			{
				msg.innerHTML = clicktoChangeTxt;
			}
			else
			{
				msg.innerHTML = clicktoContinueTxt;
			}
		}
		// step is disabled
		else
		{
			stepPanel.style.display = 'none';
			stepTab.className = 'stepTabDisabled';
			icon.src = 'images/steptabbox_disabled.gif';
			msg.innerHTML = '';
		}
		// set the icon
		if (i <= stepsVisited)
		{
			icon.src = 'images/steptabbox_checked.gif';
		}
		else
		{
			icon.src = 'images/steptabbox.gif';
		}

		// go to named anchor at top
		window.location.href='#as_top';
	}

	// set the recommend button and iFrame state
	var recommendbt1 = document.getElementById('as_recommend1');
	var recommendbt2 = document.getElementById('as_recommend2');
	var recommendbt3 = document.getElementById('as_recommend3');
	var recommendbt4 = document.getElementById('as_recommend4');
	//alert(stepsVisited);
	if (stepsVisited < stepsToRecommend && s != stepsToRecommend)
	{
		recommendbt1.innerHTML = continue_bt;
		recommendbt2.innerHTML = continue_bt;
		recommendbt3.innerHTML = continue_bt;
		recommendbt4.innerHTML = continue_bt;
	}
	else
	{
		recommendbt1.innerHTML = recommend_bt;
		recommendbt2.innerHTML = recommend_bt;
		recommendbt3.innerHTML = recommend_bt;
		recommendbt4.innerHTML = recommend_bt;
	}


	if (stepsVisited < stepsToRecommend + 1 )
	{
		// update the iFrame to show a side message
		var recommendframe = document.getElementById('as_recommend_frame');
		var page =  'step' + (s) + '.html';
		//alert ('side page:'  + page);
		recommendframe.src = page;
	}

	currentStep = s;
	stepsVisited = Math.max(s, stepsVisited);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function boolInput(f)
{
	if (f.type == 'radio')
	{
		//alert ('radio ' + f.name + ' val: ' + (f.value == '1'));
		return f.value == '1';
	}
	else
	{
		//alert ('checkbox ' + f.name + ' chk: ' + f.checked );
		return f.checked;
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function showhelp(id,v)
{
	var help = document.getElementById(id);

	if (help == null)
	{
		alert ("missing help div: " + id);
		return;
	}
	//alert(f + ': ' + ' style.display: ' + ff.style.display + ' make: ' +  v);
	if (v)
	{
		help.style.display = 'block';
	}
	else
	{
		help.style.display  = 'none';
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function showhide(f,v)
{
	var ff = document.getElementById(f);
	//alert(f + ': ' + ' style.display: ' + ff.style.display + ' make: ' +  v);
	if (v)
	{
		try
		{
			ff.style.display='table-row';
		}
		catch(e)
		{
			ff.style.display = 'block';
		}
	}
	else
	{
		ff.style.display  = 'none';
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function toggleVisible(f,v)
{
	var ff = document.getElementById(f);
	//alert(f + ' name:' + ff.name + ' type:' + ff.type + ' ' + ' style.visibility=' +  v);
	ff.style.visibility = (v) ? 'visible' : 'hidden';

}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function as_setConditionalFields(f)
{
	var v = boolInput(f);


	// special case for online and phone ...
	if (f.name == 'as_channel_online,1' || f.name == 'as_channel_phone,1')
	{
		//alert(boolInput(document.getElementsByName('as_channel_online,1')[0]) + ' ' + boolInput(document.getElementsByName('as_channel_phone,1')[0]));
		var v = boolInput(document.getElementsByName('as_channel_online,1')[0]) ||
				   boolInput(document.getElementsByName('as_channel_phone,1')[0]);


		showhide('as_bill_online',v);
		showhide('as_trf_online',v);
		//showhide('as_trfother_online',v);
		showhide('as_trfother_email',v);

	}
	else if (f.name == 'as_channel_branch,1')
	{
		showhide('as_cash_branch',v);
		showhide('as_trf_branch',v);
		showhide('as_bill_branch',v);
		showhide('as_cash_branch',v);

		showhide('as_allwithdrawals',boolInput(document.getElementsByName('as_channel_atm,1')[0]) || v);

	}
	else if (f.name == 'as_channel_atm,1')
	{
		showhide('as_cash_atm_rbc',v);
		showhide('as_cash_atm_other',v);
		showhide('as_trf_atm',v);
		showhide('as_bill_atm',v);

		showhide('as_allwithdrawals',boolInput(document.getElementsByName('as_channel_branch,1')[0]) || v);

		// TODO: need to make these condition only when travel also selected
		//var e = document.getElementsByName('as_travel,1,yn');
		//var f2 = e[0];
		//if (e[0].value == '0') f2 = e[1];
		//alert(f2[0].checked + ' ' + f2[1].checked);
		var v2 = true; //f2.checked; //boolInput(document.as_form.as_travel);
		showhide('as_cash_atm_usa', (v && v2));
		showhide('as_cash_atm_intl',(v && v2));

	}
	/*
	else if (f.name == 'as_travel,1,yn')
	{
		// TODO: need to add these fields in as well

		var e = document.getElementsByName('as_travel,1,yn');
		var f3 = e[0];
		if (e[0].value == '0') f3 = e[1];
		//alert(f2[0].checked + ' ' + f2[1].checked);
		var v = f3.checked; //boolInput(document.as_form.as_travel);

		showhide('as_ppdebit_usa',v);
		toggleVisible('can_label',v);
		var f2 = document.getElementsByName('as_channel_atm,1');
		var v2 = f2[0].checked; //boolInput(document.as_form.as_travel);
		showhide('as_cash_atm_usa', (v && v2));
		showhide('as_cash_atm_intl',(v && v2));
	}
	*/
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function expandcollapse(f)
{

	fnot = (f.value == '1') ? '0' : '1';
	//alert(f.name +'_' + f.value);
	showhide(f.name +'_' + f.value, 1);
	showhide(f.name +'_' + fnot, 0);

	//document.getElementById(f.name +'_' + f.value).style.display = 'block';
	//document.getElementById(f.name +'_' +  fnot).style.display = 'none';
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function printFunction()
{
	//alert("printFunction");
	document.getElementById('as_step1').style.display = 'block';
	document.getElementById('as_step2').style.display = 'block';
	document.getElementById('as_step3').style.display = 'block';
	window.print();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function getURLParam(strParamName)
{
	// GetURL Params value from querystring
	var qs = location.search.split('?');
	if (qs.length > 1) {
	var pairs = qs[1].split("&");
		for (var i=0; i<pairs.length; i++) {
			var namevalue = pairs[i].split("=", 2);
			var name = unescape(namevalue[0]);
			var value = unescape(namevalue[1]);
			if (name.toUpperCase() == strParamName.toUpperCase()) {
				return value;
				//break;
			}
		}
	}
	return null;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// opens the url in the target window and then focuses it
function openInTargetWindow(url, target)
{
	//alert('openInTargetWindow url=' + url + ' target=' + target);
	var theWindow = window.open(url, target, '');
	if (window.focus)
	{
		theWindow.focus();
	}
}


