
var oSubscriberData;function InitPageCode()
{var oXmlRequest=new XmlRequest();oXmlRequest.getXml("GetQuestions.jsp",cbQuestionsResponse);}
function cbQuestionsResponse(oXml)
{var oRows=oXml.documentElement.getElementsByTagName("row");var oRow;var oList=document.getElementById("questionList");if(oRows.length!=0)
{ListboxInsertItem(oList,"(No selection)","");for(var i=0;i<oRows.length;i++)
{oRow=oRows[i];ListboxInsertItem(oList,oRow.getAttribute("question"),oRow.getAttribute("id"));}}
var oUserName=document.getElementById("userName");oUserName.focus();}
function Register()
{var oButton=document.getElementById("btn_register");oButton.disabled=true;var sUsername=document.getElementById("userName").value;if(sUsername=="")
{alert("The Email Address is a required field.\nIt is your username for My KC Scout.");document.getElementById("userName").focus();oButton.disabled=false;return;}
var sMsg=ValidEmailAddress(sUsername);if(sMsg!="")
{alert(sMsg);oButton.disabled=false;return;}
var oXmlRequest=new XmlRequest();oXmlRequest.addParameter("email",sUsername);oXmlRequest.getXml("SubCheck.jsp",cbCheckResponse);}
function ValidEmailAddress(sEmailAddr)
{var sDomainChars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";var sUsernameChars=sDomainChars+"_";var sPieces=sEmailAddr.split("@");if(sPieces.length!=2)
return("The email address you entered is not well-formed.");var sUsername=sPieces[0];var sDomain=sPieces[1];if(sUsername.length==0||sDomain.length==0)
return("You appear to be missing part of your email address.");for(var i=0;i<sUsername.length;i++)
{var ch=sUsername.charAt(i);if(sUsernameChars.indexOf(ch)==-1)
{sMsg="Invalid character in the name portion of your email address: "+ch;if(ch==" ")
sMsg+="(space)";return(sMsg);}}
for(var i=0;i<sDomain.length;i++)
{var ch=sDomain.charAt(i);if(sDomainChars.indexOf(ch)==-1)
{sMsg="Invalid character in the domain portion of your email address: "+ch;if(ch==" ")
sMsg+="(space)";return(sMsg);}}
if(sDomain.indexOf(".")==-1)
return("The domain portion of your email address is incorrect.");return("");}
function cbCheckResponse(oXml)
{var oResult=oXml.documentElement;var oRows=oXml.documentElement.getElementsByTagName("row");if(oRows.length==0)
{setTimeout(FinishRegistration,100);}
else
{alert("The email address you have supplied is already registered as a Subscriber.\n"+"If you have forgotten your password, please click on the \"I forgot my password\" link.");var oButton=document.getElementById("btn_register");oButton.disabled=false;}}
function FinishRegistration()
{var oButton=document.getElementById("btn_register");oButton.disabled=true;var sPassword1=document.getElementById("password").value;if(sPassword1=="")
{alert("Please enter your password.");document.getElementById("password").focus();oButton.disabled=false;return;}
var sPassword2=document.getElementById("confirmPassword").value;if(sPassword2=="")
{alert("Please confirm your password by entering it a second time.");document.getElementById("confirmPassword").focus();oButton.disabled=false;return;}
if(sPassword1!=sPassword2)
{alert("The two passwords do not match.  Please enter your password and confirm it.");document.getElementById("password").value="";document.getElementById("confirmPassword").value="";document.getElementById("password").focus();oButton.disabled=false;return;}
var sZipCode=document.getElementById("zipCode").value;if(sZipCode=="")
{alert("Please enter your home ZIP Code.");document.getElementById("zipCode").focus();oButton.disabled=false;return;}
var nQuestion=document.getElementById("questionList").value;if(nQuestion=="")
{alert("Please select a security question.\n"+"The security question will be used in case you forget your password.");oButton.disabled=false;return;}
var sAnswer=document.getElementById("answer").value;if(sAnswer=="")
{alert("Please enter the answer to the security question.");document.getElementById("answer").focus();oButton.disabled=false;return;}
sAnswer=sAnswer.toUpperCase();var nAgreed=document.getElementById("agreed").checked;if(!nAgreed)
{alert("You must agree to the terms of the Service Agreement before you can register.");oButton.disabled=false;return;}
var oXmlRequest=new XmlRequest();oXmlRequest.addParameter("subId",0);oXmlRequest.addParameter("email",document.getElementById("userName").value);oXmlRequest.addParameter("password",sha256_digest(sPassword1));oXmlRequest.addParameter("zip",sZipCode);oXmlRequest.addParameter("question",nQuestion);oXmlRequest.addParameter("answer",sha256_digest(sAnswer));oXmlRequest.addParameter("agreed",1);oXmlRequest.getXml("SubRegistration.jsp",GetResponse);}
function GetResponse(oXml)
{var oResult=oXml.documentElement;var oRows=oXml.documentElement.getElementsByTagName("row");var nRowsAffected=oResult.getAttribute("rowsAffected");oSubscriberData=oRows[0];if(nRowsAffected==0)
{if(window.confirm("Could not create the Subscriber record:\n"+oResult.getAttribute("errMsg")+"\n\nPress OK to send the confirmation email to "+document.getElementById("userName").value+"\nor press Cancel to re-enter your data."))
{setTimeout(SendConfirmationEmail,100);}
else
{return;}}
else if(nRowsAffected==1)
{setTimeout(SendConfirmationEmail,100);}
document.getElementById("btn_register").disabled=false;}
function SendConfirmationEmail()
{var oXmlRequest=new XmlRequest();oXmlRequest.addParameter("subId",oSubscriberData.getAttribute("id"));oXmlRequest.addParameter("email",oSubscriberData.getAttribute("email"));oXmlRequest.getXml("SubSendConfEmail.jsp",cbSendEmailResponse);}
function cbSendEmailResponse(oXml)
{var oResult=oXml.documentElement;if(oResult.getAttribute("errMsg")=="Message sent.")
{document.location="SubRegistrationDone.html";}
else
{alert("There was a problem sending your confirmation email.\n"+
oResult.getAttribute("errMsg"));}}
