//Function was changed please ensure to Change on page
// Now it takes button and form  object as parameters
function afwcNext(objButton,objForm){	
	if(afwcPagePreSubmitValidate())
	{
		if(objButton != '')
		{
			objButton.src = "/images/hins/buyonline/PleaseWait.gif"	
			objButton.style.cursor = 'wait' 
		}
		objForm.afwcNav.value = 'next';
		objForm.afwcNav.disable =true;
		objForm.submit();
	}
	else
	{
		if(objButton != '')
		{
			objButton.src = "/images/hins/buyonline/btn_continue.gif"	
			objButton.style.cursor = 'hand'
		}
	 return false;
	}
}
//Function was changed please ensure to Change on page
// Now it takes button and form  object as parameters
function afwcPayOffline(objButton,objForm){
	if(objButton != '')
	{
		objButton.src = "/images/hins/buyonline/PleaseWait.gif"
		objButton.style.cursor = 'wait'	
	}
	objForm.afwcNav.value = 'payOffline';
	objForm.submit();
}
//Function was changed please ensure to Change on page
// Now it takes button and form  object as parameters
function afwcBack(objButton,objForm){
	if(objButton != '')
	{
		objButton.src = "/images/hins/buyonline/PleaseWait.gif"
		objButton.style.cursor = 'wait'	
	}	
	objForm.afwcNav.value = 'back';
	objForm.submit();
}

function afwcPopupHelps(query, width, height){
	afwcPopupHelpsAu_sob(query, width, height, 'au');
	
}

function afwcPopupHelpsAu_sob(query, width, height, au_sob)
{
    var myWidth = width
	var myHeight = height
	
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
	
	window.open('/' + 'au' + '/online_services/HealthCover/help/' + query + '.asp?au_viewMode=content-only', "Help", 
				"menubar=no,resizable=yes, scrollbars=yes, width=" + 
				myWidth + ",height=" + myHeight + " , left=" + myLeft + ",top= " + myTop);
}

function afwcCpoPopupHelps(query, width, height){
	
	var myWidth = width
	var myHeight = height
	
  var myLeft = (screen.width-myWidth)/2;
  var myTop = (screen.height-myHeight)/2;
	
	window.open('/au/online_services/HealthCover/help/' + query + '.asp?au_viewMode=content-only', "Help", 
				"menubar=no,resizable=yes, scrollbars=yes, width=" + 
				myWidth + ",height=" + myHeight + " , left=" + myLeft + ",top= " + myTop);
}



//-------------------------------------------------------------------------------------------------

function afwcCheckRequiredTextInputField(ctl, name){
	if(ctl.value == ""){
		alert("Please enter your " + name);
		ctl.focus();
		return false;
	}
	
	return true;
}

function afwcCheckRequiredTextInputField_wo(ctl, name){
	if(ctl.value == ""){
		ctl.focus();
		return false;
	}
	
	return true;
}
	
//-------------------------------------------------------------------------------------------------

function afwcGetSelectedValue(selectBox){
	var val;
	
	if(selectBox instanceof String){
		selectBox = document.getElementById(selectBox);
	}
	var idx = selectBox.selectedIndex;
	
	if(idx == undefined){
		return null;
	}else if(selectBox.options[idx] == undefined){
		return null;
	}
	
	val = selectBox.options[idx].value;
	
	// to deal with legacy practice yuk!:	
	if(val == "NOSELECTION"){
		val = "";
	}
	
	return val;	
}

//-------------------------------------------------------------------------------------------------

function afwcGetSelectedValueById(selectBoxId){
	var val;
	var selectBox;
	
	selectBox = document.getElementById(selectBoxId);			

	if(!selectBox){
		//alert('select box with id=' + selectBoxId + ' is not found3.');
		return null;
	}
	
	return afwcGetSelectedValue(selectBox);
}
function afwcDPopupHelps(query, width, height)
{		
var myWidth = width	
var myHeight = height	  
var myLeft = (screen.width-myWidth)/2;  
var myTop = (screen.height-myHeight)/2;		
window.open('/au/online_services/HealthCover/help/' + query , "Help", 				"menubar=no,resizable=yes, scrollbars=yes, width=" + 				myWidth + ",height=" + myHeight + " , left=" + myLeft + ",top= " + myTop);
}
//-------------------------------------------------------------------------------------------------

function afwcSetCheckedValue(ctlObj, value){
	if(!ctlObj)
		return;
		
	var len = ctlObj.length;
	for(var i=0; i<len; i++){
	
		if(ctlObj[i].value == value){
			ctlObj[i].checked = true;
		}else{
			ctlObj[i].checked = false;
		}
	}
}

//-------------------------------------------------------------------------------------------------

function afwcGetCheckedValue(ctlObj){
	if(!ctlObj)
		return null;
		
	var len = ctlObj.length;
	
	if((len == undefined) && ctlObj.checked){
		return ctlObj.value;
	}
	
	for(var i=0; i<len; i++){
		if(ctlObj[i].checked){
			return ctlObj[i].value;
		}	
	}
	
	return null;
}

//-------------------------------------------------------------------------------------------------

function afwcPopup(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

//-------------------------------------------------------------------------------------------------

function afwcHideControlBlock(blockName, bHide){
	var block = document.getElementById(blockName);
	if(block){
		block.style.display = (bHide)? "none":"block";
	}
}

//-------------------------------------------------------------------------------------------------

function afwcJumpToPage(objForm, pageName){
	objForm.afwcNav.value = pageName;
	objForm.submit();
}

//-------------------------------------------------------------------------------------------------

function ValidString(ChkString,ValidString)
{
	for (i=0; i<ChkString.length; i++)
	{
		if (ValidString.indexOf(ChkString.substring(i,i+1)) == -1) return false;
	}
	return true;
}
//-------------------------------------------------------------------------------------------------

// Returns true if the string only contains alpha characters- and ' for Irish and French
function isAlpha(txt)
{
	//return ValidString(txt,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '");
	var rx = /[^a-zA-Z\s]/g;
	
	// should returns no-matches
	return rx.test(txt) == false;
}
//-------------------------------------------------------------------------------------------------

// Returns true if the string only contains numeric characters (empty string = true)
function isNumeric(txt)
{
	var rx = /[^0-9]/g;
	// rx must returns no-matches
	return rx.test(txt) == false;
}

//-------------------------------------------------------------------------------------------------
function isWord(txt){
	var rx = /[^a-zA-Z\s'\-]/g;
	
	// should returns no-matches
	return rx.test(txt) == false;	
}
//-------------------------------------------------------------------------------------------------
function isPhoneNo(txt)
{
	if(!ValidString(txt,'0123456789-() ')){
		return false;
	}
	
	var phoneNumberDelim = /[\(\)\-\s]/g;
	var phone = txt.replace(phoneNumberDelim, '');
	
	if(phone.length > 10){
		return false;
	}
	
	return true;
}

//-------------------------------------------------------------------------------------------------

function alphaonly(Str){
	var Chars = /[abcd]/
	//alert ("test "+Chars.match(Str));
	
	var charsnotallowed = new RegExp(Chars)
	var IPArray=Str.match(charsnotallowed)

	alert(IPArray);
	
	//alert(Chars.test(Str));
	
	if (IPArray!=null) {
		alert("oh no!");
		return false;
	}
	else
	{
		alert("OK");
	}

	return true;
}

//var matchArray=emailStr.match(emailPat)

//-------------------------------------------------------------------------------------------------

function emailCheck(emailStr){
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is
	valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		alert("Your email address is incorrect.  Please re-enter your email address")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("Your email address contains an incorrect user-name.  Please re-enter your email address")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Your email destination IP address is incorrect.  Please re-enter your email address")
				return false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name for your email address is incorrect.  Please re-enter your email address")
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		alert("The email address must end in a three-letter domain, or two letter country")
		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="The email address is missing a hostname!"
		alert(errStr)
		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function emailCheckwo(emailStr){
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is
	valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//alert("Your email address is incorrect.  Please re-enter your email address")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		//alert("Your email address contains an incorrect user-name.  Please re-enter your email address")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Your email destination IP address is incorrect.  Please re-enter your email address")
				return false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name for your email address is incorrect.  Please re-enter your email address")
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		//alert("The email address must end in a three-letter domain, or two letter country")
		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="The email address is missing a hostname!"
		//alert(errStr)
		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

//-------------------------------------------------------------------------------------------------
// 

function afwcValidateAxDate_mod(fieldName, errorMsg){
	
	var dmy = document.getElementById(fieldName).value;
	
		var res = date_validateDateStr(dmy);	
			
		if(!(res instanceof Date)){
			if(res == -1){
				errorMsg = 'Please enter your ' + errorMsg + ' Date of Birth in the correct format dd/mm/yyyy'
			}
			else if(res == -2){
				errorMsg = 'Year of the ' + errorMsg + ' Date of Birth must be greater then 1899.'
			}
			else{
				errorMsg = res + ' of the ' + errorMsg + ' Date of Birth is not valid.'
			}			
			alert(errorMsg);
			return null;
		}	
	return res;
}

function afwcValidateAxDate(fieldName, errorMsg){
	var dmy;
	
	dmy = afwcGetSelectedValueById(fieldName + 'Day') 
		+ ' ' + afwcGetSelectedValueById(fieldName + 'Month') 
		+ ' ' + afwcGetSelectedValueById(fieldName + 'Year');

	var res = date_validateDateStr(dmy);
	
	if(!(res instanceof Date)){
		alert(errorMsg);
		return null;
	}
	
	return res;
}

function afwcValidateAxDate_wo(fieldName, errorMsg){
	var dmy;

	dmy = afwcGetSelectedValueById(fieldName + 'Day') 
		+ ' ' + afwcGetSelectedValueById(fieldName + 'Month') 
		+ ' ' + afwcGetSelectedValueById(fieldName + 'Year');
	
	dmy = fieldName
	var res = date_validateDateStr(dmy);

	if(!(res instanceof Date)){
		//alert(errorMsg);
		return null;
	}
	
	return res;
}

	//-------------------------------------------------------------------------------------------------
// Page specific validation
// This function is to validate the dependant details: first name, surname, dob, gender and 
// relation to the applicant.
// It assumes all input field names are with hi_standard1_dependant prefix

function afwcValidateDependant(dependantCount){
	var i;
	
	var bReturn = true;
	var sName, fName, dob;
	var gender, rel;
	
	//Defining Constants
	sName = 'hi_standard1__dependant2__sname';
	fName = 'hi_standard1__dependant2__fname';	
	dob      = 'hi_standard1__dependant2__dob';
	gender = 'hi_standard1__dependant2__gender';
	rel    = 'hi_standard1__dependant2__relationship';
	
	//dobDay, dobMonth, dobYear
	//dobDay = 'hi_standard1__dependant2__dobDay';
	//dobMonth = 'hi_standard1__dependant2__dobMonth';
	//dobYear  = 'hi_standard1__dependant2__dobYear';
		
	var today = new Date();
	
	var depStudentMaxDob = new Date();
	depStudentMaxDob.setFullYear(today.getFullYear() - 23);
	depStudentMaxDob.setHours(0,0,0,0);
	
	var depStudentMinDob = new Date();
	depStudentMinDob.setFullYear(today.getFullYear() - 25);
	depStudentMinDob.setHours(0,0,0,0);
	
	var depChildMinDob  = new Date();
	depChildMinDob.setFullYear(today.getFullYear() - 23)
	depChildMinDob.setHours(0,0,0,0);
	
	bReturn = true;
	
	for(i=1; i<=dependantCount; i++){
	
		var depPat = /hi_standard1__dependant[0-9]/;
				
		var depNo = i+1;
		
		var replacement = "hi_standard1__dependant" + depNo;		
		
		sName = sName.replace(depPat, replacement);
		fName = fName.replace(depPat, replacement);
		dob = dob.replace(depPat, replacement);
		gender = gender.replace(depPat, replacement);
		rel    = rel.replace(depPat, replacement);
		
		if(!eval('afwcCheckRequiredTextInputField(document.appForm.' + sName + ', "Dependant ' + i + ' Surname")')){
			return false;
		}		
		if(!eval('afwcCheckRequiredTextInputField(document.appForm.' + fName + ', "Dependant ' + i + ' First name")')){
			return false;
		}	
		if(!eval('afwcCheckRequiredTextInputField(document.appForm.' + dob + ', "Dependant ' + i + ' Date of Birth in the correct format dd/mm/yyyy")')){
			return false;
		}
		
		var dobDt = afwcValidateAxDate_mod(dob, 'Dependant ' + i)
		
		if(!dobDt){
			eval('document.appForm.' + dob + '.focus()');			
			return false;
		}
		
		var depGender = afwcGetSelectedValueById(gender);
		if(depGender == ''){
			alert('Please select Dependant ' + i + ' gender.');
			eval('document.appForm.' + gender + '.focus()');
			return false;
		}
		
		var selectedRel = afwcGetSelectedValueById(rel);
		if(selectedRel == ''){
			alert('Please select Dependant ' + i + ' relationship to you.');
			eval('document.appForm.' + rel + '.focus()');
			return false;
		}
				
		if(selectedRel == '0005'){	// dependent child, must be less than < 21yo
			if(dobDt < depChildMinDob){
				alert('An eligible Dependant child ' + i + ' must be less than 23 years of age.');				
				eval('document.appForm.' + dob + '.focus()');
				return false;
			}
		}else if(selectedRel == '0006'){	// dependant student must be 21-25 yo.
			if(dobDt < depStudentMinDob){
				alert('An eligible Student Dependant ' + i + ' must be less than 25 years of age.');
				eval('document.appForm.' + dob + '.focus()');
				return false;
			}
		}else if(selectedRel == '0007'){
			if(dobDt < depChildMinDob){
				alert('An eligible Foster Dependant ' + i + ' must be less than 23 years of age.');
				eval('document.appForm.' + dob + '.focus()');
				return false;
			}
		}
			
	}// end for
		
	return true;
		
}// end function

function afwcHideDiv (div) {
	var dErr = document.getElementById(div);
	dErr.style.visibility="hidden";
}

function afwcHideDivs (sPrefix) {
	
	var divs = document.getElementsByTagName("div");
	
    for (a=0; a < divs.length; a++) {
               if (divs[a].id.indexOf(sPrefix) != -1) {
                      afwcHideDiv(divs[a].id);
               }
    }
}
	
function afwcShowDiv (div) {
	var dErr = document.getElementById(div);
	dErr.style.visibility="visible";
}
	
function afwcChangeDivHtml(div, message) {
	var div = document.getElementById(div);
	div.innerHTML = message;
}

function afwcChangeDivErrHtml(div, message) {

	var errMessage = "<table width=\"200px\" cellpadding=\"0\" cellspacing=\"2px\"><tr><td valign=\"top\" width=\"2px\" ><img src=\"/images/hins/buyonline/sml_left_arror.gif\" /></td><td valign=\"top\" style=\"color: red;\" align=\"left\">" + message + "</td></tr></table>";
	var div = document.getElementById(div);
	div.innerHTML = errMessage;
}

// get month in intValue ( pass in short month name)
function getIntMonth(strShortMonth) {
	
	switch(strShortMonth) {
		case "JAN" :
			return 0 ;
		case "FEB" :
			return 1 ;
		case "MAR":
			return 2 ;
		case "APR":
			return 3 ;
		case "MAY":
			return 4 ;
		case "JUN":
			return 5 ;
		case "JUL":
			return 6 ;
		case "AUG":
			return 7 ;
		case "SEP":
			return 8 ;
		case "OCT":
			return 9 ;
		case "NOV":
			return 10 ;
		case "DEC":
			return 11 ;			
	}	
}

/********************************************************************/

//function  to check if age is eligible for LHC
//returns false if under 31 years old
function afwcValidateLHCAge(dob){	

	
	var today = new Date();
	var LHCMaxDob = new Date();
	LHCMaxDob.setFullYear(today.getFullYear() - 31);
	LHCMaxDob.setHours(0,0,0,0);
	 
	//returns false if under 31 years old
	if ( dob <= LHCMaxDob) {		
		return false;
	}
	else {
		return true ;
	}
}	

function convertDOB(day,month,year) {

	var dob	
	
	if ( day != "" && month != "" && year != "" ) {		
	
		// get month number
		//intMonth = getIntMonth(month);	
		
		// convert to date	
		dob = new Date();
		dob.setFullYear(year, month, day)	;
		dob.setHours(0,0,0,0);			
	}
	
	return dob;	
}	


	



