$(document).ready(function(){
    
    //activates modal, if function isn't triggered due to javascript being off
    //the link will go to the old send page.
    $('a#shareLink').live('click',function(){
        resetSharePage();
        $('#share').show();
        pageTracker._trackEvent("ShareSite", "Form");
        return false;
    });
    
    $('#share p.close').live('click', function(){
        $('#share').hide();
    });
    
    //Fix input boxes to not trigger the search when activated
    textboxes = $(".shareTxt");
    if ($.browser.mozilla) {
        $(textboxes).keypress (checkForEnter);
    } else {
        $(textboxes).keydown (checkForEnter);
    } 
    $('#shareSubmitButton').click(postSharePage);
    
});

function showsharepopup(){
    
    $('#share').center().show();
}

//if the enter key was pressed submit the form
function checkForEnter (event) {
    if (event.keyCode == 13) {
       postSharePage();
       event.preventDefault();
       return false;
    }
}

//Reset share popup to its initial state
function resetSharePage(){
    $('#txtUserEmail').val('');
    $('#txtFriendEmail').val('');
    $("div#share div#shareInput").show();
    $("div#share div#shareThankYou").hide();
}

function postSharePage(){
    //disable form while the ajax is processing
    $(".shareTxt").attr("disabled", "disabled");
    //sends post data to the ShareSite service with does validation & sends email if it passes
    //when the ajax is finish it goes to the processShare function
       $.post("/services/ShareSite.ashx", 
        {
            userEmail: $('#txtUserEmail').val(),
            friendEmail: $('#txtFriendEmail').val(),
            currentPage: window.location.href,
            
            //If the page has a hidden #videoFileName input, send its value.  
            //Otherwise send an empty string.
            videoFileName: ($("input:hidden#videoFileName").length ? $("input:hidden#videoFileName").val() : '')
        }
       ,processShare);
}

function processShare(data){
    //when this function triggers, the ajax is over so we can reenable the form.
    $(".shareTxt").removeAttr("disabled");
    //data returned from service is delimited by |,
    // so we split that to see which fields failed/passed validation
    var validationArray = data.split("|");
    var validationSuccess = true;
    //checked if the user email box passed validation, shows error box if it didn't
    if(validationArray[0] != "true"){
        validationSuccess = false;
        $("#shareErrorUserEmail").css({ display: "block" });    //Need .css() for IE6
    }else{
        $("#shareErrorUserEmail").css({ display: "none" });     //Need .css() for IE6
    }
    //checked if the friend email box passed validation, shows error box if it didn't
    if(validationArray[1] != "true"){
        validationSuccess = false;
        $("#shareErrorFriendEmail").css({ display: "block" });  //Need .css() for IE6
    }else{
        $("#shareErrorFriendEmail").css({ display: "none" });   //Need .css() for IE6
    }

    //checks validationSuccess boolean to see if either email boxes failed validation
    //if they did, show the global error message
    //if not, show thank you message
	if(validationSuccess){
	    $("span#shareRecipient").text($('#txtFriendEmail').val());
	    $("div#share div#shareInput").hide();
	    $("div#share div#shareThankYou").show();
	    pageTracker._trackEvent("ShareSite", "Submit");
    }else{
        $("#ValidationErrorGlobalMessage").show();
        pageTracker._trackEvent("ShareSite", "Invalid");
    }
}

