﻿ 
    var debug2 = true;
    var DebugElement=null;
    var DebugContexts="";
    var DebugIgnoreContexts="";
    
    function GetParentElement(startingElement, partialName)
    {
        if (IsMatch(startingElement,partialName))
        {
            return startingElement;
        }
        
        if (startingElement.parentNode!=null)
        {
            return GetParentElement(startingElement.parentNode, partialName);
        } 
        else 
        {
            return null;
        }
    }

    function GetRelativeControl(partialName, obj)
    {
       var currentControl = obj;
       var parentControl = GetParentElement(currentControl, "tdRatingCell");
       
       // Check current control
       if (IsMatch(parentControl,partialName))
       {
            return currentControl;
       }
       
       // Check sibling controls
       for(var i=0; i<parentControl.childNodes.length; i++)
       {
           var control = parentControl.childNodes[i];
           
           if (IsMatch(control,partialName))
            return control;
       }
  
       return null;
    }
    
    function IsMatch(element, partialName)
    {
        if (element.id ==null || element.id=='')
            return false;
            
        if (element.id.indexOf(partialName,0)>=0)
            return true;
        
        return false;
    }
    
    function ToggleVisibility(obj,controlName)
    {
        var control = document.getElementById(controlName);
        
        if (control==null)
            return;
           

        if (control.style.display=="none")
            control.style.display="";
        else
            control.style.display="none";
    }
    
    function Test(obj)
    {
        alert("Test function in global.js  " + obj);
        
        
        alert("Document = " + document);
        alert("Document.all = " + document.getElementById(obj));
     }
    
    function ToggleVisibilityWithImage(obj, controlName,expandImage,contractImage)
    {
        var img = obj;
        var control = document.getElementById(controlName);
        
        if (control==null || img==null)
            return;
        
        if (control.style.display=="none")
        {
            control.style.display="";
            img.src = contractImage;
        }
        else
        {
            control.style.display="none";
            img.src= expandImage;
        }
    }
    
    function ToggleVisibilityWithText(obj, controlName,expandText,contractText)
    {
        var img = obj;
        var control = document.getElementById(controlName);
        
        if (control==null || img==null)
            return;
        
        if (control.style.display=="none")
        {
            control.style.display="";
            obj.innerHTML = contractText;
        }
        else
        {
            control.style.display="none";
            obj.innerHTML = expandText;
        }
    }
    
    function CloseWindow(retVal)
    {
        DisplayDebugMessage("Close Window RetVal=" + retVal,"CloseWindow");
               
        window.top.returnValue=retVal;
        
        window.top.close();
    }
    
    function RemoveNavigationMenu()
    {
        if (window.opener==null)
        {
            var Border=30;
            var nTop=5;
            var nLeft= 5;
            var nWidth=window.screen.width-20;
            var nHeight=window.screen.availHeight-50;

            var options = "titlebar=no,statusbar=yes,menu=false,scroll=yes,resizable=yes,width=" + nWidth + ",height=" + nHeight + ",left=" + nLeft + ",top=" + nTop;
            
            window.open(location.href,"main",options);
            
            window.opener=self;
            window.top.close(); 
        }
    }

  
    function AddLoadEvent(func) 
    {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') 
        {
            window.onload = func;
        } 
        else 
        {
            window.onload = function() 
            {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    }
    
    function SetupDebugging()
    {
        DebugElement = document.createElement("<TextArea Id='DebugText' tabIndex='99999' rows='20' cols='120' name='Debugtext'></TextArea>");
        
        document.body.insertBefore(DebugElement);
        
        DebugElement.value = "Debuging has been setup!";
    }
        
    function DisplayDebugMessage(message, context)
    {
         //alert(context + " - " + message);
         
         if (!isValidContext(context))
            return false;
         
         if (DebugElement!=null)
         {
             var NewText = DebugElement.value + "\n" + context + " - " + message
                    
             DebugElement.value = NewText
         }
    }
    
    function isValidContext(context)
    {
        if ((DebugContexts=="") && (DebugIgnoreContexts==""))
            return true;
            
        if (context == "undefined")
            return false;
            
        // Determine if context should be ignored
        if (DebugIgnoreContexts!="")
        {
            var list = DebugIgnoreContexts.split(",");
        
            for(var j=0;j<list.length;j++)
            {
                if (context.indexOf(list[j])>-1)
                    return false;
            }
        }            
            
        // Determine if context should be valid
        if (DebugContexts!="")
        {    
            var list2 = DebugContexts.split(",");
        
            for(var k=0;k<list2.length;k++)
            {
                if (context.indexOf(list2[k])>-1)
                    return true;
            }
            
            return false;
        }
        
        return true;
    }
    
        
    function limittoNumeric()
    {
      var keyCode,Shift;
      
      var insideFrames = (window.top.event==null)
      var object = window.event.srcElement;
      
      if (object==null)
      {
         return;
      }
      
      if (insideFrames) {
        keyCode= window.event.keyCode;
        Shift = window.event.shiftKey;
      } else {
        keyCode= window.top.event.keyCode;
        Shift = window.top.event.shiftKey;
      }
      
      var ShiftOffset = 65536;
      
      if (Shift) {
        keyCode += ShiftOffset;
      }
      
      var numeric = (((keyCode-48 >=0) && (keyCode-48<=9)) || ((keyCode-96 >=0) && (keyCode-96<=9)))
      
      var otherkeys = "[8][9][65545][13]"	 
      
      if (String(object.value).search("\\.")==-1) {
         otherkeys += "[190][110]"
      }
      
      for (var i =33; i<=46; i++) {
        otherkeys += "[" + i + "][" + (i + ShiftOffset) + "]" 
      }
      
      var other = (otherkeys.search("\\[" + keyCode + "\\]")!=-1)
      
      if (!(numeric) && !(other)) {
        window.event.returnValue=false;
      }	
    }

    function Highlight(obj)
    {
         var object = obj?obj:window.event.srcElement;
         
         if (object==null)
              return;
      
         try
         {
            object.select();
         } catch(e)
         {
         }
    }
    
    function RefreshPage()
    {
        location.href = location.href
    }

	//this function is used to open a Child Window
	function OpenChildWindow(url, modal, AllowResize, Width, Height, Name, CancelReturnValue, showToolbar, showLocationbar,showScrollBar) {
		modal = (modal == null) ? false : modal;
		var nWidth = (Width == null) ? self.document.body.clientWidth * 0.85 + "px" : Width;			
		var nHeight = (Height == null) ? self.document.body.clientHeight * 0.85 + "px" : Height;
		
		AllowResize = (AllowResize == null) ? true : AllowResize;
		
		var strresizable = (AllowResize == true) ? "yes" : "no";
		var strToolbar = (showToolbar == true) ? "yes" : "no";
		var strLocationBar = (showLocationbar == true) ? "yes" : "no";
		var strScrollBar = (showScrollBar == true) ? "yes" : "no";
					    
		if (modal==true){
		    DisplayDebugMessage("Open url " + url,"OpenChildWindow");
            
            var result = window.showModalDialog(url,null,"center:yes;help:no;resizable:" + strresizable + ";scroll:yes;status:no;dialogHeight:" + nHeight + "px;dialogWidth:" + nWidth + "px;");
            
            if ((result==undefined)||(result=="undefined"))
                result = CancelReturnValue;
                
            DisplayDebugMessage("Result " + result,"OpenChildWindow");
                
            return result;
		}else{			
			//center the new window on the existing window
			var nLeft = self.screenLeft + ((self.document.body.clientWidth / 2) - (parseInt(nWidth) / 2));
			var nTop = self.screenTop + ((self.document.body.clientHeight /2) - (parseInt(nHeight) / 2));
		
			var win = window.open(url,Name,"titlebar=" + strToolbar + ",statusbar=yes,location=" + strLocationBar + ",toolbar=yes,scrollbars=" + strScrollBar + ",resizable=" + strresizable + ",width=" + nWidth + ",height=" + nHeight + ",left=" + nLeft + ",top=" + nTop);
			
			return win
		}			
	}   	

    
    
