﻿//
// SPRetailScripts.js
// Contains the global scripts for the SPRetail solution that are specific to this solution (i.e. non-utility scripts)
//


// IsWatermarkText(text)
// determines if the provided text is the watermark text in a textbox
function IsWatermarkText(text) 
{
    if (text == null)
        return false;

    // array that contains the watermark strings
    var watermarkTexts = [ "Ex. 225, Shape, etc...",
                           "Enter Consultant's First and Last name",
                           "Enter your friend's First and Last name",
                           "Ex. Slumber Party, Word-of-Mouth, etc...",
                           "Ex:555-555-5555"
                         ];

    // try to find the provided text in the watermark text collection
    for (var i = 0; i < watermarkTexts.length; i++) 
    {
        if (watermarkTexts[i] == text)
            return true;
    }


    // text is not watermark text
    return false;
}



// RequiredPasswordValidator(sender, args) 
// This is the validation function for custom validators that need to test if a required password field is filled
function RequiredPasswordValidator(sender, args) 
{
    // get the control to validate
    var ControlToValidate = document.getElementById(sender.controltovalidate);
    args.IsValid = true;

    // build the onfocus event string
    var onfocusString = 'onfocus="ResetPasswordValidator(\'' + sender.id + '\', \'' + ControlToValidate.id + '\');"';

    if ((ControlToValidate.value == null) || (ControlToValidate.value.length == 0) || (ControlToValidate.value == "REQUIRED")) 
    {
        // get the width (if it isn't explicitly set, get it from the css class)
        var width = ControlToValidate.style.width;
        if (StringIsNullOrEmpty(width))
            width = $style(ControlToValidate.id, 'width');

        // get the height (if it isn't explicitly set, get it from the css class)
        var height = ControlToValidate.style.height;
        if (StringIsNullOrEmpty(height))
            height = $style(ControlToValidate.id, 'height');
        
        // validation not passed...hide the control to validate
        ControlToValidate.style.display = 'none';
        
        // inject a dummy textbox and image
        sender.innerHTML = "<input id='dummy_" + ControlToValidate.id + "' type='text' readonly='readonly' style='width:" + width + "; height:" + height + "' class='RequiredFieldRed' value='REQUIRED' " + onfocusString + " /><img src='/Images/Slumberparties/icons/alertred16.gif' alt='REQUIRED' />";
        
        args.IsValid = false;
    }
}

// ResetPasswordValidator(validationControlId, trueTextboxId) 
// resets the password validator and related controls
function ResetPasswordValidator(validationControlId, trueTextboxId) 
{
    // validate input
    if (validationControlId == null || trueTextboxId == null)
        return;

    var validationControl = document.getElementById(validationControlId);
    var trueTextbox = document.getElementById(trueTextboxId);
    trueTextbox.onclick = null;
    trueTextbox.onfocus = null;

    // make sure the controls were found
    if (!trueTextbox || !validationControl)
        return;

    // remove the dummy input textbox
    validationControl.innerHTML = "<img src='/Images/Slumberparties/icons/alertred16.gif' alt='REQUIRED' />";
    
    // show the true textbox and focus
    trueTextbox.style.display = 'inline';
    trueTextbox.focus();
}


function RequiredTextValidator(sender, args) 
{
    var ControlToValidate = document.getElementById(sender.controltovalidate);
    args.IsValid = true;

    if ((ControlToValidate.value == null) || (ControlToValidate.value.length == 0) 
    || (ControlToValidate.value == "REQUIRED") || IsWatermarkText(ControlToValidate.value)) 
    {
        // get the width (if it isn't explicitly set, get it from the css class)
        var width = ControlToValidate.style.width;
        if (StringIsNullOrEmpty(width))
            width = $style(ControlToValidate.id, 'width');

        // get the height (if it isn't explicitly set, get it from the css class)
        var height = ControlToValidate.style.height;
        if (StringIsNullOrEmpty(height))
            height = $style(ControlToValidate.id, 'height');

        // add the css class to the dictionary for later use
        controlStyleDictionary.Add(ControlToValidate.id, ControlToValidate.className);

        ControlToValidate.setAttribute("class", "RequiredFieldRed");
        ControlToValidate.setAttribute("className", "RequiredFieldRed");
        ControlToValidate.style.cssText = "RequiredFieldRed";
        ControlToValidate.style.width = width;      // keep the correct width
        ControlToValidate.style.height = height;    // keep the correct height
        ControlToValidate.onclick = function() { RequiredFieldNormalText(ControlToValidate.id); };
        ControlToValidate.onfocus = function() { RequiredFieldNormalText(ControlToValidate.id); };
        ControlToValidate.value = "REQUIRED";
        sender.innerHTML = "<img src='/Images/Slumberparties/icons/alertred16.gif' alt='REQUIRED' />";
        args.IsValid = false;
    }
}

function RequiredFieldNormalText(controlId) 
{
    var src = document.getElementById(controlId);
    var width = src.style.width;
    var height = src.style.height;
    
    // get the original class name from the dictionary
    var className = controlStyleDictionary.Lookup(controlId);
    controlStyleDictionary.Delete(controlId);

    //Sets the style of the Textbox back to normal
    if (className != null && className != undefined) 
    {
        src.setAttribute("class", className);
        src.setAttribute("className", className);
        src.style.cssText = className;
    }
    src.style.width = width;
    src.style.height = height;
    src.value = "";
    src.focus();
    src.select();

    //Removes the onclick and onfocus events so that once a
    //user enters valid data it will not be erased upon
    //Clicking or refocusing on the textbox
    src.onclick =null;
    src.onfocus = null;

}


// GetImageSource(originalSrc) 
// originalSrc : the original image source 
// gets the mouseover/mouseout image src according to the provided image src
function GetImageSource(originalSrc) 
{
    if (originalSrc == null)
        return null;
    
    
    var newSrc = null;

    // .jpg
    if (originalSrc.indexOf("_o.jpg") > -1)
        newSrc = originalSrc.split("_o.jpg").join(".jpg");
    else if (originalSrc.indexOf(".jpg") > -1)
        newSrc = originalSrc.split(".jpg").join("_o.jpg");

    // .gif
    else if (originalSrc.indexOf("_o.gif") > -1)
        newSrc = originalSrc.split("_o.gif").join(".gif");
    else if (originalSrc.indexOf(".gif") > -1)
        newSrc = originalSrc.split(".gif").join("_o.gif");

    // .png
    else if (originalSrc.indexOf("_o.png") > -1)
        newSrc = originalSrc.split("_o.png").join(".png");
    else if (originalSrc.indexOf(".png") > -1)
        newSrc = originalSrc.split(".png").join("_o.png");

    // else...
    else
        newSrc = originalSrc;

    return newSrc;
}



// btnApplyTheme_ClientClick()
// closes the RadWindow and updates the window's argument to the selected theme's value
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function btnApplyTheme_ClientClick() 
{
    GetRadWindow().Argument = giftRegistryThemes.Lookup(giftRegistryThemes.Keys[currentThemeIndex]);

    CloseWindow();

    return false;
}


// btnBack_ClientClick()
// loads the previous theme preview image
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function btnBack_ClientClick() 
{
    currentThemeIndex--;

    if (currentThemeIndex < 0)
        currentThemeIndex = giftRegistryThemes.Keys.length - 1;

    LoadImage();

    return false;
}


// btnDone_ClientClick()
// Closes the RadWindow and nullifies the window's argument
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function btnDone_ClientClick() 
{
    GetRadWindow().Argument = null;

    CloseWindow();

    return false;
}


// btnNext_ClientClick()
// loads the next theme preview image
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function btnNext_ClientClick() 
{
    currentThemeIndex++;

    if (currentThemeIndex >= giftRegistryThemes.Keys.length)
        currentThemeIndex = 0;

    LoadImage();

    return false;
}


// InitializeImageIndex()
// initializes the currentThemeIndex to the argument provided to the RadWindow
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function InitializeImageIndex() 
{
    var radWindow = GetRadWindow();

    if (radWindow && radWindow.InitialSelectedIndex)
        currentThemeIndex = radWindow.InitialSelectedIndex;
    else
        currentThemeIndex = 0;

    // load the image
    LoadImage();
}


// LoadImage()
// loads the preview image src according to the currentThemeIndex
// (See page for information on the giftRegistryThemes object)
// Currently used on Windows/GiftRegistryThemePreview.aspx
function LoadImage() 
{
    var imgSampleHeader = document.getElementById('imgGiftRegistryHeaderSample');

    if (imgSampleHeader && giftRegistryThemes.Keys.length > 0 && currentThemeIndex < giftRegistryThemes.Keys.length && currentThemeIndex > -1) {
        var themeImageSrc = giftRegistryThemes.Keys[currentThemeIndex] + '.jpg';

        imgSampleHeader.src = imgSampleHeader.src.substring(0, imgSampleHeader.src.lastIndexOf("/") + 1) + themeImageSrc;
    }
}

/* RequiredQtyValidator
// Custom Validator to handle data that would be entered into a Quantity field.
*/
function RequiredQtyValidator(sender, args) {
    var ControlToValidate = document.getElementById(sender.controltovalidate);
    args.IsValid = true;

    if ((ControlToValidate.value == null) || (ControlToValidate.value.trim().length == 0)
                || (isNaN(ControlToValidate.value)) || (ControlToValidate.value < 1)) {
        radalert('The product quantity must be greater than zero. Please try again.');
        args.IsValid = false;
    }
}
