function psDwrInit(){
       useLoadingMessage();
       DWREngine.setErrorHandler(globalErrorHandler);
}
//Go to the start page
function goToErrorPage() {
  window.location = appContext + "/error.jsp";
}

function goToMainPage() {
  window.location = appContext + "/";
}

function globalErrorHandler(msg, ex){
     //Error code 1 = Session Expired --> forward to start page.
     if (ex==null || ex.errorCode == '2') {
        goToErrorPage();
     }

     if (ex.errorCode == '1') {
        goToMainPage();
     }

     if (ex.errorCode == '3') {
        alert(msg);
        return false;
     }
}
function useLoadingMessage() {
      var loadingMessage="Loading.  Please Wait...";

      DWREngine.setPreHook(function() {
        var disabledZone = $('disabledZone');
        if (!disabledZone) {
          disabledZone = document.createElement('div');
          disabledZone.setAttribute('id', 'disabledZone');
          disabledZone.style.position = "absolute";
          disabledZone.style.zIndex = "1000";
          disabledZone.style.left = "0px";
          disabledZone.style.top = "0px";
          disabledZone.style.width = "100%";
          disabledZone.style.height = "100%";
          document.body.appendChild(disabledZone);
          var messageZone = document.createElement('div');
          messageZone.setAttribute('id', 'messageZone');
          messageZone.style.position = "absolute";
          messageZone.style.top = "200px";
          messageZone.style.right = "40%";
          messageZone.style.background = "#d40000";
          messageZone.style.color = "white";
          messageZone.style.fontFamily = "Arial,Helvetica,sans-serif";
          messageZone.style.fontSize="20px";
          messageZone.style.fontWeight="bold";
          messageZone.style.padding = "5px";
          disabledZone.appendChild(messageZone);
          var text = document.createTextNode(loadingMessage);
          messageZone.appendChild(text);
        }
        else {
          $('messageZone').innerHTML = loadingMessage;
          disabledZone.style.visibility = 'visible';
        }
      });

      DWREngine.setPostHook(function() {
        $('disabledZone').style.visibility = 'hidden';
      });
}

function filterEntitiesScoas() {
	orgId = getSelectedRadioValue(document.getElementsByName('searchCriteria.organizationType'));
	if($('searchCriteria.entityId')) {
		year =	$('searchCriteria.fiscalYear');
		if(year) {
			year = $('searchCriteria.fiscalYear').value;
		}
		psDWRService.getEntitiesByOrgTypeYear(orgId, year, populateEntities);	
	}
	filterScoas(orgId);
}

function filterScoas(orgId) {
	if(!orgId) {
	 	orgId = getSelectedRadioValue(document.getElementsByName('searchCriteria.organizationType'));
	}
	if($('searchCriteria.scoa')) {
		year =	$('searchCriteria.fiscalYear');
		if(year) {
			year = $('searchCriteria.fiscalYear').value;
		}
		entityId = $('searchCriteria.entityId');
		if(entityId) {
			entityId = $('searchCriteria.entityId').value;
		}
		psDWRService.getSCOAList(year, orgId, entityId, populateScoas);
	}
}

function getSelectedRadioValue(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 buttonGroup[i].value
         }
      }
   } else {
      if (buttonGroup.checked) { return buttonGroup.value; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return "";
} 

function populateEntities(data) {
	DWRUtil.removeAllOptions('searchCriteria.entityId');
	DWRUtil.addOptions('searchCriteria.entityId', {0:"-- Please Select --"});	
	DWRUtil.addOptions('searchCriteria.entityId', data);
	DWRUtil.setValue('searchCriteria.entityId', 0);
}

function populateScoas(dataList) {
	DWRUtil.removeAllOptions('searchCriteria.scoa');
	DWRUtil.addOptions('searchCriteria.scoa', {0:"-- Please Select --"});	
	//DWRUtil.addOptions('searchCriteria.scoa', dataList,'scoaCode','description');
	DWRUtil.addOptions('searchCriteria.scoa', dataList,'description','description');
	DWRUtil.setValue('searchCriteria.scoa', 0);
}

function populateOptions(text, keyCode) {
	var re = /^[0-9a-zA-Z ]*$/;
	if(keyCode==27 || keyCode==13) {
		$('vendorNamesDiv').style.display='none';
		$('searchCriteria.name').value=text.value;
		$('searchCriteria.scoa').focus();
		return false;
	}
	if(text.value.length>2 && re.test(text.value)) {
		year =	$('searchCriteria.fiscalYear');
		if(year) {
			year = $('searchCriteria.fiscalYear').value;
		}
		psDWRService.getNamesStartingWith(text.value, year, populateNames);
	}
	return false;
}

function populateNames(optionsList) {
	var target = $('vendorNamesDiv');
	var text = $('searchCriteria.name');
	target.innerHTML = '';
	if(optionsList.length > 0) {
		for(var i=0; i < optionsList.length; i++) {
			var itemSpan = document.createElement('div');
			itemSpan.id = i;
			itemSpan.name = i;
			itemSpan.style.background="#FFFFFF";
			var itemSpanTxt = document.createTextNode(optionsList[i]);
			itemSpan.value=itemSpanTxt
			itemSpan.appendChild(itemSpanTxt);
			//Add click handler to results.
			itemSpan.onmouseover = function(){
				selectOption(this.id);
			}
			itemSpan.onmouseout=function() {
				deselectOption(this.id);
			}
			itemSpan.onclick = function() {
				var selectedOption = this.childNodes[0].nodeValue;
				text.value=selectedOption;
				target.style.display='none';
			}
			target.appendChild(itemSpan);
		}
		locXY=findXYForObj(text);
		target.style.top=locXY[1]+22+'px';
		target.style.left=locXY[0]+'px';
		target.style.position='absolute';
		target.style.display="block";
	}
}

function selectOption(selOption) {
	$(selOption).style.background='#0060FF';
}

function deselectOption(selOption) {
	$(selOption).style.background='#FFFFFF';
}

function findXYForObj(obj) {
    if (obj!=null){
	var curtop = 0;
	var curleft = 0;
    if (obj.offsetParent) {
        do {
            curleft+= obj.offsetLeft;
			curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
	}
	}
	return [curleft,curtop];
}
