﻿
    var ScreenNameOk = false;
    var PasswordsOk = false;
    var EmailsOk = false;
    var ZipCodeOk = false;

    function CheckScreenNameAvailable()
    {
        var theScreenNameBox = document.getElementById('hdnScreenName').value;
        document.getElementById("imgScreenNameLoading").style.display = "";
        document.getElementById(theScreenNameBox).disabled = true;        
        
        setTimeout(CheckScreenNameAvailableInternal, 1);
    }

    function CheckScreenNameAvailableInternal()
    {
        try
        {
            if(RPC_Initialized == false || RPC_Busy == true)
                return;
 
            var theScreenNameBox = document.getElementById('hdnScreenName').value;
            var theRequestedName = document.getElementById(theScreenNameBox).value;
            
            if(trim(theRequestedName) == "")
            {
                alert('Please enter a screen name!');
                document.getElementById("imgScreenNameLoading").style.display = "none";
                document.getElementById(theScreenNameBox).disabled = false;                
                ScreenNameOk = false;
                finalBlur()
                return;
            }
            
		    var ret = callRPC('CheckScreenNameAvailability.aspx?RequestedName=' + trim(theRequestedName),'');
            document.getElementById("imgScreenNameLoading").style.display = "none";
            document.getElementById(theScreenNameBox).disabled = false; 		    
			if(ret.error == "")
			{
			    if(ret.Available == "True")
			    {
			        document.getElementById("lblAvailable").style.color = "#e7ff8b";
			        document.getElementById("lblAvailable").innerHTML = "'" + theRequestedName + "' is available.";
			        ScreenNameOk = true;
			        finalBlur();
			    }
			    else
			    {
			        document.getElementById("lblAvailable").style.color = "Red";
			        document.getElementById("lblAvailable").innerHTML = "'" + theRequestedName + "' is not available.<br/>Please select another screen name.";
			        ScreenNameOk = false;
			        finalBlur();
			    }
            }
        }
        catch(ex)
        {
            alert("Error in : CheckScreenNameAvailableInternal() - " + ex.message);
            document.getElementById("imgScreenNameLoading").style.display = "none";
            document.getElementById(theScreenNameBox).disabled = false;  
            ScreenNameOk = false;
            finalBlur()
        }    
    }
    
    
    function CheckPasswordsEqual()
    {
        var thePasswordBox = document.getElementById('hdnPassword').value;
        var theConfirmPasswordBox = document.getElementById('hdnConfirmPassword').value;

        var password = document.getElementById(thePasswordBox).value;
        var confirmPassword = document.getElementById(theConfirmPasswordBox).value;
        
        document.getElementById("lblPasswordNoMatch").style.display = "none";
        PasswordsOk = false;
        if(password != "" && confirmPassword != "")
        {
            if(password != confirmPassword)
            {
                document.getElementById(theConfirmPasswordBox).value = "";
                document.getElementById("lblPasswordNoMatch").style.display = "";
            }
            else
            {
                if(password.length < 6)
                {
                    document.getElementById(theConfirmPasswordBox).value = "";
                    alert('Password is too short.  Must be at least 6 characters.');
                }
                else
                    PasswordsOk = true;
            }
        }
        finalBlur()
    }
    
    function CheckEmailsEqual()
    {
        var theEmailBox = document.getElementById('hdnEmail').value;
        var theConfirmEmailBox = document.getElementById('hdnEmailConfirm').value;

        var email = document.getElementById(theEmailBox).value;
        var confirmEmail = document.getElementById(theConfirmEmailBox).value;
        
        document.getElementById("lblEmailNoMatch").style.display = "none";
        EmailsOk = false;
        if(email != "" && confirmEmail != "")
        {
            if(email != confirmEmail)
            {
                document.getElementById(theConfirmEmailBox).value = "";
                document.getElementById("lblEmailNoMatch").style.display = "";
            }
            else
            {
                if(validateEmail(email) == true)
                    EmailsOk = true;
                else
                    alert('Invalid email address');
            }
        }
        finalBlur()
    }        
    
    function GetCityState()
    {
        document.getElementById("lblCityState").innerText = "";
        
        var theZipCodeBox = document.getElementById('hdnZipCode').value;
        document.getElementById("imgZipCodeLoading").style.display = "";
        document.getElementById(theZipCodeBox).disabled = true;        
        
        setTimeout(GetCityStateInternal, 1);        
    }
    
    function GetCityStateInternal()
    {
        try
        {
            if(RPC_Initialized == false || RPC_Busy == true)
                return;

            var theZipCodeBox = document.getElementById('hdnZipCode').value;
            var theZipCode = document.getElementById(theZipCodeBox).value;
            if(trim(theZipCode) == "")
            {
                alert('Please enter a zip code!');
                document.getElementById("imgZipCodeLoading").style.display = "none";
                document.getElementById(theZipCodeBox).disabled = false;   
                ZipCodeOk = false;
                finalBlur();             
                return;
            }

            var ret = callRPC('GetCityState.aspx?RequestedZip=' + trim(theZipCode), '');
            document.getElementById("imgZipCodeLoading").style.display = "none";
            document.getElementById(theZipCodeBox).disabled = false; 		    
			if(ret.error == "")
			{
			    document.getElementById("lblCityState").innerHTML = ret.CityState;
			    ZipCodeOk = true;
			    finalBlur();
            }
            else
            {
			    document.getElementById("lblCityState").innerHTML = ret.error;
			    ZipCodeOk = false;
			    finalBlur();
            }
        }
        catch(ex)
        {
            alert("Error in : GetCityStateInternal() - " + ex.message);
            document.getElementById("imgZipCodeLoading").style.display = "none";
            document.getElementById(theZipCodeBox).disabled = false; 
            ZipCodeOk = false; 
            finalBlur()
        }            
    }
        
    function finalBlur()
    {
        var theEmailBox = document.getElementById('hdnEmail').value;
        var theEmail = document.getElementById(theEmailBox).value;  
                
        if(trim(theEmail).length > 0 && validateEmail(trim(theEmail)) == true && ScreenNameOk && PasswordsOk && ZipCodeOk)   
        {
            document.getElementById("rightDisabledArrow").style.display="none";
            document.getElementById("rightArrow").style.display="";
        }
        else
        {
            document.getElementById("rightDisabledArrow").style.display="";
            document.getElementById("rightArrow").style.display="none";
        }
    }
    
    function validateEmail(email)
    {
       var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
       if(reg.test(email) == false)
       {
          return false;
       }
       else
        return true;
    }    
    
    function ScreenInit()
    {
        try
        {       
            var theZipCodeBox = document.getElementById('hdnZipCode').value;
            var theZipCode = document.getElementById(theZipCodeBox).value;   
            var theScreenNameBox = document.getElementById('hdnScreenName').value;
            var theRequestedName = document.getElementById(theScreenNameBox).value;
            
            if(theRequestedName.length > 0)
                CheckScreenNameAvailable();
                
            if(theZipCode.length > 0)    
                GetCityState();
        }
        catch(ex) {}
    }