﻿//Find a document element by tag type and id
//param:tagname - input or span
//param:realName - the id of the asp control
//Author: Slavena Savova
//Date: 12/11/2006
function getName(tagType, realName) {    
	inputArray = document.body.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if (inputArray[i].id.toString().indexOf(realName)!= -1) {
		  return inputArray[i].id;
		}
	}
}

//Get return object with tag tagType and id that contains realName
//that is child to the parentEl
function getChildObjectByName(parentEl,tagType, realName) 
{    	
	var inputArray = parentEl.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if (inputArray[i].id.toString().indexOf(realName)!= -1) {		
		  return inputArray[i];
		}
	}
	return null;
}

//This method change the style of the button(input type="submit"), because hover effect doesn't work in IE6.
//param:object - input objects that call this method
//param: action - input action:hover, when mouseover and out when mouse out
//Author: Slavena Savova
//Date: 09/10/2007
function ChangeButtonClass(object, action)
{
    if(action == "hover")
    {
        object.className = "buttonHover";
    }
    else if(action == "out")
    {
        object.className = "button";
    }    
}

//This function checkes or unchecks all the checkboxes in GridView
//param:selectAllId - the id of select all checkbox
//Author: Slavena Savova
//Date: 12/11/2006
function CheckUncheckAll(selectAllId) {
    var realId = getName("input", selectAllId);
    inputArray = document.body.getElementsByTagName("input");		        	   
    for (var i=0; i< inputArray.length; i++) {			       
       if((inputArray[i].type == 'checkbox') && (inputArray[i].id != realId))
       {
           inputArray[i].checked = document.getElementById(realId).checked;
       }				    
    }		    
}

//This function uncheck the selectallcheckbox if you check on some checkbox in GridView
//param: selectAllId - the id of select all checkbox
//Author: Slavena Savova
//Date: 12/11/2006
function UpdateState(selectAllId, itemId) {
    var realId = getName("input", selectAllId);
    document.getElementById(realId).checked = false;
    
    //Check if every checkbox in datagrid is checked
    var allChecked = true;
    inputArray = document.body.getElementsByTagName("input");		        	   
    for (var i=0; i< inputArray.length; i++) {			       
       if((inputArray[i].type == 'checkbox') && (inputArray[i].id != realId) && (inputArray[i].id.indexOf(itemId) != -1))
       {
           if(!inputArray[i].checked)
           {
                allChecked = false;
                break;
           }
       }				    
    }
    if(allChecked)
    {
         document.getElementById(realId).checked = true;
    }		
}

//This function checks if there is selected checkbox and return false if there is not.
//param: selectAllId - the id of select all checkbox
//param: itemId - the id of the checkbox we checked
//Author: Slavena Savova
//Date: 03/16/2006
function StateCheck(selectAllId, itemId) {
    var realId = getName("input", selectAllId);
   
    //Check if every checkbox in datagrid is checked
    var anyChecked = false;
    inputArray = document.body.getElementsByTagName("input");		        	   
    for (var i=0; i< inputArray.length; i++) {			       
       if((inputArray[i].type == 'checkbox') && (inputArray[i].id != realId) && (inputArray[i].id.indexOf(itemId) != -1))
       {
           if(inputArray[i].checked)
           {
                anyChecked = true;
                break;
           }
       }				    
    }
    //If there is selected
    if(anyChecked)
    {
         return true;
    }
    else
    {
        alert ('No items selected');   
        return false; 
    }	
}
    
//This function check whether there are selection and if there any selection display confirm message for deletion
//Author: Nevena Uzunova
//Date: 01/25/2006
//Update: 02/08/2007 - Mesage as parameter
function confirmMessage(checkboxName, message)
{
   return CheckSelectionsAndDisplayConfirmMessage(checkboxName,'No items selected',message)
    
//    inputArray = document.body.getElementsByTagName("input");		        	   
//    for (var i=0; i< inputArray.length; i++) {			       
//       if((inputArray[i].type == 'checkbox') && (inputArray[i].id.indexOf(checkboxName)))
//       {
//           if (inputArray[i].checked)
//                return confirm(message);
//       }			    
//    }        
//      
//    alert ('No items selected');   
//    return false; 	  
}

//Author: Nevena Uzunova
//Date 15/05/2007
function CheckSelectionsAndDisplayConfirmMessage(checkboxName, noSelectionsMessage,confirmMessage)
{
    inputArray = document.body.getElementsByTagName("input");		        	   
    for (var i=0; i< inputArray.length; i++) {			       
       if((inputArray[i].type == 'checkbox') && (inputArray[i].id.indexOf(checkboxName) != -1 ))
       {
           if (inputArray[i].checked)
                return confirm(confirmMessage);
       }			    
    }        
      
    alert (noSelectionsMessage);   
    return false; 	
}

//Clear selected items in ListBoxes
function ListboxClear(listbox)
{
    for (i= 0; i<listbox.length; i++)
    {
        if (listbox.multiple==true) {
            if (listbox[i].selected==true) {
                listbox[i].selected=false;           
            }
        }
    }    
}

//This function check ot uncheck all list of checkboxes
//param: targetId - the Id of CheckBoxList
//param: checkAllId - the Id of the Check All Id
//author: Slavena Savova
//date: 02/27/2007
function CheckUncheckAllCheckboxes(targetId, checkAllId)
{            
    inputArray = document.body.getElementsByTagName("input");
        	   
    for (var i=0; i< inputArray.length; i++) {			       
        if((inputArray[i].type == 'checkbox') && (inputArray[i].id.toString().indexOf(targetId)!= -1))
        {                   
           inputArray[i].checked = document.getElementById(getName("input", checkAllId)).checked;                 
        }				    
    }	     
}

//This function sets the default button of the page,
//i.e. if the "Enter" is clicked which button to be clicked from the page.
//param: btn - object
//author: Slavena Savova
//date: 07/17/2007
function SetDefaultButton(btn,keyEvent)
{
    if(btn!= null) 
    {
     if (document.all)    
     {
          if (keyEvent.srcElement.tagName=='TEXTAREA')
            return;
          if (keyEvent.keyCode == 13)
          {
           keyEvent.returnValue=false;
           keyEvent.cancel = true;
           btn.click();
          }
     }
     else if (document.getElementById)
             {
              if (keyEvent.target.tagName=='TEXTAREA')
                return;
              if (keyEvent.which == 13)
              {
               keyEvent.returnValue=false;
               keyEvent.cancel = true;
               btn.click();
              }
            }
    else if(document.layers)
            {
              if (keyEvent.target.tagName=='TEXTAREA')
                return;
              if(keyEvent.which == 13)
              {
               keyEvent.returnValue=false;
               keyEvent.cancel = true;
               btn.click();
              }
            }
   }    
}

//This function cancel the event of pressing the "Enter".
//author: Slavena Savova
//date: 07/18/2007
function CancelEnter(keyEvent)
{
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        if(  ((keyEvent.keyCode == 13) || (keyEvent.which == 13))
            && (keyEvent.srcElement != null)
            && (keyEvent.srcElement.tagName!='TEXTAREA')
          )
         {           
            keyEvent.cancel = true;
            keyEvent.cancelBubble=true;
            keyEvent.returnValue = false;
         }        
    } 
    else 
    {
       if(  ((keyEvent.keyCode == 13) || (keyEvent.which == 13))
            && (keyEvent.target != null)
            && (keyEvent.target.tagName!='TEXTAREA')
          )
         {
            keyEvent.preventDefault();
            keyEvent.stopPropagation();           
         }
    }
  
}

//This function shows/hides info tooltips
//author: Mario Berberyan
//date: 09/24/2008
function toggleDiv(e,id,flagit) 
{
    e=e || window.event;
    
    if (flagit=="1")
    {
        var posTop=0;
        var posLeft=0;
        if(e.clientY+document.getElementById(''+id+'').clientHeight>document.documentElement.clientHeight)
        {
            posTop=document.documentElement.clientHeight - document.getElementById(''+id+'').clientHeight+document.documentElement.scrollTop-10+"px";
        }
        else            
        {
            posTop=e.clientY+document.documentElement.scrollTop+20+"px";
        }
        
        if(e.clientX+document.getElementById(''+id+'').clientWidth>document.documentElement.clientWidth)
        {
            posLeft=e.clientX-document.getElementById(''+id+'').clientWidth+document.documentElement.scrollLeft-20+"px";
        }
        else
        {
            posLeft=e.clientX+10+document.documentElement.scrollLeft+"px";
        }
            
        if (document.all) 
        {                        
            document.all[''+id+''].style.visibility = "visible"
            document.all[''+id+''].style.top =posTop;
            document.all[''+id+''].style.left=posLeft;
        }
        else if (document.getElementById) 
        {                                    
            document.getElementById(''+id+'').style.visibility = "visible"
            document.getElementById(''+id+'').style.top=posTop;
            document.getElementById(''+id+'').style.left=posLeft;
        }
    }
    else if (flagit=="0")
    {
        if (document.all) document.all[''+id+''].style.visibility = "hidden"
        else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
    }
}
