var publicURL = "http://www.data.jumpwebspot.com/";
var secureURL = "https://www.jumpwebspot.com/jumpwebspot/";

function MoveLoginBox()
{
    //move the menu banner to the top of the screen
    var obj = document.getElementById("LoginBoxBackground");
    //get the current top value
    var scrollTop = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
    obj.style.top = scrollTop + "px";
    window.onscroll = MoveLoginBox;
    //setTimeout("MoveLoginBox()", 1000);
}

function DisableFlash()
{
    var objects = document.getElementsByTagName("object");
    
    for(var i=0; i<objects.length; ++i)
    {
        objects[i].style.visibility = "hidden";
    }
    
    //also hide any select/option tags (for IE)
    objects = document.getElementsByTagName("select");
    
    for(var i=0; i<objects.length; ++i)
    {
        objects[i].style.visibility = "hidden";
    }
}

function EnableFlash()
{
    var objects = document.getElementsByTagName("object");
    
    for(var i=0; i<objects.length; ++i)
    {
        objects[i].style.visibility = "visible";
    }

    //also hide any select/option tags (for IE)
    objects = document.getElementsByTagName("select");
    
    for(var i=0; i<objects.length; ++i)
    {
        objects[i].style.visibility = "visible";
    }
}

function FormCheck(fieldName)
{
    var formEl, allLabels, fieldID, curField, throwError;
    var i, j;
    
    //look for required fields
    formEl = document.getElementById("Form_" + fieldName);
    if(formEl)
    {
        allLabels = formEl.getElementsByTagName("label");
        if(allLabels)
        {
            for(i=0; i<allLabels.length; ++i)
            {
                if(allLabels[i].className && allLabels[i].className.indexOf("RequiredField") >= 0)
                {
                    fieldID = allLabels[i].htmlFor;
                    throwError = false;
                    if(fieldID)
                    {
                        curField = document.getElementById(fieldID);
                        switch(curField.type.toLowerCase())
                        {
                            case "text":
                            {
                                if(curField.value == "")
                                {
                                    throwError = true;
                                }
                            } break;
                            case "radio":
                            {
                                //run through the radio buttons and make sure one of them is selected
                                while(fieldID.charAt(fieldID.length - 1) >= "0" && fieldID.charAt(fieldID.length- 1) <= "9")
                                {
                                    fieldID = fieldID.substring(0, fieldID.length-1);
                                }
                                throwError = true;
                                j = 0;
                                while(curField = document.getElementById(fieldID + j))
                                {
                                    if(curField.checked)
                                    {
                                        throwError = false;
                                    }
                                    ++j;
                                }
                            } break;
                            case "checkbox":
                            {
                                if(!curField.checked)
                                {
                                    alert("You must fill in the " + allLabels[i].innerHTML + " checkbox to continue");
                                    return false;
                                }
                            } break;
                            case "file":
                            {
                                if(!curField.value || curField.value == "")
                                {
                                    throwError = true;
                                }
                            } break;
                        }
                    }
                    if(throwError)
                    {
                        alert("Please fill in the " + allLabels[i].innerHTML + " field");
                        return false;
                    }
                }
            }
        }
    }
    
    return true;
}

function GalleryActivate()
{
    var allLists = document.getElementsByTagName("div");
    var galleryDivs;
    var expandedDivs;
    
    for(var i=0; i<allLists.length; ++i)
    {
        if(allLists[i].className == "Gallery")
        {
            galleryDivs = allLists[i].getElementsByTagName("div");
            for(var j=0; j<galleryDivs.length; ++j)
            {
                if(galleryDivs[j].className == "GalleryExpanded")
                {
                    galleryDivs[j].style.display = "block";
                    expandedDivs = galleryDivs[j].getElementsByTagName("div");
                    for(var k=0; k<expandedDivs.length; ++k)
                    {
                        switch(expandedDivs[k].className)
                        {
                            case "GalleryLargePictureTitle":
                            {
                                expandedDivs[k].innerHTML = "Picture (large view)";
                            } break;
                            case "GalleryDescriptionTitle":
                            {
                                expandedDivs[k].innerHTML = "Description";
                            } break;
                            case "GalleryLargePicture":
                            {
                                expandedDivs[k].innerHTML = "No picture selected";
                            } break;
                            case "GalleryDescription":
                            {
                                if(expandedDivs[k].innerHTML == "")
                                {
                                    expandedDivs[k].innerHTML = "Please click an image below to see an expanded view to the left";
                                }
                            } break;
                        }
                    }
                }
            }
        }
    }
}

function GalleryExpand(thumbEl, url, description)
{
    var parentEl;
    var galleryDivs;
    var inputEls;
    var pricingInfo = new Array();
    
    description = decodeURIComponent(description);
    //expand the given url into the upper pane
    //look through the hierarchy above this element and look for a div with a className of "Gallery"
    parentEl = thumbEl;
    do
    {
        parentEl = parentEl.parentNode;
    } while(parentEl && parentEl.className != "Gallery");
    if(parentEl && parentEl.className)
    {
        galleryDivs = parentEl.getElementsByTagName("div");
        for(var i=0; i<galleryDivs.length; ++i)
        {
            if(galleryDivs[i].className)
            {
                switch(galleryDivs[i].className)
                {
                    case "GalleryLargePicture":
                    {
                        if(url.substring(url.length-4, url.length).toLowerCase() == ".swf")
                        {
                            ActivateSWFInto(url, "100%", "100%", galleryDivs[i], "showAll");
                        }
                        else if(url.substring(url.length-4, url.length).toLowerCase() == ".flv")
                        {
                            ActivateSWFInto("/content/watchvideo.php?VF=" + encodeURIComponent(url), "100%", "100%", galleryDivs[i], "showAll");
                        }
                        else
                        {
                            galleryDivs[i].innerHTML = "<img src=\"" + url + "\" alt=\"Large picture\" />";
                        }
                    } break;
                    case "GalleryDescription":
                    {
                        galleryDivs[i].innerHTML = "<p>" + description + "</p>";
                    } break;
                }
            }
        }
    }
}

function GalleryChangeCartButton(selectNum)
{
    var selectEl = document.getElementById("Gallery_" + selectNum + "_Selector");
    var divEls = selectEl.parentNode.getElementsByTagName("div");
    
    for(var i=0; i<divEls.length; ++i)
    {
        if(divEls[i].className == "GalleryCartButton")
        {
            divEls[i].innerHTML = selectEl.options[selectEl.selectedIndex].value;
        }
    }
}

function LoginDisplay(loginPage, siteName, forgotPasswordKey, demoSite, resellerName)
{
    var loginBox = document.getElementById("LoginBoxBackground");
    if(loginBox)
    {
        loginBox.parentNode.removeChild(loginBox);
    }
    var loginDiv = document.createElement("div");
    var newHTML = "";
    
    newHTML = "<form id=\"LoginBoxForm\" action=\"" + loginPage + "\" method=\"post\">";
    newHTML += "<div class=\"LoginBox\">";
    newHTML += "<h1>Log in</h1>";
    newHTML += "<a class=\"LoginClose\" href=\"javascript: void(0);\" onclick=\"LoginClose(); return false;\"><img src=\"" + publicURL + "images/login/20x20dkx.gif\" alt=\"Close\" /></a>";
    if(demoSite)
    {
        newHTML += "<div class=\"LoginDemoExplaination\">As a demo user, your username and password are both &quot;sample&quot;</div>";
    }
    newHTML += "<div class=\"LoginInfo\">";
    newHTML += "<input type=\"hidden\" name=\"Site\" value=\"" + siteName + "\" />";
    if(resellerName != "")
    {
        newHTML += "<input type=\"hidden\" name=\"SiteType\" value=\"" + resellerName + "\" />";
    }
    newHTML += "<label id=\"LoginUsernameLabel\" for=\"LoginUsername\">username:</label>";
    newHTML += "<input type=\"text\" name=\"Username\" id=\"LoginUsername\" size=\"30\" onkeypress=\"LoginSubmitIfEnter(event); return true;\"" + (demoSite ? " value=\"sample\"" : "") + " /><br />";
    newHTML += "<label id=\"LoginPasswordLabel\" for=\"LoginPassword\">password:</label>";
    newHTML += "<input type=\"password\" name=\"Password\" id=\"LoginPassword\" size=\"30\" onkeypress=\"LoginSubmitIfEnter(event); return true;\"" + (demoSite ? " value=\"sample\"" : "") + " /><br />";
    //newHTML += "<div id=\"LoginRememberMeContainer\"><label id=\"LoginRememberMeLabel\" for=\"LoginRememberMe\">remember me <a href=\"" + publicURL + "help/rememberme.php\" onclick=\"window.open('" + publicURL + "help/rememberme.php', '_blank', 'width=320, height=320, scrollbars=yes'); return false;\">(?)</a></label><input type=\"checkbox\" name=\"LoginRememberMe\" id=\"LoginRememberMe\" /></div><br /><br />";
    newHTML += "<div id=\"LoginForgotPassword\"><a href=\"" + secureURL + "forgotpassword.php?U=" + forgotPasswordKey + "\" onclick=\"window.open('" + secureURL + "forgotpassword.php?U=" + forgotPasswordKey + "'); return false;\">I forgot my password</a></div>";
    newHTML += "<a id=\"LoginCancel\" href=\"javascript: void(0);\" onclick=\"LoginClose(); return false;\"><img src=\"" + publicURL + "images/login/dkcancel.jpg\" alt=\"Cancel\" /></a>";
    newHTML += "<input type=\"image\" id=\"LoginSubmit\" src=\"" + publicURL + "images/login/dklogin.jpg\" alt=\"Log in\" onmouseover=\"this.src='" + publicURL + "images/login/ltlogin.jpg';\" onmouseout=\"this.src='" + publicURL + "images/login/dklogin.jpg';\" /></div>";
    newHTML += "</div>";
    newHTML += "</div>";
    newHTML += "</form>";
    
    loginDiv.className = "Overlay LoginBoxBackground";
    loginDiv.id = "LoginBoxBackground";
    loginDiv.innerHTML = newHTML;
    
    DisableFlash();
    loginDiv = document.body.insertBefore(loginDiv, null);
    MoveLoginBox();
    document.getElementById("LoginUsername").focus();
}

function LoginClose(loginPage)
{
    var loginDiv = document.getElementById("LoginBoxBackground");
    
    document.body.removeChild(loginDiv);
    EnableFlash();
}

function LoginSubmitIfEnter(e)
{
    if(!e) e = window.event;
    
    if(e && e.keyCode == 13)
    {
        document.getElementById("LoginBoxForm").submit();
    }
}

function ViewLockAspect(elID, aspect, lockToWidth)
{
    var el = document.getElementById(elID);
    
    if(el)
    {
        if(lockToWidth)
        {
            el.style.height = ((el.clientWidth ? el.clientWidth : el.offsetWidth) / aspect) + "px";
        }
        else
        {
            el.style.width = ((el.clientHeight ? el.clientHeight : el.offsetHeight) * aspect) + "px";
        }
        setTimeout("ViewLockAspect('" + elID + "', " + aspect + ", " + (lockToWidth ? "true" : "false") + ");", 100);
    }
}

function ProductCheckForm(formID)
{
    var formEl = document.getElementById("AddToCartForm" + formID);
    var allChildEls = formEl.getElementsByTagName("*");
    
    //check a product form to make sure all required fields have a value
    for(var i=0; i<allChildEls.length; ++i)
    {
        if(allChildEls[i].className && allChildEls[i].className == "FormRequired")
        {
            switch(allChildEls[i].tagName.toLowerCase())
            {
                case "select":
                {
                    if(allChildEls[i].options[allChildEls[i].selectedIndex].value == "")
                    {
                        alert("Please pick an option for the " + allChildEls[i].options[0].textContent.substring(3, allChildEls[i].options[0].textContent.length-3) + " field");
                        return false;
                    }
                } break;
            }
        }
    }
    return true;
}

