var timesSubmitted = 0;
function submitAndRedirect(form_name, url)
{
    if ( timesSubmitted != 0 )
        return false;

    timesSubmitted = timesSubmitted+1;
    document.forms[form_name].action=url;
    document.forms[form_name].submit();
    return false;
}

function submitForm(form_name)
{
    if ( timesSubmitted != 0 )
        return false;

    timesSubmitted = timesSubmitted+1;
    document.forms[form_name].submit();
    return false;
}

function submit_email(source, url_root, page)
{
    var email = "";
    if( source == 'footer' )
        email = document.forms["OptInFormFooter"].email;
    else
        email = document.forms["OptInForm"].email;

    if ( email.value == "" || email.value == "email address")
    {
        alert("Please enter email address");
        email.focus();
        return false;
    }

    if ( !isEmail(email.value) )
    {
        alert("Please enter valid email address");
        email.focus();
        return false;
    }

    launchPopupNoScroll(url_root + "popups/popup_opt_in_confirm.jsp?email=" + email.value + "&src=" + source + "&page=" + page + "&ip=" + getMyIP(), 500, 420);
    email.value="";

    return true;
}

function isEmail(email)
{
    return email!= "" && email.indexOf("@")!=-1 && email.indexOf(".")!=-1;
}


function launch_flash_trailers(trailer_url)
{
    launchPopupNoScroll(trailer_url, 600, 500);
}

function launch_review(url_root, pid, source)
{
    launchPopupNoScroll(url_root + "popups/popup_review.jsp?product_identifier=" + pid + "&src=" + source + "&ip=" + getMyIP(), 600, 470);
}

function launch_tellafriend(url_root)
{
    launchPopupNoScroll(url_root + "popups/popup_tellafriend.jsp", 600, 640);
}


function launch_macbeta(url_root)
{
    launchPopupNoScroll(url_root + "popups/popup_join_macbeta.jsp", 600, 400);
}

function launch_feedback(source, url_root, page)
{
    launchPopupNoScroll(url_root + "popups/popup_feedback.jsp" + "?src=" + source + "&page=" + page + "&ip=" + getMyIP(), 600, 550);
}

function launch_guarantee(url_root)
{
    launchPopupNoScroll(url_root + "popups/popup_guarantee.jsp", 520, 400);
}

function launch_cover_art(url_root, pid)
{
    launchPopupNoScroll(url_root + "popups/popup_cover_art.jsp?product_identifier="+pid, 500, 400);
}

function launch_awards(url_root, pid)
{
    launchPopup(url_root + "popups/popup_awards.jsp?product_identifier="+pid, 550, 450, "yes");
}

function launch_video(url_root, pid)
{
    launchPopupNoScroll(url_root + "popups/_detect_video_type.jsp?product_identifier="+pid, 550, 450);
}

function launch_photos(url_root, pid)
{
    launchPopup(url_root + "popups/popup_photos.jsp?product_identifier="+pid, 550, 450, "yes");
}

function launch_faq(url_root)
{
    launchPopup(url_root + "popups/popup_faqs.jsp", 800, 600, "yes");
}

function launch_not_available_why(url_root)
{
    launchPopup(url_root + "popups/popup_not_available_why.jsp", 550, 450, "yes");
}

function launch_seccode(url_root)
{
    launchPopup(url_root + "popups/popup_seccode.jsp", 500, 400, "yes");
}

function launch_securesite(url_root)
{
    launchPopup(url_root + "popups/popup_securesite.jsp", 500, 400, "yes");
}

function launch_privacy(url_root)
{
    launchPopup(url_root + "popups/popup_privacy.jsp", 500, 400, "yes");
}

function launch_hiw(url_root)
{
    launchPopupNoScroll(url_root + "popups/popup_hiw.jsp", 800, 480);
}

function launch_quicktour(url_root)
{
    launchPopupNoScroll(url_root + "popups/popup_quick_tour.jsp", 800, 480);
}

function launchPopupNoScroll(url, width, height)
{
    launchPopup(url, width, height, "no");
}

function launchPopup(url, width, height, scrollbar)
{
    appearance = "status=no,toolbar=no,location=no,directories=no,resizable=no,menubar=no,copyhistory=no";
    if (width > 0)
        appearance += (",width="+width);
    if (height > 0)
        appearance += (",height="+height);

    appearance += (",scrollbars=" + scrollbar);

    openWindow(url, appearance);
}

function launchWindow(url, width, height)
{
    appearance = "status=yes,toolbar=yes,location=yes,resizable=yes,menubar=yes,copyhistory=yes,scrollbars=yes";
    if (width > 0)
        appearance += (",width="+width);
    if (height > 0)
        appearance += (",height="+height);

    openWindow(url, appearance);
}

function openFAQWindow(url)
{
    openWindow(url, "width=450,height=330");
}

function openEmailWindow(url)
{
    openWindow(url, "width=450,height=390");
}

function openWindow(url, appearance )
{
    popup_win = window.open( url, "_blank", appearance);
    if (popup_win)
        popup_win.focus();
}

function goto_genre()
{
    genre = document.forms["browsebygenre"].genre;

    if ( genre.value == "")
        return false;

    submitAndRedirect("browsebygenre", genre.value);
    return true;
}

function sort_product_list()
{
    submitAndRedirect("sort_list_by", "download/public/_process_sort.jsp");
    return true;
}

function filter_product_list(filter)
{
    submitAndRedirect("filter_list_by", "download/public/_process_filter.jsp?filter="+filter);
    return true;
}