// ---------------------------------------------------------------------
// 
// Library of common javascript functions used by both the single 
// and double sliders.
//
//----------------------------------------------------------------------
function popUp(URL, width, height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=' + width + ',height=' + height +',left = 328,top = -115');");
}


var unsavedWork = false;        // determine if there is unsaved work
var pltPage = false;            // Flag the products like this page for special auto-save handling

//
// Indicate that there is unsaved work to be saved.  This is used for pages
// when upon loading, we wish to flag that there is valid work that should be saved.
//
function setUnsavedWork() {
    unsavedWork = true;
}

function setPLTPage() {
    setUnsavedWork();
    pltPage = true;
}


//
// Pop up a confirmation dialog if there is unsaved work
// and return the results.  If there is no unsaved work,
// simply return true.
//
function checkForUnsavedWorkConfirm(actionToTake) {
    if (isIE5Mac) { 
        // it's broken on IE5
        return true;
    }
    if (unsavedWork) {
        var confirmValue = confirm("It appears you touched at least one slider\n"
            + "but did not submit your changes for this page.\n"
            + "Do you want to discard your changes and\n"
            + actionToTake + "?");
        return confirmValue;
    } else {
        return true;
    }
}

//
// To be overridden on price page
//
var priceSpinnerQuestion = false;

//
// Submit the form, if there is unsaved work
// and return the results.  If there is no unsaved work,
// simply return true.
//
function checkForUnsavedWorkSubmit(forwardAction, qaId) {
    if (unsavedWork) {
        //if (window.sliders.length > 0) {
        //  alert("Num forms:" + document.forms.length + ". Unsaved work found. About to submit " +  document.forms[0].name + 
        //          "\nwindow.sliders.length: " + window.sliders.length +
        //          "\nwindow.sliders[0].sliderType: " + window.sliders[0].sliderType);
        //} else {
        //  alert("Num forms:" + document.forms.length + ". Unsaved work found. About to submit " +  document.forms[0].name + 
        //          "\nwindow.sliders.length: " + window.sliders.length);
        //}

        // Call the appropriate slider javascript to set the slider values only if there exists any to submit:
        if (!pltPage) {
        	if (priceSpinnerQuestion) {
        		if (! validatePMinLessThanOrEqualToPMax()) {
        			return false;
        		}
            } else if (window.sliders && window.sliders.length > 0) {
                if (window.sliders[0].sliderType == "dual") {
                    sliderSetValues(window.sliders[0]);
                } else if (window.sliders[0].sliderType == "single") {
                    saveSliderSettings(document.forms[0], window.sliders);
                }
            }
        } else {
            //alert("PLT Page!");
            //alert("document.forms[0].productInMind[1].checked=" + document.forms[0].productInMind[1].checked);
            //alert("document.forms[0].productInMind[0].checked=" + document.forms[0].productInMind[0].checked);
            if (document.forms[0].productInMind[1].checked == true && 
                 (brandSelectList.selectedIndex == 0 ||
                 (brandSelectList.selectedIndex > 0 && productFamilySelectList.selectedIndex == 0))
               ) {
               // Invalid PLT Page state, don't autosave just follow href
               //alert("PLT Page in invalid state - no autosave");
               return true;
            }
        }
        
        document.forms[0].formPostedForwardAction.value = forwardAction;
        
        if(document.forms[0].formPostedForwardQaId){
        	document.forms[0].formPostedForwardQaId.value = qaId;
        }
        //document.forms[0].formPostedSubmit.value = "Submit";  // This does not seem to get carried over after a explicit form.submit()
        document.forms[0].submit();
        
        //alert("forwardAction: " + document.forms[0].formPostedForwardAction.value + 
        //    "\nformPostedSubmit: " + document.forms[0].formPostedSubmit.value +
        //    "\nDone!");
              
        // We return false here because we do not wish the original href onclick to forward
        // to the target since the submit on the form itself will appropriately forward
        // to the target after the update of the responses.
        return false;
    } 
    return true;
}


var slidersDragged = new Array();
var numSlidersDragged = 0;

//
// Tooltip for slider.  Requires a div with an id of slidertip in it somewhere on the page.
//
function displayTooltip(text, left, top) {
    if (! isIE5Mac && numSlidersDragged < 2) {
        var shouldDisplayTooltip = getElement("displaySliderTooltip");
        if (shouldDisplayTooltip && shouldDisplayTooltip.value != "false") {
            var tooltip = getElement("slidertip");
            if (tooltip.style.visibility.toString() != "visible") {
                tooltip.innerHTML = text;
                tooltip.style.top = (top + 20) + "px";
                tooltip.style.left = (left - 75) + "px";
                tooltip.style.visibility = "visible";
            }
        }
    }
}


function addSliderToDraggedList(sliderId) {
    //
    // Use an associative array to remember
    // which sliders have been dragged.
    // If more than one has been dragged, set 
    // a form variable to indicate that tooltips
    // should no longer be displayed.
    //
    if (! slidersDragged[sliderId]) {
        slidersDragged[sliderId] = sliderId;
        numSlidersDragged++;
        if (numSlidersDragged > 1) {
            setNoSliderTooltipFormVariable();
        }
    }
}

function setNoSliderTooltipFormVariable() {
    var hiddenField = getElement("displaySliderTooltip");
    if (hiddenField) {
        hiddenField.value = "false";
    }
}

function scheduleHideSliderToolTip() {
    if (! isIE5Mac) {
        window.setTimeout("hideSliderToolTip()", 500);
    }
}

function hideSliderToolTip() {
    var tooltip = getElement("slidertip");
    tooltip.style.visibility = "hidden";
    tooltip.left = "-1000px";
    tooltip.top = "-1000px";
    tooltip.innerHTML = "";
}


// ---------------------------------------------------------------------
// create the slider specified by some slider[x] object.
function slider_render(obj) {
var i;
var span;

    with (window.document)
    {
        if (old)
        {
            write("<BR>No browser support for sliders<BR>");
            return;
        }
    
        // now write out all the bits
    
        slider_render_scale(obj);
        slider_render_pane(obj);
    
        // calculate tabs between ticks
        if ( obj.tick_tabs == null ) {
            obj.tick_tabs = (obj.scale_width) / (obj.ticks-1.0);

        }
        
        // draw ticks and labels
        for (i=0; i< obj.ticks; i++)  {
            slider_render_tick(i, obj);
            slider_render_label(i, obj);
        }
        
        slider_render_styluses(obj);
    
        // Install event handlers
        if (obj.interactive == true) {
            if (moz || dom2) {
                span = getElementById(obj.span_id);
                span.addEventListener("mousedown",slider_stylus_mousedown, 0);
                span.addEventListener("mouseup",slider_stylus_mouseup, 0);
                span.addEventListener("mousemove",slider_stylus_mousemove, 0);
                span.addEventListener("mouseout",slider_stylus_mouseout, 0);
            }
            if ( ie4 || ie5 || ie6 || ie7 ) {
                span = all(obj.span_id);
                span.onmousedown = slider_stylus_mousedown;
                span.onmouseup = slider_stylus_mouseup;
                span.onmousemove = slider_stylus_mousemove;
                span.onmouseout = slider_stylus_mouseout;
            }
        }
    }
}


// ---------------------------------------------------------------------
// render the scale "slot" or line
function slider_render_scale(obj)
{
    with (window.document) {
    write("<SPAN><SPAN " +
        "STYLE=\"" +
            "z-index:1; margin:0; padding:0; " +
            "position: absolute; " +
            "top:" + (obj.top + (obj.stylus_height/2) - (obj.scale_height/2) + 2) + "px; " +
            "left:" + obj.left + "px; border-left: 1px solid #a2b2b1; border-right: 1px solid #a2b2b1" +
        "\" >");

    write("<IMG " +
        "SRC=\""+obj.scale_image+"\" " +
        "HEIGHT=\"" + obj.scale_height + "\" " +
        "WIDTH=\"" + obj.scale_width + "\" " +
        ">");
    write("</SPAN></SPAN>");
    }
}



// ---------------------------------------------------------------------
// a mark along the scale
function slider_render_tick(i,obj)
{
    with (window.document) {
    write("<SPAN><SPAN " +
            "STYLE=\"" +
            "z-index:1;" +
            "position: absolute; " +
            "top:" + (obj.top - 1) + "px; " +
            "left:" + ((obj.left + i*obj.tick_tabs) - (obj.tick_width / 2)) + "px; " +
            "font-size:" + obj.tick_width + "px; " +
            "\">" );

    write("<IMG " +
            "SRC=\"" + obj.tick_image + "\" " +
            "HEIGHT=\"" + obj.tick_height + "\" " +
            "WIDTH=\"" + obj.tick_width + "\" " +
            ">");
    write("</SPAN></SPAN>");
    }
}


// ---------------------------------------------------------------------
// make sure position x is never beyond the allowed bounds of the slider
function slider_normalise(x,obj)
{
    var xmin, xmax;
    // calculate new stylus location

    xmin = obj.left;
    xmax = obj.left + obj.scale_width;
    //window.status = "xmin = " + xmin + " xmax = " + xmax + " x = " + x;

    x = ( x <= xmin ) ? xmin : x;
    x = ( x >= xmax ) ? xmax : x;

    return x;
}



// ---------------------------------------------------------------------
// if the slider isn't continuous==true , align x with a tick mark.
function slider_align(x,obj)
{
    var tab = 0;
    if (!obj.continuous)        // must align with a tick
    {
    while ( (x -= obj.tick_tabs) > obj.left - obj.tick_tabs/2 )
    {
        tab++;
    }
    x = obj.left + tab * obj.tick_tabs;
    }
    return x;
}