/****************************************************************************

                           I S E   B E L F A S T

                Copyright 2001 British Telecommunications plc

*****************************************************************************


                     M O D U L E   I N F O R M A T I O N

*****************************************************************************


    File Name       :  passwordReset.js

    Author          :  Niall O'Hagan (Computing Partners, Belfast)

    Version         :  

    Last Revision   :  

    Status          :  


    Description     :  Javascript Validation for the password reset tool.


*****************************************************************************

                 M O D U L E   E N T R Y   P O I N T S

*****************************************************************************


*****************************************************************************

                           C H A N G E    L O G

*****************************************************************************

  Date         Who      Tag    Description
  ----         ---      ---    -----------
  28/10/2002   NOH	---    Created
  07/01/2003   GB       C01    Updated Reg Exp for Username.
  15/01/2003   GB       C02    Changed the validation for DOB to be the same as that
                               used in registration.
  12/02/2003   GB       C03    Fixed the Javascript to check for a 2 digit day and month.
*****************************************************************************/


// **************************************************************************
// **** Window use
// **************************************************************************

function openBareWindow( page, id, height, width )
{
   var w1 = window.open(page, id,"height="+height+",width="+width+",top=50,left=50,screenx=50,screeny=50,directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes");
}

function openWindow( page, id, height, width )
{
   var w2 = window.open(page, id,"height="+height+",width="+width+",top=50,left=50,screenx=50,screeny=50,directories=no,location=yes,menubar=yes,status=yes,toolbar=yes,resizable=yes,scrollbars=yes");
}




// **************************************************************************
// **** Error Check 
// **************************************************************************
function validFieldsCheck()
{
   if( userNameErrorFlag )
   	return false;

   if( secAnswerErrorFlag )
   	return false;

   if( dobErrorFlag )
   	return false;

   if( postcodeErrorFlag )
   	return false;

   if( passwordErrorFlag )
   	return false;

   if( passwordConfirmErrorFlag )
   	return false;
   	
   return true;
}


// **************************************************************************
// **** Username validation
// **************************************************************************
//C01
//usernameValid = /^[a-zA-Z][a-zA-Z0-9\.\_]{0,55}$/
usernameValid = /^[a-zA-Z][-\.a-zA-Z0-9\.\_]{0,55}$/
userNameErrorFlag = false;

function validateUsername( username, usernameErrorMessageID )
{
   if( username.match(usernameValid) == null ) 
   {  display(usernameErrorMessageID);
      userNameErrorFlag = true;
   }
   else
   {  hide(usernameErrorMessageID);
      userNameErrorFlag = false;
   }
}



function validateUsernameNonNull( username, usernameErrorMessageID )
{
   if( username.length == 0 )
   {  display(usernameErrorMessageID);
      userNameErrorFlag = true;
   }
   else
   {  hide(usernameErrorMessageID);
      userNameErrorFlag = false;
   }
}


// **************************************************************************
// **** Security Answer validation 
// **************************************************************************
//secAnswerValid = "^[A-Za-z][-A-Za-z 0-9']*$";
//secAnswerValid = /^[- '0-9a-zA-Z\.?]*$/
secAnswerValid = /^[- '0-9a-zA-Z\.?]*$/
secAnswerErrorFlag = false;

function validateSecurityAnswer( secAnswer, secAnswerErrorMessageID )
{
   if( secAnswer.match(secAnswerValid) == null || secAnswer.length == 0)
   {  display(secAnswerErrorMessageID);
      secAnswerErrorFlag = true;
   }
   else
   {  hide(secAnswerErrorMessageID);
      secAnswerErrorFlag = false;
   }
}


function validateSecurityAnswerNonNull( secAnswer, secAnswerErrorMessageID )
{
   if( secAnswer.length == 0 )
   {  display(secAnswerErrorMessageID);
      secAnswerErrorFlag = true;
   }
   else
   {  hide(secAnswerErrorMessageID);
      secAnswerErrorFlag = false;
   }
}

//C02
// **************************************************************************
// **** Date of Birth validation 
// **************************************************************************

function validateDate(inDay, inMonth, inYear) { // Validate Date.

	var fullDate = inDay + inMonth + inYear;
	
	regDate = /^\d{1,8}$/
			//	  3    1 -  1    2  -  2   0    0    0
	if (fullDate.match(regDate) != null && inDay > 0 && inDay < 32 && inMonth > 0 && inMonth < 13)  {
		return true;
	
	} else {
		return false;
	
	}

};


dobErrorFlag = false;

function validateDOB( dob_day, dob_month, dob_year, dobErrorMessageID )
{
   
   dateRes = validateDate(dob_day, dob_month, dob_year);
   validDay = /^\d{1,2}$/;
   var myDate = new Date();
   var myCrYear = myDate.getFullYear();
  //C03
   if (!dateRes || dob_day > 31  || dob_month > 12 || dob_year > myCrYear || dob_year.length < 4 || dob_day.length < 2 || dob_month.length < 2  ) {
      display(dobErrorMessageID);
      dobErrorFlag = true;
   }
   else
   {
      hide(dobErrorMessageID);
      dobErrorFlag = false;
   }
}


// **************************************************************************
// **** Postcode validation 
// **************************************************************************
// postcodeValid = "^[a-zA-Z][a-zA-Z0-9][ 0-9a-zA-Z]*$";
postcodeValid = /^[a-zA-Z][a-zA-Z]?[0-9]([0-9]|[a-zA-Z])[0-9]?[a-zA-Z][a-zA-Z]$/
postcodeErrorFlag = false;

function validatePostcode( postcode, postcodeErrorMessageID )
{
   postcode = postcode.split(" ");
   postcode = postcode.join("");
   if( postcode.match(postcodeValid) == null )
   {  display(postcodeErrorMessageID);
      passwordErrorFlag = true;
   }
   else
   {  hide(postcodeErrorMessageID);
      passwordErrorFlag = false;
   }
}


// **************************************************************************
// **** Password validation 
// **************************************************************************
passwordValid = "^[0-9a-zA-Z]*[0-9][0-9a-zA-Z]*$";
passwordErrorFlag = false;

function validatePassword( password, passwordErrorMessageID )
{
   // Check Length and characters for validity
   if(password.length < 8 || password.length > 16 || password.match(passwordValid) == null )
   {  display(passwordErrorMessageID);
      postcodeErrorFlag = true;
   }
   else
   {  hide(passwordErrorMessageID);
      postcodeErrorFlag = false;
   }
}


// **************************************************************************
// **** Password Confirmation validation 
// **************************************************************************
passwordConfirmValid = "^[0-9a-zA-Z]*[0-9][0-9a-zA-Z]*$";
passwordConfirmErrorFlag = false;

function validatePasswordConfirm( password, confirm, confirmLengthErrorMessageID, confirmMatchErrorMessageID )
{
   // Check Length for validity
   if( confirm.length == 0 )
   {  display(confirmLengthErrorMessageID);
      passwordConfirmErrorFlag = true;
   }
   else
   {  hide(confirmLengthErrorMessageID);
      passwordConfirmErrorFlag = false;
   }

   // Check matches password
   if( password != confirm )
   {  display(confirmMatchErrorMessageID);
      passwordConfirmErrorFlag = true;
   }
   else
   {  hide(confirmMatchErrorMessageID);
      passwordConfirmErrorFlag = false;
   }
}


// **************************************************************************
// **** Item visibility functions 
// **************************************************************************

// NOTE1: Stylesheet MUST specify 'position' attribute for items affected by
//        these function, to ensure operation in Netscape 4.
// NOTE2: It is also advisable to use 'visibility' as 'hidden' in
//        the stylesheet as well to default the items to hidden
// NOTE3: These functions should be provided with the text ID of the item 
//        to hide, not the item itself.

function hide( itemId )
{
   if (document.layers)                            // Netscape 4
   {  document.layers[itemId].visibility = "hide"
   }
   else if (document.all)                          // Internet Explorer 4+
   {  document.all(itemId).style.visibility='hidden';
   }
   else if (document.getElementById)               // Netscape 6 & Internet Explorer 5+
   {  document.getElementById(itemId).style.visibility='hidden';
   }
}

function display( itemId )
{
   if (document.layers)                            // Netscape 4
   {  document.layers[itemId].visibility='show';
   }
   else if (document.all)                          // Internet Explorer 4+
   {  document.all(itemId).style.visibility='visible';
   }
   else if (document.getElementById)               // Netscape 6 & Internet Explorer 5+
   {  document.getElementById(itemId).style.visibility='visible';
   }
}
