﻿$(document).ready(function() {
    // Show rotate					   
    $("#rotate").show();

    /* Attach the events */

    // re-position heroarea nav on window resize events
    $(window).resize(function() {
        //showHeroAreaNav();
    });

    // Cancel click event
    $("#btnCancelSignUp").click(function() {
        hidePopUpReg();
    });

    // Submit signup form
    $("#btnSubmitSignUp").click(function() { SubmitForm(); });

    function SubmitForm() {
        var isValid = 0;
        var isFNameValid = true;
        var isLNameValid = true;
        var isOrganizationValid = true;
        var isCapchaValid = true;
        if (jQuery.trim($("#txtFName").val()) == '') {
            $("#rqFName").show();
            isValid = 1;
            isFNameValid = false;
        }
        else {
            $("#rqFName").hide();
        }

        if (jQuery.trim($("#txtLName").val()) == '') {
            $("#rqLName").show();
            isValid = 2;
            isLNameValid = false;
        }
        else {
            $("#rqLName").hide();
        }

        if (jQuery.trim($("#txtOrganization").val()) == '') {
            $("#rqOrganization").show();
            isOrganizationValid = false;
            isValid = 3;
        }
        else {
            $("#rqOrganization").hide();
        }

        if (!checkEmail($("#txtEmail").val())) {
            $("#rqEmail").show();
            isValid = 4;
        }
        else {
            $("#rqEmail").hide();
        }


        if ($("input[name='radioContentPref']:checked").val() == 'college' || $("input[name='radioContentPref']:checked").val() == 'employer') {
            $("#rqContentPref").hide();

        }
        else {
            $("#rqContentPref").show();
            isValid = 5;
        }


        if (jQuery.trim($("#txtCapcha").val()) == '') {
            $("#rqCapcha").show();
            isValid = 6;
            isCapchaValid = false;
        }
        else {
            $("#rqCapcha").hide();
        }

        if (isValid != 0) {
            $("#spMessage").fadeIn("slow");
            $("#spMessage").html("Please insert required values.");
            if (isFNameValid == false)
                $("#txtFName").focus();
            else if (isLNameValid == false)
                $("#txtLName").focus();
            else if (isOrganizationValid == false)
                $("#txtOrganization").focus();
            else if (isCapchaValid == false)
                $("#txtCapcha").focus();
            return false;
        }

        $("#progressBackgroundFilter").show();
        CallServer("AddUser" + delimiter + $("#txtFName").val() + delimiter + $("#txtLName").val() + delimiter + $("#txtOrganization").val() + delimiter + $("#txtEmail").val() + delimiter + $("#txtPhone").val() + delimiter + $("#txtCapcha").val() + delimiter + $("input[name='radioContentPref']:checked").val() + delimiter + $("#txtPhoneCountryCode").val() + delimiter + $("#txtPhoneAreaCode").val() + delimiter + $("#txtPhoneExtension").val());
    }

    // Newsletter nemail box enter event
    $("#JtextBox").keypress(function(e) {
        if (e.which == 13) {
            e.preventDefault();
            e.stopPropagation();
            $("#btnJoin").trigger('click');
            return false;
        }
    });

    // Any of the pop-up form's enter event
    $("#display > input").keypress(function(e) {
        if (e.which == 13) {
            e.preventDefault();
            e.stopPropagation();
            $("#btnSubmitSignUp").trigger('click');
            return false;
        }
    });

    $("#JtextBox").focus(function() {
        if ($("#JtextBox").val() == 'your e-mail') {
            $("#JtextBox").attr("value", "");
        }
    });

    $("#JtextBox").blur(function() {
        if ($("#JtextBox").val() == '') {
            $("#JtextBox").attr("value", "your e-mail");
        }
    });

    // Join now click event
    $("#btnJoin").click(function() {
        ShowForm();
    });

    function ShowForm() {
        $('#display').center();
        CreteBlockingContainer();
        $("#spMessage").fadeOut("slow");
        $("#txtFName").attr("value", "");
        $("#txtFName").keypress(function(e) { CheckKeyPress(e); });
        $("#txtLName").attr("value", "");
        $("#txtOrganization").attr("value", "");
        $("#spNewsletterEmail").hide();
        $("#txtEmail").attr("value", "");
        $("#txtEmail").blur(function() { ValidateEmailAddress(); });
        //$("#txtPhoneCountryCode").attr("value", "");
        $("#txtPhoneAreaCode").attr("value", "");
        $("#txtPhone").attr("value", "");
        $("#txtPhoneExtension").attr("value", "");	
        $("#display").removeClass();
        $("#display").addClass("loginPopupContainer_spotlight") 
        $("#display").fadeIn(500, showBlocking);
        $("#txtFName").focus();
        $("#txtCapcha").attr("value", "");
        $("#CancelSignUp").keydown(function(e) {           
            if (e.keyCode == 9) {
                $("#txtFName").focus();
                e.returnValue = false; // for IE
                if (e.preventDefault) e.preventDefault(); // for Mozilla
            } 
        });


    }

    function ValidateEmailAddress() {
        if (!checkEmail($("#txtEmail").val())) {
            $("#spMessage").html("Please provide a valid email address.");

            return false;
        }

        $("#progressBackgroundFilter").show();
        CallServer("CheckUser" + delimiter + $("#txtEmail").val());
    }



});

// Get server data after callback
function ReceiveServerData(arg, context) {
    //alert(arg)
    $("#progressBackgroundFilter").hide();
    var result1 = arg.split(delimiter);
    //alert(result1[0])
    if (result1[0] == 'AddUser') {
        AddUserCallback(result1[1]);
    }
    else if (result1[0] == 'CheckUser') {
        CheckUserCallback(result1[1]);
    }
    else if (result1[0] == 'Unsubscribe') {
        CheckUnsubscribeCallback(result1[1]);
    }
}

// Add User call back method for after getting response from server
function AddUserCallback(result) {
    if (result == '1') {
        $("#display").fadeOut("slow");
        $("#newsletterSignUp").hide();
        $("#newsletterSignIn").show();
        $("#newsletterSignIn").html("Thanks for registering for our spotlight online newsletter.");
        $("#signup_reg_section").removeClass();
        $("#signup_reg_section").addClass("signup_reg_section_loggedin_spotlight")  
        hideBlocking();

    }
    else if (result == '5') {
        $("#spMessage").fadeIn("slow");
        $("#spMessage").html("You have incorrectly typed the text in the image - please try again.");
        $("#whatever").contentWindow.location.reload(1);
    }
    else {
        $("#spMessage").fadeIn("slow");
        $("#spMessage").html("Failed to add new user.");
    }
    
}

// Newsletter check user call back method for after getting response from server
function CheckUserCallback(result) {    
    if (result == '1') {
        $("#newsletterSignUp").hide();
        $("#spMessage").show();
        $("#spMessage").html("You are already in our system as a member, and you will automatically get the newsletter.");

    }
    else if (result == '2') {
        $("#newsletterSignUp").hide();
    $("#spMessage").show();
    $("#spMessage").html("You are already registered for our newsletter.");
    }
    else if (result == '4') {
        $("#newsletterSignUp").hide();
    $("#spMessage").show();
    $("#spMessage").html("You are registered for our newsletter.");
    }    
}

// Unsubscribe newsletter call back method for after getting response from server
function CheckUnsubscribeCallback(result) {
    $("#spUnsubscribeNewsletter").fadeIn("slow");
    $("#spUnsubscribeNewsletter").html("You have been unsubscribed from newsletter successfully.");
    $("#signup_reg_section").removeClass();
    $("#signup_reg_section").addClass("signup_reg_section_loggedin_spotlight")        
   
}

// Hide registration Pop-up
function hidePopUpReg() {
       
    $("#spMessage").fadeOut(500,hideBlocking);
    $("#display").fadeOut("slow");
    $("#rqFName").hide();
    $("#rqEmail").hide();
    $("#rqLName").hide();
    $("#rqCapcha").hide();
    $("#rqOrganization").hide();
    
     
}

// Check valid Email
function checkEmail(value) {
    if (jQuery.trim(value) == '') {
        return false;
    }

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(value)) {
        return false;
    }
    return true;
}

// Show Hero area nav in absolute position
function showHeroAreaNav() {
    var hrNavPos = $("#signup_reg_section");
    var pos = hrNavPos.position();
    var posLeft = pos.left;
    posLeft = posLeft - 148;
    $("#hrActions").css("left", posLeft);
    $("#hrActions").show();
}

jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}


function SetModalPopupPosition(popUp) {

    if (popUp == undefined || popUp == null)
        var popUp = document.getElementById('display');
    if (popUp == null) return;

    $('#display').center();
    if ($('#display').is(':visible')) {
        CreteBlockingContainer();
    }
}

window.onresize = SetModalPopupPosition;

function hideBlocking() {  
    $("#divBlockingContainer").css({ 'display': 'none' });
}
function showBlocking() {    
    $("#divBlockingContainer").css({ 'display': 'block' });
}

function CreteBlockingContainer() {
    var blockingConatiner;
    var hasCreatedEarlier = false;
    if (null != document.getElementById('divBlockingContainer')) {
        blockingConatiner = document.getElementById('divBlockingContainer');
    }
    else {
        blockingConatiner = document.createElement('div');
        blockingConatiner.id = 'divBlockingContainer';
    }
    var documentHeight = $(document).height();
    var documentWidth = $(document).width();

    blockingConatiner.style.position = 'absolute';
    blockingConatiner.style.width = documentWidth + 'px'; //document.body.clientWidth + 'px';
    blockingConatiner.style.height = documentHeight + 'px'; //document.body.clientHeight + 'px';
    blockingConatiner.style.left = 0 + 'px';
    blockingConatiner.style.top = 0 + 'px';
    blockingConatiner.style.zIndex = 19;
    blockingConatiner.className = 'blockingContainer';
    document.body.appendChild(blockingConatiner);
    blockingConatiner.style.display = '';
    //window.scrollTo(0, 0);
}

function CheckKeyPress(e) {
    var eventObject
    if (navigator.userAgent.indexOf("Firefox") != -1)
        eventObject = e.which;
    else if (navigator.userAgent.indexOf("MSIE") != -1)
        eventObject = window.event.keyCode;
    else if (navigator.userAgent.indexOf("Netscape") != -1)
        eventObject = e.which;
    else
        eventObject = window.event.keyCode;

    if (eventObject == 13) {
        e.preventDefault();
        e.stopPropagation();       
        submitForm();
        return false;
    }
    else
        return eventObject;
}		

