var __base_version=1,
    __show_errors=false;

function IsControlKey(event)
{
    if(event.altKey == true)
        return true;
    if(event.ctrlKey == true)
        return true;
    
    return false;
}            
function IsTabKey(event)
{
    if(event.keyCode==9)
        return true;
        
    return false;
}
function IsShiftKey(event)
{
    if (event.keyCode == 16)
        return true;

    return false;
}
function IsFuncKey(event)
{
    if(event.keyCode==8) //Backspace
        return true;

    if(event.keyCode==46) //Delete
        return true;

    if(event.keyCode==9) //Tab
        return true;
    
    if(event.keyCode==37) //Left arrow
        return true;

    if(event.keyCode==39) //Right arrow
        return true;

    if(event.keyCode==38) //Up arrow
        return true;

    if(event.keyCode==40) //Down arrow
        return true;
 
     if(event.keyCode==36) //Home
        return true;

    if(event.keyCode==35) //End
        return true;

    if(event.keyCode==39) //Page Up
        return true;
        
    if(event.keyCode==39) //Page Down
        return true;
        
    return false;
}
function IsUpArrowKey(event)
{
    if (event.keyCode == 38) //Up arrow
        return true;
}
function IsDownArrowKey(event)
{
    if (event.keyCode == 40) //Down arrow
        return true;
}
function IsLeftBracketKey(event)
{
    if((event.shiftKey==true) && (event.keyCode==9))
        return true;
        
    return false;
}
function IsRightBracketKey(event)
{
    if((event.shiftKey==true) && (event.keyCode==0))
        return true;
        
    return false;
}
function IsNumbericKey(event)
{
    if(event.shiftKey==true)
        return false;
        
 
    // 0 ---- 48
    // 9 ---- 57           
    if((event.keyCode>47) &&(event.keyCode<58))
        return true;

    // 0 ---- 96
    // 9 ---- 105
    if((event.keyCode>95) && (event.keyCode<106))
        return true;
        
    return false;
}
function EnableControlOnCheckBoxClick(checkBoxId,controlId,onclick)
{
	var eCheckBox=$get(checkBoxId);
	if(eCheckBox==null)
		return;

	var eControl=$get(controlId);
	if(eControl==null)
		return;

    eControl.disabled = eCheckBox.checked ? false : true;
    eControl.className = "button";
	
	//At this point, I deliberately built this into the function even though it creates
	//a nongeneric bahavior in a seemingly generic function. I did this to minimize the 
	//number of changes that would occur if I added a paramater ("onclick") and changed
	//it everywhere it's called. Currently, the only time that this function is used
	//is to associate the Verify checkbox with the Finish button. This will have to change
	//if we decide to start using it generically.
    if(eControl.onclick==null)
    {
        eControl.onclick="$get('"+controlId+"').style.visibility='hidden'";

        if (eControl.addEventListener)  
            eControl.addEventListener('click',new Function(eControl.onclick),false); 
        else if (eControl.attachEvent)  
            eControl.attachEvent('onclick',new Function(eControl.onclick)); 
    }
}

function CopySelectedItemToTextArea(listId,txtId,option,maxLen,resetSelection)
{
    //Option==0: Override txtId content w/selected VALUE
    //Option==1: Append selected VALUE to the beginning
    //Option==2: Append selected VALUE to the end
    //Option==3: Override txtId content w/selected TEXT
    //Option==4: Append selected TEXT to the beginning
    //Option==5: Append selected TEXT to the end
    
    if(option<0)
        return;
    if(option>5)
        return;
    var eList=$get(listId);
    if(eList==null)
        return;
    var eTextArea=$get(txtId);
    if(eTextArea==null)
        return;

    var selectedText = "";
    if (option < 3)
        selectedText = eList.options[eList.selectedIndex].value;
    else
        selectedText = eList.options[eList.selectedIndex].text;

    if (selectedText.length == 0 || selectedText == "< N/A >" || selectedText == "< Select ... >" || selectedText == "< Blank >")
        return;
    
    if((option == 0 || option == 3) && CheckMaxLengthAndShowMessage(selectedText, maxLen))
        eTextArea.value=selectedText;
    else if (option == 1 || option == 4)
    {
        var newValue=selectedText;
        if(eTextArea.value.length>0)
            newValue=newValue+"; " + eTextArea.value;
        
        if (CheckMaxLengthAndShowMessage(newValue, maxLen))
            eTextArea.value=newValue;
    }
    else if (option == 2 || option == 5)
    {
        var newValue=eTextArea.value;
        if(newValue.length>0)
            newValue=newValue+"; ";
        
        newValue=newValue+selectedText;
        
        if (CheckMaxLengthAndShowMessage(newValue, maxLen))
            eTextArea.value=newValue;
    }
    
    if (resetSelection == "True")
        eList.selectedIndex=0;
}
function CheckMaxLengthAndShowMessage(text,maxLen)
{
    if (maxLen == 0)
        return true;
        
    if (text.length >= maxLen)
    {
        msg = 'Cannot add data because the maximum number '; 
        msg += 'of allowable characters (' + maxLen + ') '; 
        msg += 'will be exceeded.  Your data was not added.  Please check your data.'; 

        alert(msg);
        
        return false;
    }
    
    return true;
}
function CopySelectedItemToTextAreaWithoutSemicolon(listId, txtId, option, maxLen, resetSelection)
{
    //Option==0: Override txtId content w/selected VALUE
    //Option==1: Append selected VALUE to the beginning
    //Option==2: Append selected VALUE to the end
    //Option==3: Override txtId content w/selected TEXT
    //Option==4: Append selected TEXT to the beginning
    //Option==5: Append selected TEXT to the end
    
    if(option<0)
        return;
    if(option>5)
        return;
    var eList=$get(listId);
    if(eList==null)
        return;
    var eTextArea=$get(txtId);
    if(eTextArea==null)
        return;

    var selectedText = "";
    if (option < 3)
        selectedText = eList.options[eList.selectedIndex].value;
    else
        selectedText = eList.options[eList.selectedIndex].text;

    if (selectedText.length == 0 || selectedText == "< N/A >" || selectedText == "< Select ... >" || selectedText == "< Blank >")
        return;

    if ((option == 0 || option == 3) && CheckMaxLengthAndShowMessage(selectedText, maxLen))
        eTextArea.value=selectedText;
    else if (option == 1 || option == 4)
    {
        var newValue=selectedText;
        if(eTextArea.value.length>0)
            newValue=newValue+" " + eTextArea.value;
        
        if (CheckMaxLengthAndShowMessage(newValue, maxLen))
            eTextArea.value=newValue;
    }
    else if (option == 2 || option == 5)
    {
        var newValue=eTextArea.value;
        if(newValue.length>0)
            newValue=newValue+" ";
        
        newValue=newValue+selectedText;
        
        if (CheckMaxLengthAndShowMessage(newValue, maxLen))
            eTextArea.value=newValue;
    }

    if (resetSelection == "True")
        eList.selectedIndex = 0;
}

function getScreenCenterY() 
{   
    return getScrollOffset()+(getInnerHeight()/2);   
}   
  
function getScreenCenterX() 
{   
    return(document.body.clientWidth/2);   
}   
  
function getInnerHeight() 
{   
    var y;   
    if (self.innerHeight)   
        y = self.innerHeight;   
    else if (document.documentElement && document.documentElement.clientHeight)  
        y = document.documentElement.clientHeight;   
    else if (document.body) 
        y = document.body.clientHeight;   
    return y;   
}   
  
function getScrollOffset() 
{   
    var y;   
    if (self.pageYOffset)  
        y = self.pageYOffset;   
    else if (document.documentElement && document.documentElement.scrollTop)   
        y = document.documentElement.scrollTop;   
    else if (document.body)  
        y = document.body.scrollTop;   
    return y;   
}

//  ** NOTHING ** should use this function in the future.  The proper way to do this
//  is with the $get function.  However, there are alot of places calling it and it handles
//  errors (invalid parameters) differently than $get().  Specifically, if 'arguments' are
//  undefined, this function just silently returns null while $get blows up.  This was originally
//  named "$" which conflicted with jQuery so to avoid breaking anything and to fix this conflict,
//  renamed this method to $getControl.  When possible, any callers of this function should be
//  changed to call $get.
$getControl=function()
{
  var elements = new Array();

  for (var i=0;i<arguments.length;i++) 
  {
    var element=arguments[i];
    if (typeof(element)=='string')element=document.getElementById(element);
    if (arguments.length==1)return element;

    elements.push(element);
  }
  return elements;
}

document.FindElements=function(element,tag)
{
    var elementsFound=new Array();
    
    if(tag==undefined || tag==null)
        tag='*';
        
    var elements=document.getElementsByTagName(tag);
    for(var i=0;i<elements.length;i++)
    {
        if(elements[i].id.search(element)>-1)
            elementsFound.push(elements[i]);
    }
    return elementsFound;
}

__getGUID=function()
{
    var hex=function(){return (((1+Math.random())*0x10000)|0).toString(16).substring(1)}
    return (hex()+hex()+hex()+hex()+hex()+hex()+hex()+hex()).toUpperCase();
}

__handle_error=function(err)
{
    if(__show_errors)throw err;
    return err.description;
}


function StyleSheet (style_sheet)
{
    this.Sheet=(style_sheet==null)?null:this.Get(style_sheet);
}

//************************StyleSheet
StyleSheet.prototype.Get=function(style_sheet)
{
    if(style_sheet==null)
    {
        return document.styleSheets[0];
    }
    else
    {
        for (i=0;i<document.styleSheets.length;i++)
        {
            if(document.styleSheets[i].href.search(style_sheet+'.css')>-1)
            {
                return document.styleSheets[i];
            }
        }
    }
}

StyleSheet.prototype.GetAttribute=function(className, attribute)
{
    if(this.Sheet==null)
        return '';
        
    var sheet=this.Sheet;

    try
    {
        for (i=0;i<sheet.rules.length;i++)
        {
            if(sheet.rules[i].selectorText.toLowerCase().indexOf(className.toLowerCase())>-1)
                return eval('sheet.rules[i].style.'+attribute);
        }
    }
    catch(e){}

    return '';
}
//*************************************************************

__addEvent=function(control,js,event_name)
{
    if (typeof (control) == 'string') control = $getControl(control);
        
    var change_handler=null;
    
    switch(event_name)
    {
        case 'onmouseover':
            if(__eventExists(control.onmouseover,js))
                return;
            control.onmouseover=js;
            change_handler = new Function(control.onmouseover); 
            break;
        case 'onmouseout':
            if(__eventExists(control.onmouseout,js))
                return;
            control.onmouseout=js;
            change_handler = new Function(control.onmouseout); 
            break;
        case 'onchange':
            if(__eventExists(control.onchange,js))
                return;
            control.onchange=js;
            change_handler = new Function(control.onchange); 
            break;
        case 'onclick':
            if(__eventExists(control.onclick,js))
                return;
            control.onclick=js;
            change_handler = new Function(control.onclick); 
            break;
    }
    if (control.addEventListener)  
        control.addEventListener(event_name.substr(2),change_handler,false); 
    else if (control.attachEvent)  
        control.attachEvent(event_name,change_handler); 
}

__eventExists=function(currentEventValue, newEventValue)
{
    if(currentEventValue!=null && currentEventValue.indexOf(newEventValue)>-1)
        return true;
    return false;
}

__removeEvent=function(control,js,event_name) 
{ 
  if (control.detachEvent)
  { 
    control.detachEvent(event_name,new Function(js)); 
    eval('control.'+event_name+'=null;'); 
  } 
  else 
    control.removeEventListener(event_name.substr(2),js,false); 
}
 
__clearEvent=function(control,event_name) 
{ 
    var fnc=eval('control.'+event_name);
    if(typeof(fnc)=='string')fnc=new Function(fnc);
    if (control.detachEvent)
    { 
        control.detachEvent(event_name,fnc); 
        eval('control.'+event_name+'=null;'); 
    } 
    else 
        control.removeEventListener(event_name.substr(2),fnc,false); 
}

__replaceEvent=function(control,js,event_name) 
{
    __clearEvent(control,event_name);
    __addEvent(control,js,event_name);
}

function FormatControl(control, required)
{
    if (control != null && control != 'undefined')
    {
        control.innerHTML = control.innerHTML.replace("<FONT color=red>*</FONT> ", "");
        control.innerHTML = control.innerHTML.replace("<FONT COLOR=RED>*</FONT> ", "");

        if (required)
            control.innerHTML = "<FONT color=red>*</FONT> " + control.innerHTML;
    }
}

function findPosX(obj)
{
    return getPosition(obj).x;
}

function findPosY(obj)
{
    return getPosition(obj).y;
}

function getPosition(element)
{
    var left = 0;
    var top = 0;

    if (element != null)
    {
        // Try because sometimes errors on offsetParent after DOM changes.
        try
        {
            while (element.offsetParent)
            {
                // While we haven't got the top element in the DOM hierarchy
                // Add the offsetLeft
                left += element.offsetLeft;
                // If my parent scrolls, then subtract the left scroll position
                if (element.offsetParent.scrollLeft) { left -= element.offsetParent.scrollLeft; }

                // Add the offsetTop
                top += element.offsetTop;
                // If my parent scrolls, then subtract the top scroll position
                if (element.offsetParent.scrollTop) { top -= element.offsetParent.scrollTop; }

                // Grab
                element = element.offsetParent;
            }
        }
        catch (e)
        {
            // Do nothing
        }

        // Add the top element left offset and the windows left scroll and subtract the body's client left position.
        left += element.offsetLeft + document.body.scrollLeft - document.body.clientLeft;

        // Add the top element topoffset and the windows topscroll and subtract the body's client top position.
        top += element.offsetTop + document.body.scrollTop - document.body.clientTop;
    }

    return { x: left, y: top };
}
