<!-- hide from JavaScript-challenged browsers
	
	/* =============================================================================
	This file contains all the common/generic javascript fucntions that can
	be used on the website. If adding a function, please preceed each function with 
	the function info (as set out below), leaving five blank lines between each 
	function.
	If you are wishing to access any of the functions below you must place the
	following (simply copy):
	<script language="JavaScript" src="/include/inc_javascript.js" type="text/javascript"></script>
	and place in the head section of the web page.
	============================================================================= */



	/* =============================================================================
	Function:	
	Purpose:
	Parameters:	
	Returns:	
	Calling:	
	Notes:		
	============================================================================= */




	/* =============================================================================
	Function:	checkBlank
	Purpose:	Checks whether form text field(s) are blank or not.
	Parameters:	* form field name [req] - the field from the form you want to test
				* "error message" [req] (string) - display text
	Returns:	Boolean. True if field is not blank, false if field is blank
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
  					var isOK = checkBlank(form.name, "your name.",
										  form.address, "your address.");
  					if (!isOK) {
    					return false;
  					} 
				}
	Notes:		Multiple pairs can be entered, you must have a comma separating all arguments.
				Limitations - Returns true if just a whitespace has been entered.
	============================================================================= */
	function checkBlank() {
  		for (var i = 0; i < arguments.length; i += 2) {
			if (!arguments[i].value) {
				alert("Please enter " + arguments[i+1]);
      			arguments[i].focus();
      			return false;
    		}
  		}
  		return true;
	}





	/* =============================================================================
	Function:	checkList
	Purpose:	Checks whether an item has been selected from a drop-down list.
	Parameters:	* form field name [req] - the field from the form you want to test
				* "error message" [req] (string) - display text must be enclosed in double quotation marks.
	Returns:	Boolean. True if an item has ben selected, False if no item has been selected.
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
				  var isSelected = checkList(form.state, "your state.");
				  if (!isSelected) {
				    return false;
				  } 
				}
	Notes:		Limitations - You must have a null option in the first position e.g. "-select-" or ""
	============================================================================= */
	function checkList() {
	  if (arguments[0].selectedIndex == 0) {
	    alert ("Please select " + arguments[1]);
	    arguments[0].focus();
	    return false;
	  }
	  else return true;
	}





	/* =============================================================================
	Function:	checkRadio
	Purpose:	Checks whether a radio button has been selected from a group.
	Parameters:	* form field name [req] - the radio button group you want to test
            	* "error message" [req] (string) - display text must be enclosed in double quotation marks.
	Returns:	Boolean. True if an radio button has been selected, False if no radio button has been selected.
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
				  var isSelected = checkRadio(form.radioGroupName, "an option from the radio button group.");
				  if (!isSelected) {
				    return false;
				  }
				}
	Notes:		Limitations - This function is only useful if no radio button has been pre-selected when page is loaded.
	============================================================================= */
	function checkRadio() {
	  var checkedButton = false;
	  var question = arguments[0];
	  var message = arguments[1];
	  for (var b = 0; b < question.length; b++) {
		if (question[b].checked) {
		  checkedButton = true;
		}
	  }
	  if (checkedButton == false) {
		alert("Please select " + message);
		question[0].focus();
	  }
	  return checkedButton;
	} //end of function checkRadio





	/* =============================================================================
		Function:	change_disabled(strId,strAction)
		Purpose:	Toggles the form element's property between disabled/enabled
		Parameters:	strId -  the id of the form element to toggle
					strAction - either 'disable' or 'enable'
		Returns:	-
		Calling:	-
		============================================================================= */
	function change_disabled(strId,strAction) {
		if (strAction == 'disable') {
			document.getElementById(strId).disabled=true
			clearForm(document.getElementById(strId))
		}
		else {
			document.getElementById(strId).disabled=false
		}
	}	
	


	function checkbox_disable(objToCheck,strToDisable) {
		if (objToCheck.checked) {
			change_disabled(strToDisable,'enable')
		}
		else {
			change_disabled(strToDisable,'disable')
		}
	}

	
	
	/* =============================================================================
		Function:	clearForm(formElement)
		Purpose:	Clears all the form element types
		Parameters:	formElement - the form object
		Returns:	-
		Calling:	-
		============================================================================= */
	function clearForm(){
		var formElement;
		for (var i=0;i<arguments.length;i++) {

			//check if is string or object
			if (document.getElementById(arguments[i])){
				formElement = document.getElementById(arguments[i]);
			}else {
				formElement = arguments[i];
			}

			switch(formElement.type)
			{
				case 'undefined': return;
				case 'radio': formElement.checked = false; break;
				case 'checkbox': formElement.checked = false; break;
				case 'select-one': formElement.selectedIndex = 0; break;
		
				case 'select-multiple':
					for(var x=0; x < formElement.length; x++) 
						formElement[x].selected = false;
					break;
		
				default: formElement.value = "";
			}

		}
	}


	function numonly(daObject){
	daObject.value=daObject.value.replace(/[^0-9]/g, '')
	}

	function dateonly(daObject){
	daObject.value=daObject.value.replace(/[^0-9/]/g, '')
	}




/* =============================================================================
	Function:	show(strId)
	Purpose:	Displays the div as passed.
	Parameters:	-
	Returns:	-
	Calling:	
	============================================================================= */
	function show() {
		var strId;
		for (var i=0;i<arguments.length;i++) {
			strId = arguments[i];
			if (document.getElementById(strId)){
				document.getElementById(strId).style.display = 'block';
			}else if (strId.style.display = 'none'){
				strId.style.display = 'block';
			}
			
		}
	}





	/* =============================================================================
	Function:	hide(strId)
	Purpose:	Displays the div as passed.
	Parameters:	-
	Returns:	-
	Calling:	
	============================================================================= */
	function hide(strId) {
		var strId;
		for (var i=0;i<arguments.length;i++) {
			strId = arguments[i];
			if (document.getElementById(strId)){
				document.getElementById(strId).style.display = 'none';
			}else if (strId.style.display = 'block'){
				strId.style.display = 'none';
			}
		}
	}



	/* =============================================================================
	Function:	emailCheck
	Purpose:	Checks whether the form email address submitted is valid or not.
	Parameters:	emailStr - the value of the email field.
	Returns:	Boolean. True if email address is value, false if invalid.
	Calling:	Usually used in the form validate function: 
			<form method="post" action="destination.asp" onsubmit="return validate(this)">
			* Validate function code
			function validate(form) {
				if (!emailCheck(form.email.value)){
  					form.email.focus();
  					return false;
  				}
			}
	Notes:		Taken from www.goonline.com
	============================================================================= */
	//function emailCheck(emailStr) {
	//	re = new RegExp("^([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*\@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*)$");
	//	if (!re.exec(emailStr)) {
	//		alert('Your email address is invalid. It must be in the format user@server.domain');
	//		return false;
	//	}
	//	return true;
	//}
	function emailCheck(emailStr) {
		var checkTLD=1;

		//Not currently using the known domain names as it is too hard to accurately maintain
		//Update the list of known domain names here
		//var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|info|name|pro|museum|coop|aero)$/;

		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			alert("Your email address seems incorrect (please check for an @ symbol, and full stops.)\n\nPlease check and try again.");
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				alert("The username in the Email address contains invalid characters.\n\nPlease check and try again.");
				return false;
			}
		}
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				alert("The domain name in the Email address contains invalid characters.\n\nPlease check and try again.");
				return false;
			}
		}
		if (user.match(userPat)==null) {
			alert("The username in your Email address doesn't seem to be valid.\n\nPlease check and try again.");
			return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					alert("The destination IP address in your Email address is invalid.\n\nPlease check and try again.");
					return false;
				}
			}
			return true;
		}
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				alert("The domain name in your Email address does not seem to be valid.\n\nPlease check and try again.");
				return false;
			}
		}
		//if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		if (checkTLD && domArr[domArr.length-1].length<2) {
			//alert("Your Email address must end in a well-known domain or two letter " + "country code.\n\nPlease check and try again.");
			alert("Your Email address must end in a domain or two letter country code.\n\nPlease check and try again.");
			return false;
		}
		if (len<2) {
			alert("Your Email address is missing either a hostname, domain or country code!\n\nPlease check and try again.");
			return false;
		}
		return true;
	}





	/* =============================================================================
	Function:	formAction
	Purpose:	Posts form to the specified script/page.  Used when forms span over more than one page.
	Parameters:	form [req] - the current form
            		displayPage [req] - the destionatio script/page
        Returns:	
        Calling:	* To use the formAction function, the action value in the form tag may or may not have a value.	
        		<form method="post" action="" onsubmit="return validate(this)"> OR
			<form method="post" action="destination.asp" onsubmit="return validate(this)">
			* To call the formAction function you must assign an onclick event to the submit button.
			onclick="formAction(this.form, 'alt_destination.asp')"

        Notes:		
	============================================================================= */
	function formAction(form, displayPage) {
	   form.action = displayPage;
	}


	
	

	/* =============================================================================
	Function:	maintainCheck
	Purpose:	Maintains only one checkbox is selected from a group of checkboxes.
	Parameters:	* checkbox number [req] - starts with 1
	Returns:	Boolean. True if an radio button has been selected, False if no radio button has been selected.
	Calling:	* You call the maintainCheck function using the onClick event when adding checkboxes to the form:
  				<input type="checkbox" name="checkBox1" value="1" onClick="maintainCheck(1)">
  				<input type="checkbox" name="checkBox2" value="1" onClick="maintainCheck(2)">
  				<input type="checkbox" name="checkBox3" value="1" onClick="maintainCheck(3)">
				* You must declare an array with all the CheckBox names to be included in the group
  				var checkBoxArray = new Array (checkBox1, checkBox2, checkBox3);
	Notes:		
	============================================================================= */
	function maintainCheck(checkThis){
		for (var i=1; i<=checkBoxArray.length; i++) { // uncheck all boxes that are checked
			if(eval("document.form.checkBox" + i + ".checked")){
				eval("document.form.checkBox" + i + ".checked = false");
			}
		}
		eval("document.form.checkBox" + checkThis + ".checked = true"); // check the check box that was selected
	}





	/* =============================================================================
	Function:	openWindow
	Purpose:	Opens a pop-up window containing specified page.
	Parameters:	* page [req] - consists of the path (if not in same dir. as calling page) and file name of the page to display.
			* windowname [req] - the name of the window, must be unique to open a new window.
			* w - window width
			* h - window height
			* t - window position from top
			* l - window position from let
	Returns:	-
	Calling:	To call the function openWindow to display the page "info/career_obj.htm" in a window called "popup" use the following:
			<a href="javascript:openWindow('info/career_obj.htm','popup', 600, 300, 100, 250);">IMAGE OR LINK TEXT GOES IN HERE</a>
	Notes:		
	============================================================================= */
	function openWindow(page, windowname, w, h, t, l) {
		var propStr = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=" + w + ",height=" + h + ",top=" + t + ",left=" + l;
		popupWin = window.open(page, windowname, propStr)
	}
	
	
	
	
	/* =============================================================================
	Function:	openWindowDetails
	Purpose:	Opens a pop-up window specifically for e-series use. Used for members wanting to change their membership details in iMIS.
	Parameters:	-
	Returns:	-
	Calling:	To call the function openWindowDetails use the following:
				<a href="javascript:openWindowDetails();">IMAGE OR LINK TEXT GOES IN HERE</a>
	============================================================================= */
	function openWindowDetails() {
		popupWin = window.open('https://federal.apesma.asn.au/scriptcontent/index.cfm', 'popup',
		'toolbar,location=,directories,status,scrollbars,resizable,width=650,height=400,top=50,left=120')
	}




	/* =============================================================================
	Function:	openWindowPayment
	Purpose:	Opens a pop-up window specifically for e-series use. Used for members wanting to make sub payments via iMIS
	Parameters:	-
	Returns:	-
	Calling:	To call the function openWindowPayment use the following:
				<a href="javascript:openWindowPayment();">IMAGE OR LINK TEXT GOES IN HERE</a>
	============================================================================= */
	function openWindowPayment() {
		popupWin = window.open('https://federal.apesma.asn.au/source/apesma/duespaid.cfm', 'popup',
		'toolbar=1,location=0,directories=0,menubar=0,status=0,scrollbars=1,resizable=1,width=650,height=400,top=50,left=120')
	}



	/* =============================================================================
	START OF DATE VALIDATION FUNCTIONS
 	IMPORTANT NOTE:	The following functions are for use when validating date variables
					to be in dd/mm/yyyy format.
					* DHTML date validation script.
					Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 
 	Purpose:	Validating date variables to be in dd/mm/yyyy format.
	Calling:	When calling in your validate function first ensure the input field has
				the correct structure by:
				
				if (form.theDate.value.search(/^\d{2}\/\d{2}\/\d{4}$/) == -1) {
					alert ("Please enter a valid date in the format \"dd/mm/yyyy\".");
					form.theDate.focus();
					return false;
				}
				
				If the date has the correct structure then call the functions below by:
				
				var BlnCorrectDate = isDate(form.theDate.value);
				if (BlnCorrectDate != 0) {
					form.theDate.focus();
					return false;
				}

	============================================================================= */
	// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}

	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	
	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   } 
	   return this
	}
	
	function isDate(dtStr){
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strDay=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			alert("The date format should be: dd/mm/yyyy.")
			return 1
		}
		if (strMonth.length<1 || month<1 || month>12){
			alert("Please enter a valid month. The date format must be: dd/mm/yyyy.")
			return 2
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			alert("Please enter a valid day. The date format must be: dd/mm/yyyy.")
			return 1
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
			return 3
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			alert("Please enter a valid date. The date format must be: dd/mm/yyyy.")
			return 1
		}
	return 0
	}
	/* =============================================================================
	END OF DATE VALIDATION FUNCTIONS
	============================================================================= */

// done hiding -->
