// common.js

var g_recordIdentifierValue; //global variable
var g_error_record_view = "Please select the record to be viewed."; //global variable
var g_error_record_delete = "Please select the record to be deleted."; //global variable
var g_error_record_update = "Please select the record to be updated."; //global variable
var g_error_no_mapping_data = "There is no data to be mapped."; //global variable
var g_error_record_history = "Please select a record to view its history."; //global variable
var g_error_role_history = "Please select a Role to view its history."; //global variable
var g_error_protocol_history = "Please select a Protocol to view its history."; //global variable
var g_error_item_history = "Please select a Item to view its history."; //global variable
var g_contextPath;//global variable , it is initialized in header.jsp
//var g_popUpWindow;//global variable , it is used to ensure that there is only one PopUp window open at any given instance of time


function gfn_callMasterFileHistoryHstSumryAction(programRefererValue,programRefererIdValue,tableName,arrayKeyCol,arrayKeyVal,backButtonAction){

	var host = document.location.host;
	var protocol = document.location.protocol;
	var actionName = 'MasterFileHistoryHstSumryAct.do';

	var form = document.createElement("form");	// Create a form

	if(programRefererValue!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'programReference';
	    el.value = programRefererValue;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(programRefererIdValue!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'programReferenceId';
	    el.value = programRefererIdValue;	
		form.appendChild(el); 	// Add input element to the form    	
    }
	
	if(tableName!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'tableName';
	    el.value = tableName;	
		form.appendChild(el); 	// Add input element to the form    	
    }
	
    if(arrayKeyCol!=null){
    	for(var i=0;i<arrayKeyCol.length;i++){
    		var el = document.createElement('input'); // Create input element
			el.type = 'text';
		  	el.name = 'arrayKeyCol';
		    el.value = arrayKeyCol[i];	
			form.appendChild(el); 	// Add input element to the form
    	}  		
    }
    
    if(arrayKeyVal!=null){
    	for(var i=0;i<arrayKeyVal.length;i++){
    		var el = document.createElement('input'); // Create input element
			el.type = 'text';
		  	el.name = 'arrayKeyVal';
		    el.value = arrayKeyVal[i];	
			form.appendChild(el); 	// Add input element to the form
    	}  		
    }
    
    if(backButtonAction!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'backButtonAction';
	    el.value = backButtonAction;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}

function gfn_callMasterFileHistoryFldVlsAction(userId,updateType,updateCount,dateTime,userCode){

	var host = document.location.host;
	var protocol = document.location.protocol;
	var actionName = 'MasterFileHistoryFldVlsAct.do';

	var form = document.createElement("form");	// Create a form
    
    if(userId!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'userId';
	    el.value = userId;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(updateType!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'updateType';
	    el.value = updateType;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(updateCount!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'updateCount';
	    el.value = updateCount;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(dateTime!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'dateTime';
	    el.value = dateTime;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(userCode!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'userCode';
	    el.value = userCode;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}

function gfn_callMasterFileHistoryFldDtlsAction(fieldName){

	var host = document.location.host;
	var protocol = document.location.protocol;
	var actionName = 'MasterFileHistoryFldDtlsAct.do';

	var form = document.createElement("form");	// Create a form

	if(fieldName!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'fieldName';
	    el.value = fieldName;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}

function gfn_callMasterFileHistorySearchResultDtlsAction(dateTime,oldValue,newValue,userCode,updateType){

	var host = document.location.host;
	var protocol = document.location.protocol;
	var actionName = 'MasterFileHistorySearchResultDtlsAct.do';

	var form = document.createElement("form");	// Create a form

	if(dateTime!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'dateTime';
	    el.value = dateTime;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(oldValue!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'oldValue';
	    el.value = oldValue;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(newValue!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'newValue';
	    el.value = newValue;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(userCode!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'userCode';
	    el.value = userCode;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
    if(updateType!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'updateType';
	    el.value = updateType;	
		form.appendChild(el); 	// Add input element to the form    	
    }
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}

function gfn_setFormAction(formNumber,actionName) {
	document.forms[parseInt(formNumber)].action=g_contextPath + "/" + actionName;	
}

function callAction(actionName,recordIdentifierName,errorMessage,programRefererName,programRefererValue,programRefererIdName,programRefererIdValue){
	
	var recordIdentifierValue = getRecordIdentifierValue();
	
	if(recordIdentifierValue==null && recordIdentifierName!=null){
		if(g_recordIdentifierValue==null){		
			gfnDisplayError(errorMessage);
			return;
		}else{
		    recordIdentifierValue = g_recordIdentifierValue;
		}
	}

	var host = document.location.host;
	var protocol = document.location.protocol;

	var form = document.createElement("form");	// Create a form

    if(recordIdentifierName!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = recordIdentifierName;
	    el.value = recordIdentifierValue;	
		form.appendChild(el); 	// Add input element to the form
    }
    
    if(programRefererName!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = programRefererName;
	    el.value = programRefererValue;	
		form.appendChild(el); 	// Add input element to the form
    }
    
    if(programRefererIdName!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = programRefererIdName;
	    el.value = programRefererIdValue;	
		form.appendChild(el); 	// Add input element to the form
    }
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}


function gfnMakeUppercase()
{
      var val=window.event.keyCode;

      if(val>="97" && val<="122")
        window.event.keyCode=val-32;
}


function gfnAllowNumberOnly()
{    
	 if ((event.keyCode < 48) || (event.keyCode > 57)){
	 	event.returnValue = false;
	 } 	  
	
}


function gfnMaxLengthTextArea(cobj,max)
{
	if ((cobj!=null) && (max!=null)) 
	{
		if (cobj.value.length >= max) 
		{
			cobj.value = cobj.value.substring(0,max)
			window.event.keyCode=0;
			return false;
		}
	}
}


function gfnDisplayError(errorMessage){	

	errorMessage = "<img src=\"images/error.gif\"/>&nbsp;<font color='red'>"+errorMessage+"</font><br/>";
	document.getElementById("messageArea").innerHTML = errorMessage;
}
function gfnDisplayInfo(errorMessage){	

	errorMessage = "<img src=\"images/information.gif\"/>&nbsp;<font color='blue'>"+errorMessage+"</font><br/>";
	document.getElementById("messageArea").innerHTML = errorMessage;
}
function gfnDisplayWarning(errorMessage){	

	errorMessage = "<img src=\"images/warning.gif\"/>&nbsp;<font color='#de6103'>"+errorMessage+"</font><br/>";
	document.getElementById("messageArea").innerHTML = errorMessage;
}

function gfnClearMessageArea(){
	document.getElementById("messageArea").innerHTML = "";
}









function gfn_moveToRight(formNumber)
   {   
	  var x=document.forms[parseInt(formNumber)].unmapped;
	  if(x.selectedIndex > -1){
		while(x.selectedIndex > -1){		  	  
      	  var Text=x.options[x.selectedIndex].text;
	 	  var value=x.options[x.selectedIndex].value;
  		  var opt = document.createElement("option");
          document.forms[parseInt(formNumber)].mapped.options.add(opt);
          opt.text = Text;
          opt.value= value;
          x.remove(x.selectedIndex)	
		}
	  }      
   }
   
function gfn_moveAllToRight(formNumber)
   {
	  var x=document.forms[parseInt(formNumber)].unmapped;
	  for (i=0;i<x.length;i++)
  	  {
     	x.options[i].selected = true;
  	  }
	  if(x.selectedIndex > -1){
		while(x.selectedIndex > -1){		  	  
      	  var Text=x.options[x.selectedIndex].text;
	 	  var value=x.options[x.selectedIndex].value;
  		  var opt = document.createElement("option");
          document.forms[parseInt(formNumber)].mapped.options.add(opt);
          opt.text = Text;
          opt.value= value;
          x.remove(x.selectedIndex)	
		}
	  }      
   }
   
function gfn_moveToLeft(formNumber)
   {
      var x=document.forms[parseInt(formNumber)].mapped;
	  if(x.selectedIndex > -1){
		while(x.selectedIndex > -1){	      
	      var Text=x.options[x.selectedIndex].text;
	      var value=x.options[x.selectedIndex].value;
	      var opt = document.createElement("option");
	      document.forms[parseInt(formNumber)].unmapped.options.add(opt);
	      opt.text = Text;
	      opt.value= value;
	      x.remove(x.selectedIndex)
	 	}
 	  }
   }

function gfn_moveAllToLeft(formNumber)
   {
      var x=document.forms[parseInt(formNumber)].mapped;
      for (i=0;i<x.length;i++)
  	  {
     	x.options[i].selected = true;
  	  }
	  if(x.selectedIndex > -1){
		while(x.selectedIndex > -1){	      
	      var Text=x.options[x.selectedIndex].text;
	      var value=x.options[x.selectedIndex].value;
	      var opt = document.createElement("option");
	      document.forms[parseInt(formNumber)].unmapped.options.add(opt);
	      opt.text = Text;
	      opt.value= value;
	      x.remove(x.selectedIndex)	 
	 	}
 	  }
   }
   
   
function gfn_selectMappedContents(formNumber)
  { 		
    List = document.forms[parseInt(formNumber)].mapped;
    for (i=0;i<List.length;i++)
  	{
     	List.options[i].selected = true;
  	}
    return true;
    
  }
  
function gfn_selectMappingDataContents(formNumber)
  { 		
    unmappedList = document.forms[parseInt(formNumber)].unmapped;
    for (i=0;i<unmappedList.length;i++)
  	{
     	unmappedList.options[i].selected = true;
  	}
  	
  	mappedList = document.forms[parseInt(formNumber)].mapped;
    for (i=0;i<mappedList.length;i++)
  	{
     	mappedList.options[i].selected = true;
  	}
  	
    return true;
    
  }
  
  
function gfn_getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function gfn_getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = gfn_getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function gfn_getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function gfn_getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = gfn_getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function


function gfn_ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function gfn_rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function gfn_trim(str, chars) {
    return gfn_ltrim(gfn_rtrim(str, chars), chars);
}

function UnderConstructionAct(programRefererValue,programRefererIdValue)
{
	setRecordIdentifierValue(document.getElementById('protocolId').value);
	callAction("UnderConstructionAct.do","protocolId","","programReference",programRefererValue,"programReferenceId",programRefererIdValue);
}

function returnParentPage(actionName){

	var host = document.location.host;
	var protocol = document.location.protocol;
 
	
	document.forms[0].action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	
	document.forms[0].method = "POST"; 	// Add method attributes
	document.forms[0].submit();
}

function enable_save(){
	document.forms[0].save.disabled=false;
}
function enable_save1(){
	document.forms[1].save.disabled=false;
}


//For MyProfile Action

function callMyProfileAction(){

	var host = document.location.host;
	var protocol = document.location.protocol;
	var actionName = 'UpdateProfileInitAct.do';
	
	var form = document.createElement("form");	// Create a form
	var parentId = "147";
	//if(dateTime!=null){
  		var el = document.createElement('input'); // Create input element
		el.type = 'text';
	  	el.name = 'parent_program_id';
	    el.value = parentId;	
		form.appendChild(el); 	// Add input element to the form    	
   // }   
    
	document.body.appendChild(form);	// Add form to the document body

	form.action = protocol + '//' + host + g_contextPath + "/" + actionName; 	// Add action
	form.method = "POST"; 	// Add method attributes
	form.submit(); //submit
}


/*function gfn_popUpWindow(popUpWindowName) {
	if (popUpWindow)
	{
		if(!popUpWindow.closed){
			popUpWindow.close();
		}		
	}
	popUpWindow = window.open ("",popUpWindowName,'left=0,top=0,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=yes');	
	//popUpWindow.focus();
	return true;
}*/



