// this function is for standard edit screens with Update, Cancel, Update and Add Another 
// if the user pressed cancel we want to go straight to the previous screen
// however if the user wants to save then we want to validate the form first by calling validateEditForm()
// which each programmer implements for their edit screen
// all buttons call the same controller JSP so we utilize a hidden field named "FormAction" and set 
// its value to Cancel or UpdateExit or UpdateAddAnother to indicate to that controller JSP what action 
// to take.
// the hardcoding of the button name, FormAction name and FormAction value is not the nicest way fo doing 
// things but must be strictly adhered to since the list controller JSP depends on the FormAction.value
// if we attempted to submit the form then the buttons do not get submitted since they are not submit
// buttons hence we must utilize a hidden field
//
function submitEditForm(button){
   if (button.name == "Cancel") {
      button.form.formAction.value=button.name;   
      button.form.submit();
   } 
   else { 
     if (validateEditForm(button.form)) {
         button.form.formAction.value=button.name;   	       
         button.form.submit();
      }
   }
}

// some edit screens have a selection list for update and add another rather than a button, this 
//  function handles those edit screen selection list choices
//
function specialAddSubmitEditForm(aList){
  if (aList.selectedIndex > 0) {
     if (validateEditForm(aList.form)) {
        aList.form.formAction.value = "UpdateAddAnother";
        aList.form.submit();
     } else {
	    aList.options[aList.selectedIndex].selected = false;
     } 
  }
}

// this function is for standard lists with Next, Previous, Add and Delete
// we want to display a confirmation box if the user is deleting and only proceed if the user
// chooses to delete
// however if the user chooses to add then we want to go straight to the add item screen
// if user chooses next then we want to display the next set of data
// if user chooses previous then we want to display the previous set of data
// both add and delete call the same controller JSP so we utilize a hidden field named "FormAction" and set 
// its value to Add or Delete to indicate to that controller JSP what action to take.
// the hardcoding of the button name, FormAction name and FormAction value is not the nicest way fo doing 
// things but must be strictly adhered to since the list controller JSP depends on the FormAction.value
// if we attempted to submit the form then the buttons do not get submitted since they are not submit
// buttons hence we must utilize a hidden field
//
function submitListForm(button){
	 if (button.name == "Delete") {
      var ids=getIdsToDelete(button.form);
      if (ids.length >1 ) {
         // rsh using ids to delete in application framework.
         if (confirm("Please press the OK button to delete the selected items")) {
            button.form.formAction.value=button.name;
	        button.form.idsToDelete.value=ids;
	        button.form.submit();
	 	 }
      }
      else  {
	 	alert("Please select one or more list items before attempting a delete");
      }
   } 
   else {
      // this is for "Add", "Next", "Previous" and any other buttons the user adds
      button.form.formAction.value=button.name;   
      button.form.submit();
   }   		
}

// This function is called for special buttons
// this function is for standard lists with Next, Previous, Add and Delete
// we want to display a confirmation box if the user is deleting and only proceed if the user
// chooses to delete
// however if the user chooses to add then we want to go straight to the add item screen
// if user chooses next then we want to display the next set of data
// if user chooses previous then we want to display the previous set of data
// both add and delete call the same controller JSP so we utilize a hidden field named "FormAction" and set 
// its value to Add or Delete to indicate to that controller JSP what action to take.
// the hardcoding of the button name, FormAction name and FormAction value is not the nicest way fo doing 
// things but must be strictly adhered to since the list controller JSP depends on the FormAction.value
// if we attempted to submit the form then the buttons do not get submitted since they are not submit
// buttons hence we must utilize a hidden field
//

function submitUserListForm(button){									
    if (button.name == "Delete") {
      if (listItemsSelectedCount(button.form) != 0) {
         if (confirm("Please press the OK button to continue with delete")) {
	        button.form.formAction.value=button.name;
	        button.form.submit();
	 }
      }
      else 
      {
	 alert("Please select one or more list items before attempting a delete");
      }
   } 
   else
   {
      // this is for "Add", "Next", "Previous" and any other buttons the user adds
      button.form.formAction.value=button.name;   
      button.form.submit();
   }   
}

// some list screens have a selection list for add rather than a button, this function handles
//  those list screen selection list choices
//
function specialAddSubmitListForm(aList){
  if (aList.selectedIndex > 0) {
    aList.form.formAction.value = "Add";
    aList.form.submit();
  }
}


function errorsFoundMsg() {
   var errMsg = "Hi there. Sorry, but some errors were detected on the form.";
   errMsg += "\nPlease correct and resubmit or choose Cancel to exit without saving";
   alert(errMsg);
}


function openPopUpWindow(window_http, window_name, window_left, window_top, window_width, window_height, window_scrollable, window_resizable ){

	window_options = "'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable="+window_resizable+",scrollbars="+window_scrollable+",copyhistory=no,left="+window_left+",top="+window_top+",height=" + window_height + ",width=" + window_width + "'";
	window.open( window_http, window_name, window_options );
}


function replaceApostrophy(aString){
	var returnString = "";
	var lengthOfString = 0;
	var i=0;
	lengthOfString = aString.length;
	i = 0;
	if (lengthOfString>0){
		while (i<lengthOfString){
			if (aString.charAt(i)=="'"){
				if (i==(lengthOfString-1)){
					returnString += "''";
				}
				else{
					if (aString.charAt(i+1)=="'"){
						returnString += "''";
						i++;
					}
					else
						returnString += "''";
				}
			}
			else
				returnString += aString.charAt(i);
			i++;
		}
	}
	else{
		returnString = aString;
	}

	return returnString;

}

function getNumberSelected(form){
	var counter=0;
    var len = form.elements.length;
    for(i=1; i < len; i++){
        if(form.elements[i].type == "checkbox" && form.elements[i].checked==true){
        	counter++;
        }
    }
    return counter;
   
}

function getIdsToDelete(form){
	var idsToDelete="";
    var len = form.elements.length;
    for(i=1; i < len; i++){
        if(form.elements[i].type == "checkbox" && form.elements[i].checked==true){
        	idsToDelete=idsToDelete+","+form.elements[i].name;
        }
    }
    return idsToDelete;
}




