﻿//****************************************************************************
//	Copyright (C) 2004 Endeavor Commerce, LLC.  All rights reserved.
//	Script:		CEJava.js
//	Version:	
//              7.1.0  - Initial creation
//	
//	Description:	This script contains all Smart Catalog UI interaction
//					with the Smart Catalog web controls. It handles all
//					dynamic presentation of configuration items.
//****************************************************************************

function uiDisplayPopupWindow(url, title)
{
	var windowW="800px"; // wide
	var windowH="600px"; // high
	
	var windowX = (screen.width/2)-(windowW/2);
	var windowY = (screen.height/2)-(windowH/2);
	/*	
	var popupProps = "left=" + windowX + ",top=" + windowY + ",height=" + windowH + 
		",width=" + windowW + ",menubar=no,resizable=yes,toolbars=no,scrollbars=yes,status=no";
	*/
    // Diaplog properties.
    var popupProps = "dialogLeft:" + windowX + ";dialogTop:" + windowY + ";dialogHeight:" + windowH + 
		";dialogWidth:" + windowW + ";menubar:no;resizable:yes;toolbars:no;scrollbars:yes;status:no;";
		
	var MoreInfoWindow = window.showModelessDialog(url, window, popupProps);
	MoreInfoWindow.focus(); 
}

function uiOpenPopupWindow(url, title)
{
	var windowW=800; // wide
	var windowH=600; // high
	
	var windowX = (screen.width/2)-(windowW/2);
	var windowY = (screen.height/2)-(windowH/2);
		
	var popupProps = "left=" + windowX + ",top=" + windowY + ",height=" + windowH + 
		",width=" + windowW + ",menubar=no,resizable=yes,toolbars=no,scrollbars=yes,status=no";
		
	var MoreInfoWindow = window.open(url, title, popupProps, ""); 
	MoreInfoWindow.focus(); 
}

// Enforces unique radio button selection for a group of Radio Buttons in a repeater.
function SetUniqueRadioButton(nameregex, current)
{
    re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i];
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

function RedirectParentWindow(url)
{
    var xWin = window.dialogArguments;
    xWin.location.href = url;
}

function DisplayOrHideElement(elementId)
{
    var el = document.getElementById(elementId); 
    if(el)
    {
        if(el.style.display == 'none')
            el.style.display='';
        else
            el.style.display='none';
    }
}
// Updates the Css Class of the given element
function ChangeCssClass(elementId, targetClass, sourceClass)
{
    var el = document.getElementById(elementId); 
    if(el)
        el.setAttribute(sourceClass, targetClass);
}

function DisplayElement(elementId)
{
    var el = document.getElementById(elementId); 
    if(el)
        el.style.display='';
}

function HideElement(elementId)
{
    var el = document.getElementById(elementId); 
    if(el)
        el.style.display='none';
}

// This method will validate the min and max values of a textbox control
// and alert the user if they have exceeded the constraints.
function ValidateKeywords(controlID, min, max, productID, minimumMessage, maximumMessage)
{
    //debugger
    var errMsg = "";
    var el = document.getElementById(controlID);
    if(el)
    {
        var arrValues = el.value.split(" ");
        // Validate Min
        if(min != "")
        {
            if(Number(min) > arrValues.length)
            {
                
                errMsg = minimumMessage + " " + min;
            }
        }        
        // Validate Max
        if(max != "")
        {
            if(Number(max) < arrValues.length)
            {
                // Reset value to highest amount of words.
                var newValue = "";
                for(var i = 0; i < Number(max); i++)
                {
                    newValue += arrValues[i] + " ";
                }
                el.value = newValue;
                errMsg = maximumMessage + " " + max;
            }
        }
        
        // Display message to user.
        if(errMsg != "")
        {
            alert(errMsg);
        }
        else
        {
             var elUserInput = document.getElementById("UserInput" + productID);
             if(elUserInput)
             {
                elUserInput.value = el.innerText;
             }      
        }        
    }
    return false;
}

// This method will validate the minimum number of characters of a textbox control
// and alert the user if they have not met the constraints.
function ValidateFreeText(controlID, min, productID, message)
{
    var errMsg = "";
    var el = document.getElementById(controlID);
    if(el)
    {
        var value = el.value;
        // Validate Min
        if(min != "")
        {
             if(Number(min) > value.length)
            {
                errMsg = message + " " + min;
            }
        }
        
        // Display message to user.
        if(errMsg != "")
        {
            alert(errMsg);
        }
        else
        {
             var elUserInput = document.getElementById("UserInput" + productID);
             if(elUserInput)
             {
                elUserInput.value = el.innerText;
             }      
        }
    }
    return false;
}