var voteVideoUrl = "";

function triggerEBConversion(ebConversionID) {
    //alert("Conversion ID: " + ebConversionID);
    
    try {
        var oScript = document.createElement("SCRIPT");
        var ebRand = Math.random()+ ' ';
        ebRand = ebRand * 1000000;
        var conversionURL = "http://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=" + ebConversionID + "&rnd=" + ebRand;
        oScript.src = conversionURL;
        document.body.appendChild(oScript);
    }
    catch(e) { /* do nothing */ }
}

function switchEpisodeVideo(type) {
    if (type == "B") {
        jQuery("#mainEpisodeVideo").attr("class", "Hide");
        jQuery("#altEpisodeVideo").attr("class", "CurrentVideos");
    } else {
        jQuery("#mainEpisodeVideo").attr("class", "CurrentVideos");
        jQuery("#altEpisodeVideo").attr("class", "Hide");
    }
}

function changeEpisodeVideo(episode) {
    jQuery("#episode_id").val(episode);
    document.episodeVideoForm.submit();
}

function validateEmail(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1) {
       return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}

function resetSubscriberForm() {
    $("#fname").attr("value", "");
    $("#surname").attr("value", "");
    $("#email").attr("value", "");
    $("#language").attr("value", "");
    $("#country").attr("value", "");
    $("#day").attr("value", "");
    $("#month").attr("value", "");
    $("#year").attr("value", "");
    $("#subsError").attr("class", "MessageResult");
    $("#subsError").html("");
}

function changeLanguage(language) {
    $("#lang").attr("value", language);
    document.languageForm.submit();
}

function sendVote() {
    triggerEBConversion(voteButtonId);

    var vote_id = jQuery("#vote_id").val();
    var choiceA = document.getElementById("choiceA");
    var choiceB = document.getElementById("choiceB");
    if (choiceA.checked || choiceB.checked) {
        var choice = ((choiceA.checked) ? choiceA.value : choiceB.value);
        votingToFB(choice)
        jQuery.post("vote/save", { choice: choice, vote_id: vote_id },
            function(data) {
                if (data == 'success') {
                    document.getElementById("voteForm").className = "VoteForm Hide";
                    document.getElementById("confirmVote").className = "ConfirmVote";
                }
            }
        );
    }
    return false;
}


