﻿// JScript File

//LogIn
function fLogin()
{
    if(fValidateLogin()== true)
    {                 
        fCallServerCode(fGetSignInInfo(), 'LOGIN_LB_UPD');
        flb_Hide(true);  
    }      
}

function fLoginServerData(arg, context)
{
    if(arg != null)
    {      
        if(arg == "NotVerified")
        {
            alert("Account Not Verified");
        }
        else if(arg == "LoginFailed")
        {
            alert('Invalid Email or Password');
        }
        else
        {        

            var argArray =arg.split("|");
            
            document.getElementById('ctl00_lblUser').innerHTML = "Welcome: " +  argArray[1];                   
            document.getElementById('ctl00_signedInDiv').style.display='block';
            document.getElementById('ctl00_signInDiv').style.display='none';
            
            document.getElementById('ctl00_imgAvatar').src = argArray[3];
            document.getElementById('ctl00_imgAvatar').style.display='block';
           
            RemovePchUserInfoCookie();     
            
            if(gotoPage != "")
            {
                document.location = gotoPage;
                gotoPage="";
            }   
        }
    }
    
    //store the Email in the hidden field on succesful login.
    document.getElementById('ctl00_hidEmail').value = document.getElementById('txtEmail').value;
    
    //Clear Form Data
    document.getElementById('txtEmail').value = "";
    document.getElementById('txtPassword').value = "";
}

function fGetSignInInfo()
{
    var VUserName = "";
    var VPassword = "";
    var VRememberme ="";
    
    if(document.getElementById('txtEmail') != null)
        VUserName = document.getElementById('txtEmail').value;
    if(document.getElementById('txtPassword') != null)
        VPassword = document.getElementById('txtPassword').value;
    if(document.getElementById('chkRememberMe') != null)
        VRememberMe = document.getElementById('chkRememberMe').checked;        

        strLoginData = "UserName~" + VUserName + "^" + 
                 "Password~" + VPassword+ "^" + 
                 "RememberMe~"+ VRememberMe;
                 
       return strLoginData;              
}


//Registration
function fRegistration()
{
    if(fGetRegInfo() != "")
    {
        fCallServerCode(fGetRegInfo(), 'REG_POPUP');
    }
}

function fRegServerData(arg, context)
{    
   if(arg != null)
    {      
        var argArray =arg.split("|");

        if(argArray[0] == "RegistrationSuccessful")
        {        
            document.getElementById("lblRegThankYou").innerHTML=argArray[1];
            window.location.reload();
            //fLaunchRegThanks();       
            //document.location.href = document.location.href;   
        }
        else if(argArray[0] == "CustomerAlreadyExists")
        {
            alert('This ID already exits. Click on Forgot password or Register with a new email address.');
        }
        else if(argArray[0] == "DOBCheckFalse")
        {
            window.location='http://pch.com/sorryPCHLotto';
        }
        else
        {
            alert('Could Not Register');
        }
    }
}

//function fnPageValidated()
//{   
//    var varValid = Page_ClientValidate();      
//    if(varValid)
//    {
//      if(document.getElementById(hidEmail).value != document.getElementById(Email).value)
//       {           
//            fCallServerCode(fGetRegInfo(), 'REG_LB_UPD');                     
//       }       
//       fUpdateInfo();
//    }  
//}

function fGetRegInfo()
{  
    var VTitle = "";
    var VFName = "";
    var VLName = "";
    var VAddress1 = "";
    var VAddress2 = "";
    var VCity = "";
    var VState = "";
    var VZip = "";
    var VEmail = "";
    var VConfirmEmail = "";
    var VddlDate = "";
    var VddlMonth = "";    
    var VddlYear = "";
    var VPassword = "";
    var VConfirmPassword = "";    
    var VhidRemCookie = "";
    var VhidRemCookieShown = "";
    var VhidPchHouse = "";
    var VAccSourceId ="";
    var VTokenId ="";
    var strRegData = "";
    
    if(document.getElementById('ddlTitle') != null)
        VTitle = document.getElementById('ddlTitle').options[document.getElementById('ddlTitle').selectedIndex].text;
    if(document.getElementById('fNameWrap') != null)
        VFName = document.getElementById('fNameWrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('lNameWrap') != null)
        VLName = document.getElementById('lNameWrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('a1Wrap') != null)
       VAddress1 = document.getElementById('a1Wrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('a2Wrap') != null)
       VAddress2 = document.getElementById('a2Wrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('cityWrap') != null)
        VCity = document.getElementById('cityWrap').getElementsByTagName('input').item(0).value;     
    if(document.getElementById('ctl00_ddlState') != null)
        VState = document.getElementById('ctl00_ddlState').options[document.getElementById('ctl00_ddlState').selectedIndex].value;
    if(document.getElementById('zipWrap') !=null)
        VZip = document.getElementById('zipWrap').getElementsByTagName('input').item(0).value;  
    if(document.getElementById('emailWrap') != null)
        VEmail = document.getElementById('emailWrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('confirmWrap') != null)
        VConfirmEmail = document.getElementById('confirmWrap').getElementsByTagName('input').item(0).value;
    if(document.getElementById('ddlDay') != null)
        VddlDate = document.getElementById('ddlDay').options[document.getElementById('ddlDay').selectedIndex].text;
     if(document.getElementById('ddlMonth') != null)
        VddlMonth = document.getElementById('ddlMonth').options[document.getElementById('ddlMonth').selectedIndex].text;
    if(document.getElementById('ddlYear') != null)
       VddlYear = document.getElementById('ddlYear').options[document.getElementById('ddlYear').selectedIndex].text;
    if(document.getElementById('password') != null)
       VPassword = document.getElementById('password').getElementsByTagName('input').item(0).value;  
    if(document.getElementById('confirmPassword') != null)
       VConfirmPassword = document.getElementById('confirmPassword').getElementsByTagName('input').item(0).value;    
        
    if( ValidateRegistration(VTitle, VFName, VLName, VAddress1, VAddress2, VCity, VState, VZip,
                                 VddlDate, VddlMonth, VddlYear, VEmail, VConfirmEmail, 
                                 VPassword, VConfirmPassword) == false)
    {
        return "";
    }
     
    strRegData = "Title~" + VTitle + "^" + 
                 "FName~" + VFName + "^" +   
                 "LName~" + VLName + "^" +   
                 "Address1~" + VAddress1 + "^" +   
                 "Address2~" + VAddress2 + "^" +   
                 "City~" + VCity + "^" +   
                 "State~" + VState + "^" +   
                 "Zip~" + VZip + "^" +   
                 "ddlDate~" + VddlDate + "^" +   
                 "ddlMonth~" + VddlMonth + "^" +   
                 "ddlYear~" + VddlYear + "^" +   
                 "Email~" + VEmail + "^" +
                 "Password~" + VPassword + "^" +
                 "ConfirmPassword~" + VConfirmPassword + "^" +
                 "hidRemCookie~" + VhidRemCookie + "^" +   
                 "hidRemCookieShown~" + VhidRemCookieShown + "^" + 
                 "hidPchHouse~" + VhidPchHouse + "^" + 
                 "AccSourceId~" + VAccSourceId + "^" +
                 "TokenId~" + VTokenId ;                
           
                 
   return strRegData;   

}


// Reset Password
function fResetPassword()
{
    fCallServerCode(fGetResetPasswordInfo(), 'PASSWORD_RESET');
}

function fResetPasswordServer(arg, context)
{

}

function fGetResetPasswordInfo()
{
    var strUserCode = GetQuery('UserCode');    
    var strNewPasword = document.getElementById('');
    var strConfirmPasword = document.getElementById('');
    
    strResetPwdData = "UserCode~" + strUserCode + "^" + 
                 "NewPassword~" + strNewPasword + "^" + 
                 "ConfirmPwd~"+ strConfirmPasword;
                 
    return strResetPwdData;
}

//End: Reset Password 


//Handling Server Callbacks
function fHandleServerData(arg, context)
{
        if(arg != null)
        {   
            switch(context)
            {
                case 'PASSWORD_RESET':
                
                    fResetPasswordServer(arg, context);
                
                break;               
                
            }
            
        }
}

//Utilities

//Function to get Query string
function GetQuery(query) 
{ 
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
return val;
}
}
} 

//End: Utilities