/******************************************************************************
**		File: core.js
**		Name: Core Javascript functions
**		Desc: These Javascript functions can be used in any ASPX pages.
**                        
**
**		Auth: Danielle Sheridan
**		Date: 03/02/2003
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:  		Author:   				Description:
**		--------  	--------      			-----------------------------------
**		20/10/03	Danielle Sheridan	Deleted dragLayer()
**		16/09/03	Israel Obinyan			Added checkBrowser()
**		03/02/03  John Mabbott			Added clearText() and autoTab()
**		27/02/03 	Danielle Sheridan	Added selectReveal()
**		09/05/03	Israel Obinyan			Added setFocus()
*******************************************************************************/

// Check the browser version
function checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL)
{
   var newURL='';
   var verStr=navigator.appVersion;
   var app=navigator.appName;
   var version = parseFloat(verStr);

   if (app.indexOf('Netscape') != -1)
   {
      if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
      else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
   }
   else if (app.indexOf('Microsoft') != -1)
   {
      version = parseFloat(verStr.charAt(22));
      if (version >= IEvers || verStr.indexOf(IEvers) != -1)
      {
         if (IEpass>0) newURL=(IEpass==1)?URL:altURL;
      }
      else
      {
         if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;
      }
   }
   else if (OBpass>0)
   {
      newURL=(OBpass==1)?URL:altURL;
   }

   if (newURL)
   {
      window.location=unescape(newURL);
      document.MM_returnValue=false;
   }
}

// Automatically clears the field on focus.
function clearText(thefield)
{
   if (thefield.defaultValue == thefield.value)
   {
      thefield.value = "";
   }
}

// function for automatically tabbing to the next field once a field has been filled
//===========================================================
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}

function MM_checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v4.0
 		  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
 		  if (app.indexOf('Netscape') != -1) {
 			 if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
 			 else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
 		  } else if (app.indexOf('Microsoft') != -1) {
 			 if (version >= IEvers || verStr.indexOf(IEvers) != -1)
 			  {if (IEpass>0) newURL=(IEpass==1)?URL:altURL;}
 			 else {if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;}
 		  } else if (OBpass>0) newURL=(OBpass==1)?URL:altURL;
 		  if (newURL) { window.location=unescape(newURL); document.MM_returnValue=false; }
 		}


// Change the colour of TR tags when used as links.
// ******************************************
// Do not use deprecated by changeClass
// ******************************************
function changeColour(id,colour)
{
	tablecolour = ''
		if (colour == '') { 
			id.bgColor = tablecolour;
		}
		else {
			id.bgColor = colour;
		}
}


// function for changing the class of an object
function changeClass(objID,newClass) { 
	blankClass = ''
		if (newClass == '') { 
			objID.className = blankClass;
		}
			else {
		objID.className = newClass;
		}
}


// function for hiding/showing any object
function hideAndShowObject(objID)
{
	if(document.getElementById(objID).style.display == 'block')
	{
		document.getElementById(objID).style.display = 'none';
	}
	else
	{
		document.getElementById(objID).style.display = 'block';
	}
	
}
// functions for hiding/showing any object
function showObject(objID) {
	document.getElementById(objID).style.display = 'block';
}
	
function hideObject(objID) {
	document.getElementById(objID).style.display = 'none';
}


// function for changing the showing/hiding an object on dropdown selection
// **************************************************************
function selectReveal(objID,state) { 
		if (state == 'hide') { 
			document.getElementById(objID).style.display = 'none';
		}
		if (state == 'show') { 
			document.getElementById(objID).style.display = 'inline'
		}
		else {
			document.getElementById(objID).style.display = 'none';
		}
}


// function for changing a property of an object
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

// functions for changing the value of an input field object
function changeValue(objID,v) {
 document.getElementById(objID).value = v;
}

function writeCloseButtonDiv()
{
    document.write("<div id='buttons'><input type='submit' onclick='javascript: window.close();return false' value='Close window' title='Close this browser window' tabindex='501' class='formbutton'></div>");
}

function displayMousePointer()
{
   if (typeof(Page_IsValid) != "undefined" && Page_IsValid == true)
   {
      MM_showHideLayers('mouseWaitDiv','','show');
   }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { // controls the swapping and restoration of image states
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { // preloads roll-over images
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { // controls the swapping  of image states
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { // opens popup windows
  window.open(theURL,winName,features);
}


function MM_jumpMenu(targ,selObj,restore){ // controls jump menus
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function newWindow(targetwindow,winName,features) { //opens new browser windows from dropdown menus
	targetwindow = window.form1.dropdown.value
	window.open(targetwindow,winName,features);
 }

function tmt_findObj(n){ 
	var x,t; if((n.indexOf("?"))>0&&parent.frames.length){t=n.split("?");
	x=eval("parent.frames['"+t[1]+"'].document.getElementById('"+t[0]+"')");
	}else{x=document.getElementById(n)}return x;
}

function MM_showHideLayers() { //  shows and hides layers
  var i,p,v,obj,args=MM_showHideLayers.arguments;if(document.getElementById){
   for (i=0; i<(args.length-2); i+=3){ obj=tmt_findObj(args[i]);v=args[i+2];
   v=(v=='show')?'visible':(v='hide')?'hidden':v;
   if(obj)obj.style.visibility=v;}} else{
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }}
}

function BW_reload() {location.reload();} // controls page reloads

function MM_goToURL() { // controls jump menus
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function BW_centerLayers() { //centers layers
	if (document.layers || document.all || document.getElementById){
		var winWidth, winHeight, i, horz, vert, width, height, offsetX, offsetY, negX, negY, group, x, y, args;
		args = BW_centerLayers.arguments;
		
		onresize = BW_reload;

				
		winWidth = (document.all)?document.body.clientWidth:window.innerWidth;
		winHeight = (document.all)?document.body.clientHeight:window.innerHeight;
				
		for (i=0; i<(args.length-9); i+=10) {
			horz    = args[i+1];
			vert    = args[i+2];
			width   = parseInt(args[i+3]);
			height  = parseInt(args[i+4]);
			offsetX = parseInt(args[i+5]);
			offsetY = parseInt(args[i+6]);
			negX    = args[i+7];
			negY    = args[i+8];
		
			x = ((winWidth - width)/2) + offsetX;
			y = ((winHeight - height)/2) + offsetY;
						
			x = (negX=='false' && (x < 0))?0:x;
			y = (negY=='false' && (y < 0))?0:y;
				
			layerObj = (document.getElementById)?document.getElementById(args[i]):MM_findObj(args[i]);
			
			if (layerObj!=null) {
				layerObj = (layerObj.style)?layerObj.style:layerObj;
				layerObj.left = (horz=="true")?x:layerObj.left;
				layerObj.top = (vert=="true")?y:layerObj.top;
			}
		}
	}
}

function ContextHelp(topic) {
	childWindowURL = 'ContextHelp.aspx#';
	childWindow = window.open(childWindowURL+topic,'Help', 'toolbar=no,scrollbars=yes,width=300,height=300,resizable=yes');
	childWindow.focus();
}

function setFocus(objID)
{
	document.getElementById(objID).focus();
}

function hover(objID)
{
	document.getElementById(objID.id).style.color = '#FF0000';
	document.getElementById(objID.id).style.cursor = 'hand';
}
function hoverout(objID)
{
	document.getElementById(objID.id).style.color = '#000000';		
}

// JavaScript Document
function openSummaryWindow()
{
   var url = "https://www.halifaxcards.co.uk/NASApp/halifaxcards/do/SummaryBox?sc=" + document.pageForm.sourceCode.value + "&special=nobrand";
   window.open(url, 'cardSummary', 'scrollbars=yes,menubar=no,status=no,toolbar=no,resizable=no,dependent=yes,width=600,height=400')
}

function openBrWindow(theURL,winName,features) 
{		
	if (pageForm.SelectedCard != null)
	{
		// we are on the chooseCard page
		theURL = theURL + "?CardType=" + pageForm.CardType.value;
	}		
	// opens popup windows			
	window.open(theURL,winName,features);
}

function displaySection() 
{	
	if (document.pageForm.SummaryPage__ctl0_summaryDetails__ctl0_HasAdditionalCardholder)
	{
		if(document.pageForm.SummaryPage__ctl0_summaryDetails__ctl0_HasAdditionalCardholder.value == "False")
			hideObject('additionalcards');
	}
	
	if(document.pageForm.SummaryPage__ctl0_summaryDetails_HasAdditionalCardholder)
	{
		if(document.pageForm.SummaryPage__ctl0_summaryDetails_HasAdditionalCardholder.value == "False")
			hideObject('additionalcards');
	}
}
	
function showEmploymentTextBox(triggerID) 
{	
	var titleDDL = document.pageForm.FinancialPage__ctl0_currentEmployment_lbx_EmploymentDetails_OccupationStatus.value;
	if (titleDDL == 'Self-employed')
	{
		document.getElementById(triggerID).style.display = 'inline';
		if(document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value == '')
			document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = 'Nature of self-employment';

	}	
	else if(titleDDL == 'Retired')
	{
		document.getElementById(triggerID).style.display = 'inline';
		if(document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value == '')
			document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = 'Employment before retirement';

	}
	else {
		//Reset the Other field to be blank
		document.getElementById(triggerID).style.display = 'none';
		document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = '';
	}		
}
	
function updateEmploymentTextBox(triggerID)
{
	var titleDDL = document.pageForm.FinancialPage__ctl0_currentEmployment_lbx_EmploymentDetails_OccupationStatus.value
	if (titleDDL == 'Self-employed')
	{
		document.getElementById(triggerID).style.display = 'inline';
		document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = 'Nature of self-employment';
	}
	else if (titleDDL == 'Retired')
	{
		document.getElementById(triggerID).style.display = 'inline';
		document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = 'Employment before retirement';
	}
	else {
		//Reset the Other field to be blank
		document.getElementById(triggerID).style.display = 'none';
		document.pageForm.FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther.value = '';
	}
}
	
function hideCardholder(objVal)
{      
	hideObject(objVal);
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_FirstName1.value='';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_Surname1.value ='';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_lbx_Cardholder_TitlePrefix1.value = '';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_TitlePrefixOther1.value ='';
	showTextBox('PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_TitlePrefixOther1', 'Other', 'Unselected');
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_Middleinitial1.value ='';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_BirthDay1.value ='dd';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_BirthMonth1.value ='mm';
	document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_BirthYear1.value ='yyyy';
}

IssuerFinancialPage = function()
{
	showEmploymentTextBox('FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther' );
	setFocus('FinancialPage__ctl0_currentEmployment_lbx_EmploymentDetails_OccupationStatus')      
}

IssuerPersonalPage = function()
{	
	showTextBox('PersonalPage__ctl0_personalDetails_nameAndContactDetails_tb_TitlePrefixOther', 'Other', document.pageForm.PersonalPage__ctl0_personalDetails_nameAndContactDetails_lbx_TitlePrefix.value);
	showTextBox('PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_tb_Cardholder_TitlePrefixOther1', 'Other', document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_cardholder1_lbx_Cardholder_TitlePrefix1.value);
	if (document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_AdditionalCardYes.checked == true) showObject('additionalcards');
	if (document.pageForm.PersonalPage__ctl0_personalDetails_additionalCardholders_AdditionalCardNo.checked == true) hideCardholder('additionalcards');
	setFocus('PersonalPage__ctl0_personalDetails_nameAndContactDetails_lbx_TitlePrefix');
}

IssuerIntroductionPage = function()
{	
	//setFocus('ChooseCardPage__ctl0_nameAndContactDetails_tb_FirstName');	
	if (document.pageForm.IntroductionPage__ctl0_JointApplicantQuestion_rb_JointApplicantYes.checked)
		showObject('jointDetails');
	if (document.pageForm.IntroductionPage__ctl0_JointApplicantQuestion2_rb_SameAddressNo.checked)
		showObject('jointPostcode');	
	checkBrowser(5.0,0,2,5.0,0,2,2,'','oldbrowser.htm');
}

function showTextBox(triggerID, triggerValue, currentValue) 
{
	if (currentValue == triggerValue) { 
		document.getElementById(triggerID).style.display = 'inline';
	}
	else {
		document.getElementById(triggerID).style.display = 'none';
		document.getElementById(triggerID).value = 'Enter here';
	}
}

function FinancialLoad()
{
	if (typeof BrandFinancialPage != "undefined"){BrandFinancialPage();}		
	else if (typeof IssuerFinancialPage != "undefined"){IssuerFinancialPage();}
}

function PersonalDetailsLoad()
{
	if (typeof BrandPersonalPage != "undefined"){BrandPersonalPage();}	
	else if (typeof IssuerPersonalPage != "undefined"){IssuerPersonalPage();}
}

function IntroductionLoad()
{
	if (typeof BrandIntroductionPage != "undefined"){BrandIntroductionPage();}	
	else if (typeof IssuerIntroductionPage != "undefined"){IssuerIntroductionPage();}
}

BrandPersonalPage = function()
{	
	if(document.pageForm.PersonalPage__ctl0_nameAndContactDetails_tb_TitlePrefixOther)
		showTextBox('PersonalPage__ctl0_nameAndContactDetails_tb_TitlePrefixOther', 'Other', document.pageForm.PersonalPage__ctl0_nameAndContactDetails_lbx_TitlePrefix.value);
	if(document.pageForm.PersonalPage__ctl0_nameAndContactDetails2_tb_TitlePrefixOther)
		showTextBox('PersonalPage__ctl0_nameAndContactDetails2_tb_TitlePrefixOther', 'Other', document.pageForm.PersonalPage__ctl0_nameAndContactDetails2_lbx_TitlePrefix.value);
}

BrandFinancialPage = function()
{
	showEmploymentTextBox('FinancialPage__ctl0_currentEmployment_tb_EmploymentDetails_OccupationStatusOther' );
	setFocus('FinancialPage__ctl0_currentEmployment_lbx_EmploymentDetails_OccupationStatus');      
	if (document.pageForm.FinancialPage__ctl0_bankDetails2_applicantDetails_rb_SameAccountNo.checked)
		showObject('bank2');
}