// Disables targetObject if the length of text in sourceObject
// is zero. Should be called with onkeyup attribute.
function onTextFieldStateChange(textField, targetObject)
{
	if (textField.value.length == 0)
	{
		targetObject.disabled = true;
	}
	else
	{
		targetObject.disabled = false;	
	}
}
function onListStateChange(list, targetObject) 
{
	// Disable the search button if the selected item is
	// the 'please select' placeholder

	if (list.selectedIndex == 0)
	{
		targetObject.disabled = true;
	}
	else
	{
		targetObject.disabled = false;
	}
}

var checkedCount =0;

function onCheckBoxClick(ocheckbox, button, listname) 
{

	if (ocheckbox.checked) 
		checkedCount++;
	else 
		checkedCount--;

	button.disabled = checkedCount <= 0;			
/*
   var oForm = ocheckbox.form;
   
   var benabled = false;
   var iloop = 0;
   var selement = "";

   // loop through checkboxes
   while(!benabled)
   {
      // construct element name
      selement = listname + "[" + iloop + "].selected";

      // skip if element does not exist
      if(!oForm.elements[selement])
      {
         break;
      }      
      
      // set to true if any checkbox is checked
      if(oForm.elements[selement].checked)
      {
         benabled = true;
      }
      
      iloop++;  
   }
   
   
   // set button state
   button.disabled = !benabled;
   */
}

// setActionIndex(oButton, sAction, ivalue)
// Description:
// Sets the actionindex field on the form. Assumes the button is within a form. The name
// of the button can be of the form ButtonName or ButtonName[0] for indexed buttons.
// Parameters:
// oButton - The button from which the action was invoked. 
// sAction - The action to be performed.
// ivalue  - Value.
// Example of use:
// <html:button value="Add" property="AddNewAltName" onclick="performAction(this,'AddAlternateName.do')" /> 

function setActionIndex(oButton, sAction, ivalue)
{
   var oForm = oButton.form;
   oForm.actionIndex.value=ivalue;
   oForm.action=sAction;	

//    submit action
   oForm.submit();
}


// performAction(oButton, sAction)
// Description:
// Performs an action by taking two parameters. Assumes the button is within a form. The name
// of the button can be of the form ButtonName or ButtonName[0] for indexed buttons.
// Parameters:
// oButton - The button from which the action was invoked. 
// sAction - The action to be performed.
// Example of use:
// <html:button value="Add" property="AddNewAltName" onclick="performAction(this,'AddAlternateName.do')" /> 

function performAction(oButton, sAction)
{
	hideForm(oButton);
	performActionBase(oButton, sAction);
}

function performActionShowForm(oButton, sAction)
{
	performActionBase(oButton, sAction);
}

function performActionBase(oButton, sAction)
{
   var oForm = oButton.form;
   var pathName = oForm.action;
   var i = pathName.lastIndexOf("/");

   pathName = pathName.substring(0,i+1);
   sAction = pathName + sAction;

   //set actionIndex if the action is from an indexed button.
   var sButtonName = oButton.name;
   var indexedRegExp = /\[([0-9]+)\]/ig;
   var sIndex = indexedRegExp.exec(sButtonName);

   var sParameters = "index=" ;

   if (sIndex!=null)
   {
      sIndex = RegExp.$1;
      if (sAction.indexOf("?")>0) 
      {
          sParameters = "&" + sParameters + sIndex ;
      }
      else
      {
          sParameters = "?" + sParameters + sIndex;
      }
   }
   else
   {
      sParameters = ""
   }

   sAction = sAction + sParameters;
   
   //oForm.actionIndex.value=sIndex;
   oForm.action=sAction;
   oForm.submit();
   
   //only request action once
   oButton.disabled=true;
}


// performActionWithForward(oButton, sAction, sForward)
// Description:
// Performs an action by taking three parameters. Assumes the button is within a form. The name
// of the button can be of the form ButtonName or ButtonName[0] for indexed buttons.
// Assumes the action being invoked accesses the actionForward to send on to the next page.
// Parameters:
// oButton - The button from which the action was invoked. 
// sAction - The action to be performed.
// sForward - the action or URL to forward.
// Example of use:
// <html:button value="Add" property="AddNewAltName" onclick="performAction(this,'AddAlternateName.do','AgencyIdentity')" /> 

function performActionWithForward(oButton, sAction, sForward)
{
   if (sForward !="" )
   {
       sAction = sAction + "?CurrentPage="+sForward;
   }

   //peform actions
   performAction(oButton, sAction);
};

// performActionWithConfirmation(oButton, sAction, sMessage)
// Description:
// Confirms action before executing an action. Assumes the button is within a form. The name
// of the button can be of the form ButtonName or ButtonName[0] for indexed buttons.
// Parameters:
// oButton - The button from which the action was invoked. 
// sAction - The action to be performed.
// sMessage - The question that will be displayed to user and will ask for confirmation.
// Example of use:
// <html:button value="Add" property="AddNewAltName" onclick="performActionWithConfirmation(this,'SaveAgency.do', 'You are about to save Agency. Please click OK')" /> 

function performActionWithConfirmation(oButton, sAction, sMessage)
{
//	hideForm(oButton);
   var bConfirmed = true; // do action if no message

   if (sMessage !="" )
   {
      // confirm action
      bConfirmed = window.confirm(sMessage);
   }

   if(bConfirmed)
   {
      //peform actions
      performAction(oButton, sAction);
   }
	else
	{
		performAction(oButton, "DummyAction.do");
	}
}

// performActionWithForwardAndConfirmation(oButton, sAction, sForward, sMessage)
// Description:
// Performs an action by taking three parameters. Assumes the button is within a form. The name
// of the button can be of the form ButtonName or ButtonName[0] for indexed buttons.
// Assumes the action being invoked accesses the actionForward to send on to the next page.
// Parameters:
// oButton - The button from which the action was invoked. 
// sAction - The action to be performed.
// sForward - the action or URL to forward.
// Example of use:
// <html:button value="Add" property="AddNewAltName" onclick="performAction(this,'AddAlternateName.do','AgencyIdentity', 'Do you really want to do this?')" /> 

function performActionWithForwardAndConfirmation(oButton, sAction, sForward, sMessage)
{
   var bConfirmed = true; // do action if no message

   if (sMessage !="" )
   {
      // confirm action
      bConfirmed = window.confirm(sMessage);
   }

   if(bConfirmed)
   {
      //peform action
      performActionWithForward(oButton, sAction, sForward);
   }
}



// Performs an action without submitting the form.
// Set newWindow to true to open the link in a new window.
function performLinkedAction(link, newWindow)
{
	if (newWindow)
	{
		if (link != "")
		{
			var secondaryLabel = getSecondaryLabel();
			date = new Date();

			// Add seed to URL as IE uses cached HTML page if window is already open, instead
			// of re-invoking the action. This is a Bad Thing.

			if (link.indexOf("?") == -1) link += "?";
				else link += "&";

			link += "seed=" + date.getTime();
			secondaryWindow = window.open(link, secondaryLabel, 'resizable=yes,toolbar=yes,location=no,status=yes,scrollbars=yes');
			secondaryWindow.focus();

		}
	}
	else
	{
		document.location=link;
	}
}

function openLinkInParent(link) {
	this.opener.document.location=link;
}

