﻿initFrm();
function initFrm() {
    try{
        if (document.getElementById("frmValid").value == "N") {
            document.getElementById("btnSubmit").style.visibility = "hidden";
        }
    }catch(e){}
    try{
        if (document.getElementById("popInfoMessage").value == "True"){
            setTimeout("showInfoMessage()",500);
        }
    }catch(e){}
    try{
        if (document.getElementById("DELETEPROCESSCOMPLETE").value == "TRUE"){
            setTimeout("resetFrm();",3000);
        }
    } catch (e) { }
}

function mouseOverWTAlertType(rDiv) {
    document.getElementById("divWTAlert" + rDiv).style.backgroundColor = "cornflowerblue";
    document.getElementById("divWTAlertTypeDescDisp").innerHTML = document.getElementById("divWTAlertTypeDesc" + rDiv).innerHTML;
}

function mouseOutWTAlertType(rDiv) {
    document.getElementById("divWTAlert" + rDiv).style.backgroundColor = "GhostWhite";
    //document.getElementById("divWTAlertTypeDescDisp").innerHTML = "";
}

function showInfoMessage(){
    try{
        try{
            if (document.getElementById("showPWRecovery").value == "True"){
                document.getElementById("divForgotPassword").style.display = "block";
            }
        }catch(ex){}
        //get page size
        var winHeight = document.documentElement.clientHeight;
        if (typeof(window.innerHeight) == 'number'){
            winHeight = window.innerHeight;
        }else if (winHeight == 0){
           winHeight = document.body.clientHeight;
        }
        var winWidth = document.documentElement.clientWidth;
        if (typeof(window.innerWidth) == 'number'){
            winWidth = window.innerWidth;
        }else if (winWidth == 0){
           winWidth = document.body.clientWidth;
        }
        //show background
        document.getElementById("divBackgroundHolder").style.width = winWidth + "px";
        document.getElementById("divBackgroundHolder").style.height = winHeight + "px";
        document.getElementById("divBackgroundHolder").style.position = "absolute";
        document.getElementById("divBackgroundHolder").style.left = "0px";
        document.getElementById("divBackgroundHolder").style.top = "0px";
        document.getElementById("divBackgroundHolder").style.backgroundColor = "black";
        document.getElementById("divBackgroundHolder").style.filter = "alpha(opacity=25)";
        document.getElementById("divBackgroundHolder").style.opacity = 0.25;
        document.getElementById("divBackgroundHolder").style.ifindex = "99";
        document.getElementById("divBackgroundHolder").style.display = "block";
        document.getElementById("divBackgroundHolder").style.ifindex = "100";
        //show matches
        document.getElementById("divInfoMessage").style.position = "absolute";
        document.getElementById("divInfoMessage").style.width = "450px";
        document.getElementById("divInfoMessage").style.height = "200px";
        document.getElementById("divInfoMessage").style.left = (winWidth / 2) - 225 + "px"; //getPositionLeft(document.getElementById("txt" + frmItem));
        document.getElementById("divInfoMessage").style.top = (winHeight / 2) - 100 + "px"; //getPositionTop(document.getElementById("txt" + frmItem)) + 22;
        document.getElementById("divInfoMessage").style.display = "block";
        document.getElementById("divInfoMessage").style.ifindex = "101";
        setTimeout("hideInfoMessage()",20000);
    }catch(e){
        alert(document.getElementById("divInfoMessage").innerHTML);
    }
    
}
var doNotHide = false;
function hideInfoMessage(){
    if (doNotHide == false){
        document.getElementById("divInfoMessage").style.display = "none";
        document.getElementById("divBackgroundHolder").style.display = "none";
    }
}

function resetFrm(){
    document.getElementById("searchContact").value = "RESET";
    try{
        frmSignup.submit();
    }catch(e){
        document.getElementById("frmSignup").submit();
    }
}

function hidePWRecovery(){
    doNotHide = false;
    document.getElementById("divShowPWRecovery").style.display = "none";
    document.getElementById("divBackgroundHolder").style.display = "none";
}

function submitPWRecoveryFrm(){
    CreateXMLHttp();
    if(XMLHttp){
        var requestUrl = "AjaxPublicTools.aspx?FCN=SHOWPWRECOVERY&PID=" + document.getElementById("pageRecID").value + "&txtRecoveryInput=" + document.getElementById("txtRecoveryInput").value;
        doNotHide = true;
        XMLHttp.onreadystatechange = handlePWRecoveryRes;
        XMLHttp.open("GET", requestUrl, true);
        XMLHttp.send(null);	
    }    
}

function showPWRecovery(){
    CreateXMLHttp();
    if(XMLHttp){
        var requestUrl = "AjaxPublicTools.aspx?FCN=SHOWPWRECOVERY&PID=" + document.getElementById("pageRecID").value;
        doNotHide = true;
        XMLHttp.onreadystatechange = handlePWRecoveryRes;
        XMLHttp.open("GET", requestUrl, true);
        XMLHttp.send(null);	
    }    
}

function handlePWRecoveryRes(){
    if(XMLHttp.readyState == 4){
        IsInProg = false;
        if(XMLHttp.status == 200){		
            document.getElementById("divShowPWRecovery").innerHTML = XMLHttp.responseText;
            var winHeight = document.documentElement.clientHeight;
            if (typeof(window.innerHeight) == 'number'){
                winHeight = window.innerHeight;
            }else if (winHeight == 0){
               winHeight = document.body.clientHeight;
            }
            var winWidth = document.documentElement.clientWidth;
            if (typeof(window.innerWidth) == 'number'){
                winWidth = window.innerWidth;
            }else if (winWidth == 0){
               winWidth = document.body.clientWidth;
            }
            //show background
            document.getElementById("divBackgroundHolder").style.width = winWidth + "px";
            document.getElementById("divBackgroundHolder").style.height = winHeight + "px";
            document.getElementById("divBackgroundHolder").style.position = "absolute";
            document.getElementById("divBackgroundHolder").style.left = "0px";
            document.getElementById("divBackgroundHolder").style.top = "0px";
            document.getElementById("divBackgroundHolder").style.backgroundColor = "black";
            document.getElementById("divBackgroundHolder").style.filter = "alpha(opacity=25)";
            document.getElementById("divBackgroundHolder").style.opacity = 0.25;
            document.getElementById("divBackgroundHolder").style.display = "block";
            document.getElementById("divBackgroundHolder").style.ifindex = "100";
            //show matches
            document.getElementById("divShowPWRecovery").style.position = "absolute";
            document.getElementById("divShowPWRecovery").style.width = "450px";
            document.getElementById("divShowPWRecovery").style.height = "200px";
            document.getElementById("divShowPWRecovery").style.left = (winWidth / 2) - 225 + "px"; //getPositionLeft(document.getElementById("txt" + frmItem));
            document.getElementById("divShowPWRecovery").style.top = (winHeight / 2) - 100 + "px"; //getPositionTop(document.getElementById("txt" + frmItem)) + 22;
            document.getElementById("divShowPWRecovery").style.display = "block";
            document.getElementById("divShowPWRecovery").style.ifindex = "101";
            try{
                if (document.getElementById("varAutoSubmit").value == "True"){
                    setTimeout("try{frmSignup.submit()}catch(e){document.getElementById('frmSignup').submit();}",4000);
                }else{
                    setTimeout("focusPWInput()",500);
                }
            }catch(e){}
        }else{
        }
    } 
}

function focusPWInput(){
    document.getElementById("txtRecoveryInput").focus();
}

function checkFrm() {
    var totFlds = document.getElementById("varTotalFields").value;
    var okToSubmit = true;
    var noSubmitMsg = "";
    var errNum = -1;
    for (i=0;i < parseFloat(totFlds);i++){
        if ((document.getElementById("req" + i).value == "Y") && (okToSubmit == true)){
            //window.alert(i + "|" + totFlds + ": " + document.getElementById("fdisptxt" + i).value);
            if (document.getElementById("ftype" + i).value == "SEL"){
                //drop
                if (document.getElementById("drp" + i).options[document.getElementById("drp" + i).selectedIndex].value.length == 0){
                    noSubmitMsg = "Please select an option for " + document.getElementById("fdisptxt" + i).value;
                    okToSubmit = false;
                    errNum = i;
                }
            }else{
                //txt
                if (document.getElementById("txt" + i).value.length == 0){
                    if (document.getElementById("txt" + i).readOnly == false){
                        noSubmitMsg = "Please enter information for " + document.getElementById("fdisptxt" + i).value;
                        okToSubmit = false;
                        errNum = i;
                    }
                }else{
                    if (document.getElementById("fformat" + i).value == "NUM"){
                        if (IsPhone(document.getElementById("txt" + i).value) == false){
                            noSubmitMsg = "Please enter a valid telephone number for " + document.getElementById("fdisptxt" + i).value;
                            okToSubmit = false;
                            errNum = i;
                        }
                    }else if (document.getElementById("fformat" + i).value == "EMAIL"){
                        if (IsEmail(document.getElementById("txt" + i).value) == false){
                            noSubmitMsg = "Please enter a valid email address for " + document.getElementById("fdisptxt" + i).value;
                            okToSubmit = false;
                            errNum = i;
                        }
                    }else{
                        if (document.getElementById("txt" + i).value.length == 0){
                            noSubmitMsg = "Please enter information for " + document.getElementById("fdisptxt" + i).value;
                            okToSubmit = false;
                            errNum = i;
                        }
                    }
                }
            }
        }
    }
    if ((document.getElementById("searchContact").value == "NONE") || (document.getElementById("searchContact").value == "FOUND")){
        if (document.getElementById("MapUpdate").value == "FALSE"){
            var numLst = document.getElementById("varListCount").value;
            var oneList = false;
            if (okToSubmit == true){
                for (p=1;p<=parseFloat(numLst) && oneList == false;p++){
                    if (document.getElementById("chkList" + p).checked == true){
                        oneList = true;
                    }
                }
                if (oneList == false){
                    noSubmitMsg = "Please choose at least one list";
                    okToSubmit = false;
                }
            }         
        }
        
    }          
    if (okToSubmit == true){
        try{
            if (document.getElementById("varSecurityQuestionsCount").value > 0){
                //check questions
                var QCount = document.getElementById("varSecurityQuestionsCount").value;
                for(x=0;x<parseFloat(QCount);x++){
                    if (document.getElementById("PWQuestion" + x).options[document.getElementById("PWQuestion" + x).selectedIndex].value == "ENTERTEXT"){
                        if (document.getElementById("txtPWQuestion" + x).value.length == 0){
                            okToSubmit = false;
                            noSubmitMsg = "Please enter a password recovery question for question number " + (x + 1);
                        }
                    }
                    if (document.getElementById("txtPWAnswer" + x).value.length == 0){
                        okToSubmit = false;
                        noSubmitMsg = "Please complete your password recovery questions.";
                    }
                }
            }
        }catch(e){//security questions null
        }
    }     
    if (okToSubmit == false){
        alert(noSubmitMsg);
        if (errNum > -1){
            if (document.getElementById("ftype" + errNum).value == "SEL"){
                document.getElementById("drp" + errNum).focus();
            }else{
                document.getElementById("txt" + errNum).focus();
            }
        }
    }else{
        try{
            frmSignup.submit();
        }catch(e){
            document.getElementById("frmSignup").submit();
        }
    }
}

function chkCellEmailFormat(vInp){
    var key;
    try{
        if (window.event){
            key = window.event.keyCode;
        }else{
            var e = this;
            //key = event.which;
            key = (window.event) ? event.keyCode : e.keyCode;
        }
    }catch(ex){
        alert(ex.message);
    }
    if ((key != 8)&&(key != 37)&&(key != 38)&&(key != 39)&&(key != 40)) {
        var inpVal = document.getElementById(vInp).value;
        if (inpVal.length > 0){
            var ValidChars="0123456789";
            var newVal="";
            var telNumLen=0;
            if (inpVal.length > 9){
                for (i=0;i<inpVal.length;i++){
                    if ((ValidChars.indexOf(inpVal.charAt(i)) > -1)&&(newVal.length < 10)){
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                }
            }else if(inpVal.length > 6){
                newVal = "";
                for (i=0;i<inpVal.length;i++){
                    if (ValidChars.indexOf(inpVal.charAt(i)) > -1){
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                }
            }else{
                newVal = "";
                for (i=0;i<inpVal.length;i++){
                    if (ValidChars.indexOf(inpVal.charAt(i)) > -1){
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                }
            }
            if (telNumLen==0){newVal=""};
            document.getElementById(vInp).value = newVal;
        }        
    }
}

function chkTelFormat(vInp){
    var key;
    try{
        if (window.event){
            key = window.event.keyCode;
        }else{
            var e = this;
            //key = event.which;
            key = (window.event) ? event.keyCode : e.keyCode;
        }
    }catch(ex){
        alert(ex.message);
    }
    //if ((window.event && window.event.keyCode != 8)&&(window.event && window.event.keyCode != 37)&&(window.event && window.event.keyCode != 38)&&(window.event && window.event.keyCode != 39)&&(window.event && window.event.keyCode != 40)) {
    if ((key!=8)&&(key!=37)&&(key!=38)&&(key!=39)&&(key!=40)){
        var inpVal = document.getElementById(vInp).value;
        if (inpVal.length > 0){
            var ValidChars="0123456789";
            var newVal="(";
            var telNumLen=0;
            if (inpVal.length > 9){
                for (i=0;i<inpVal.length;i++){
                    if (ValidChars.indexOf(inpVal.charAt(i)) > -1){
                        if (newVal.length==14){newVal = newVal + " x";}
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                    if (newVal.length==4){newVal = newVal + ") ";}
                    if (newVal.length==9){newVal = newVal + "-";}
                }
            }else if(inpVal.length > 6){
                newVal = "";
                for (i=0;i<inpVal.length;i++){
                    if (ValidChars.indexOf(inpVal.charAt(i)) > -1){
                        //if (newVal.length==14){newVal = newVal + " x";}
                        if ((newVal.length==3)&&(inpVal.length > 6)){newVal = newVal + "-";}
                        //if (newVal.length==9){newVal = newVal + "-";}
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                }
            }else{
                newVal = "";
                for (i=0;i<inpVal.length;i++){
                    if (ValidChars.indexOf(inpVal.charAt(i)) > -1){
                        newVal = newVal + inpVal.charAt(i);
                        telNumLen+=1;
                    }
                }
            }
            if (telNumLen==0){newVal=""};
            document.getElementById(vInp).value = newVal;
        }        
    }
}


function IsPhone(sText) {
   var ValidChars = "0123456789, #()-x";
   var IsNumber=true;
   var Char;
   for (t = 0; t < sText.length && IsNumber == true; t++) 
      { 
      Char = sText.charAt(t); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsEmail(eText) {
    var emailstring = eText;
    var ampIndex = emailstring.indexOf("@");
    if ((emailstring.length > 0) &&
    (ampIndex > 1)) {
        var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
        var dotIndex = afterAmp.indexOf(".");
        if (dotIndex > 0) {
	        var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	        var beforeAmp = emailstring.substring(0,(ampIndex));
	        var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/
	        if ((emailstring.indexOf("@") != "-1") &&
		        (emailstring.length > 5) &&
		        (dotIndex > 1) &&
		        (afterAmp.length > 0) &&
		        (beforeAmp.length > 1) &&
		        (afterDot.length > 1) &&
		        (email_regex.test(emailstring)) ) {
			        return true;
	        } else {
			        return false;
	        }
        } else {
	        return false;
        }
    } else {
        return false;
    }
}

var XMLHttp;
function CreateXMLHttp(){    
    try {
        XMLHttp = new XMLHttpRequest();
    }catch (e){ 
        try{
            XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e){
            try{ 
                XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e) { 
                return false;
            } 
        }
    }
}

var IsInProg = false;

function getAjaxMatch(frmItem){
    CreateXMLHttp();
    if(XMLHttp){
        if (IsInProg == false){
            if (document.getElementById("ftype" + frmItem).value == "TXT") {
                //if (document.getElementById("txt" + frmItem).value.length > 5){
                    IsInProg = true;
                    var requestUrl = "AjaxPublicTools.aspx?FCN=GETTOPMATCHES&PID=" + document.getElementById("pageRecID").value + "&SEARCHITEM=" + frmItem + "&SEARCHTEXT=" + document.getElementById("txt" + frmItem).value + "&SUID=" + document.getElementById("varPageID").value;
                    document.getElementById("divTopMatches").innerHTML = "<img src='images/status_anim.gif' alt='Please wait...' />&nbsp;Please wait while we find the closest matches to your query...<br /><br />";
                    document.getElementById("divTopMatches").innerHTML += "<input type='button' style='display:none;' id='btnDoRefresh' name='btnDoRefresh' onclick='refreshPage();' value='Taking too long? Try Refreshing' />"
                    document.getElementById("divTopMatches").style.position = "absolute";
                    //get page size
                    var winHeight = document.documentElement.clientHeight;
                    if (typeof(window.innerHeight) == 'number'){
                        winHeight = window.innerHeight;
                    }else if (winHeight == 0){
                       winHeight = document.body.clientHeight;
                    }
                    var winWidth = document.documentElement.clientWidth;
                    if (typeof(window.innerWidth) == 'number'){
                        winWidth = window.innerWidth;
                    }else if (winWidth == 0){
                       winWidth = document.body.clientWidth;
                    }
                    //show background
                    document.getElementById("divBackgroundHolder").style.width = winWidth + "px";
                    document.getElementById("divBackgroundHolder").style.height = winHeight + "px";
                    document.getElementById("divBackgroundHolder").style.position = "absolute";
                    document.getElementById("divBackgroundHolder").style.left = "0px";
                    document.getElementById("divBackgroundHolder").style.top = "0px";
                    document.getElementById("divBackgroundHolder").style.backgroundColor = "black";
                    document.getElementById("divBackgroundHolder").style.filter = "alpha(opacity=25)";
                    document.getElementById("divBackgroundHolder").style.opacity = 0.25;
                    document.getElementById("divBackgroundHolder").style.display = "block";
                    document.getElementById("divBackgroundHolder").style.ifindex = "100";
                    //show matches
                    document.getElementById("divTopMatches").style.width = "450px";
                    document.getElementById("divTopMatches").style.height = "200px";
                    document.getElementById("divTopMatches").style.left = (winWidth / 2) - 225 + "px"; //getPositionLeft(document.getElementById("txt" + frmItem));
                    document.getElementById("divTopMatches").style.top = (winHeight / 2) - 100 + "px"; //getPositionTop(document.getElementById("txt" + frmItem)) + 22;
                    document.getElementById("divTopMatches").style.display = "block";
                    document.getElementById("divTopMatches").style.ifindex = "101";
                    setTimeout("showRefreshButton()",10000);
                    IsDone = false;
                    XMLHttp.onreadystatechange = HandleMatchRes;
                    XMLHttp.open("GET", requestUrl, true);
                    XMLHttp.send(null);	
                //}
            }
        }                
    }
}

function disableEnterKey(e){
    var key;
    if (window.event){
        key = window.event.keyCode;
    }else{
        key = e.which;
    }
    if (key == 13){
        if (document.getElementById("enterAction").value == "CHECKFRM"){
            checkFrm();
        }
        return false;
    }else{
        return true;
    }
}

function PWRecoverEnter(e){
    var key;
    if (window.event){
        key = window.event.keyCode;
    }else{
        key = e.which;
    }
    if (key == 13){
        submitPWRecoveryFrm();
        return false;
    }else{
        return true;
    }
}

function doJSEnter(e,frmItem){
    var key;
    if (window.event){
        key = window.event.keyCode;
    }else{
        key = e.which;
    }
    if (key == 13){
        getAjaxMatch(frmItem);
    }
}

var IsDone = false;

function refreshPage(){
    location.href = "Signup.aspx?SUID=" + document.getElementById("varPageID").value;
}

function showRefreshButton(){
    if (IsDone == false){
        document.getElementById("btnDoRefresh").style.display = "block";
    }
}

function getPositionLeft(This){
    var el = This;var pL = 0;
    while(el){pL+=el.offsetLeft;el=el.offsetParent;}
    return pL
}

function getPositionTop(This){
    var el = This;var pT = 0;
    while(el){pT+=el.offsetTop;el=el.offsetParent;}
    return pT
}

function HandleMatchRes(){
    if(XMLHttp.readyState == 4){
        IsInProg = false;
        if(XMLHttp.status == 200){		
            document.getElementById("divTopMatches").innerHTML = "<div style='width:100%;text-align:right;'><input type='button' id='btnCloseMatches' name='btnCloseMatches' value='Cancel' onclick='closeMatches();' /></div>" + XMLHttp.responseText;
            //document.getElementById("divTopMatches").innerHTML = XMLHttp.responseText;
            IsDone = true;
        }else{
        }
    } 
}

function chooseMatchValue(frmItem,itemValue,itmID){
    try{
        document.getElementById("txt" + frmItem).value = itemValue;
        document.getElementById("divTopMatches").style.display = "none";
        document.getElementById("divBackgroundHolder").style.display = "none";
        var requestUrl = "AjaxPublicTools.aspx?FCN=SETSELITEM&ITEMID=" + itmID;
        XMLHttp.open("GET", requestUrl, true);
        XMLHttp.send(null);	
        checkFrm();
    }catch(e){}
}

function closeMatches(){
    document.getElementById("divTopMatches").style.display = "none";
    document.getElementById("divBackgroundHolder").style.display = "none";
}

function PWQuestionChange(QNum){
    if (document.getElementById("PWQuestion" + QNum).options[document.getElementById("PWQuestion" + QNum).selectedIndex].value == "ENTERTEXT"){
        document.getElementById("PWEnterQuestion" + QNum).style.display = "block";
    }else{
        document.getElementById("PWEnterQuestion" + QNum).style.display = "none";
    }
}

function deleteInfo(){
    if (confirm("You will NOT be notified with any future alerts if you remove your information.  Continue?")){
        document.getElementById("searchContact").value = "DELETE";
        try{
            frmSignup.submit();
        }catch(e){
            document.getElementById("frmSignup").submit();
        }
    }
}
