/*
*****************************************************************************************************************************
name:common.js

common javascript functions used across all pages
*****************************************************************************************************************************
*/



/*
use for setting focus on the proper submit button if there is more than one on a page
*/

function checkKey(buttonName,e){
		
 var key;

	      if(window.event)
	              key = window.event.keyCode;     //IE
	      else
	              key = e.which;     //firefox
	    
	      if (key == 13)
	        {
	            //Get the button the user wants to have clicked
	            var btn = document.getElementById(buttonName);
	            if (btn != null)
	            { //If we find the button click it
	                btn.click();
	                event.keyCode = 0
	            }
	        }
		}




/*

==============================================================================
search functions
==============================================================================
*/
/*
Globals
*/
var MAIN_SEARCH_FORM_TEXT_BOX='txtKeyword';
var SEARCH_RESULT_PAGE='SiteSearch.aspx?searchKeyword=';
var QPARM_SEARCH_TEXT='searchKeyword'
var SEARCH_RESULT_CMDBTN_1='cmdSearchdocTest';
var SEARCH_RESULT_CMDBTN_2='';
var SEARCH_RESULT_CMDBTN_3='';
var SEARCH_RESULT_FORM='form1';
var SEARCH_RESULT_TEXT_ELEMENT='txtKeyword';
var SPLIT_CHAR_QPARM='&';
var SPLIT_CHAR_QVALUE='=';
var RESULT_NULL='null';
/*
==============================================================================
utility functions
==============================================================================
*/
/*
get query string parms
*/
function getParameter( parameterName ) {
  var queryString = window.location.search.substring(1).toLowerCase();
  var parameters = new Array();
  parameters = queryString.split(SPLIT_CHAR_QPARM);
  for(var i = 0; i < parameters.length; i++) {
    if (parameters[i].indexOf(parameterName.toLowerCase())>=0) {
      var parameterValue = new Array();
      parameterValue = parameters[i].split(SPLIT_CHAR_QVALUE);
      return parameterValue[1];
    }
  }
  return RESULT_NULL;
}
/*
==============================================================================
search functions
==============================================================================
*./
/*
execute main multi-search
*/
function executeSearch(){

var searchWord=document.getElementById(MAIN_SEARCH_FORM_TEXT_BOX).value;
var searchUrl=SEARCH_RESULT_PAGE + searchWord;

window.location=searchUrl;

return 1;
}

/*
multi search function - tailored to execute multiple search portlets at once on one page
*/
function setFlagAndSubmitMultiSearch(){
	var search='';
	search=getParameter(QPARM_SEARCH_TEXT);
	populateHiddenSearchFields(search);
	document.getElementById(SEARCH_RESULT_CMDBTN_1).click();
	document.getElementById(SEARCH_RESULT_CMDBTN_2).click();
	
	document.getElementById(SEARCH_RESULT_FORM).submit();
}
/*
populate all hidden search text fields so multiple searches can pick up keywords to search with
*/
function populateHiddenSearchFields(keywordText)
{
	var cnt;
	var elementId = '';
	for (i=0; i < document.forms[0].elements.length; i++)
	{
		if (document.forms[0].elements[i].id.indexOf(SEARCH_RESULT_TEXT_ELEMENT)!=-1)
		{
			elementId = document.forms[0].elements[i].id;
			var hiddenKeyword=document.getElementById(elementId);
			if(hiddenKeyword!=null){
				hiddenKeyword.value=keywordText;
			}
		}
	}
}

/*
submit normal search function - not multi search, used in conjunction with search portlet normal usage
*/
function setFlagAndSubmit(controlName){
	document.getElementById(controlName).value = 'true';
	document.getElementById('Form1').submit();
}

/*
submit normal search function - not multi search, used in conjunction with search portlet normal usage
*/
function setFlagAndSubmit3(controlName){
	document.getElementById(controlName).value = 'true';

	//nextreviewdate processing for the search query
	var nrdId;
	nrdId=document.getElementById('hdnNextReviewDateId');
	if(nrdId!=null){
	  var nrdParm;
	  var iConstraintId=nrdId.value.indexOf('_',nrdId.value);
	  var constraintId = nrdId.value.substring(iConstraintId + 1);
	  var nextConstraintId = nrdId.value.substring(0,iConstraintId + 1) + (parseInt(constraintId)+1) + '';

	  nrdParm=document.getElementById('ddlNextReviewDate').options[document.getElementById('ddlNextReviewDate').selectedIndex].value;
	  if(nrdParm!='0'){
	    switch(nrdParm)
	    {
	    case '31':
	      document.getElementById(nrdId.value).value='{datecalc}(dd,-1,today)';
	      document.getElementById(nextConstraintId).value='{datecalc}(dd,31,today)';
	      break;
	    case '61':
	      document.getElementById(nrdId.value).value='{datecalc}(dd,30,today)';
	      document.getElementById(nextConstraintId).value='{datecalc}(dd,61,today)';
	      break;
	    case '91':
	      document.getElementById(nrdId.value).value='{datecalc}(dd,60,today)';
	      document.getElementById(nextConstraintId).value='{datecalc}(dd,91,today)';
	      break;
	    case '121':
	      document.getElementById(nrdId.value).value='{datecalc}(dd,90,today)';
	      document.getElementById(nextConstraintId).value='{datecalc}(dd,121,today)';
	      break;
	    default:
	      document.getElementById(nrdId.value).value='1900-01-01';
	      document.getElementById(nextConstraintId).value='2076-01-01';
	      break;
	    }
	  }
	  else{
	    document.getElementById(nrdId.value).value='1900-01-01';
	    document.getElementById(nextConstraintId).value='2076-01-01';
	  }
	}

	document.getElementById('Form1').submit();
}


/*
=======================================================================================================
Search constraint functions
=======================================================================================================
*/
/*
 get the constraints used for the search and repopulate the controls after a post back
*/
function setSearchConstraints()
{
	var check=document.getElementById('hdnConstraints');
	if(check!=null){

			// set all constraints and then set keyword

			// constraints
			var keys=document.getElementById('hdnConstraints').value;
			var values=document.getElementById('hdnValues').value;

			var elements=keys.split(',');
			var vals=values.split(',');
			var hasError='false';

			for(var i=0;i<elements.length;i++)
			{
	    			var ele=document.getElementById(elements[i]);
				if(ele!=null){
					if(vals[i]!=''){
						ele.value=vals[i];
					}
				}
				else{
					hasError='true';
			        	break;
				}

			}

			// keyword
			var keywordTextId=document.getElementById('searchKeyWordId').value;
			var keywordText=document.getElementById(keywordTextId);
			if(keywordText!=null){
					var keywordData=document.getElementById('searchKeyword').value;
					keywordText.value=keywordData;
			}
	}
}
/*
==============================================================================
latest updates functions
==============================================================================
*/



/*
*****************************************************************************************************************************
name:common.js ---- end
*****************************************************************************************************************************
*/
