
//document.onkeypress = docKeyDown;
//document.onkeyup= docKeyDown;
//document.onkeydown = docKeyDown;

function getObj(id){
	ret=null;
	try{    ret=document.getElementById(id); }catch(e){}
	return ret;
}

function ShowImg(image,id,imageHref,width,height){
    m = getObj(id);
    href = getObj(imageHref);
    href.href = image;
    m.src = image;
    if(width > 235)
        m.width = 235;
     else
        m.width = width;
}

function openfile(id,site, from)
{
	var loc = site + '/Common/OpenFile.aspx?from=' + from + '&id=' + id;
    //w = window.location = loc;
	OpenDocument(loc);
}

function openFileForObject(objID, site, from, entityID, fieldID)
{
	var loc = site + '/Common/OpenFile.aspx?from=' + from + '&id=' + objID + '&entID=' + entityID + '&fieldID=' + fieldID;
	var uagent = (navigator.userAgent) ? navigator.userAgent.toLowerCase() : '';
	if(uagent.indexOf('linux') > -1){
		w = window.location = loc;
	}
	else{
		OpenDocument(loc);
	}
}

function OpenDocument(site){
	var iframeId = "DownloadPanel";
	if(!jQuery('#' + iframeId)[0]){
		jQuery("body").append('<iframe id="' + iframeId + '" style="display:none;" />');
	}
	jQuery('#' + iframeId).attr('src', site);
}

function printResults(site){
    w = window.open(site + '/Common/PrintMembers.aspx?all=0', null, 'resizable=no,scrollbars=yes status=no, width=650, height=800, left=10, top=10');
}

function printAll(site){
    w = window.open(site + '/Common/PrintMembers.aspx?all=1', null, 'resizable=no, scrollbars=yes, status=no, width=650, height=800, left=10, top=10');
}

function printOne(site, id){
    w = window.open(site + '/Common/Print.aspx?all=0&NID='+id+'', null, 'resizable=no,scrollbars=yes status=no, width=650, height=800, left=10, top=10');
}

function printMany(site,fv1,fv2,fv3){
    w = window.open(site + '/Common/Print.aspx?all=1&fv1='+fv1+'&fv2='+fv2+'&fv3='+fv3+'', null, 'resizable=no, scrollbars=yes, status=no, width=650, height=800, left=10, top=10');
}

function setFocus(id){
    if(getObj(id))
        getObj(id).focus();
}

function docKeyDown(e)
{
    if (!e) 
        var e = window.event
    
    if (e.target) 
        targ = e.target
    else if (e.srcElement) 
        targ = e.srcElement
    
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode
    
    
    if(targ.tagName != "INPUT" && targ.tagName != "TEXTAREA")
        return false;
}

function keyUp(e, id)
{
    try { 
        k = event.keyCode   // ie
    }catch(e){}
    
    try { 
        k = e.keyCode       // mozilla
    }catch(e){}
    
    if(k == 13)
    {
        setFocus(id);
        getObj(id).click();
    }
}

function searchClick(e, id){
    if(getObj(id).value == "Search...")
        getObj(id).value = ""; 
}

function searchOut(e, id){
    if(getObj(id).value == "")
        getObj(id).value = "Search..."
}

function showDownloadPanel(id,displ)
{
    setTimeout("viewPanel("+id.id+")",10);
}

function viewPanel(id)
{
    id.style.display="block";
}

function hidePanel(id)
{
    id.style.display="none";
}

function redirectIBOX(id)
{
    setTimeout("getObj('"+id+"').click();",1000);
}

// seagull used this
function StretchLast(toStretch)
{
	for(var controlNumber in toStretch)
	{
		var stretchToControl = getObj(toStretch[controlNumber]);
		if(stretchToControl != null)
		{
			
			var availableSize = stretchToControl.offsetHeight;
			var floatAvailableSize = 0;
			var toStrechControlID = "";
			var toStrechControlOffset = 0;
			var floatControlsIDToStretch = new Array();
			
			
			//check if are controls inside to stretch. Takes only last
			for(var childControlsNumber in stretchToControl.childNodes)
			{
				var childElement = stretchToControl.childNodes[childControlsNumber];
				var childElementheight = childElement.offsetHeight;
				var setFloat = false;
				var cssFloat = null;
				
				if(!isNaN(childElementheight) && childElementheight != undefined){
					
					if( window.getComputedStyle ) {
						cssFloat = window.getComputedStyle(childElement,null).cssFloat;
					} else if( childElement.currentStyle ) {
						cssFloat = childElement.currentStyle.styleFloat;
					}
					if(cssFloat != "left" && cssFloat != "tight"){
						availableSize = availableSize - childElementheight - 4;
					} else {
						if(childElementheight > floatAvailableSize){
							floatAvailableSize = availableSize - childElementheight - 4;
							setFloat = true;
							if(floatAvailableSize > 0){
								floatControlsIDToStretch[childControlsNumber] = childElement.id;
							}
						}
					}
					
				}
				
				//check if last
				if( childElement.id !== undefined && childElement.id != "" && getObj(childElement.id + "ToStretch") != null && getObj(childElement.id + "ToStretchOffset") != null && !setFloat){
					try{
						toStrechControlID = getObj(childElement.id + "ToStretch").value;
						toStrechControlOffset = getObj(childElement.id + "ToStretchOffset").value;
						if(stretchToControl.lastElementChild != undefined && childElement.id == stretchToControl.lastElementChild.id){
							break;
						}
					}
					catch(e){}
				} 
			}
			
			//stretch control
			if(toStrechControlID != "" && availableSize > 0 && floatControlsIDToStretch.length < 1){
				var toStrechControl = getObj(toStrechControlID);
				toStrechControl.style.height = (toStrechControl.offsetHeight + availableSize - toStrechControlOffset) + "px";
			} else if (floatControlsIDToStretch.length > 0 && floatAvailableSize > 0)
				{
					for(floatControlIDToStretch in floatControlsIDToStretch){
						var childElementID = floatControlsIDToStretch[floatControlIDToStretch];
						if(childElement != null){
							try{
								toStrechControlID = getObj(childElementID + "ToStretch").value;
								toStrechControlOffset = getObj(childElementID + "ToStretchOffset").value;
								var toStrechControl = getObj(toStrechControlID);
								toStrechControl.style.height = (toStrechControl.offsetHeight + floatAvailableSize - toStrechControlOffset) + "px";
							}
							catch(e){}
						}
					}
				}
		}
	}
}

function SetGridSelection(selectionHolderID, selectedHolderID, add){
	var selectionHolder = getObj(selectionHolderID);
	var selectedHolder = getObj(selectedHolderID);
	if(selectionHolder != null && selectedHolder != null){
		if(add && selectionHolder.value.search("#" + selectedHolder.value + "#") < 0){
			selectionHolder.value = selectionHolder.value + "#" + selectedHolder.value + "#";
		} else if (selectionHolder.value.search("#" + selectedHolder.value + "#") > -1){
			selectionHolder.value = selectionHolder.value.replace("#" + selectedHolder.value + "#","")
		}
	}
}

function SetGridSelectionSelectAll(gridID, selectionHolderID, selectionControlID){
	if(!selectionControlID){
		selectionControlID = '_selectAll_Ch';
	}
	var add = !jQuery('input[id$=' + selectionControlID + ']').attr('checked');
	jQuery('input[id$=' + selectionControlID + ']').attr('checked', add);
	SetGridSelectAll();
	var grid = getObj(gridID);
	var selectionHolder = getObj(selectionHolderID);
	
	if(grid != null && selectionHolder != null){
		selectionHolder.value = "";
		var checkboxes = grid.getElementsByTagName('input');
		for(checkboxID in checkboxes){
			checkboxes[checkboxID].checked = add;
			if(add && checkboxes[checkboxID].type == "hidden"){
				selectionHolder.value = selectionHolder.value + "#" + checkboxes[checkboxID].value + "#";
			}
		}
	}		
}

$(document).ready(function(){
	SetGridSelectAll();
});

function SetGridSelectAll(){
	if(jQuery('input[id$=_selectAll_Ch]')[0]){
		var add = jQuery('input[id$=_selectAll_Ch]').attr('checked');
		if(add){
			jQuery('img[id$=_selectAll]').attr('src','/CC/Images/16_clear_all.gif')
		}
		else{
			jQuery('img[id$=_selectAll]').attr('src','/CC/Images/16_select_all.gif')
		}
	}
}
