// mbapp.js - works with mbappForm.cfm

var displayWindow;
var originalDuesAmt, initialdualDisc, originalPracAAmt, holdforaplus, holdmsg, errormsg, hardmsg, ethicsOK, ethicsclickcount, glchildWindow,curcolhdrdesc,newcountry,displayasconf, isitlic;

var inparray = new Array();

function initializeForm()
{
	holdmsg = document.getElementById('holdmsg').value;
	errormsg = document.getElementById('errormsg').value;


	displayasconf = document.getElementById('displayasconf').value;
	if (displayasconf == 1){
			return;
	}
	licensedval = document.getElementById('licensedval').value;
	chgCountry();
	chgiCountry();
	chglicCountry();
	chgempCountry();
	doLicensed(licensedval);


	// if ethicsOK is false, then submit is false
	ethicsOK =	document.getElementById('idethicsOK').checked;
	ethicsclickcount = 0;

	enableSubmitReset();
	// go to focus field, if set
	var bm = document.getElementById('bm').value;
	if (bm != '') {
		alert(errormsg);
		document.getElementById(bm).focus();
	}
}

function enableSubmitReset() {

	ethicsOK = document.getElementById("idethicsOK").checked;
	// no hold from A+
	// this is checking the yes/no radio button on the ethics statement
	if (ethicsOK) {
		//ethicsOK true
		document.getElementById('idSubmit').disabled=false;
	}
	else {
		//ethicsOK not true
		document.getElementById('idSubmit').disabled=true;
	}
}


function doEthics() {
	// set javascript variable, from the value the user clicked in the form
	ethicsOK = document.getElementById("idethicsOK").checked;
	if (ethicsOK) {
		//ethicsOK true
		ethicsclickcount = ethicsclickcount + 1;
		if (ethicsclickcount > 1) {
				// open a window with the ethics statement
				openChild('../interest/ethics1.cfm','duesUseHelpWindow');
				// change the radio button back to "No"
				// document.getElementById("idethicsNOTOK").checked = 1;
		}
	}
	else {
		//ethicsOK not true   ... change to no-op
		ethicsclickcount = ethicsclickcount + 1;
	}

	enableSubmitReset();
}

function dispHold()
{
	alert(holdmsg);
}


function appendDecimal(thisNumber)
{
	var pos,numberAsString,stringWithDecimal;
	// convert value to a string
  	numberAsString = thisNumber.toString();
  	pos = numberAsString.search(/\./);
  	if(pos == -1) {
		// there is no decimal. Assume this is a whole dollar amount
  	 	stringWithDecimal = numberAsString + ".00";
  	}
  	else {
  		if (pos == numberAsString.length - 2)
  			stringWithDecimal = numberAsString + "0";
  		else
   			stringWithDecimal = numberAsString;
   	}
  	return stringWithDecimal;
}



function isSubmittalApproved()
{
	// this is a sort of fake routine, right now
 	setButtonStatus(false);
 	FormIsValid = true;      // if any further validation is required, replace "true" with the function name
  	if(!FormIsValid) {
  	 	setButtonStatus(true);
   		return false;
    }
  	else {
   		return true;
   	}
}


function viewElements()
{
	if (displayWindow == null || displayWindow.closed)
		displayWindow = window.open('','WDWindow');
	displayWindow.document.write('<html>');
	displayWindow.document.write('<body>');
	viewWindowElements(displayWindow);
}


function viewChildElements()
{
	viewWindowElements(window);
}


function viewWindowElements(displayWindow)
{
	var i, j;

	for (i = 0; i < document.forms.length; i++)
	{
	  for (j = 0; j < document.forms[i].elements.length; j++)
	  {
	   	displayWindow.document.write('document.forms['+i+'].elements['+j+']: '
	   					+ document.forms[i].elements[j].name + ' : '
						+ document.forms[i].elements[j].value);
	  	if (document.forms[i].elements[j].checked)
	  		displayWindow.document.write(' : checked');
	  	displayWindow.document.write('<br>');
	  }
    }
}


function closeChild(xwindow)
{
	if (xwindow != null && !xwindow.closed) xwindow.close();
}


function openChild(file,window)
{
	childWindow = open(file,window,'resizable=yes,width=800,height=400,left=0,top=0,scrollbars=yes');
	childWindow.focus();
    if (childWindow.opener == null) childWindow.opener = self;
    // set the global handle
    glchildWindow = childWindow;
	return childWindow;
}

function openAfflHelp(formcd,ConstitID,bookmark)
{

	var myfile,mywindow;
	// building a url that contains the constitid and name (which might contain an apostrophe)
	//.replace(/''/g,'\|') and .replace(/\|/g,'')
	myfile=formcd + 'help.cfm?ConstitID='+ ConstitID +'&membname=' + document.getElementById('membname').value + '\&membernum=' + document.getElementById('ConstitID').value + '#' + bookmark;

	mywindow='duesUseHelpWindow';
	// childWindow = open(myfile,mywindow,'resizable=yes,width=800,height=400,left=0,top=0,scrollbars=yes');
	childWindow = open(myfile,mywindow,'resizable=yes,left=0,top=0,width=800,height=550,scrollbars=yes');
	childWindow.focus();
    if (childWindow.opener == null) childWindow.opener = self;
    // set the global handle
    glchildWindow = childWindow;
	// return childWindow;
	return;
}

function LicCountryInstruction() {
	alert('For verification purposes, please select only one country/state/province in which you are 	licensed as a psychologist. If you are licensed in multiple countries/states/provinces, you will have the opportunity to provide this information at a later time in the APA Directory Survey');
}

function chgCountry() {
	// executes when a person picks a new country
	var newcountry;

	if (displayasconf == 0) {
		newcountry = document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value;
		// set the value of the select
		document.getElementById('Country').value = newcountry;
	}
	else {
		newcountry = document.getElementById('Country').value;
	}
	if (newcountry.substr(0, 3) == 'USA'){
		// USA
		document.getElementById('state').style.display = '';
		document.getElementById('canProvince').style.display = 'none';
		document.getElementById('province').style.display = 'none';

		document.getElementById('licState').style.display = '';
		document.getElementById('liccanProvince').style.display = 'none';
		document.getElementById('licProvince').style.display = 'none';

	}
	else if (newcountry.substr(0, 3) == 'CAN'){
		// Canada
		document.getElementById('state').style.display = 'none';
		document.getElementById('canProvince').style.display = '';
		document.getElementById('province').style.display = 'none';

		document.getElementById('licState').style.display = 'none';
		document.getElementById('liccanProvince').style.display = '';
		document.getElementById('licProvince').style.display = 'none';
	}
	else {
		// foreign
		document.getElementById('state').style.display = 'none';
		document.getElementById('canProvince').style.display = 'none';
		document.getElementById('province').style.display = '';

		document.getElementById('licState').style.display = 'none';
		document.getElementById('liccanProvince').style.display = 'none';
		document.getElementById('licProvince').style.display = '';
	}
}

function chglicCountry() {
	// executes when a person picks a new iCcountry
	var newcountry;

	if (displayasconf == 0) {
		newcountry = document.getElementById('licCountry').options[document.getElementById('licCountry').selectedIndex].value;
		// set the value of the select
		document.getElementById('licCountry').value = newcountry;
	}
	else {
		newcountry = document.getElementById('licCountry').value;
	}

	if (newcountry.substr(0, 3) == 'USA'){
		// USA
		document.getElementById('licState').style.display = '';
		document.getElementById('liccanProvince').style.display = 'none';
		document.getElementById('licProvince').style.display = 'none';

	}
	else if (newcountry.substr(0, 3) == 'CAN'){
		// Canada
		document.getElementById('licState').style.display = 'none';
		document.getElementById('liccanProvince').style.display = '';
		document.getElementById('licProvince').style.display = 'none';
	}
	else {
		// foreign
		document.getElementById('licState').style.display = 'none';
		document.getElementById('liccanProvince').style.display = 'none';
		document.getElementById('licProvince').style.display = '';
	}
}

function chgempCountry() {
	// executes when a person picks a new iCcountry
	var newcountry;

	if (displayasconf == 0) {
		newcountry = document.getElementById('empCountry').options[document.getElementById('empCountry').selectedIndex].value;
		// set the value of the select
		document.getElementById('empCountry').value = newcountry;
	}
	else {
		newcountry = document.getElementById('empCountry').value;
	}
}

function chgiCountry() {
	// executes when a person picks a new iCcountry
	var newcountry;

	if (displayasconf == 0) {
		newcountry = document.getElementById('iCountry').options[document.getElementById('iCountry').selectedIndex].value;
		// set the value of the select
		document.getElementById('iCountry').value = newcountry;
	}
	else {
		newcountry = document.getElementById('iCountry').value;
	}

	if (newcountry.substr(0, 3) == 'USA'){
		// USA
		document.getElementById('iState').style.display = '';
		document.getElementById('icanProvince').style.display = 'none';
		document.getElementById('iProvince').style.display = 'none';

	}
	else if (newcountry.substr(0, 3) == 'CAN'){
		// Canada
		document.getElementById('iState').style.display = 'none';
		document.getElementById('icanProvince').style.display = '';
		document.getElementById('iProvince').style.display = 'none';
	}
	else {
		// foreign
		document.getElementById('iState').style.display = 'none';
		document.getElementById('icanProvince').style.display = 'none';
		document.getElementById('iProvince').style.display = '';
	}
}

 function ConfigureAddressForCountry(thisCountryCode,idStateCaption,idStateDrop,idProvDrop,idProvText,idZipCaption)
  {
  switch(thisCountryCode)
   {
   case "":
   case "USA":
   case "APO":
    document.getElementById(idStateCaption).firstChild.nodeValue="State:";
    document.getElementById(idZipCaption).firstChild.nodeValue="Zip code:";
    document.getElementById(idStateDrop).style.display="";
    document.getElementById(idProvDrop).style.display="none";
    document.getElementById(idProvText).style.display="none";
    break;
   case "CAN":
    document.getElementById(idStateCaption).firstChild.nodeValue="Province:";
    document.getElementById(idZipCaption).firstChild.nodeValue="Postal code:";
    document.getElementById(idStateDrop).style.display="none";
    document.getElementById(idProvDrop).style.display="";
    document.getElementById(idProvText).style.display="none";
    break;
   default:
    document.getElementById(idStateCaption).firstChild.nodeValue="Province:";
    document.getElementById(idZipCaption).firstChild.nodeValue="Postal code:";
    document.getElementById(idStateDrop).style.display="none";
    document.getElementById(idProvDrop).style.display="none";
    document.getElementById(idProvText).style.display="";
    break;
   }
  }

 function openPubChild(file,window)
{
	var myfile,mywindow,childWindow;

	// note: this is NOT full screen on a 1024 x 768 monitor. Perhaps we could sniff this
	childWindow = open(file,window,'resizable=yes,width=780,height=550,left=0,top=0,scrollbars=yes,menubar=yes,toolbar=yes');
	childWindow.focus();
    if (childWindow.opener == null) childWindow.opener = self;
    // set the global handle
    glchildWindow = childWindow;
    // does NOT return the window handle, so [object] does not appear in the main subscription form
	return;
}

function chkMajor (fieldname) {
    var mymajor = document.getElementById(fieldname).value;
    //alert('chkMajor');
    //alert('fieldname =' + fieldname);
    //alert('mymajor =' + mymajor);
    if (mymajor.substr(0, 2) == 'h1' || mymajor.substr(0, 2) == 'h2') {
    	alert('Please do not select a heading in this list. Headings start with dashes. (---)');
	}
}

function doLicensed(isitlic) {
	if (isitlic == 1) {
		// person is licensed. Disable planning to license, and set value to blank
		document.getElementById('pursuing1').checked = false;
		document.getElementById('pursuing0').checked = false;
		document.getElementById('pursuing1').disabled = true;
		document.getElementById('pursuing0').disabled = true;
		// enable licensure country and state
		document.getElementById('licCountry').disabled = false;
		document.getElementById('licState').disabled = false;
		document.getElementById('licProvince').disabled = false;
		document.getElementById('liccanProvince').disabled = false;
	}
	else {
		// person is NOT licensed. Enable planning to license
		document.getElementById('pursuing0').disabled = false;
		document.getElementById('pursuing1').disabled = false;
		// disable licensure country and state
		document.getElementById('licCountry').disabled = true;
		document.getElementById('licState').disabled = true;
		document.getElementById('licProvince').disabled = true;
		document.getElementById('liccanProvince').disabled = true;


	}
}




