﻿//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) {
    var inputArray = document.body.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if (inputArray[i].id.toString().indexOf(realName)!= -1) {
		  return inputArray[i].id;
		}
	}
}

//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 getNameInParent(tagType, realName) {
    var inputArray = parent.document.body.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if (inputArray[i].id.toString().indexOf(realName)!= -1) {
		  return inputArray[i].id;
		}
	}
}

function getObjectByName(tagType, realName) 
{    
	var inputArray = document.body.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if ((inputArray[i].id.toString().indexOf(realName)!= -1) && (inputArray[i].id.toString().length  == inputArray[i].id.toString().indexOf(realName) + realName.toString().length)) {		
		  return inputArray[i];
		}
	}
	return null;
}

// Returns an array of elements from a certain Tag and Id
// Param: tagName - tag name of the element
// Param: elementId - Id of the element
// Author: Yavor Ivanov
// Date: 04/04/2008   
function getElementsByTagNameAndId(tagName,elementId)
{
    var resArray=new Array();
    
    var elementArray=document.getElementsByTagName(tagName);
    for(var j=0;j<elementArray.length;j++)
    {
        if (elementArray[j].id.indexOf(elementId)!= -1)
            resArray.push(elementArray[j]);
    }
    
    return resArray;
}

// Returns an array of elements from a certain Tag and Id
// Param: tagName - tag name of the element
// Param: elementId - Id of the element
// Author: Yavor Ivanov
// Date: 04/04/2008   
function getChildElementsByTagNameAndId(parentEl,tagName,elementId)
{
    var resArray=new Array();
    
    var elementArray=parentEl.getElementsByTagName(tagName);
    for(var j=0;j<elementArray.length;j++)
    {
        if (elementArray[j].id.indexOf(elementId)!= -1)
            resArray.push(elementArray[j]);
    }
    
    return resArray;
}

//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
//params: normalClassName - class of the button
//params: hoverClassName - hover class of the button
//Author: Slavena Savova
//Date: 09/10/2007
function ChangeButtonClass(object, action, normalClassName, hoverClassName)
{
    if(action == "hover")
    {
        object.className = hoverClassName;
    }
    else if(action == "out")
    {
        object.className = normalClassName;
    }    
}

//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);
    var 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;
    var 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;
    var 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)
{
    var 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)
{            
    var 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.srcElement.tagName!='EMBED')
          )
         {           
            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.target.tagName!='EMBED')
          )
         {            
            keyEvent.preventDefault();
            keyEvent.stopPropagation();           
         }
    }
}

//This method gets the distance between the leftmost part of the browser 
//and the left side of the element
//param:obj - the element to find the distance for
//Author: Mario Berberyan
//Date: 02/11/2008
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

//This method gets the distance between the toptmost part of the browser 
//and the top side of the element
//param:obj - the element to find the distance for
//Author: Mario Berberyan
//Date: 02/11/2008
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

// This function returns the first next sibling element from a certain tag name
// Param: startObject - 
// Param: tagName - 
// Author: Yavor Ivanov
// Date: 03/28/2008 
function GetFirstNextSibling(startObject, tagName)
{
	if (startObject == null)
		return null;
	if (startObject.tagName == tagName)
		return startObject;
	else
		return GetFirstNextSibling(startObject.nextSibling, tagName);
}

// This function returns the first next sibling element from a certain tag name ,and with a certain id
// Param: startObject - The first sibling from which we start
// Param: tagName - Tag name of the search object
// Param: Id - Id of the searched object
// Author: Yavor Ivanov
// Date: 04/19/2008 
function GetFirstNextSiblingById(startObject,tagName,objectId)
{
	if (startObject == null)
		return null;
	if ((startObject.tagName == tagName)&&(startObject.id.toString().indexOf(objectId)!= -1))
		return startObject;
	else
		return GetFirstNextSiblingById(startObject.nextSibling,tagName,objectId);
}

// This function returns the first previous sibling element from a certain tag name
// Param: startObject - 
// Param: tagName - 
// Author: Yavor Ivanov
// Date: 04/01/2008 
function GetFirstPreviousSibling(startObject, tagName)
{
	if (startObject == null)
		return null;
	if (startObject.tagName == tagName)
		return startObject;
	else
		return GetFirstNextSibling(startObject.previousSibling, tagName);
}


// This function returns the first parent element for a certain tag name
// Param: startObject - The object from which we start to search
// Param: tagName - Tag name of the needed object
// Author: Yavor Ivanov
// Date: 03/28/2008 
function GetParent(startObject, tagName)
{
	if (startObject == null)
		return null;
	if (startObject.tagName == tagName)
		return startObject;
	else
		return GetParent(startObject.parentNode, tagName);
}

// This function returns the first parent element for a certain tag name and id
// Param: startObject - The object from which we start to search
// Param: tagName - Tag name of the needed object
// Param: objectId - Id of the needed object
// Author: Yavor Ivanov
// Date: 03/28/2008 
function GetParentById(startObject,tagName,objectId)
{
	if (startObject == null)
		return null;
	if ((startObject.tagName == tagName)&&(startObject.id!=null)&&(startObject.id.indexOf(objectId)!= -1))
	{	  
		return startObject;		
    }
	else
		return GetParentById(startObject.parentNode,tagName,objectId);
}


//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 getRealName(tagType, realName) {
    var inputArray = document.body.getElementsByTagName(tagType);
	for (i = 0; i < inputArray.length; i++) {
		if (inputArray[i].id.toString().indexOf(realName)!= -1) {
		  return inputArray[i].id;
		}
	}
}

//Generates random string identifier
//Author: Boyan Georgiev
//Date: 05/13/2008	
function GenerateRandomString() 
{
   var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
   var string_length = 40;
   var randomstring = '';
   for (var i=0; i<string_length; i++) 
   {
               var rnum = Math.floor(Math.random() * chars.length);
               randomstring += chars.substring(rnum,rnum+1);
   }
   return randomstring;
}                                  

//Encodes string from characters that are not suiteble for JavaScript and HTML
//Quote(') is replaced with  &apos;(taken from the XHTML Character Entity Reference)
//Author: Javor Ivanov
//Date: 05/14/2008	
function EncodeString(stringToEscape)
{   
   var singleQuote=/\'/g;
   var quote=/\"/g;
   var lt=/</g;
   var gt=/>/g;
   var backSlash=/\\/g;
   var ampersant=/&/g;
   
   var singleQuoteReplaced="&apos;";      
   var quoteReplaced="&quot;";
   var ltReplaced="&lt;";
   var gtReplaced="&gt;";
   var backSlashReplaced="&#92;";
   var ampersantReplaced="&amp;";
    
   var encodedString=stringToEscape.replace(ampersant,ampersantReplaced);
   encodedString=encodedString.replace(singleQuote,singleQuoteReplaced);
   encodedString=encodedString.replace(quote,quoteReplaced);
   encodedString=encodedString.replace(lt,ltReplaced);
   encodedString=encodedString.replace(gt,gtReplaced);
   encodedString=encodedString.replace(backSlash,backSlashReplaced);   
   
   return  encodedString;
}

//Decodes strings from characters that are not suiteble for JavaScript and HTML
//&apos; is replaced with quote(')  (taken from the XHTML Character Entity Reference)
//Author: Javor Ivanov
//Date: 05/14/2008	
function DecodeString(stringToDecode)
{
   var singleQuote="\'";
   var quote='\"';
   var lt="<";
   var gt=">";
   var backSlash="\\";
   var ampersant="&";
   
   var singleQuoteReplaced=/&apos;/g;      
   var quoteReplaced=/&quot;/g;
   var ltReplaced=/&lt;/g;
   var gtReplaced=/&gt;/g;
   var backSlashReplaced=/&#92;/g;
   var ampersantReplaced=/&amp;/g;
    
   var decodedString=stringToDecode.replace(ampersantReplaced,ampersant);
   decodedString=decodedString.replace(singleQuoteReplaced,singleQuote);
   decodedString=decodedString.replace(quoteReplaced,quote);
   decodedString=decodedString.replace(ltReplaced,lt);
   decodedString=decodedString.replace(gtReplaced,gt);
   decodedString=decodedString.replace(backSlashReplaced,backSlash);   
   
   return decodedString;
   
}

//Thi method clear the selection of the radio button list or checkbox list.
//Author: Slavena Savova
//Date: 06/12/2008	
function ClearButtons(listName)
{ 
    var arr = document.getElementsByTagName("input");
    for (var i=0; i < arr.length; i++) 
    {
        if((arr[i].id.indexOf(listName) != -1) && (arr[i].checked == true))
        {
            arr[i].checked = false;            
        }
    } 
} 

// Checks if the version of the flash must be updated 
// Param: swfObject - SWFObject of the flash
// Author: Yavor Ivanov
// Date: 05/12/2008 
function CheckIfMustUpdateFlashPlayer(swfObject)
{        
    var playerVersion = swfObject.installedVer; 
    var mustUpdateFlashPlayer = false;
    //alert(playerVersion.major);
    //alert(playerVersion.minor);  
    //alert(playerVersion.rev);    
    if (playerVersion.major < 9)
           mustUpdateFlashPlayer = true;
    else
    if (playerVersion.major == 9 && playerVersion.minor == 0 && playerVersion.rev <= 115)
           mustUpdateFlashPlayer = true;
    
    return mustUpdateFlashPlayer;                      
}
