//Initializing regular expressions
var regex = /[0-9._-]*@([.-]+\;<>{}.)$/;
var iChars = "!@#$%^&* ()+=-[]\\\';,./{}|\":<>?";
var iCharsRegisterPhone = "!@#$%^&* ()=-[]\\\';,./{}|\":<>?";

//Initializing the global variable for storing mobile number
var regExFriendInvite=/^([a-zA-Z0-9])$/;

function frmRegisterPhone(){
    if($('#txtPhone').val()==''){
         alert("Please enter your mobile number");
         $('#txtPhone').focus();         
         return false;
     }

    else if($('#txtPhone').val()!=''){
     for (var i = 0; i < $('#txtPhone').length; i++) {
            if (iCharsRegisterPhone.indexOf($('#txtPhone').val().charAt(i)) != -1) {
            alert("Please enter valid mobile number");
            $('#txtPhone').focus();
            $('#txtPhone').val('');
            return false;
            }
        }
    }
}

function validateCode(){
    if($('#txtValidateCode').val()==''){
        alert("Please enter the code");
        return false;
    }
    
    else{
        phoneNumber=$('#hidPhone').val();
        $("#divMessage").html("processing");
        $.ajax({
        type: "GET",
        url: "processCode.php?txtCode="+$('#txtValidateCode').val()+"&txtPhone="+phoneNumber,
        success:validateCodeResponse
    });
    }
}

function validateCodeResponse(data){
    //alert(data);
    if(data=="OK")
    {
        $("#divCode").hide();
        $("#divMessage").html("");
        $('#txtMobileNo').val(phoneNumber);
        $("#divRegistrationForm").show();
    }
    else
        $("#divMessage").html("<center><span class='impMsg'>The code you have entered is invalid!</span></center>");
}

function validateRegistrationForm(){
     var todaysDate = $('#hidDate').val();
     var txtDob = $('#txtDob').val();
     //alert("Todays Date:"+todaysDate);
     //alert("DOB :"+txtDob);

    if($('#hidConformPswd').is(':visible')){      
     if($('#txtPassword').val()==''){
         alert("Please enter the password");
         $('#txtPassword').focus();
         return false;
     }

     if($('#txtConfirmPassword').val()==''){
         alert("Please enter the confirm password");
         $('#txtConfirmPassword').focus();
         return false;
     }
    
     else if($('#txtPassword').val()!=$('#txtConfirmPassword').val()){
         alert("Your confirmation password does not match");
         $('#txtConfirmPassword').val('');
         $('#txtConfirmPassword').focus();
         return false;
     }
     }
     
     if($('#txtFullName').val()==''){
         alert("Please enter your full name");
         $('#txtFullName').focus();
         return false;
     }

     else if($('#txtFullName').val()!=''){
     for (var i = 0; i < $('#txtFullName').length; i++) {
            if (iChars.indexOf($('#txtFullName').val().charAt(i)) != -1) {
            alert("Please enter valid full name");
            $('#txtFullName').focus();
            $('#txtFullName').val('');
            return false;
            }
        }
     }
     
     if (regex.test($('#txtFullName').val())==true){
            $('#txtFullName').val('');
            $('#txtFullName').focus();
            alert("Please enter valid full name");
            return false;
        }

     if($('#txtDob').val()==''){
         alert("Please select your date of birth");
         return false;
     }

     else if(txtDob >= todaysDate){

        alert("Incorrect date of birth");
        $('#txtDob').val('');
        $('#txtDob').focus();
        return false;
    }

     if($("input[@name=rdoGender]:checked").val()==''){
         alert("Please select your gender");
         return false;
     }

     if($('#txtEmail').val()==''){
         alert("Please enter your email address");
         $('#txtEmail').focus();
         return false;
     }

     if (regex.test($('#txtEmail').val())==true){
            $('#txtEmail').val('');
            $('#txtEmail').focus();
            alert("Please enter correct email address");
            return false;
        }

     if($('#txtCity').val()==''){
         alert("Please enter your city");
         $('#txtCity').focus();
         return false;
     }
     
     else if($('#txtCity').val()!=''){
     for (i = 0; i < $('#txtCity').length; i++) {
            if (iChars.indexOf($('#txtCity').val().charAt(i)) != -1) {
            alert("Please enter correct city");
            $('#txtCity').focus();
            $('#txtCity').val('');
            return false;
            }
        }
     }
     else
         return true;
}

function showConfirmPassword(){
    $('#hidConformPswd').show();
}

//form grid with table and apply pagination
function ShowGrid()
{
        if($('.GridTable').length)
        {
                var xCount = $('.GridTable')[0].rows.length;

                if(xCount>=1){
                        $('.GridTable').jTPS( {scrollStep:1, scrollDelay:30} );
                                $('.GridTable tbody tr:not(.stubCell)').bind('mouseover mouseout',
                                        function (e) {e.type == 'mouseover' ? $(this).children('td').addClass('hilightRow') : $(this).children('td').removeClass('hilightRow');}
                        );
           }
           else
           {
                        var firstRow = $('.GridTable')[0].rows[0];
                        var secondRow = $('.GridTable')[0].rows[1];

                        var secondRows = secondRow.cells[0];

                        $(firstRow).hide();
                        $(secondRows).html('<br><center><span class="noRecords">No record found !</span> </center> <br>');

           }
        }
}
//Validating the invite friend activity before processing
function inviteFriendProcess(){
    
    if($('#txtToMobile').val()==''){
         alert("Please enter the mobile number of your friend");
         $('#txtToMobile').focus();
         return false;
     }

     if($('#txtToMobile').val()!=''){
           if((isNaN($('#txtToMobile').val()))){
             alert("Please enter  valid mobile number");
             $('#txtToMobile').val('');
             $('#txtToMobile').focus();
             return false;
         }
    } 
   
    if($('#txtMessage').val()==''){
         alert("Please enter the message");
         $('#txtMessage').focus();
         return false;
     }

    if($('#txtMessage').val()!=''){
          $("#divMessage").show();
          $("#tblInvite").hide();
          $.ajax({
            type: "GET",
            url: "inviteFriendsProcess.php?txtToMobile="+$('#txtToMobile').val()+"&txtMessage="+$('#txtMessage').val(),
            success:validteCodeResponse
        }); 
    } 
}

function validteCodeResponse(data)
{
    alert("Your invitation has been sent.");
    tb_remove();
}

function getCredit(){
    //alert($('#selectCredit option:selected').val());
    var amnt=$('#selectCredit option:selected').val();
    $('#amount').val(amnt);

    $('#notify_url').val(" freemmsindia.com/thankYou.php?userID="+$.cookie('userID')+"&amount="+amnt);
    $('#return_url').val(" freemmsindia.com/paymentReturnProcess.php?userID="+$.cookie('userID')+"&amount="+amnt);
}

function validateNewMobile(){
    
    if($('#txtNewMobile').val()==''){
         alert("Please enter the new mobile number");
         $('#txtNewMobile').focus();
         return false;
     }


    if($('#txtNewMobile').val()!=''){
        if((isNaN($('#txtNewMobile').val()))){
             alert("Please enter valid mobile number");
             $('#txtNewMobile').val('');
             $('#txtNewMobile').focus();             
        }
        else{
            $('#btnChangeMobile').attr("disabled", true);
            $.ajax({
                type: "GET",
                url: "changeMobileProcess.php?txtNewMobile="+$('#txtNewMobile').val(),
                success:validateNewMobileResponse
            });
        }
    }        
}

function validateNewMobileResponse(data){
   if(data=="OK")
    {
        $('#divEnterNewMobile').hide();
        $('#divNewCode').show();
    }
    else{
         $('#btnChangeMobile').attr("disabled", false);
        alert(data);
    }
}

function validateNewCode(){    
    if($('#txtNewCode').val()==''){
         alert("Please enter the code");
         $('#txtNewCode').focus();
         return false;
     }

     if($('#txtNewCode').val()!=''){
           if((isNaN($('#txtNewCode').val()))){
             alert("Please enter valid code");
             $('#txtNewCode').val('');
             $('#txtNewCode').focus();
             return false;
         }
    }
        $('#btnEnterCode').attr("disabled", true);
        $.ajax({
            type: "GET",
            url: "checkChangeMobileProcess.php?txtCode="+$('#txtNewCode').val()+"&txtPhone="+$('#txtNewMobile').val(),
            success:validateNewCodeResponse
        });
 
}

function validateNewCodeResponse(data){
    if(data=="OK")
    {
        $('#divNewCode').show();
        alert("Your new mobile number has been changed.\nIt will reflect from next time you login");
        tb_remove();
        window.location="http://www.freemmsindia.com/logout.php";
    }
    else{
        alert("Invalid code entered");
        $('#txtNewCode').val('');
        $('#btnEnterCode').attr("disabled", false);        
    }
}
