 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//var sessionValue = 0; // No Session
$(document).ready(function(){
var sessionActive ;
sessionActive = 1;
$("#myAccount").html('<img src="images/myaccount_login.gif" alt="Ace2three_Login" width="92" height="20" />');
               // sessionValue = 0; // No Session
                $("#Forgot").show();
                $("#logoutBtnHtml").hide();
                
$("#passwordReset").click(function(){
            var password = $("#password").val();
            var confirmpassword = $("#confirmpassword").val();
            var userid = $("#userid").val();
            if(password!="" && confirmpassword.length>=6){
                if(password == userid){
                    alert('Password cannot be same as Nickname');
                    return false;
                }else if(password !=confirmpassword){
                    alert('Passwords do not match');
                    return false;
                 }
                else{
                    document.form1.action="changePassword.jsp";
                    document.form1.submit();
                     //window.location.href= 'changePassword.jsp';
                }

            }else if (confirmpassword <=6){
                alert('Password should have at least 6 characters');
                return false;
            }else{
                 document.form1.action="changePassword.jsp";
                 document.form1.submit();
            }
});

$("#myAccount").click(function(){
     /*   $(".date").hide();
        if($("#remember").is(':checked')){
            //alert("hello");
        }
        else{
            $("#userid_warn,#pwd_warn").hide();
            $("#userid,#pwd").val("");
            $("#userid_warn,#pwd_warn").text("");
        }*/
    $.post("/ValidateSession.servlet",function(xml){
    //   $.post("ValidateSession",function(xml){
          //  var s = data.toString().trim();
           var s =  $("valid",xml).text();
            if(s=='New'){
          //  if(sessionValue==0){
               document.getElementById('light').style.display='block';
               document.getElementById('fade').style.display='block';
            }
            else {
                document.getElementById('light').style.display='none';
                document.getElementById('fade').style.display='none';
                window.location.href="redirectPage.jsp";
            }

        });
});

$("#Forgot,#submit_fp_reset").click(function(){

window.location.href='forgotPassword.jsp';
});
$("#closeMark").click(function()
{
    $(".date").show();

    document.getElementById('light').style.display='none';
    document.getElementById('fade').style.display='none';
    document.getElementById('test').style.display='none';
});
$("#closeMark2").click(function(){
    $(".date").show();
    document.getElementById('light_forgot').style.display='none';
    document.getElementById('fade_forgot').style.display='none';
    document.getElementById('test').style.display='none';
});
$("#ForgotInLogin").click(function()
{
//    document.getElementById('light').style.display='none';
//    document.getElementById('fade').style.display='none';
//    document.getElementById('light_forgot').style.display='block';
//    document.getElementById('fade_forgot').style.display='block';
//    document.getElementById('test').style.display='none';
    window.location.href='forgotPassword.jsp';
});


$("#HomeLink").click(function(){

   $.post("/ValidateSession.servlet",function(xml){
   //      $.post("ValidateSession",function(xml){
           // var s = data.toString().trim();
           var s =  $("valid",xml).text();
            if(s=='New'){
           // if(sessionValue==0){
               window.location.href="index.html";
            }
            else {
               window.location.href="redirectPage.jsp";
            }

        });
});

    $("#submit_fp").click(function()
    {

        var userid = $("#useridfp").val();
        var emailid  = $("#emailidfp").val();
        if(userid =="" && emailid ==""){
            $("#pwd_warnFP").text("Please enter Nick Name / Email.");
        }
        else if(userid!=""){
             if(userid.length>=4 ){
                $.post("/ForgotPassword.servlet", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml){
                 //$.post("ForgotPassword", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml)  {
                    var res =  $("status",xml).text();
                    $("#pwd_warnFP").text("");
                    $("#pwd_warnFP").show();
                    if(res.indexOf("MailSent")!=-1){
                        var mailid = res.split(":");
                        var replaceStr = mailid[1].split("@");
                        var newStr = replaceStr[0];
                        var uMailid = newStr.replace(newStr.substr(2, newStr.length) ,"****");
                        uMailid = uMailid+replaceStr[1];
                        $("#fpDts").empty("");
                        $("#fpDts").removeClass("frgt_main");

                        if(userid!=null)
                            $("#forgotMsg").text("An email with a link to reset your password has been sent to  "+uMailid+". Please click on the link to reset your password.");
                        else
                            $("#forgotMsg").text("An email with your Nick Name and a link to reset your password has been sent to "+uMailid+". Please click on the link to reset your password.");
                    }
                    else if(res.indexOf("Sorry")!=-1) {
                       $("#fpDts").empty("");
                        $("#fpDts").removeClass("frgt_main");
                       $("#forgotMsg").replaceWith("Sorry, we are unable to process your request. Please try again in a few minutes. ");
                    }
                    else{
                        $("#pwd_warnFP").text(res);
                    }
                });
             }else if(userid.length<4){
             $("#pwd_warnFP").text("Please enter valid Nick Name.");
             }
        }
        else if(emailid!=""){
                if(isValidEmailAddress(emailid)){
                    $.post("/ForgotPassword.servlet", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml)
                // $.post("ForgotPassword", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml)
                {
                    var res =  $("status",xml).text();
                    $("#pwd_warnFP").text("");
                    $("#pwd_warnFP").show();
                    if(res.indexOf("MailSent")!=-1){
                        var mailid = res.split(":");
                        var replaceStr = mailid[1].split("@");
                        var newStr = replaceStr[0];
                        var uMailid = newStr.replace(newStr.substr(2, newStr.length) ,"****");
                        uMailid = uMailid+replaceStr[1];
                        $(".frgt_main").empty("");
                        $("#fpDts").removeClass("frgt_main");
                        if(userid!=null)
                            $("#forgotMsg").text("An email with a link to reset your password has been sent to  "+uMailid);
                        else
                            $("#forgotMsg").text("An email with your Nick Name and a link to reset your password has been sent to "+uMailid);
                    }
                    else if(res.indexOf("Sorry")!=-1) {
                       $(".frgt_main").empty("");
                       $("#fpDts").removeClass("frgt_main");
                       $("#forgotMsg").replaceWith("Sorry, we are unable to process your request. Please try again in a few minutes. ");
                    }
                    else{
                        $("#pwd_warnFP").text(res);
                    }
                });
                }
                else{
                     $("#pwd_warnFP").text("Please enter valid Email Id.");
                }

        }

       /* if(userid.length>=4 || isValidEmailAddress(emailid))
        {

          $.post("/ForgotPassword.servlet", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml)
          //$.post("ForgotPassword", {forgotPassworduserid:userid,forgotPasswordemailid:emailid}, function(xml)
            {
                var res =  $("status",xml).text();
                $("#pwd_warnFP").text("");
                $("#userid_warnFP").text("");
                $("#pwd_warnFP").show();
                if(res.indexOf("Your password has been sent.")!=-1)
                    $("#pwd_warnFP").html('<span class="font10_grn">'+res+'</span>');
                else
                    $("#pwd_warnFP").text(res);
             });
        }
        else
        {
            if(userid==""){
                $("#userid_warnFP").show();
                $("#userid_warnFP").text("Please enter Nick Name.");
            }

            else
            {
                if(userid.length<4){
                    $("#userid_warnFP").show();
                    $("#userid_warnFP").text("Please enter valid Nick Name.");
                }

                else{
                    $("#userid_warnFP").hide();
                    $("#userid_warnFP").text("");
                }

            }
            if(emailid==""){
                $("#pwd_warnFP").show();
                $("#pwd_warnFP").text("Please enter Email.");
            }

            else
            {
                if(!isValidEmailAddress(emailid)){
                    $("#pwd_warnFP").show();
                    $("#pwd_warnFP").text("Please enter valid Email.");
                }
                else{
                    $("#pwd_warnFP").hide();
                    $("#pwd_warnFP").text("");
                }

            }
            } */
        return false;
    });

/*
                $("#submit_fp").click(function()
                {
                    var userid = $("#useridfp").val();
                    var emailid  = $("#emailidfp").val();
                    //Clear Prev Error Messages
                    $("#pwd_warnFP").text("");
                    $("#userid_warnFP").text("");
                    $("#ForgotPassword").validate
                    ({
                        onfocusout: false, // Disabiling onfocus validation
                        onkeyup: false,   // Disabiling onkeyup validation
                        rules:
                        {
                            forgotPassworduserid: {
                            required: true,
                            minlength: 4,
                            maxlength: 15
                            },
                            forgotPasswordemailid:
                            {
                            required: true,
                            email:true
                            }
                        } , //Rules
                        errorElement: "div",
                       // errorContainer: "#userid_warnFP, #pwd_warnFP",
                          //  errorLabelContainer: "#userid_warnFP",
                         errorPlacement: function(error, element) {
                        offset = element.offset();
                        error.insertAfter(element)
                        error.addClass('loginAlertMsg');  // add a class to the wrapper
                  //      error.addClass("forgtPw");
                       // error.css('left', offset.left + element.outerWidth());
                        //error.css('top', offset.top);

                    },
                             submitHandler: function(form)
                             {//Form Submit
                                 var res;
                                 $.post('forgotPasswordUpdate.jsp', $("#ForgotPassword").serialize(), function(data)
                                 {
                                          res= data.trim().toString() ;
                                        //  alert(res);
                                                 $("#pwd_warnFP").text("");
                                                 $("#userid_warnFP").text("");
                                                 $("#pwd_warnFP").text(res);
                                    });
                            } //Submit Handler

                        }); //Form Validate

                }); // onsubmit forgot Password
*/
        $("#signin_submit").click(function()
        {
            var user=$("#userid").val();
            var pwd=$("#pwd").val();
        if(user.length>=4 && user.length<=14 && pwd.length>=6 && pwd.length<=20) {
            $("#userid_warn").text("");
            $("#pwd_warn").text("");
            $("#test").show();
            var redirectStatus = $("#referFriendLogin").val();
           $.post("/ValidateUserLoginDts.servlet", {userid:user,password:pwd}, function(xml){
            //   $.post("ValidateUserLoginDts", {userid:user,password:pwd}, function(xml){
                var st =  $("report",xml).text();
                if(st =='OK'||st=='Session'){
                    if(redirectStatus =='referFriend'){
                        document.location.href="referFriend.do";
                    }
                    else if(redirectStatus =='Purchase'){
                        document.location.href="buyRealChips.jsp";
                    }
                    else{
                        var query = window.location.href;
                        if(query.indexOf("?") != -1){
                        vals = query.split('?');
                        bonusCode = vals[1];
                        if(bonusCode != 'undefined' && bonusCode != null && bonusCode.indexOf("=") != -1 ){
                        var bonusChk = bonusCode.split('=');
                        var chkCode = bonusChk[0];
                        if(chkCode == 'bonusCode'){
                        document.location.href ="buyRealChips.jsp?bonusCode="+bonusChk[1];
                        }
                        else{
                             document.location.href ="redirectPage.jsp";
                        }
                        }
                        }
                        else{
                             document.location.href ="redirectPage.jsp";
                        }
                    }
                    return true;
                }
                else{
                    $("#pwd_warn").show();
                    $("#test").hide();
                    $("#pwd_warn").html(st);
                    return false;
                }
            });
            return false;
        }
        else
        {
            if(user=="")
            {
                $("#userid_warn").show();
                $("#userid_warn").text("Please enter Nick Name.");
               // return false;
            }
            if(user!="")
            {
                if(user.length<4)
                {
                    $("#userid_warn").show();
                     $("#userid_warn").text("Please enter valid Nick Name.");
                    // return false;
                }
                else
                {
                    $("#userid_warn").hide();
                    $("#userid_warn").text("");
                }
            }
            if(pwd=="")
            {
                $("#pwd_warn").show();
                $("#pwd_warn").text("Please enter Password.");
               // return false;
            }
            if(pwd!="")
            {
                if(pwd.length<6)
                {
                    $("#pwd_warn").show();
                    $("#pwd_warn").text("Please enter valid Password.");
                  //  return false;
                }
                else
                {
                    $("#pwd_warn").hide();
                     $("#pwd_warn").text("");
                }
            }
            return false;
        }
        return false;
    });


    $("#contactSubmit").click(function()
        {
            var email=$("#Email").val();
            var subject=$("#Subject").val();
            var mess=$("#Msg").val();

        if(isValidEmailAddress(email) && subject!="" && mess!="")
        {
             var dataString = 'email='+ email + '&subject=' + subject + '&message=' + mess;
             $('#contact_form').html('<img  src="images/loader.gif" alt=""/>');
             $.ajax({
                  type: "POST",
                  url: "/ContactUs.servlet",
                  //  url: "ContactUs",
                  data: dataString,
                  success: function() {

                    $('#contact_form').html("<div id='mes'> </div>");
                    $('#mes').addClass("alertMsg_center").html("Your message has been sent to Ace2Three Team.")
                    .hide()
                    .fadeIn(1500, function() {
                      $('#message').append("<img id='checkmark' src='images/check.png' alt=''/>");
                    });
                  }
                  });
        }
        else
        {
            if(!isValidEmailAddress(email))
            {
                $("#emailError").text("Please enter valid Email.");
            }
            else
            {
                 $("#emailError").text("");
            }
            if(subject == "")
            {
                $("#subjectError").text("Please enter Subject.")
            }
            else
            {
                $("#subjectError").text("");
            }
            if(mess == "")
            {
                $("#messageError").text("Please enter Message.");
            }
            else
            {
                $("#messageError").text("");
            }
            return false;
        }

        return false;
        });
        $("#contactReset").click(function()
        {
            $("#emailError,#subjectError,#messageError").text("");
        });
            function isValidEmailAddress(emailAddress)
            {
                var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                return pattern.test(emailAddress);
            }
            function loadCity(){
                $("#loadCity").load("getCityList.jsp");
            }


         });
         function indexSessionChk(){
                // $.post("/ValidateSession.servlet",function(xml){
                //removed sessionCheck for the bug id 1005.
                return true;
               /*   $.post("ValidateSession",function(xml){

            var s =  $("valid",xml).text();
            alert(s);
            if(s!='New'){ // Session Active
                sessionActive = 1;
                $("#myAccount").html('<img src="images/backtomyaccount.png" alt="Ace2three_Login" />');
              //  sessionValue = 1;// Session Exist
                $("#Forgot").hide();
                $("#logoutBtnHtml").show();
                
            }
            else{
                sessionActive = 0;
                $("#myAccount").html('<img src="images/myaccount_login.gif" alt="Ace2three_Login" width="92" height="20" />');
               // sessionValue = 0; // No Session
                $("#Forgot").show();
                $("#logoutBtnHtml").hide();
               
            }
            return false;
        });*/
        }
        function sessionChk(){
            return true;
                 // $.post("/ValidateSession.servlet",function(xml){
                 //removed sessionCheck for the bug id 1005.
                 /*$.post("ValidateSession",function(xml){
            var s =  $("valid",xml).text();
            if(s!='New'){ // Session Active
                sessionActive = 1;
                $("#myAccount").html('<img src="images/backtomyaccount.png" alt="Ace2three_Login" />');
              //  sessionValue = 1;// Session Exist
                $("#Forgot").hide();
                $("#logoutBtnHtml").show();

            }
            else{
                sessionActive = 0;
                $("#myAccount").html('<img src="images/myaccount_login.gif" alt="Ace2three_Login" width="92" height="20" />');
               // sessionValue = 0; // No Session
                $("#Forgot").show();
                $("#logoutBtnHtml").hide();
            }
            getInvalidLinkUserId();
            return false;

        });*/
        }
        function framesessionChk(){
                  $.post("ValidateSession.servlet",function(xml){
                //  $.post("ValidateSession",function(xml){
            var s =  $("valid",xml).text();
            if(s!='New'){ // Session Active
                sessionActive = 1;
                $("#myAccount").html('<img src="images/backtomyaccount.png" alt="Ace2three_Login" />');
              //  sessionValue = 1;// Session Exist
                $("#Forgot").hide();
                $("#logoutBtnHtml").show();

            }
            else{
                sessionActive = 0;
                $("#myAccount").html('<img src="images/myaccount_login.gif" alt="Ace2three_Login" width="92" height="20" />');
               // sessionValue = 0; // No Session
                $("#Forgot").show();
                $("#logoutBtnHtml").hide();
            }
            getInvalidLinkUserId();
            return false;

        });
        }
	function getInvalidLinkUserId(){
              var query = window.location.href;
              if(query.indexOf('?') != -1){
                        vals = query.split('?');
                        userid = vals[1];
                        if(userid != 'undefined' && userid != null && userid.indexOf("=") != -1  ){
                        var chkUid = userid.split("=");
                        var chkId = chkUid[0];
                        if(chkId == 'uid'){
                        document.getElementById("uid").innerHTML = chkUid[1];
                        }
                        }
                  }

        }
        var bonusCode;
           function logoutSession(){
            sessionActive = 0;
        }

        function referAndEarnSession(){
                if(sessionActive==1){
                    window.location.href="referFriend.do";
                }
                else{
                   window.location.href="referFriendPromo.html";
                }
        }
        function referAndEarn(){
            sessionActive = 0;
                if(sessionActive==1){
                    window.location.href="referFriend.do";
                }
                else{
                   window.location.href="referFriendPromo.html";
                }
        }
        function logoutPlayer(){
          //  alert('Logging out the Player');
           sessionActive = 0;
            window.location.href='logout.jsp';
        }
        // Remove alert messages on login close

//        function removeMsgsOnForgotPwd(){
//           ;
//        }



