// Globals
var baseURL = location.href.substring(location.href.lastIndexOf('/'));
var objectArray = new Object();
var currSlideID;
var courseInProgress = 0;
// Process Request
ajaxEngine.registerRequest('processAjax',baseURL);

initHttpsAccess();

function processAjax(ax_action, xmlform, xmlaction, asynchronous){
	var storeHist = false;
	
   if(!xmlform && !xmlaction){
   	storeHist = true;
      var splitted = ax_action.split(',');
      if(splitted[0]){
         ax_action = splitted[0];
      }
      if(splitted[1]){
         xmlform = splitted[1];
      }
      if(splitted[2]){
         xmlaction = splitted[2];
      }
   }
      
   sid = '';
   ax_xmlform = '';
   ax_xmlaction = '';
   ax_type = 'ax_type=AJAX';
   processForm = $('process');
   var hideMessage = true;
   if(xmlaction){
      if(ax_action == 'AJMessages' || xmlaction.indexOf('apply') != -1 || xmlaction.indexOf('editField') != -1){
         hideMessage = false;
      }
   }
   // Detect Changes...
   if(processForm.changes_made){
      if(processForm.changes_made.value == "true" && xmlaction.indexOf('save') == -1 && hideMessage){
         var temp_action = ax_action + ",";
         if(xmlform){
            temp_action += xmlform;
         }
         temp_action += ",";
         if(xmlaction){
            temp_action += xmlaction;
         }
         temp_action = "unescape('"+temp_action+"')";
         var html = 'You have made some changes which have not yet been saved.<br/>What would you like to do?';
         html += "<br/><br/><span class=\"btn\"><a title=\"Cancel Action and Go Back to Save Your Work\" href=\"javascript:errorRecoveryPop('ERROR');\">Go Back</a></span>";
         html += "<br/><span class=\"btn\"><a title=\"Discard Changes. Continue Processing.\" href=\"javascript:errorRecoveryPop('ERROR');axResetChanges();processAjax("+temp_action+");\">Continue Anyway</a></span>";
         setStatusPop('ERROR',html);
         return;
      }
   }
   if(processForm.PHPSESSID){
      sid = 'PHPSESSID='+processForm.PHPSESSID.value;
   }

   if(processForm.ax_action){
      processForm.ax_action.value = ax_action;
      ax_action = 'ax_action='+processForm.ax_action.value;
   }

   if(xmlform){
      var formXML = '<form_data>';
      if (!(xmlform instanceof Array)){tempArray = new Array(1);tempArray[0]=xmlform;xmlform=tempArray;}
      for(a = 0; a < xmlform.length; a++) {
         formXML += '<form>';
         formXML += '<name>'+xmlform[a]+'</name>';
         formXML += '<elements>';
         allNodes = Form.getElements(xmlform[a]);
         for(i = 0; i < allNodes.length; i++) {
            value = Form.Element.getValue(allNodes[i]);
            if(value || (value == '' && allNodes[i].type != 'hidden' && !(allNodes[i].ondblclick))){
               if (!(value instanceof Array)){tempArray = new Array(1);tempArray[0] = value; value = tempArray;}
               for(j = 0; j < value.length; j++) {
                  tag = allNodes[i].name?allNodes[i].name:allNodes[i].id;                  
                  tag = tag.replace('/', '-');
                  if(allNodes[i].className){
                  	if(allNodes[i].className == 'date'){
	                  	var d=parseDate(value[j]);
	                  	if(d != null){value[j] = formatDate(d,'yyyy-MM-dd');}	// Default Date Format
                  	}
                  }
                  if(value[j] != 'Please Select...' && tag.length > 1){
                     formXML += '<'+tag+'><![CDATA['+value[j]+']]></'+tag+'>';
                  }
               }
            }
         }
         formXML += '</elements>';
         formXML += '</form>';
      }
      formXML += '</form_data>';
      ax_xmlform = 'ax_xmlform='+formXML;
   }

   if(xmlaction){
      processForm.ax_xmlaction.value = xmlaction;
      ax_xmlaction = 'ax_xmlaction='+processForm.ax_xmlaction.value;
   }
   //send the dummy frame history to disable the back and forward buttons	
	// 
	
	if((storeHist && self.processHistory) || (self.forceHistory && self.processHistory)){
		processHistory('processAjax', ax_type, ax_action, sid, ax_xmlform, ax_xmlaction);
	}
   if($('pagecontent-activity'))Element.show('pagecontent-activity');
   if(!asynchronous){
	   ajaxEngine.sendRequest('processAjax', ax_type, ax_action, sid, ax_xmlform, ax_xmlaction);
	}else{
		// Synchronus Ajax!
		ajaxEngine.sendSyncRequest('processAjax', ax_type, ax_action, sid, ax_xmlform, ax_xmlaction);
	}   
   
   if($('changes')){
      if(hideMessage){
         axResetChanges();
      }
   }
}

// Process Response
var ajaxAction = Class.create();
var difference = 0;

ajaxAction.prototype = {
   initialize: function() {difference = 0;},

   ajaxUpdate: function(ajaxResponse) {   	
      this.processXML(ajaxResponse);      
   },
   
   processXML: function(xml){
   	var elements = xml.getElementsByTagName("element");      	      	
   	
      for(var i = 0; i < elements.length; i++){      	     	 	
         var node = elements[i];
         var type = null;
         var id = null;

         if(node.getElementsByTagName("type")[0]){
            type = node.getElementsByTagName("type")[0].firstChild.nodeValue;
         }

         if(node.getElementsByTagName("id")[0]){
            id = node.getElementsByTagName("id")[0].firstChild.nodeValue;
         }
         
         if(type == 'createAccordion'){
            var panelHeight = node.getElementsByTagName("value")[0].firstChild.nodeValue;
            this.createAccordion(id, panelHeight);
         }else if(type == 'setAccordion'){
            //setAccordHeight();
         }else if(type == 'createSlider'){
         	var properties = new Array();
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               properties[node.getElementsByTagName("property")[p].firstChild.nodeValue] = node.getElementsByTagName("value")[p].firstChild.nodeValue;
            }
            var slider = 'slider'+id;
            if(id == 1){
	            objectArray[slider] =  new Control.Slider('handle'+id, 'track'+id, {
						sliderValue: properties['amount'],
						onSlide: function(v) { $('track'+id+'_amount').value =  (parseInt(v * 100 * properties['maximum']) / 100).toFixed(properties['rounding']); if(currSlideID == id){if(id == 1){objectArray['slider2'].setValue(v)}else{objectArray['slider1'].setValue(v)}}},
						onChange: function(v) { $('track'+id+'_amount').value =  (parseInt(v * 100 * properties['maximum']) / 100).toFixed(properties['rounding']); if(currSlideID == id){if(id == 1){objectArray['slider2'].setValue(v)}else{objectArray['slider1'].setValue(v)}}}
					});
				}else{
					objectArray[slider] =  new Control.Slider('handle'+id, 'track'+id, {
						sliderValue: properties['amount'],
						onSlide: function(v) { $('track'+id+'_amount').value =  (parseInt(v * 100 * properties['maximum']) / 100).toFixed(properties['rounding']); if(currSlideID == id){if(id == 1){objectArray['slider2'].setValue(v)}else{objectArray['slider1'].setValue(v)}}},
						onChange: function(v) { v = Math.max(v - properties['amount_adj'],0);$('track'+id+'_amount').value =  (parseInt(v * 100 * properties['maximum']) / 100).toFixed(properties['rounding']); if(currSlideID == id){if(id == 1){objectArray['slider2'].setValue(v)}else{objectArray['slider1'].setValue(v)}}},
						disabled:true
					});
				}
         }else if(type == 'addDroppables'){
            droppablesArea = 'droppablesArea';
            droppablesClass = 'class';
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               if(prop == 'droppablesArea'){
                  droppablesArea = value;
               }
               if(prop == 'droppablesClass'){
                  droppablesClass = value;
               }
            }
            Droppables.add(droppablesArea, {accept:droppablesClass, onDrop:function(element){var newElement = document.createElement('li');newElement.id = 'new_'+element.id;newElement.title = element.title;newElement.innerHTML=element.innerHTML;$(droppablesArea).appendChild(newElement);}});
         }else if(type == 'createDraggable'){
            draggableAreaT = 'draggableArea';

         	for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               if(prop == 'draggableArea'){
                  draggableAreaT = value;
               }
            }
            createDraggable(draggableAreaT);
         }else if(type == 'createSortable'){
            // Defaults
            sortableArea = 'sortableDivs';
            draggableAreaT = 'draggableArea';
            alignment = 'vertical';

            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               if(prop == 'draggableArea'){
                  draggableAreaT = value;
               }
               if(prop == 'sortableArea'){
                  sortableArea = value;
               }
               if(prop == 'noSortableArea'){
                  sortableArea = false;
               }
               if(prop == 'alignment'){
                  alignment = value;
               }
            }
            createSortable(draggableAreaT, alignment);
            //if(sortableArea)createSortableDivs(sortableArea);
         }else if(type == 'editRecord'){
            on_load();
         }else if(type == 'unBlockUI'){
            ahutil.unblockui();
         }else if(type == 'createLiveGrid'){
            var properties = new Array();
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               properties[node.getElementsByTagName("property")[p].firstChild.nodeValue] = node.getElementsByTagName("value")[p].firstChild.nodeValue;
            }
            this.createLiveGrid(id, properties);
         }else if(type == 'createDragDrop'){
            var containment = new Array();
            containment.push(id);
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;

               if(prop == 'containment'){
                  containment.push(value);
               }
            }
            this.createDragDrop(id, containment);
         }else if(type == 'createToolTip'){
            var toolTipId = node.getElementsByTagName("value")[0].firstChild.nodeValue;
            this.createToolTip(id, toolTipId);
         }else if(type == 'createTree'){
            this.createTree();
         }else if(type == 'showCentralPop'){
            showCentralPop(id);
         }else if(type == 'hideCentralPop'){
            hideCentralPop(id);            
         }else if(type == 'createAccDrag'){
         	// Must be 'null' instead of null to work... Changed back to null in delayedAction method
         	var contentId = 'null';
         	var windowHtml = 'null';
         	var windowTitle = 'null';
         	var windowLevel = 'null';
         	for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;

               if(prop == 'contentId'){
                  contentId = value;
               }else if(prop == 'innerHTML'){
                 	windowHtml = value;
               }else if(prop == 'title'){
               	windowTitle = value;
               }else if(prop == 'level'){
               	windowLevel = value;
               }
            }
         	delayedActionCreateAccDrag(contentId, id, escape(windowHtml), escape(windowTitle), windowLevel, 1500); // 1.5 seconds delay
         }else if(type == 'autoFixHeight'){
         	var referenceId = null;
         	var offset = null;
         	for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               if(prop == 'referenceId'){
                  referenceId = value;
               }else if(prop == 'offset'){
                 	offset = value;
               }
            }
         	autoFixHeight(id,referenceId,offset);
         }else if(type == 'removeAccDrag'){
         	removeAccDrag(id);
         }else if(type == 'updateAccDrag'){
         	var windowHtml = node.getElementsByTagName("value")[0].firstChild.nodeValue;
         	updateAccDrag(id, windowHtml);
         }else if(type == 'setAccDragError'){
         	setAccDragError(id);
         }else if(type == 'createAutocompleter'){
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;

               if(prop == 'dspAreaId'){
                  var dspAreaId = value;
               }else if(prop == 'paramList'){
                  var paramList = value;
               }
            }
            this.createAutocompleter(id, dspAreaId, paramList);
         }else if(type == 'insertHTML'){
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop1 = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               this.insertHTML(id, prop1, value);
            }
         }else if(type == 'insertAltHTML'){
         	for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop1 = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
               this.insertAltHTML(id, prop1, value);
            }
         }else if(type == 'setStatusView'){
	      	setStatusView(id);
         }else if(type == 'setStatusPop'){
         	value = '';
         	for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
            }
 
         	setStatusPop(id, value);         
			}else if(type == 'assignInPlaceEditor'){				
				var IPEAction = false;
				var IPEClass = '';
				for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop1 = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               value = node.getElementsByTagName("value")[p].firstChild.nodeValue;               
			      if(prop1 == 'class'){
                  IPEClass= value;                     
            	}else if(prop1 == 'action'){
	            	IPEAction= value;                     
            	}
            }
            assignInPlaceEditor(id,IPEClass,IPEAction);
			}else if(type == 'setupLinks'){
            myAccordViewer.setupLinks(id);
			}else if(type == 'initializeWindow'){
            myAccordViewer.initializeWindow(id);			            
         }else if(type == 'removeElement'){
            Element.remove(id);            
         }else if(type == 'setAccordViewerContent'){
         	var element = document.getElementsByClassName('topContent', 'accordViewer')[0];
				element.innerHTML = node.getElementsByTagName("value")[0].firstChild.nodeValue;
				revertWait();
         }else if(type == 'setMessage'){
	      	txt = node.getElementsByTagName("value")[0].firstChild.nodeValue;
	      	setMessage(id, txt);
         }else if(type == 'initializeLink'){
            myAccordViewer.initializeLink(id);
         }else if(type == 'activateLink'){
         	myAccordViewer.activate(null, $(id));
         }else if(type == 'deactivateLink'){
         	myAccordViewer.deactivate();
         }else if(type == 'logout'){
         	$('process').ax_action.value='AHLogout';
         	$('process').submit();
                  }else if(type == 'submitForm'){
         	$(id).submit();         	
         }else if(type == 'clearForm'){
         	$(id).reset();         	
         }else if(type == 'resetCommunity'){
         	commDisplay = false;        	
         }else if(type == 'toggleCourseView'){
         	toggleCourseView(id, node.getElementsByTagName("value")[0].firstChild.nodeValue);
         }else if(type == 'dynamicActivateCard'){
				myAccordViewer.element = new Object();
				myAccordViewer.element.title = node.getElementsByTagName("value")[0].firstChild.nodeValue;
				myAccordViewer.element.rel = '';
				myAccordViewer.element.params = 'aVWidth=740,aVHeight=440';
	         myAccordViewer.close_course_without_test_reminder
		         = node.getElementsByTagName("value")[4].firstChild.nodeValue;

  // var options = options || {};
  // options.isolateCard = (
  //   options.isolateCard
  //     ? '&isolateCard=' + options.isolateCard
  //     : ''
  // );
  // myAccordViewer.cardOptions = options;

				if(node.getElementsByTagName("value")[3].firstChild.nodeValue == '0'){
					myAccordViewer.windowType = 'media';
					myAccordViewer.contentToFetch = id;
					myAccordViewer.showCardToggle = true;
					myAccordViewer.packageId = node.getElementsByTagName("value")[1].firstChild.nodeValue;;
					myAccordViewer.packageTitle = node.getElementsByTagName("value")[2].firstChild.nodeValue;
				}else{
					myAccordViewer.windowType = 'external';
					myAccordViewer.contentToFetch = node.getElementsByTagName("value")[3].firstChild.nodeValue;
					myAccordViewer.packageId = node.getElementsByTagName("value")[1].firstChild.nodeValue;;
					myAccordViewer.packageTitle = node.getElementsByTagName("value")[2].firstChild.nodeValue;
					myAccordViewer.showCardToggle = false;
				}
				myAccordViewer.setupDimensions(true);
				myAccordViewer.displayAccordViewer(true);
				myAccordViewer.monitorKeyboard(true);	
				myAccordViewer.loadInfo(true);	   
			}else if(type == 'clearRequestInterval'){
         	clearServerRequest(id);	
         }else if(type == 'fileUploadComplete'){
         	fileUploadComplete(id);				
        	}else{
            for(var p = 0; p < node.getElementsByTagName("property").length; p++){
               prop1 = node.getElementsByTagName("property")[p].firstChild.nodeValue;
               if(node.getElementsByTagName("value")[0].firstChild){
               	value = node.getElementsByTagName("value")[p].firstChild.nodeValue;
              	}else{
              		value = '';
              	}

               if(type == 'changeStyle'){
                  prop2 = prop1;
                  prop1 = 'style';
               }else{
                  prop2 = null;
               }
               this.changeProperty(id, prop1, prop2, value);
            }
         }
         if($(id)){
            if($('changes')){
               assignOnChangeEventHandlers(id);
            }
         }        
      }
      if($('pagecontent-activity'))Element.hide('pagecontent-activity');// process type of action
      
   },

   changeProperty: function(id, prop1, prop2, value){
      if($(id)){
         if(prop2 != null){
            $(id)[prop1][prop2] = value;
         }else{
         	if(prop1 == 'innerHTML'){	// Clear Circular References
      			purge($(id));
      		}
      		if(id == 'pagecontent-inner'){
      			if($('pagecontent-innerLegacy') != undefined){
      				Element.hide('pagecontent-innerLegacy');
      			}
      			Element.show('pagecontent-inner');
      		}
            $(id)[prop1] = value;
         }
         
         if(id == "pageheaderTxt"){
         	var tempTitle = $(id).innerHTML;
         	tempTitle = tempTitle.replace('&amp;','&');
         	tempTitle = tempTitle.replace('&amp;','&');
         	document.title = tempTitle;         	
         }
      }
   },
	
	insertHTML: function(id, prop1, value){
      if($(id)){
      	if(prop1 == 'before'){
      		var tii = new Insertion.Before(id, value);
      	}else if(prop1 == 'after'){
      		new Insertion.After(id, value);
      	}else if(prop1 == 'top'){
      		new Insertion.Top(id, value);
      	}else if(prop1 == 'bottom'){
      		new Insertion.Bottom(id, value);
      	}      	
      }
   },
   
   insertAltHTML: function(id, prop1, value){
		var currentRow = $(id);
		var div = document.createElement('div');
    	div.innerHTML = '<table><tbody>' + value + '</tbody></table>';
    	var content =  $A(div.childNodes[0].childNodes[0].childNodes);

    	if(prop1 == 'before'){
	  		content.each((function(fragment) {
	      	currentRow.parentNode.insertBefore(fragment, currentRow);
	    	}));
	  	}else if(prop1 == 'after'){
	  		content.each((function(fragment) {
      		currentRow.parentNode.insertBefore(fragment, currentRow.nextSibling);
    		}));
	  	}else if(prop1 == 'top'){
	  		content.reverse(false).each((function(fragment) {
	      	currentRow.insertBefore(fragment, currentRow.firstChild);
	    	}));
	  	}else if(prop1 == 'bottom'){
	  		content.each((function(fragment) {
      		currentRow.appendChild(fragment);
    		}));
	  	}    
   },
   
   createAccordion: function(id, pHeight){
      if($('footer')){
         if(pHeight < 10){
            counter = pHeight;
         }else{
            counter = 1;
         }
         if(document.body.clientHeight){
            pHeight = document.body.clientHeight;
         }else{
            pHeight = window.innerHeight;
         }
         pHeight = pHeight - $('footer').offsetHeight - 175 -(13*counter);

      }
      var opts = {panelHeight:pHeight
                 };

      objectArray[id] = new Rico.Accordion( id, opts);
   },
	
   createLiveGrid: function(id, properties){
      // Get Column Options
      var colParams = new Array();
      var data = properties['columns'].parseQuery();
      for (var i in data) {
         if(i != 'extend'){
            var array = [i, parseInt(data[i])];
            colParams.push(array);
         }
      }

      // Get Request Parameters
      var requestParams = new Array();
      var data = properties['requestParameters'].parseQuery();
      for (var i in data) {
         if(i != 'extend'){
            var param = i+'='+data[i];
            requestParams.push(param);
         }
      }

      var opts = {prefetchBuffer: true
                 ,requestParameters: requestParams
                 ,columns: colParams
                 ,scrollerBorderRight: '1px solid #ffffff'
                 ,canSortDefault: true
                 ,canHideDefault: true
                 ,allowColResize: true
                 ,canFilterDefault: false
                 ,highlightElem: 'none'
                 ,visibleRows: 'data'
              };

      var buffer = new Rico.Buffer.AjaxSQL(baseURL, {foundRowCount:true,requestParameters:requestParams});

      objectArray[id] = new Rico.LiveGrid (id, buffer, opts);
   },

   createDragDrop: function(id, containIds){
      var opts = {dropOnEmpty:true
                 ,containment:containIds
                 ,constraint:false
                 ,ghosting:true
                 };

      Sortable.create(id, opts);
   },

   createToolTip: function(activatorId, toolTipId){
      Tooltip.add(activatorId, toolTipId);
      Tooltip.showEvent = "click";
      Tooltip.hideEvent = "click";
   },

   createTree: function(){
      autoInit_trees();
   },

   createAutocompleter: function(id, dspAreaId, paramList){
      var opts = {parameters: paramList,
                  method: "post",
                  afterUpdateElement: this.searchLiveGrid
                 };
      new Ajax.Autocompleter(id, dspAreaId, baseURL, opts);
      $(id).focus();
   },

   searchLiveGrid: function(element, selected){
      processAjax(document.dataSelect.ax_action.value, 'dataSelect', document.dataSelect.ax_xmlaction.value);
   },

   updateHeader: function(liveGrid, offset) {
      //$('rowCount').innerHTML = "Record " + (offset+1) + " - " + (offset+liveGrid.metaData.getPageSize()) + " of " + liveGrid.metaData.getTotalRows();
   }
};

ajaxAction = new ajaxAction();
ajaxEngine.registerAjaxObject('ajaxAction', ajaxAction);

// Garbage Removal
function purge(d) {
	// Exlicit GC
    var a = d.attributes, i, l, n;    
    var xname;
    var tempCounter = 0;
    
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            if(a[i].specified){
	            n = a[i].name;
	            if (typeof d[n] === 'function') {
	                d[n] = null;	               
			      }
				}
        }
    }
    if(d.className != 'label'){
	    a = d.childNodes;
	    if (a) {
	        l = a.length;
	        for (i = 0; i < l; i += 1) {
	            purge(d.childNodes[i]);
	        }
	    }
	 }    
}

// Missing Prototype Functions
function $w(string){
  string = string.strip();
  return string ? string.split(/\s+/) : [];
}


// Force HTTPS Access
function forceHttpsAccess(){
// Not Applicable... Implemented for Self Service in 
//	var location = window.location;
//	if(location.protocol == 'http:' && (location.hostname == 'www.assethealth.com' || location.hostname == 'www.myghu.com')){
//		window.location = 'https://' + location.hostname + location.pathname;
//	}
}

// Init HTTPS Access
function initHttpsAccess(){
	var location = window.location;
	if(location.protocol == 'http:' && (location.hostname == 'www.assethealth.com' || location.hostname == 'assethealth.com' || location.hostname == 'www.myghu.com')){
//		if(location.hostname == 'assethealth.com'){
//			window.location = 'https://www.' + location.hostname + location.pathname;
//		}else{
			window.location = 'https://' + location.hostname + location.pathname;
//		}
	}
}

//Logout function wrapper
function processLogout(){
	$('process').ax_action.value='AHLogout';
   $('process').submit();
}

//Logout function wrapper & Close Window
function processLogoutClose(){

	$('process').ax_action.value='AHLogout';
    $('process').submit();

		//closes IE and prompts, FireFox closes tab only
		//var win=window.open("about:blank","_self");
		//win.close();

		//close with prompt
		//window.close();
		
		//close without prompt
		//window.opener = self;
		//self.close();
		
		//close without prompt (this works in IE only)
		window.opener = self;
		self.close();
		
		//for FireFox since self.close doesn't work in FireFox this will redirect FireFox to the Login page
		window.location=window.location;
}

//Logout function wrapper for Inactive Logout
function processLogoutInactive(){

	$('process').ax_action.value='AHLogoutInactive';
    $('process').submit();
    //window.location = 'https://www.assethealth.com/web/AHInactive.html';

}

//Start checking of server Ajax requests
var serverRequests = new Object();
function checkServerRequests(requestId){
	serverRequests[requestId] = setInterval('processAjax(\'AJMessages\',\'\',\'checkServerRequests\')', 1000);
}

function clearServerRequest(requestId){
	if(serverRequests[requestId] != undefined){
		clearInterval(serverRequests[requestId]);
	}
}

function	delayedAction(id,prop1,value,delay){
	if(!Element.visible(id)){
		value = unescape(value);
		$(id)[prop1] = value;	
		if(value != ''){
			Element.show(id);
			if(id == 'central-popup'){	// Special for Popup
				var maxHeight = $('pagecontent-inner').getHeight() - 200 + 67 ;
				if($('central-popup').getHeight() > maxHeight){
					$('central-popup').style['height'] = maxHeight + 'px';
				}else{
					$('central-popup').style['height'] = '';
				} 
			}
		}
	}else{
		if(!delay)delay = 0;	
		 delay = delay + Math.floor(Math.random( )* 101);
		 self.setTimeout('delayedAction(\''+id+'\',\''+prop1+'\',\''+value+'\')', delay);
  	}
}

function	delayedActionCreateAccDrag(contentId, id, windowHtml, windowTitle, windowLevel, delay){
	if((id == 'SSPopUp' || id == 'WinPop') && $(id)){		
		 if(!delay)delay = 0;	
		 delay = delay + Math.floor(Math.random( )* 101);
		 self.setTimeout('delayedActionCreateAccDrag(\''+contentId+'\',\''+id+'\',\''+windowHtml+'\',\''+windowTitle+'\',\''+windowLevel+'\')', delay);
  	}else{
	  	windowHtml = unescape(windowHtml);
	  	windowTitle = unescape(windowTitle);
	  	if(contentId == 'null')contentId = null;
	  	if(windowHtml == 'null')windowHtml = null;
	  	if(windowTitle == 'null')windowTitle = null;
	  	if(windowLevel == 'null')windowLevel = null;	  	
	  	createAccDrag(contentId, id, windowHtml, windowTitle, windowLevel);
  	}
}