/* ------------------------------------------------------------------
** Page 
** ------------------------------------------------------------------ */
var Page = Class.create();

// <NOTE>
// By BNA
// The following constants are REAL class names that will be used for object
// creation, that's why they are written as strings rather than numbers.
// </NOTE>

var PAGE_INFO = {
  INBOX		       : {
    ID 	     : "Inbox",
    SEARCHABLE : true,
    LABEL      : "inbox"
  },
  SUGGESTIONS	       : {
    ID         : "Suggestions",
    SEARCHABLE : true,
    LABEL      : "suggestions"
  },
  COMPLAINTS	       : {
    ID         : "Complaints",
    SEARCHABLE : true,
    LABEL      : "complaints"
  },
  INQUIRIES	       : {
    ID         :"Inquiries",
    SEARCHABLE : true,
    LABEL      : "inquiries"
  },
  CustomerThanks : {
    ID         :"CustomerThanks",
    SEARCHABLE : true,
    LABEL      : "CustomerThanks"
  },
  SECURE_APPS	       : {
    ID         :"SecureApps",
    SEARCHABLE : true,
    LABEL      : "secureapplications"
  },
  OUT_OF_SPECIALTY_APPS	       : {
    ID         :"OutOfSpecialtyApps",
    SEARCHABLE : true,
    LABEL      : "outofspecilatyapplications"
  },
  TRASH	       : {
    ID         :"Trash",
    SEARCHABLE : true,
    LABEL      : "trash"
  },
  USER_PROFILE	       : {
    ID         :"UserProfile",
    SEARCHABLE : false,
    LABEL      : "userprofile"
  },
  USER_PROFILE_EDITOR       : {
    ID         :"UserProfileEditor",
    SEARCHABLE : false,
    LABEL      : "User Profile Editor"
  },
  PUBLIC_TECH_SUPPORT	       : {
    ID         :"PublicTechSupport",
    SEARCHABLE : false,
    LABEL      : "publictechsupport"
  },
  SYSTEM_TECH_SUPPORT	       : {
    ID         :"SystemTechSupport",
    SEARCHABLE : true,
    LABEL      : "systemtechsupport"
  },
  LOGO		       : {
    ID 	       : "Logo",
    SEARCHABLE : false,
    LABEL      : "logo"
  },
  NEW_USER	       : {
    ID         : "NewUser",
    SEARCHABLE : false,
    LABEL      : "new user"
  },
  NEW_APPLICATION      : {
    ID         : "NewApplication",
    SEARCHABLE : false,
    LABEL      : "new application"
  },
  DRAFT_EDITOR         : {
    ID         : "DraftEditor",
    SEARCHABLE : false,
    LABEL      : "draft editor"
  },
  DRAFT                : {
    ID         : "Draft",
    SEARCHABLE : true,
    LABEL      : "draft"
  },
  KNOWLEDGE_BASE       :  {
    ID         : "KnowledgeBase",
    SEARCHABLE : true,
    LABEL      : "knowledge base"
  },
  USER_ACCOUNTS	       : {
    ID         : "UserAccounts",
    SEARCHABLE : true,
    LABEL      : "user accounts"
  },
  APPLICATION_TYPES    : {
    ID         : "ApplicationTypes",
    SEARCHABLE : true,
    LABEL      : "application types"
  },
  SERVICES	       : {
    ID         : "Services",
    SEARCHABLE : true,
    LABEL      : "services"
  },
  SYSTEM_USERS	       : {
    ID         : "SystemUsers",
    SEARCHABLE : true,
    LABEL      : "system users"
  },
  SYSTEM_ROLE	       : {
    ID         : "SystemRole",
    SEARCHABLE : true,
    LABEL      : "system role"
  },
  DEPT_ROLE	       : {
    ID         : "DeptRole",
    SEARCHABLE : true,
    LABEL      : "dept role"
  },
  PAGE_ROLE_PRIV   : {
    ID         : "Privileges",
    SEARCHABLE : true,
    LABEL      : "Privileges"
  },
  ADD_DEPARTMENT_ROLES : {
    ID         : "AddDepartmentRoles",
    SEARCHABLE : false,
    LABEL      : "add department roles"
  },
  WORKFLOW	       : {
    ID         : "WorkFlow",
    SEARCHABLE : false,
    LABEL      : "workflow"
  }, 
  APPLICATION_STATES   : {
    ID         : "ApplicationStates",
    SEARCHABLE : false,
    LABEL      : "application states"
  }, 
  KPI		       : {
    ID         : "KPI",
    SEARCHABLE : false,
    LABEL      : "kpi"
  },
  SYSTEM_SETUP         : {
    ID         : "SystemSetup",
    SEARCHABLE : false,
    LABEL      : "system setup"
  },
  SYSTEM_USER	       : {
    ID         : "SystemUser",
    SEARCHABLE : true,
    LABEL      : "system user"
  },
  ROLE_USER	       : {
    ID         : "RoleUser",
    SEARCHABLE : true,
    LABEL      : "Role user"
  },
  WORKFLOW	       : {
    ID         : "Workflow",
    SEARCHABLE : true,
    LABEL      : "Workflow"
  },
  SERVICE_PROVIDER     : {
    ID         : "ServiceProvider",
    SEARCHABLE : true,
    LABEL      : "service provider"
  },
  STAGE		       : {
    ID         : "Stage",
    SEARCHABLE : false,
    LABEL      : "stage"
  },
  WORKFLOW_STAGES		       : {
    ID         : "WorkflowStages",
    SEARCHABLE : true,
    LABEL      : "WF Stages"
  },
  WORKFLOW_DEPT		       : {
    ID         : "WorkflowDept",
    SEARCHABLE : true,
    LABEL      : "WF Dept"
  },
  APPLIED_VIA		       : {
    ID         : "AppliedVia",
    SEARCHABLE : true,
    LABEL      : "Applied Via"
  },
  REP_APPLICATIONCNT   : {
    ID         : "RepApplicationCnt",
    SEARCHABLE : true,
    LABEL      : "Applications Number"
  },  
  REP_CLOSDAPPLICATION   : {
    ID         : "RepClosdApplication",
    SEARCHABLE : true,
    LABEL      : "Closed Applications"
  },
  REP_CALLCNT		       : {
    ID         : "RepCallCnt",
    SEARCHABLE : true,
    LABEL      : "Calls Count"
  },
  REPORT_DATE		       : {
    ID         : "ReportDate",
    SEARCHABLE : true,
    LABEL      : "Reports By Date"
  }, 
  REP_APPLICANT		       : {
    ID         : "RepApplicant",
    SEARCHABLE : true,
    LABEL      : "Reports By Applicants"
  },
  
  REPORT_DURATION : {
    ID         : "ReportDuration",
    SEARCHABLE : true,
    LABEL      : "Reports By Processing Time"
  },
  REPORT_APP_WORKFLOW : {
    ID         : "ReportAppWorkflow",
    SEARCHABLE : true,
    LABEL      :  "Report Application Workflow"
  }, 
  APP_BY_GENDER_AGE : {
    ID         : "AppByGenderAge",
    SEARCHABLE : true,
    LABEL      :  "Applicants By Gender Age"
  },
  REPORT_EMP_EVALUATION : {
    ID         : "ReportEmpEvaluation",
    SEARCHABLE : true,
    LABEL      :  "ReportEmpEvaluation"
  },
  CUSTOMER_SURVEY : {
    ID         : "CustomerSurvey",
    SEARCHABLE : true,
    LABEL      :  "Customer Survey"
  },
  CUSTOMER_SURVEY_FORM : {
    ID         : "CustomerSurveyForm",
    SEARCHABLE : true,
    LABEL      :  "Customer Survey Form"
  },
  COORDINATORS_INFO : {
    ID         : "CoordinatorsInfo",
    SEARCHABLE : true,
    LABEL      : "System Users Info"
  },
  DELAYED_APPS : {
    ID         : "DelayedApps",
    SEARCHABLE : true,
    LABEL      : "Delayed Applications"
  },
  CLOSED_APPS : {
    ID         : "ClosedApps",
    SEARCHABLE : true,
    LABEL      : "Closed Applications"
  },
  WORKFLOW_ROLE		       : {
    ID         : "WorkflowRole",
    SEARCHABLE : true,
    LABEL      : "WF Roles"
  },
  CUSTOMER_THANKS:{
  	ID			: "Customer_Thanks",
	SEARCHABLE	: true,
	LABEL		: "Customer Thanks"
  },
  CUSTOMER_THANKS_REPORT:{
	ID			: "CustomerThanksReport",
	SEARCHABLE	: true,
	LABEL		: "Customer Thanks Report"
  },
  APPLICATION_TYPES:{
	ID			: "ApplicationTypes",
	SEARCHABLE	: true,
	LABEL		: "ApplicationTypes"
  },
  COUNT_APP_CLIENT:{
	ID			: "CountAppClient",
	SEARCHABLE	: true,
	LABEL		: "ApplicationTypes"
  },
  Department_Map:{
	ID			: "DepartmentMap",
	SEARCHABLE	: true,
	LABEL		: "DepartmentMap"
  }
}

Page.FindElementPosition = function(object)
{
	var currentLeft	= 0;
	var currentTop  = 0;
	
	if(object.offsetParent)
	{
		currentLeft = object.offsetLeft;
		currentTop  = object.offsetTop;
		
		while(object = object.offsetParent)
		{
			currentLeft += object.offsetLeft;
			currentTop  += object.offsetTop;
		}
	}
	
	return [currentLeft,currentTop+10];
}

Page.SetElementPosition = function(object, layer)
{
	var layerObject = $(layer);
		
	var currentStyle = layerObject.style.visibility;
	$(layer+"Content").innerHTML = "";
	
	layerObject.style.display = "";
	layerObject.style.visibility = "visible";
		
	var coordinates = Page.FindElementPosition(object);

    var xPos = (document.body.clientWidth - 500)/2;	
	
	layerObject.style.left = xPos + "px";
	layerObject.style.top  = coordinates[1] + "px";
}

Page.CallByAjax = function(url,ref)
{
    var request = Try.these(

        function() { return new XMLHttpRequest() },

        function() { return new ActiveXObject("Msxml2.XMLHTTP") },

        function() { return new ActiveXObject("Microsoft.XMLHTTP") }

    );

    request.onreadystatechange = function()
    {
        if(request.readyState == 4 && request.status == 200)
        {   
            var responseHtml = request.responseText;
         
            if(ref != "")
            {
                var element = $(ref);
                if (element.tagName.toLowerCase() !="input")
                {
                    element.innerHTML = "";
                    element.innerHTML = responseHtml;
                }
                else
                {
                    element.value=responseHtml;
                }
            }

            Page.PopulateHtmlLabels();
        }

    }

    request.open("POST", url, true);
    request.send(null);
}

Page.PopulateHtmlLabels =  function() {
  
  var elementLabelsDiv = $("cst_item_labels");
  if(elementLabelsDiv) {
    var hshItemLabelsInfo = 
      window.eval("(" + elementLabelsDiv.innerHTML.strip() + ")");
    
    var arrElementLabelHashes = hshItemLabelsInfo.labels;  
    arrElementLabelHashes.each(
      function(hshLabel) {
	var label = hshLabel.label;
	
	var element = $(hshLabel.id);
	if(element) {
	
	  if(element.tagName.toLowerCase() != "input"){
	    element.innerHTML = label;
	  } else {
	    element.value = label;
	  }
	  
	  //Removing the Element from the array
	  if(hshItemLabelsInfo.remove_ids == 1){
	    element.removeAttribute("id");
	  }
	}
    
	/*
	var element = document.getElementsByName(hshLabel.id);
	if(element) {
		
	  for(i=0;i<element.length;i++)
	  {
		  if(element[i].tagName.toLowerCase() != "input"){
			element[i].innerHTML = label;
		  } else {
			element[i].value = label;
		  }
	  
		  //Removing the Element from the array
		  if(hshItemLabelsInfo.remove_ids == 1){
			element[i].removeAttribute("id");
		  }
	  }
	  
	}
	*/
	
      } 
    );
	
	//Removing the item labels div after populate is complete
    elementLabelsDiv.parentNode.removeChild(elementLabelsDiv);
  }
}

Page.LookupPageInfoHashById = function(pageId) {
  
  return $H(PAGE_INFO).values().detect(
    function(pageInfoHash){
      return pageInfoHash.ID == pageId;
    }
  );
  
}

Page.LookupPageInfoHashByLabel = function(pageLabel) {
  
  return $H(PAGE_INFO).values().detect(
    function(pageInfoHash){
      return pageInfoHash.LABEL == pageLabel;
    }
  );
  
}

Page.SetProgressIconDisplay = function(isEnabled){
  
  $("progress").style.display = isEnabled ? "" : "none";
  
}

Page.prototype = Object.extend(
  
  new Object(),
  {
    initialize: function(){},
    
    //**--------------------Public--------------------**//
    GetId: function(){
      return this.id;
    },
    
    GetInternalId: function() {
      return this.internalId;
    },
    
    IsSearchable: function(){
      return Page.LookupPageInfoHashById(this.GetId()).SEARCHABLE;
    },
    
    GetLabel: function() {
      return Page.LookupPageInfoHashById(this.GetId()).LABEL;
    },
    
    Load: function(){},
    
    // <Output>
    // return a booleqan to either allow or prevent next page loading.
    // </Output>
    UnLoad: function() {
      return true;
    },
    
    //**--------------------Private--------------------**//
    
    
    _LoadHtml: function(hshOperationParams) {
      
      var pageContainerElementId = "content";
      
      var pageLoadOperation = 
	OperationFactory.Get().GetOperation(Operation.TYPE.LOAD_PAGE_HTML,
					    Operation.RESULT_TYPE.TEXT);
      
      pageLoadOperation.SetParam(
	Operation.TYPE.LOAD_PAGE_HTML.PARAMS["PAGE_ID"],
	this.GetId()
      );
      
      if(hshOperationParams) {
	for(operationParamName in hshOperationParams){
	  pageLoadOperation.SetParam(
	    operationParamName,
	    hshOperationParams[operationParamName]
	  )
	}
      }
      
      pageLoadOperation.Execute();
      
      $(pageContainerElementId).innerHTML = "";
      new Insertion.Bottom(pageContainerElementId, 
			   pageLoadOperation.GetResult());
      
      Page.PopulateHtmlLabels();
    },
	
    //**-------------------- Members --------------------**//
    id           : null,
    internalId   : null,
    xsltInstance : null,
    xmlDocument	 : null
  }
);