function setPageSize(){
var pintHeight;
if (self.innerHeight) // all except Explorer
{
	pintHeight=(self.innerHeight-241);
	document.getElementById('tls_main').style.height=pintHeight+'px';	
	
}
else if (document.documentElement && document.documentElement.clientHeight)	// Explorer 6 Strict Mode
{
	pintHeight=(document.documentElement.clientHeight-240);
	document.getElementById('tls_main').style.height=pintHeight;
}
else if (document.body) // other Explorers
{
	pintHeight=(document.body.clientHeight-240);
	document.getElementById('tls_main').style.height=pintHeight;
}
}

function setFocus(){
 var flag=false;
 for(z=0;z<document.forms.length;z++){
  var form = document.forms[z];
  var elements = form.elements;
  for (var i=0;i<elements.length;i++){
    var element = elements[i];
    if((element.type == 'textarea' || element.type == 'text') &&
      !element.readOnly &&
      !element.disabled){ 
      element.focus();
	  flag=true;
     break;
    }
  }
  if(flag)break;
 }
}

function comsOpenPopupSize(pstrURL, pstrWindowName, pintWidth, pintHeight, pstrResize, pstrScroll, pstrMenuBar, pstrToolBar, pstrLocation) {
	var pobjWindow, pintTop, pintLeft, pstrNNResize="", pstrNNScroll="";
	
	pintTop = (screen.availHeight/2-(pintHeight/2));
	pintLeft = (screen.availWidth/2-(pintWidth/2));

	if (pintWidth>screen.availWidth*0.95) {
		pintWidth=screen.availWidth*0.95;
	}
	if (pintHeight>screen.availHeight*0.95) {
		pintHeight=screen.availHeight*0.95;
	}

	if (pstrResize == 'yes'){
		pstrNNResize = 'resizable';
	}
	else {
		pstrNNResize = '';
	}

	if (pstrScroll == 'yes'){
		pstrNNScroll = 'scrollable';
	}
	else {
		pstrNNScroll = '';
	}
	
	if (navigator.appName == 'Microsoft Internet Explorer') {
		pobjWindow = window.open(pstrURL, pstrWindowName, "directories=yes, location="+pstrLocation+", menubar="+pstrMenuBar+", resizable="+ pstrResize +", scrollbars="+ pstrScroll +", status=yes, toolbar="+pstrToolBar+", height="+pintHeight+", width="+pintWidth+",top="+pintTop+",left="+pintLeft);
	}	
	else {
		pobjWindow = window.open(pstrURL,pstrWindowName,pstrNNResize+","+pstrNNScroll+",height="+pintHeight+", width="+pintWidth);
	}
	pobjWindow.focus();
}


function getOffsetTop(obj) {
  if (obj.offsetParent) {
    return obj.offsetTop + getOffsetTop(obj.offsetParent);
  }  
  return obj.offsetTop;
}
function getOffsetLeft(obj) {
  if (obj.offsetParent) {
    return obj.offsetLeft + getOffsetLeft(obj.offsetParent);
  }  
  return obj.offsetLeft;
}

function repositionIfOffPage(objContainer, objItem) {

  var objectXPos = getOffsetLeft(objItem);
  var objectYPos = getOffsetTop(objItem);
  var objectBottom = objectYPos + objItem.offsetHeight;
  var objectRHS = objectXPos + objItem.offsetWidth;
  var containerBottom = objContainer.scrollTop + objContainer.offsetHeight - 25; // subtract 25 to allow for scrollbar
  var containerRHS = objContainer.scrollLeft + objContainer.offsetWidth - 25; // subtract 25 to allow for scrollbar

  if (objectRHS > containerRHS) {
    // Object is offscreen to right - reposition up and left
    var newObjYPos = objectYPos - objItem.offsetHeight - 10;  
    var newObjXPos = objectXPos - objItem.offsetWidth - 40;  

    if (newObjYPos < objContainer.scrollTop) {
      // New position would have been off the page up
      newObjYPos = newObjYPos + 80;
    }
    
    if (newObjYPos > 0 && newObjXPos > 0) {
      ///objItem.style.top = newObjYPos + "px";  
      objItem.style.left = newObjXPos + "px";  
    }
  } else if (objectBottom > containerBottom) {
    // Object is offscreen to bottom - reposition up
    var newObjYPos = objectYPos - objItem.offsetHeight - 10;  
    if (newObjYPos > 0) {
      objItem.style.top = newObjYPos + "px";  
    }
  }
}

var newMsgDiv;

function showMsg(icon, msg) {

  hideMsg();

  // Create Floaty Div
  var msgXPos = getOffsetLeft(icon) + 30;
  var msgYPos = getOffsetTop(icon);
  
  newMsgDiv = document.createElement("DIV");
  newMsgDiv.style.position = "absolute";
  newMsgDiv.style.top = msgYPos + "px";
  newMsgDiv.style.left = msgXPos + "px";
  newMsgDiv.width = "60px";

  // Create Table 
  var newTable = document.createElement("TABLE");
  newTable.style.width = "200px";
  newTable.style.borderColor = "black";
  newTable.style.borderWidth = "1px";
  newTable.style.borderStyle = "solid";
  newTable.style.backgroundColor = "#fff5ee";

  // Create row and cell for message
  var newRow = newTable.insertRow(0);
  var newCell = newRow.insertCell(0);
  newCell.innerHTML = msg;

  // Add them to the body
  newMsgDiv.appendChild(newTable);
  document.body.appendChild(newMsgDiv);

  // Change the icon style
  icon.className = "iconhover";
  
  // Check that the div is not off the page
  repositionIfOffPage(document.body, newMsgDiv);

}

function hideMsg(icon) {
  if (newMsgDiv != null) {
    icon.className = "icon";
    document.body.removeChild(newMsgDiv);
  }
  newMsgDiv = null;
}	

function getFormGroup(name) {
	return document.getElementsByName(name);
}

function getRadio(name) 
{elements = getFormGroup(name);
if (elements)
/* loop over all the radio buttons */
	for (i = 0; i < elements.length; i++)
	if (elements[i].checked)
		return elements[i];
	
		/* either no group by that name was foundor none were selected */
		return null;	
}

function getRadioValue(name) {
	element = getRadio(name);
	
	if (element) 
		return element.value;
	
	/* there must not have been a radio buttonselected */
	return '';
	 
	}
	
	
	function addDropdownValue(pstrObjectId, pstrOptionId, pstrOptionText, pblnSelected){
		var pobjSelect = document.getElementById(pstrObjectId);
	    var pobjNewOption = document.createElement('option');
		pobjNewOption.text = pstrOptionText;
		pobjNewOption.value = pstrOptionId;
		pobjNewOption.selected = pblnSelected;
		
		  try {
		    pobjSelect.add(pobjNewOption, null); // standards compliant; doesn't work in IE
		  }
		  catch(ex) {
			pobjSelect.add(pobjNewOption); // IE only
		  }
	}
	
	function clearDropdown(pstrId, pstrDefaultEmptyOption){
		  var elSel = document.getElementById(pstrId);
		  var i;
		  for (i = elSel.length - 1; i>=0; i--) {
		      elSel.remove(i);
		  }		
		  
		  addDropdownValue(pstrId, "", pstrDefaultEmptyOption);
		  addDropdownValue(pstrId, "", "---------------");		  
	}
	
	function populateItems(pstrSelectId, pstrSelectedItemId, pstrDefaultEmptyOption, pstrCustomerFilterValue, pstrTypeIdFilterValue){
	
		if(pstrCustomerFilterValue != ""){
			addDropdownValue(pstrSelectId, "", "Loading ...", true);
		
			new Ajax.Request('tls_ajax_item_list.jsp?Flt_Customer=' + pstrCustomerFilterValue + "&Flt_CertificateType=" + pstrTypeIdFilterValue,
			  {
				method: 'get', 
				onComplete: function(transport) {
					populateResponse(transport,pstrSelectId, pstrSelectedItemId, pstrDefaultEmptyOption)
				},
			    onFailure: function(){ 
			    	alert('An error has occured, please try again soon.') 
			    }				
			  });				
		  }else{
		  	clearDropdown(pstrSelectId, pstrDefaultEmptyOption);
		  }		  
	}		
	
	function populateLocations(pstrSelectId, pstrSelectedItemId, pstrDefaultEmptyOption, pstrCustomerFilterValue){
		if(pstrCustomerFilterValue != ""){
			new Ajax.Request('tls_ajax_location_list.jsp?Flt_Customer=' + pstrCustomerFilterValue,
			  {
				method: 'get', 
				onComplete: function(transport) {
					populateResponse(transport,pstrSelectId, pstrSelectedItemId, pstrDefaultEmptyOption)
				},
			    onFailure: function(){ 
			    	alert('An error has occured, please try again soon.') 
			    }				
			  });				
		  }else{
		  	clearDropdown(pstrSelectId, pstrDefaultEmptyOption);
		  }
	}		
	
	function populateResponse(transport,pstrSelectId, pstrSelectedItemId, pstrDefaultEmptyOption){
		clearDropdown(pstrSelectId, pstrDefaultEmptyOption);
	    var response = trim(transport.responseText);
	    
	    //look for |
	    var palValues = response.split("\n");
		
		for(var pintIndex=0; pintIndex < palValues.length; pintIndex++){
			if(palValues[pintIndex] != ""){
				var palNameIdPair = palValues[pintIndex].split("|");
				var pstrId = palNameIdPair[0];
				var pstrName = palNameIdPair[1];				
				var pblnSelected = false;
				
				if(pstrSelectedItemId == pstrId){
					pblnSelected = true;
				}
				
				addDropdownValue(pstrSelectId, pstrId, pstrName, pblnSelected);
			}
		}	   		
	}		
	
	
function getItem(pstrId){
	return document.getElementById(pstrId);
}


function hideItem(pstrId, pblnClearValue){
//	alert("Hiding: "  + pstrId);
	document.getElementById(pstrId).style.display="none";

}

function showItem(pstrId){
//	alert("Showing: "  + pstrId);
	document.getElementById(pstrId).style.display="";
}

function setItem(pstrId,pstrValue){
//	alert("Showing: "  + pstrId);
	document.getElementById(pstrId).innerHTML=pstrValue;
} 

function setAndShowItem(pstrId,pstrValue){
//	alert(pstrId + " = " + pstrValue);
	setItem(pstrId,pstrValue); 
	showItem(pstrId);
}
