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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';		
		if (stf_verifyDomainCompliance(val))  errors+='- '+nm+' must contain an e-mail address.\n';		
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
		if (nm=='Zip' && val.length < 5) errors += '- '+nm+' length should be 5 characters.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function ShowNewAccountForm(ddl, acct) {
	var div = document.getElementById("divChurchNames")
	if (div != null) {
		div.innerHTML = "";
	}
	context = "newaccountForm";
	if (document.getElementById(context) == null) {
		return;
	}
	if (ddl.value != "") {
		//alert(ddl.value);
		xmlHttp=stf_GetXmlHttpObject()		
		params = "ChurchCode=" + ddl.value + "&acct=" + acct;
		//alert(params);
		var url = "bio_form.php";
		xmlHttp.open("POST", url, true);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");		
		xmlHttp.onreadystatechange=stf_stateChanged;
		xmlHttp.send(params);
	}
	else {
		document.getElementById(context).innerHTML = "";
	}
		
}

function showOffer(isChecked) {
	var row = document.getElementById("offertoryRow");
	if (row != null) {
		if (isChecked) {
			row.style.display="";
		}
		else {
			row.style.display="none";
		}		
	}
}
function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
	  return false;

   return true;
}
function stf_validateUserName(UserName) {
		xmlHttp=stf_GetXmlHttpObject()		
		params = "UserName=" + UserName;
		//alert(params);
		var url = "checkUserName.php";
		xmlHttp.open("POST", url, true);
		bTestPassword = false;
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");		
		xmlHttp.onreadystatechange=stf_stateChangedUserName;
		xmlHttp.send(params);	
}
bTestPassword = true;
function stf_stateChangedUserName() {
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if (stf_alltrim(xmlHttp.responseText) != "") {
				alert(xmlHttp.responseText );
				bTestPassword = false;
				document.getElementById("txtUserName").focus();
			}
		}
	} 
	catch(err)	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}

function stf_getChurchNameOptions(e,txt) {
	var evt = window.event;
	if (evt == null) {
		evt = e
	}
	if (evt != null && evt.keyCode == 40) {
		ddl = document.getElementById("ddlChurchListing");
		if (ddl != null) {
			ddl.options[0].selected = true;
			ddl.focus();
			return;
		}
		else {
			alert("Type in the church code for the church you wish to add.");
		}
	}
	if (evt != null && evt.keyCode == 13) {
		txt = document.getElementById("txtOrgs") ;
		acctNum = document.getElementById("txtAcctNum");		
		bCont = (acctNum != null && stf_isNumeric(acctNum.value) && acctNum.value > 0); //the acct number is valid;
		if (bCont)
			ShowNewAccountForm(txt, acct);
		else
			return;
			//stf_notSupported();		
	}		
	if (txt.value.length > 1) {
	xmlHttp=stf_GetXmlHttpObject()		
	params = "n=" + txt.value; 
	//alert(params);
	context = "divChurchNames";
	var url = "churchNames.php";
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");		
	xmlHttp.onreadystatechange=stf_stateChanged;
	xmlHttp.send(params);	
	}
	else {
		var div = document.getElementById("divChurchNames");
		if(div != null) {
			div.innerHTML = "";
		}
	}
}
function stf_setChurchCode(code) {
	txt = document.getElementById("txtOrgs") ;
	txt.value = code;
	//ShowNewAccountForm(txt);
}
function stf_highlightChurch(rowNum) {
	var row = document.getElementById("divChurchCode" + rowNum);
	if (row != null) {
		row.className = "divSelected";
	}
}
function stf_ddlShowNewAccountForm(ddl, acct) {
	var evt = window.event;	
	if(evt.keyCode == 13) {
		txt = document.getElementById("txtOrgs") ;
		txt.value = ddl.value;
		ShowNewAccountForm(txt, acct);
	}
}
function stf_validatePassword(pwd) {
	if (bTestPassword) {

		txt = document.getElementById("txtUserName")
		if (txt == null) {
			stf_notSupported();
		}
		if (pwd.length < 8 || pwd.length > 16) {
			alert("Password must be between 8 and 16 characters");
			return;
		}
		if (txt.value == pwd) {		
			alert("Password may not be the same as the username");
			return;
		}
	}
	bTestPassword = true;
}
function stf_validatePassword2(pwd) {
	stf_validatePassword(pwd)
	pwd1 = document.getElementById("txtPassword1");
	if (pwd1.value != pwd) {
		alert("Passwords do not match");
		pwd1.focus();
	}
}
function showUserAccount(acct, code) {
	xmlHttp=stf_GetXmlHttpObject()		
	if (xmlHttp == null) {
		stf_notSupported();
		return;
	}
	context = "bioform";
	if (document.getElementById(context) == null) {
		return;
	}
	var url = "bio_form.php";
	xmlHttp.open("POST", url, true);
	var params = "acct=" + acct + "&ChurchCode=" + code + "&mode=edit";
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");		
		xmlHttp.onreadystatechange=stf_stateChanged;

	xmlHttp.send(params);
}
function stf_checkOnBillingAddress() {
	bChange = false;
	arrTxt = ["txtAddress1", "txtAddress2", "txtCity", "txtState", "txtZip"]; 
	arrOrg = ["orgAddress1", "orgAddress2", "orgCity", "orgState", "orgZip"]; 
	param = "";
	for (var nIndex = 0; nIndex < arrTxt.length; nIndex++) {
		txt = document.getElementById(arrTxt[nIndex]);
		org = document.getElementById(arrOrg[nIndex]);
		param = param + "&" + arrTxt[nIndex] + "=" + txt.value;
		if (txt == null || org == null) {
			stf_notSupported();
			alert(nIndex);
			return false;
		}
		//alert(txt.value)
		//alert(org.value)
		bChange = bChange || (txt.value != org.value)
		//alert(bChange);
	}
	if (bChange) {
		switch (stf_getBrowser()) {
			case BROWSER_TYPE_MSIE:
				screenY = document.body.scrollTop + document.body.clientHeight;				
				offsetY = 300;//((document.body.clientHeight - document.documentElement.clientHeight) / 2) + document.documentElement.scrollTop;
				offsetX = document.body.scrollLeft + document.body.clientWidth;
				offsetTop = document.body.scrollTop;
				width = document.body.clientWidth;
				height = document.body.clientHeight;
			break;
			case BROWSER_TYPE_UNKNOWN:
			case BROWSER_TYPE_FIREFOX:
			case BROWSER_TYPE_CHROME:
			case BROWSER_TYPE_SAFARI:
			case BROWSER_TYPE_OPERA:
			case BROWSER_TYPE_KONQUEROR:
			case BROWSER_TYPE_OMNIWEB:
			default:
				screenY = window.pageYOffset + window.innerHeight;
				offsetY = (window.innerHeight + window.pageYOffset)/ 2 ;
				offsetX = window.pageXOffset;
				width = window.innerWidth;
				height = window.innerHeight;
			break;
		}
		xmlHttp=stf_GetXmlHttpObject()		
		context = "billingAddressEdit";
		
		bk = document.getElementById("divBioPage");
		bk.className = "backgroundDisable";		
		divBackground = document.getElementById("bkBackground");
		divBackground.style.position = "absolute";
		divBackground.style.top =  "0px";
		divBackground.style.left = "0px";
		divBackground.style.width = "100%";		
		divBackground.style.height = screenY + "px";
		//alert(divBackground);
		var url = "billingAddressInfo.php";
		xmlHttp.open("POST", url, true);
		acct = document.getElementById("baseAccountId");		
		//alert(acct);
		var params = "acct=" + acct.value + param;
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");		
		xmlHttp.onreadystatechange=stf_modifyBillingAddress;

		xmlHttp.send(params);
		return false;
	}
	//alert('not ready to move on');
	return true;
}
function stf_add2BillingAddress() {
}
function stf_modifyBillingAddress() {
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if (stf_alltrim(xmlHttp.responseText) != "") {
				msg = document.getElementById("billingAddressEdit");		
				msg.style.display ="";
				msg.innerHTML=xmlHttp.responseText ;
				
				//alert(msg.clientWidth);
				//alert(width);
				divLeft = (width-650) / 2;
				//alert(divLeft);
				msg.style.position = "absolute";
				msg.style.left = divLeft + "px";
				msg.style.top = offsetY + "px";
				
		//alert("style set");
		//return false;
			}
		}
	} 
	catch(err)	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function stf_CloseBillingAddress2() {
	divBackground = document.getElementById("bkBackground");
	divBackground.style.position = "absolute";
	divBackground.style.top = "0px";
	divBackground.style.left = "0px";
	divBackground.style.width = "0px";	
	divBackground.style.height = "0px";
	bk = document.getElementById("divBioPage");
	bk.className = "";
	msg = document.getElementById("billingAddressEdit");
	msg.style.display ="none";
}
function stf_changeDefaultBillingAddress(bDoChange) {
	if (bDoChange) {
		//alert("change");
		id = document.getElementById("hidDefaultBaId").value;
		//alert(id);
		stf_ChangeBillingAddress(id);
		/*stf_notavailable();
		stf_CloseBillingAddress2();*/
	}
	else {
		var frm = document.getElementById('frmEnrollment');
		if (frm == null) {
			alert('form not found');
		}
		else {			
			frm.submit();
		}
	}
}
function stf_showOtherBillingAddressToChange() {	
	var url = "getBillingAddressList.php";
	xmlHttp.open("POST", url, true);
	acct = document.getElementById("baseAccountId");		
	//alert(acct);
	var params = "acct=" + acct.value;
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");		
	xmlHttp.onreadystatechange=stf_stateChangeShowOtherBillingAddress;

	xmlHttp.send(params);	
	//stf_CloseBillingAddress2();
}
function stf_stateChangeShowOtherBillingAddress() {
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if (stf_alltrim(xmlHttp.responseText) != "") {
				msg = document.getElementById("billingAddressEdit");		
				msg.style.display ="";
				msg.innerHTML=xmlHttp.responseText ;
				
				//alert(msg.clientWidth);
				//alert(width);
				divLeft = (width-650) / 2;
				//alert(divLeft);
				msg.style.position = "absolute";
				msg.style.left = divLeft + "px";
				msg.style.top = offsetY + "px";
				
		//alert("style set");
		//return false;
			}
		}
	} 
	catch(err)	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function stf_ChangeOtherAddress() {
	chkBoxs = document.getElementsByName("chkBillingAddresses");
	//alert(chkBoxs.selectedIndex);
	bSelected = false;
	for(nIndex = 0; nIndex < chkBoxs.length; nIndex++) {
		//alert(chkBoxs[nIndex].checked);
		//alert(chkBoxs[nIndex].value);
		if (chkBoxs[nIndex].checked) {
			bSelected = true
			id = chkBoxs[nIndex].value;			//alert(id);
			stf_ChangeBillingAddress(id);
		}
	}
	if (!(bSelected) ) {
		alert("Please select an address to change or press Cancel to return to the address edit page.");
		return;
	}
	//stf_notavailable();
	stf_CloseBillingAddress2();
}
function stf_ChangeBillingAddress(id) {
	//alert(id);
	xmlHttp=stf_GetXmlHttpObject()		
	var url = "changeBillingAddress.php";
	xmlHttp.open("POST", url, true);
	var addr1 = document.getElementById("txtAddress1").value; 
	//alert("addr1" + addr1);
	var addr2 = document.getElementById("txtAddress2").value; 
	//alert("addr2" + addr2);
	var city = document.getElementById("txtCity").value; 
	//alert("city" + city);
	var state = document.getElementById("txtState").value; 
	//alert("state" + state);
	var zip = document.getElementById("txtZip").value; 
	//alert("zip");
	var params = "id=" + id + "&addr1=" + addr1 + "&addr2=" + addr2 + "&city=" + city + "&state=" + state  + "&zip=" + zip;
	//alert(params);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");		
	xmlHttp.onreadystatechange=stf_stateChangeBillingAddressChanged;
	xmlHttp.send(params);	
}
function stf_stateChangeBillingAddressChanged() {
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if (stf_alltrim(xmlHttp.responseText) != "") {
				alert(xmlHttp.responseText);
			}
			else {
				var frm = document.getElementById('frmEnrollment');
				if (frm == null) {
					alert('form not found');
				}
				else {			
					frm.submit();
				}
			}
		}
	} 
	catch(err)	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function stf_AddBillingAddressChangePayments() {
}
function stf_AddBillingAddress() {
}
function stf_MailVerificationPassword(id) {
	xmlHttp=stf_GetXmlHttpObject()		
	var url = "mailVerificationAnswer.php";
	xmlHttp.open("POST", url, true);
	params = "id=" + id
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");		
	xmlHttp.onreadystatechange=stf_stateChangeMailVerificationAnswer;
	xmlHttp.send(params);	
}
function stf_stateChangeMailVerificationAnswer() {
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if (stf_alltrim(xmlHttp.responseText) != "") {
				alert(xmlHttp.responseText);
			}
		}
	} 
	catch(err)	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function stf_showBlock(block)  {
	var section  = document.getElementById("spHead" + block);
	val = section.innerHTML
	regEx = new RegExp(/\+/);
	
	if (val.match(regEx)) {		
		//alert("match");
		section.innerHTML = val.replace(regEx, "-");
		style = ""
	}
	else {
		//alert("no match")
		regEx = new RegExp(/\-/);
		section.innerHTML = val.replace(regEx, "+");
		style = "none"
	}
	switch (block) {		
		case 1:
			document.getElementById("trVerificationQuestion").style.display = style;
			document.getElementById("trVerificationAnswer").style.display = style;
			document.getElementById("trVerificationSpace").style.display = style;
			break;
		case 2:
			document.getElementById("trUserNameComment").style.display = style;
			document.getElementById("trUserName").style.display = style;
			document.getElementById("trUserNameSpace").style.display = style;
			break;
		case 3:		
			document.getElementById("trPasswordComment").style.display = style;
			document.getElementById("trPassword1").style.display = style;
			document.getElementById("trPassword2").style.display = style;
			document.getElementById("trPasswordSpace").style.display = style;
			break;
	}
}
function stf_verifyDomainCompliance(emailAddress) {
	val = emailAddress.toUpperCase();
	if (val == null || val.length == 0)
		return true;
	domains = [".COM", ".NET", ".ORG", ".BIZ", ".INFO", ".TV", ".CC", ".US", ".EDU", ".GOV", ".CO", ".DE"];
	isInvalid = true;
	for (nIndex =  0; nIndex < domains.length; nIndex++) {
		domain = domains[nIndex];
		isInvalid = isInvalid && (val.length - val.indexOf(domain) != domain.length)
	}	
	return isInvalid;
}
function stf_backToNoLogin(){
	frm = document.getElementById('frmBioForm2');
	frm.action = 'newAccountNoLogin.php';
	frm.submit();
}
