var tools = {
	getStringBuilder: function() {
        var data = [];
        var counter = 0;
        return {
            // adds string s to the stringbuilder
            append: function(s) { data[counter++] = s; return this; },
            // removes j elements starting at i, or 1 if j is omitted
            remove: function(i, j) { data.splice(i, j || 1); return this; },
            // inserts string s at i
            insert: function(i, s) { data.splice(i, 0, s); return this; },
            // builds the string
            toString: function(s) { return data.join(s || ""); return this; }
	    };
    }
};

//creating console object to prevent errors when debugging in browsers without console
if (!document.console) {
    document.console = {
        log: function() { }
    };
}


function setRow(row){
	row.style.background = '#e8eaf3';
}

function resetRow(row){
	row.style.background = '#f7f7f7';
}

function adaptColumns(cdivToAdapt,cdivToCompare,difference){
		
  var divToAdapt = document.getElementById(cdivToAdapt);
  var divToCompare = document.getElementById(cdivToCompare);

    if ((divToAdapt)  && (divToAdapt.clientHeight  < divToCompare.clientHeight)){
		//divToAdapt.style.height = (divToCompare.clientHeight-26) + 'px';
		divToAdapt.style.height = (divToCompare.clientHeight-difference) + 'px';
	}
}
		
function minHeight(cdiv, minHeight) {
    var div = document.getElementById(cdiv);
    if (div && div.offsetHeight < minHeight) {
	    div.style.height = minHeight + 'px';
    }
}

function alignHeight(cdiv1, cdiv2) {
    var div1 = document.getElementById(cdiv1);
    var div2 = document.getElementById(cdiv2);

    if (!div1 || !div2) return;

    if (div1.offsetHeight < div2.offsetHeight) {
	    div1.style.height = (div2.offsetHeight - 1) + 'px';
	} else {
	    div2.style.height = (div1.offsetHeight - 1) + 'px';
	}
}
		

// xGetElementById, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetElementById(e)
{
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function escapeString(str)
{
    return escape(str); 
}

function hasCssClass(element,c) 
{
	
	element = xGetElementById(element);
	if (element) 
	{
		var reg = new RegExp('\\b'+c+'\\b', 'i');
		return reg.test(element.className);
	}
	return false;
}

function removeCssClassName(element, cssClass)
{
	element = xGetElementById(element);
	if ( cssClass.length > 0 && element.className.length > 0 )
	{
		var reg = new RegExp('\\b'+cssClass+'\\b', 'ig');
		var str = element.className;
		element.className = str.replace(reg, "");
	}
}

function addCssClassName(element, cssClass) 
{
	element = xGetElementById(element);
	if ( !hasCssClass(element) ) { element.className = (element.className + " " + cssClass);}
}

function toggleCssClassName(element, cssClass)
{
	element = xGetElementById(element);
	if ( hasCssClass(element, cssClass) ) 
		removeCssClassName(element, cssClass);
	else
		addCssClassName(element, cssClass);
}

function togglebox(cDiv){

	element = xGetElementById(cDiv);
	linktext = xGetElementById('divToHide_linktext');
	
	if (hasCssClass(element, 'visible')){
		removeCssClassName(element, 'visible');
		addCssClassName(element, 'hidden');
		linktext.innerHTML = '&raquo; Show filter';
		
	}
	else {
		removeCssClassName(element, 'hidden');
		addCssClassName(element, 'visible');
		linktext.innerHTML = '&raquo; Hide filter';
	}

}



// Opens a window suitable for showing a pdf.
//takes additional parameters which are converted to querystrings, eg. OpenPDFWindow("filepath","CompanyId=8","ReportId=10")
function OpenPDFWindow(pdfPath) 
{
    var sb = tools.getStringBuilder()
                  .append("/Handlers/OpenFile.ashx?File=")
                  .append(pdfPath);
    if(arguments.length > 1) {
        for (var i = 1, j = arguments.length; i < j; i++) {
            sb.append("&").append(arguments[i]);
        }
    }
    var url = sb.toString();
    window.open(url, '_blank', 'width=530,height=750,resizable=yes').focus();
}

// Opens a window suitable for showing help file.
function OpenHelpWindow(url)
{
    window.open(url, 'help', 'width=230,height=300,resizable=yes').focus();
}

// Opens a window suitable for showing a print view of a report. Width and height are optional parameters
function OpenPrintReportWindow(url, width, height) {
    width = typeof width == 'number' ? width : 670;
    height = typeof height == 'number' ? height : 850;
    window.open(url, '_blank', 'width=' + width + ',height='+ height + ',resizable=yes,scrollbars=yes').focus();
}

function OpenReportWindow(url, width, height) { 
    window.open(url, '_blank', 'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes').focus();
}

function expandIt(id) {     
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';
		} else {
			document.getElementById(id).style.display = 'none';			
		}	
	} else { 
		if (document.layers) {	
			if (document.id.display == "none"){
				document.id.display = 'block';
			} else {
				document.id.display = 'none';
			}
		} else {
			if (document.all.id.style.visibility == "none"){
				document.all.id.style.display = 'block';
			} else {
				document.all.id.style.display = 'none';
			}
		}
	}
}

function showIt(whichElementId, ShowTextId, HideTextId)
{
    expandIt(whichElementId);
    setDisplayStyle (HideTextId, 'none');
    setDisplayStyle (ShowTextId, 'block');
}

function hideIt(whichElementId, ShowTextId, HideTextId)
{
	expandIt(whichElementId);
	setDisplayStyle (HideTextId, 'block');
	setDisplayStyle (ShowTextId, 'none');
}

function setDisplayStyle (whichElementId, displayStyle)
{
	if (document.getElementById) // DOM3 = IE5, NS6
	   	document.getElementById(whichElementId).style.display = displayStyle;
	else 
	{ 
		if (document.layers) 
			document.whichElementId.display = displayStyle;
		else
		    document.all.whichElementId.style.display = displayStyle;
	}
}

function popupMailMessage(subject, body) {

  var to = "";
//  var cc = "";
//  var bcc = "";
  
//  var subject = "A Preformatted Email Message";
//  var body = 
//      "Chandler,\n\n\tI'm sorry, I can't make it tonight. " +
//      "I have to rearrange my sock drawer. " +
//      "\n\nSincerely,\n\nMonica"

  // BUILD MAIL MESSAGE COMPONENTS 
  var doc = "mailto:" + to + 
      "?subject=" + escape(subject) + 
      "&body=" + escape(body); 

  // POP UP EMAIL MESSAGE WINDOW
  window.location = doc;
}