var code4 = '';

//Send share information to shareVideo.php
function shareVideo(vkey,num){
    //Get objects
    toUsers = $('#to').val();
    fromUser = $('#username').val();
    msg = $('#message').val();
    code = $('#captcha4').val();

    //Check for errors
    if(toUsers == ""){
    	alert("Please enter some pimps tnaflix usernames or emails to send to.");
    	return false;
    }
    if(fromUser == ""){
    	alert("Please enter your username or name.");
    	return false;
    }
    if(code == ""){
    	alert("Please enter the code for the captcha.");
   		return false;
    }

    //disable share button and display loading gif
 	$('#shareVideoButton').attr('disabled', 'disabled');
    $('#shareFieldSetMsg').html('<br /><img src="images/spinna.gif" alt="Loading..." />');
    
    $.ajax({
        type: "POST",
        url: '/ajax/shareVideo.php',
        data: ({
                vkey: vkey,
                code: code,
                tousers: toUsers,
                fromusers: fromUser,
                msg: msg,
                num: num
              }),
        success: showShareResult
    });
    
    return false;
}

function xmasShareVideo(vkey){
    //Get objects
    toUsers = $('#to').val();
    fromUser = $('#username').val();
    code = $('#captcha4').val();
    msg = $('message').val();

    //Check for errors
    if(toUsers == ""){
    	alert("Please enter some pimps tnaflix usernames or emails to send to.");
    	return false;
    }
    if(fromUser == ""){
    	alert("Please enter your username or name.");
    	return false;
    }
    if(code == ""){
    	alert("Please enter the code for the captcha.");
   		return false;
    }

    //disable share button and display loading gif
 	$('#shareVideoButton').attr('disabled', 'disabled');
    $('#shareFieldSetMsg').html('<br /><img src="images/spinna.gif" alt="Loading..." />');
    
    $.ajax({
        type: "POST",
        url: '/ajax/shareVideo.php',
        data: ({
                vkey: vkey,
                code: code,
                codeKey: 4,
                xmas: 1,
                tousers: toUsers,
                fromusers: fromUser,
                msg: msg
              }),
        success: showShareResult        
    });    

    return false;
}

function showShareResult(xhr){    
    //Enable share button and delete loading gif
    $('#shareVideoButton').attr('disabled', '');
    $('#shareFieldSetMsg').html('');

    var messageResponse = eval("("+xhr+")");
    if (messageResponse[0] == 'success') {
        //Clear share form only friends and message
        $('#to').val('');
        $('#message').val('');
        $('#captcha4').val('');
    
        //Regenerate captcha
        $('#captchaCode4').attr('src', 'captcha.php?num=4&'+Math.random());
    
        //Display success message
        alert(messageResponse[1]);

//        if(messageResponse[2] == 'updateBtnNoCaptcha'){
//            document.getElementById('shareVideoButton').onclick = new Function('shareVideo($("#vkey").val(),1,1)');
//            document.getElementById('flagVideoButton').onclick = new Function('sndFlag(document.getElementById("reason").value,1,1)');
//            document.getElementById('submitRight').onclick = new Function('sndComment($("#commentpost").val(),$("#UID").val(),$("#VID").val(),1,1);');
//        } else if(messageResponse[2] == 'updateBtn') {
//            document.getElementById('shareVideoButton').onclick = new Function('captchaDialog("Share Video","To share this video","","shareVideo",$("#vkey").val())');
//            document.getElementById('flagVideoButton').onclick = new Function('captchaDialog("Flag Video","To flag this video","","sndFlag",document.getElementById("reason").value)');
//            document.getElementById('submitRight').onclick = new Function('addComment($("#commentpostFirst").val(),$("#UID").val(),$("#VID").val())');
//        }

        $('#shareVideoButton').attr('disabled', false);
      } else {    
    	//Regenerate captcha
        $('#captchaCode4').attr('src', 'captcha.php?num=4&'+Math.random());

    	//Display error
        alert(messageResponse[1]);
    }       
}
