/*
Expand and close the  field sets containing a set of fields associated with a category in the symbol.
expandImageSrc and collapseImageSrc are variables passed from the symbol editor control (in a script block) and contain the 
expand and collapse image urls.
*/
function ExpandCollapseImage(imgID, expCol)
{
  var elem = document.getElementById(imgID+'FieldSet');
  var imgBtn = document.getElementById(imgID+'ExpColImg');
  if (elem != null)
  {
    if (imgBtn.title == 'Expand')
    {
      elem.style.display = 'block';
      elem.style.visibility = 'visible';
      imgBtn.src = collapseImageSrc;
      imgBtn.title = 'Collapse';
    }
    else
    {
      elem.style.display = 'none';
      elem.style.visibility = 'hidden';
      imgBtn.src = expandImageSrc;
      imgBtn.title = 'Expand';
     }
   }
   return false;
}

/*
Hides the symbol editor control.
*/
function HideControl()
{
  var elem = document.getElementById("BoundingTable");
  if (elem != null)
  {
    elem.style.display = 'none';
    elem.style.visibility = 'hidden';
  }
  return false;
}

/*
Starts the color picker control.
This function is called by the color button associated with a color (RGB) property of a symbol.
It will position the color picker at the SW corner of the color button.  The RGB and chosen color values for the color picker control
will be retrieved from the RGB text fields and color button's background color property and set accordingly.
*/
function RunColorPicker(colorFldIDPrefix, evnt)
{
  var elem = null;
  //var evnt = null;
  var posX = 0, posY = 0;

  var scrollX, scrollY;
    
  // netscape or firefox
  if (document.all)
  {
     if (!document.documentElement.scrollLeft)
        scrollX = document.body.scrollLeft;
     else
        scrollX = document.documentElement.scrollLeft;
              
     if (!document.documentElement.scrollTop)
        scrollY = document.body.scrollTop;
     else
        scrollY = document.documentElement.scrollTop;
  }   
  else
  {
     scrollX = window.pageXOffset;
     scrollY = window.pageYOffset;
  }

  if (window.Event)
  {
    //evnt = window.Event;
    elem = evnt.target;
    posX = evnt.pageX + scrollX;
    posY = evnt.PageY + scrollY;
  }
  else
  {
    evnt = event;
    elem = event.srcElement;
    posX = evnt.clientX + scrollX;
    posY = evnt.clientY + scrollY;
  }
 
 // get a handle to the hidden field in the color picker control that needs to hold the calling control's id
  var callingElem = document.getElementById('cpCallingControlID');
  // store the calling button's id in the 'cpCallingControlID' hidden field on the color picker control
  // this will be used in the ApplyColorPicker() javascript function to set the chosen color and RGB values on the color picker
  // back to the calling control set (color button's background color and RGB text boxes)
  callingElem.value = colorFldIDPrefix;
  var cpElem = document.getElementById('colorPickerTable');
  var cpChosenColor = document.getElementById('ChosenColor');
  var IfrRef = document.getElementById('DivShim');
  if (cpElem != null)
  {
    if (cpElem.style.visibility == 'visible')
    {
      cpElem.style.visibility = 'hidden';
      cpElem.style.display = 'none';
      
      IfrRef.style.display = "none";
    }
    else
    {
   // alert(posX);
   //Added Mar 3 2006
   //Comented the 2 lines below and hardocded the values on the next 2 lines
      //cpElem.style.pixelLeft = posX;
     // cpElem.style.pixelTop = posY;
 
     cpElem.style.left = '254px';
     cpElem.style.top = '30px';
           
      cpElem.style.visibility = 'visible';
      cpElem.style.display = 'block';
      cpElem.style.position = 'absolute';
   
      IfrRef.style.width = cpElem.offsetWidth;
      IfrRef.style.height = cpElem.offsetHeight;
      IfrRef.style.top = cpElem.style.top;
      IfrRef.style.left = cpElem.style.left;
      
      IfrRef.style.zIndex = cpElem.style.zIndex;
      cpElem.style.zIndex = IfrRef.style.zIndex + 1;
     
      IfrRef.style.visibility = 'visible';
      IfrRef.style.position = 'absolute';
    
      IfrRef.style.display = "block";
      document.getElementById('cpRed').value = document.getElementById(colorFldIDPrefix + 'R').value;
      document.getElementById('cpGreen').value = document.getElementById(colorFldIDPrefix + 'G').value;
      document.getElementById('cpBlue').value = document.getElementById(colorFldIDPrefix + 'B').value;
      cpChosenColor.style.backgroundColor = elem.style.backgroundColor;
    }
  }
  return false;
}

/*
Hides the color picker control when it is showing and use clicks on the form outside of the color picker area.
*/
function ClickedOnForm()
{
  var clickedElem = null;
  // netscape or firefox
  if (window.Event)
    clickedElem = window.Event.target;
  else
    clickedElem = event.srcElement;
  if (clickedElem == null || clickedElem.id.indexOf('ColorPicker') != -1 || 
     (clickedElem.type != null && clickedElem.type.indexOf('checkbox') != -1) || 
      clickedElem.id.indexOf('applyBtn') != -1 || clickedElem.id.indexOf('previewBtn') != -1 || clickedElem.id.indexOf('reset') != -1 ||
      clickedElem.id.indexOf('cancelBtn') != -1 || clickedElem.id.indexOf('SymbolTypes') != -1)
    return true;
  
  var cpElem = document.getElementById('colorPickerTable');
  if (cpElem != null)
  {
    if (cpElem.style.visibility == 'visible')
    {
      cpElem.style.visibility = 'hidden';
      cpElem.style.display = 'none';
      var IfrRef = document.getElementById('DivShim');
      IfrRef.style.visibility = 'hidden';
      IfrRef.style.display = 'none';
    }
  }
  return false;
}

/*
Initiates the apply action on the color picker.
*/

function ClickedOnColorPicker()
{
  var clickedElem = null;
  // netscape or firefox
  if (window.Event)
    clickedElem = window.Event.target;
  else
    clickedElem = event.srcElement;
  if (clickedElem == null || clickedElem.id.indexOf('ApplyBtn') != -1)
  {
    ApplyColorPicker();
  }
  return false;
}

/*
Applies the RGB and background color from the color picker to the calling color controls that includes 
a set of RGB text boxes and a color picker button.
*/
function ApplyColorPicker()
{
  var clrR = document.getElementById('cpRed');
  var clrG = document.getElementById('cpGreen');
  var clrB = document.getElementById('cpBlue');
  var callingElemID = document.getElementById('cpCallingControlID').value;
  var callingElemR = document.getElementById(callingElemID + 'R');
  callingElemR.value = clrR.value;
  var callingElemG = document.getElementById(callingElemID + 'G');
  callingElemG.value = clrG.value;
  var callingElemB = document.getElementById(callingElemID + 'B');
  callingElemB.value = clrB.value;
  var cpChosenColor = document.getElementById('ChosenColor');
  var callingElemBtn = document.getElementById(callingElemID + 'ColorPickerBtn');
  callingElemBtn.style.backgroundColor = cpChosenColor.style.backgroundColor;
  return false;
}


// BELOW JAVASCRIPT FROM COLOR PICKER USER CONTROL, NOW USED BY COLOR PICKER WEB CONTROL

// DEBUG MODE
var debug = false;

// detect browser
var isIE = (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0);

/* function SetColor ( TDElement colImg )
   sets the color of chosen bar on top to selected color
*/
function SetColor(colImg)
{
  var chosen = document.getElementById('ChosenColor'); 
  chosen.style.backgroundColor=colImg.style.backgroundColor;  
  //chosen.innerHTML = colImg.alt; 
  var hexColorStr = chosen.style.backgroundColor;
  var red = 0, green = 0, blue = 0;
  // if not hex value get rgb from rgb string for netscape/firefox
  if (hexColorStr.indexOf(["#"]) == -1) 
  {
    if (hexColorStr.indexOf(["rgb"]) != -1)
    {
      var i = 0, j = 0;
      hexColorStr = hexColorStr.slice(4,-1);
      while(hexColorStr[i] != ',' && i < 20)
      {
        i++;
      }
      red = hexColorStr.slice(0,-(hexColorStr.length - i));
      i++;
      j = i;
      while(hexColorStr[j] != ',' && j < 20)
      {
        j++;
      }
      green = hexColorStr.slice(i,0-(hexColorStr.length - j));
      j++;
      blue = hexColorStr.slice(j);
    }
  }
  else
  {
    red = parseInt(hexColorStr.substring(1,3),16);
    green = parseInt(hexColorStr.substring(3,5),16);
    blue = parseInt(hexColorStr.substring(5,7),16);
  }
//  if (debug)
//  {
//    alert("HexColorStr="+hexColorStr);
//    alert("RED=" + red);
//    alert("GREEN=" + green);
//    alert("BLUE=" + blue);
//  }
  document.getElementById('cpRed').value = red; 
  document.getElementById('cpGreen').value = green; 
  document.getElementById('cpBlue').value = blue; 
}

/* function GetR ()
   gets the Red value
*/
function GetR()
{
  var redElem = document.getElementById('cpRed'); 
  
  return redElem.value;
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
/* function isValid ( string parm, string val )
   checks if val has parm in it
*/
function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
/* function isNum ( string parm, string numb )
   checks if val is a number
*/
function isNum(parm) {return isValid(parm,numb);}

/* function isLower ( string parm, string lwr )
   checks if val is lower case
*/
function isLower(parm) {return isValid(parm,lwr);}

/* function isUpper ( string parm, string upr )
   checks if val is upper case
*/
function isUpper(parm) {return isValid(parm,upr);}

/* function isAlpha ( string parm, string lwr+upr )
   checks if val is alpha
*/
function isAlpha(parm) {return isValid(parm,lwr+upr);}

/* function isAlphanum ( string parm, string lwr+upr+numb )
   checks if val is alphanumeric
*/
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);} 
 
/* function ShowColorPicker ( string visibleValue )
   show color picker dialog if visible value flag is 'visible', else hide it if 'hidden'
*/
function ShowColorPicker(visibleValue)
{
  var elem = document.getElementById("colorPickerTable");
  if (elem != null)
  {
    if (visibleValue == 'hidden')
    {
      elem.style.display = 'none';
      elem.style.visibility = 'hidden';
      var IfrRef = document.getElementById('DivShim');
      IfrRef.style.visibility = 'hidden';
      IfrRef.style.display = 'none';
    }
    else
    {
      elem.style.display = 'block';
      elem.style.visibility = 'visible';
      var IfrRef = document.getElementById('DivShim');
      IfrRef.style.visibility = 'block';
      IfrRef.style.display = 'visible';
    }
  }
}

/* function PreviewColor ()
   preview color in upper bar
*/
function PreviewColor()
{
  var red = document.getElementById('cpRed').value; 
  var green = document.getElementById('cpGreen').value; 
  var blue = document.getElementById('cpBlue').value; 
  
  // empty color has RGB values set to empty strings
  if (red.length == 0 && green.length == 0 && blue.length == 0)
  {
    var chosen = document.getElementById('ChosenColor'); 
    chosen.style.backgroundColor = '#F5F5DC';  
  }
  else
  {
    iRed = parseInt(red);
    iGreen = parseInt(green);
    iBlue = parseInt(blue);
  
    if (isNum(red) == false || iRed < 0 || iRed > 255)
    {
        document.getElementById('cpRed').value = '0';
        iRed = 0;
    }
    if (isNum(green) == false || iGreen < 0 || iGreen > 255)
    {
        document.getElementById('cpGreen').value = '0';
        iGreen = 0;
    }
    if (isNum(blue) == false || iBlue < 0 || iBlue > 255)
    {
        document.getElementById('cpBlue').value = '0';
        iBlue = 0;
    }

    var chosen = document.getElementById('ChosenColor'); 
    chosen.style.backgroundColor = '#' + Dec2Hex(iRed) + Dec2Hex(iGreen) + Dec2Hex(iBlue);  
  }
}

var hexChars = "0123456789ABCDEF";
/* function Dec2Hex ()
   convert a decimal to hexadecimal value
*/
function Dec2Hex (d)
{
  var a = d % 16;
  var b = (d - a)/16;
  hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
  return hex;
}

