/**
 * @file: downloader.js
 * @description: downloader script
 * @copyright: sellturnkey.com
 **/
var videoId = "";

//disable step1
function disableStep1 (value)
{
    document.downloader.video_url.disabled = value;
    document.downloader.video_step1.disabled = value;
}

//disable step 2
function disableStep2 (value)
{
    if (value == true)
        document.getElementById("step_2").style.display = "none";
    else
        document.getElementById("step_2").style.display = "block";
}

//go step 1
function goStep1 ()
{
    disableStep2 (true);
    disableStep1 (false);
}

//get step1 status
function getStep1Status ()
{
    var videoUrl = document.downloader.video_url.value;
    
    //check video url
    if (videoUrl == '')
    {
        alert ("Error: Please enter video URL!");
        document.downloader.video_url.focus ();
        return false;
    }
    
    //check youtube
    var r1 = new RegExp("youtube\.com");
    if (videoUrl.match(r1))
    {
        //check for video id
        var r2 = new RegExp("v=([^&]+)");
        m = r2.exec(videoUrl);
        if (m != null)
        {
            videoId = m[1];
            if (videoId == '')
            {
                alert ("Error: Can't find any video id. Please check the URL!");
                document.downloader.video_url.focus ();
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            alert ("Error: Can't find any video id. Please check the URL!");
            document.downloader.video_url.focus ();
            return false;
        }
    }
    else
    {
        alert ("Error: Please enter video URL from YouTube only!");
        document.downloader.video_url.focus ();
        return false;
    }

}

//go to step 2
function goStep2 ()
{
    if (getStep1Status ())
    {
        disableStep1 (true);
        disableStep2 (false);
        var url = "http://www.youtube.com/watch?v=" + videoId;
        document.getElementById("youtube_url").innerHTML = "<a href='" + url +"' target='_blank'>" + url + "</a>";
        document.downloader.page_source.value = "";
        document.downloader.video_id.value = videoId;
    }
}

//check final form
function checkFinalForm ()
{
    var pageSource = document.downloader.page_source.value;
    if (pageSource == '')
    {
            alert ("Error: Please enter the page source!");
            document.downloader.page_source.focus ();
            return false;
    }
    else
        disableStep1 (false);
}

//on error image
function onImgErrorSmall(source)
{
    source.src = "images/blank.jpg";
    source.onerror = "";
    return true;
}

//get two form
function getTwoForm (videoUrl)
{
    document.downloader.video_url.value = videoUrl;
    goStep2 ();
}
