// xGrid Helpers

// gets array of elements that have given CSS class
// via http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(oElm, strTagName, oClassNames){
 var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
 var arrReturnElements = new Array();
 var arrRegExpClassNames = new Array();
 if (typeof oClassNames == "object"){
  for (var i = 0; i < oClassNames.length; i++){
   arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
  }
 } else {
  arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
 }
 var oElement;
 var bMatchesAll;
 for (var j=0; j<arrElements.length; j++) {
  oElement = arrElements[j];
  bMatchesAll = true;
 
  for (var k=  0; k < arrRegExpClassNames.length; k++) {
   if (!arrRegExpClassNames[k].test(oElement.className)) {
    bMatchesAll = false;
    break;                      
   }
  }
  
  if (bMatchesAll) {
   arrReturnElements.push(oElement);
  }
 }
 
 return (arrReturnElements)
}

// ---
/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}
// ---
if(typeof Array.prototype.push != "function"){
	Array.prototype.push = ArrayPush;
	function ArrayPush(value){
		this[this.length] = value;
	}
}
// ---


function ToggleSelectAll(chkState)
{
	if(chkState == null)
	{
		chkState = document.getElementById("chkSelectAll").checked;
	}
	lstChks = getElementsByClassName(document,"span","AutoCheck");
	for(i=0; i< lstChks.length; i++ )
	{
		lstSpan = lstChks[i].getElementsByTagName("input");
		lstSpan[0].checked = chkState;
	}
}

///Emails all selected users in the User Grid whom have an e-mail associated with them.
function EmailAllSelected()
{
    var mailToString = "mailto:";
    var emailAddresses = "";    
    lstChks = getElementsByClassName(document,"span","AutoCheck");
    lstEmailBoxes = getElementsByClassName(document,"a","EmailBox");            
    for (i=0; i< lstChks.length; i++)
    {
        lstSpan = lstChks[i].getElementsByTagName("input");
        if(lstSpan[0].checked)
        {    
            if ((emailAddresses == "")&&(lstEmailBoxes[i] != mailToString))                
                    emailAddresses = lstEmailBoxes[i];            
            else            
                if (lstEmailBoxes[i] != mailToString)
                    emailAddresses += ";" + lstEmailBoxes[i];            
        }                
    }
    
    regEx = /mailto:/gi;
    emailAddresses += mailToString
    emailAddresses = emailAddresses.replace(regEx,"");             
    
    if (emailAddresses == "")
    {
        alert("No Valid Email addresses selected");
        return;
    }
        
    window.open(mailToString + emailAddresses);    
}

function GetSelectedCount()
{

    var lstChks = getElementsByClassName(document,"span","AutoCheck");    
    var lstSpan;
    var selectedCount = 0;
    
    for (i=0; i<lstChks.length; i++)
    {
        lstSpan = lstChks[i].getElementsByTagName("input");
        if(lstSpan[0].checked)
        {    
           selectedCount += 1;
        }       
    }
    
    return selectedCount;
}

