﻿
//this is currently set for 59 min; timeout is 60 min
var timeout = setTimeout("redirectToLogin()", 3540000);
//this is set for every 5 seconds for testing
//var timeout = setTimeout("redirectToLogin()", 5000);

//redirectToHttps();

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("UA-8573025-1");
pageTracker._trackPageview();
} catch(err) {}


//function redirectToHttps()
//{
//    if (window.location.protocol == "http:")
//    {
//        window.location = "https://www.projectcentipede.org";
//        return false;
//    }
//}

function MyCentipedeValidation() {
    var titleBox = document.getElementById(titleBoxId);
    if (titleBox.value.length < 1) {
        alert("The title is required.");
        return;
    }
    else if (titleBox.value.length > 50) {
        alert("The title must be 50 characters or less.");
        return;
    }

    var authorBox = $get(authorBoxId);
    if (authorBox.value.length < 1)
    {
        alert("The author is required.");
        return;
    }
    else if (authorBox.value.length > 50) {
        alert("The author must be 50 characters or less.");
        return;
    }

    var summaryBox = $get(summaryBoxId);
    if (summaryBox.value.length < 1) {
        alert("The summary is required.");
        return;
    }
    else if (summaryBox.value.length > 200) {
        alert("The summary must be 200 characters or less.");
        return;
    }
    var list = document.getElementById(mediaTypeListId);
    var file = document.getElementById(fileUploadId); 
    var currentMediaType = list.options[list.selectedIndex].value;
    var currentFileName = file.value;
    
    if (currentMediaType == "Link") {
        var txtLink = $get(linkBoxId);
        if (txtLink.value.length > 500)
        {
            alert ("The link must be 500 characters or less.");
            return;
        }
        var hiddenButtonForLinks = document.getElementById(hiddenButtonForLinksId);
        hiddenButtonForLinks.click();
    }
    else if (currentMediaType == "YouTube Code")
    {
        var txtYouTube = $get(txtYouTubeId);
        if (txtYouTube.value.length > 500)
        {
            alert ("The YouTube code must be 500 characters or less.");
            return;
        }
        var hiddenButtonForLinks = document.getElementById(hiddenButtonForLinksId);
        hiddenButtonForLinks.click();
    }
    else {
        //alert ("execution gets to within the else statement"); 
            //needed to allow submission if the form is in edit mode
            if (file.style.display == "none")
            {
                submitForm();
                return;
            }
            if (currentFileName == "")
                alert ("Please select a file.");
            else
            {
                disableButton();
                if (currentMediaType == "Entire Curriculum as Zip")
                {
                    //alert ("execution gets to right before checkForZip"); 
                    checkForZip(currentFileName);
                }
                else
                    MyWebservice.validateType(currentMediaType, currentFileName, getValidation, onFailed);
            }
        } 
}

function getValidation(success) {
    if (success)
        submitForm();
    else {
        restoreButton();
        alert("The selected file is either missing a file extension or is not the correct file type.");
    }
}

function submitForm () {
    var hiddenButtonHandle = document.getElementById(hiddenButtonId);
    hiddenButtonHandle.click();
}

function checkForZip(fileName) {
    //alert ("execution gets to checkForZip"); 
    var realmList = document.getElementById(realmListId);
    var curriculumList = document.getElementById(curriculumListId);

    MyWebservice.checkForZip(fileName, realmList.options[realmList.selectedIndex].value,
       curriculumList.options[curriculumList.selectedIndex].value, getZipCheck, onFailed);
}

function getZipCheck(returnCode) {
   // alert (returnCode);
    if (returnCode == 0) {
        restoreButton();
        alert("The selected file is either missing a file extension or is not the correct file type.");
    }
    else if (returnCode == 1) {
        restoreButton();
        alert("Another zip for this curriculum already exists.  " +
        "If the already-existing file needs to be deleted, please request deletion using the Edit Request Form");
    }
    else //returnCode == 2
        submitForm();
}        

function onFailed() {
    restoreButton();
    alert("The server is having difficulty processing your request.  This difficulty could be the result of a connection issue.  " +
    "If the problem persists please contact us using the link at the bottom right of this page.");
}

function disableButton ()
{
    var buttonHandle = document.getElementById("Button1");
    buttonHandle.value = "Processing...";
    buttonHandle.disabled = true;
}

function restoreButton() {
    var buttonHandle = document.getElementById("Button1");
    buttonHandle.value = "Submit";
    buttonHandle.disabled = false;
}

function hideFileUpload() {
    processing();
    var mediaTypeList = document.getElementById(mediaTypeListId);
    var currentMediaType = mediaTypeList.options[mediaTypeList.selectedIndex].value;
    var file = document.getElementById(fileUploadId);
    var fileLabel = document.getElementById(fileUploadLabelId);

    if (currentMediaType == "Link" || currentMediaType == "YouTube Code") {
        fileLabel.style.display = "none";
        file.style.display = "none";
    }
    else
        showFileUpload();
}

function showFileUpload ()
{
    var file = document.getElementById(fileUploadId);
    var fileLabel = document.getElementById(fileUploadLabelId);
    fileLabel.style.display = "inline";
        file.style.display = "inline";
        }
        
function disable (id)
{
        var listToDisable = $get(id);
        listToDisable.disabled = true;
}

function confirmDelete()
{
    return (confirm ("Are you sure you want to delete this resource?"));
}

function redirectToLogin()
{
    window.location = "loginAndSignUp.aspx?loggedOut=true";
    return false;
}

function resetTimeout()
{
    clearTimeout(timeout);
    timeout = setTimeout("redirectToLogin()", 3540000);
}

function processing()
{
    var processingElem = $get("processingDiv");
    processingElem.innerHTML = "processing...";
    processingElem.style.width = "500px";
    processingElem.style.height = "100px";
    processingElem.style.fontSize = "12px";
    processingElem.style.fontColor = "#003366";
    processingElem.style.fontWeight = "bold";
    processingElem.style.fontFamily = "Verdana"
    processingElem.style.textAlign = "left";
    processingElem.style.backgroundColor = "#FFFFFF";
    processingElem.style.zIndex = "30";
    processingElem.style.position = "absolute";
}