Ext.onReady(function(){
	// parse through any forms and remove the Visage masking on the url
	unmaskFormActions();
	//Ext.BLANK_IMAGE_URL = '/images/ext/resources/images/default/s.gif';  // Ext 2.0
}
)
var WEB_MV_VALUE_MARKER = "@VM@";

/**
 * Submits a form, and calls any apt javascript that needs to run
 * before a form submit
 * @param form The form object to submit  
 */
function submitFormObject(form) {
	// if it's got a pop-up target, make it visible
	var portalHolder = Ext.get('portal-holder');
	if (portalHolder != undefined && portalHolder != null) {
		var portalForm = portalHolder.dom.parentNode;
		if (portalForm != null && portalForm != undefined &&
			portalForm.target=="portletDetailPopupFrame") {
			//make the pop-up visible
			Ext.getCmp('portletDetailPopupWindow').show();
		}
		//document.getElementById('portletDetailPopupFrame').contentWindow.document.body.onload=simpleAlert;
		//var the_height=document.getElementById('portletDetailPopupFrame').contentWindow.document.body.scrollHeight + 300;
		//document.getElementById('portletDetailPopupFrame').height=the_height;
		//if (the_height > 200) {
			//Ext.getCmp('portletDetailPopupWindow').setHeight(the_height);
		//}
		//alert (the_height);
		//Ext.getCmp('portletDetailPopupWindow').setHeight(the_height + 200);
	}
	form.submit();
	//alert (the_height);
}

/**
 * Removes all data in a form
 * From http://www.javascript-coder.com/javascript-form/javascript-reset-form.htm
 */
function clearForm(oForm) {
  var elements = oForm.elements;
  oForm.reset();
  for(i=0; i<elements.length; i++) {
  	field_type = elements[i].type.toLowerCase();
  	switch(field_type) {
    	case "text":
    	case "password":
    	case "textarea":
		case "hidden":  
			elements[i].value = "";
			break;
       
		case "radio":
		case "checkbox":
			if (elements[i].checked) {
			elements[i].checked = false;
      		}
      	break;

    	case "select-one":
    	case "select-multi":
			elements[i].selectedIndex = -1;
			break;
    	
    	default:
			break;
  	}
  }
}

function clearPortalPopup() {
	// change title
	Ext.getCmp('portletDetailPopupWindow').setTitle('Loading...');
	// put loading page into pop-up
	var loadingUrl = getAptScriptConfigValue('popup-loading-page-url',
			'http://www.aptsolutions.net/rest/1.2/pages/loading.html');
	document.getElementById('portletDetailPopupFrame').contentWindow.location.replace(loadingUrl);
}

/**
 * Gets a configuration value from the config element on the page. If no
 * value is found, the default passed in will be returned
 * @param defaultValue Value to return if no config element is found
 */
function getAptScriptConfigValue(name, defaultValue) {
	var returnValue = defaultValue;
	// do we have a config element?
	var coreScriptElem = Ext.DomQuery.selectNode('apt-script-config');
	if (coreScriptElem != null & coreScriptElem != undefined) {
		// try to get the attribute
		var configValue = coreScriptElem.getAttribute(name);
		if (configValue != null && configValue != undefined) {
			returnValue = configValue;
		}
	}
	// escape any standard values
	return unmaskUrl(returnValue);
}

/**
 * Gets the text for a label from the script config
 */
function getLabel(name, defaultValue) {
	return getAptScriptConfigValue(name, defaultValue);
}

function resizePopupToContent() {
	//always shuffle to top left corner 
	var popupWindow = Ext.getCmp('portletDetailPopupWindow');
	popupWindow.setPosition(25,25);
	
	try {
		// try setting pop-up title as first H1 on page, then use H2 if none found.
		// Hides the heading from the pop-up so we don't get it twice
		// first off, check we have access to the contents of the page - domain and port must be the same as ours
		//var popupDomain = document.getElementById('portletDetailPopupFrame').contentWindow.document.domain;
		//var myDomain = document.domain;
//		var popupUrlFull = document.getElementById('portletDetailPopupFrame').contentWindow.document.URL;
		// trim down to finish after port
//		var portStartIndex = popupUrlStart.indexOf(':', 5);
//		var portEndIndex = popupUrlStart.indexOf('/', portStartIndex);
//		var popupUrlStart = popupUrlFull.substr(0, portEndIndex);
//		var myUrlStart = document.URL.substr(0, popupUrlStart.length);
//		var fistHeading;
//		if (popupUrlStart == myUrlStart) { 
//			var firstHeading = Ext.DomQuery.selectNode('h1',
//					document.getElementById('portletDetailPopupFrame').contentWindow.document.body);
//			if (firstHeading == null) {
//				firstHeading = Ext.DomQuery.selectNode('h2',
//					document.getElementById('portletDetailPopupFrame').contentWindow.document.body);
//			}
//			firstHeading.style.display = 'none';
//		}
	
		var the_height=document.getElementById('portletDetailPopupFrame').contentWindow.document.body.scrollHeight + 80;
		if (the_height < 300) {
			popupWindow.setHeight(300);
		} else if (the_height > 800) {
			popupWindow.setHeight(800);
		} else {
			popupWindow.setHeight(the_height);
		}
//		if (firstHeading != null && firstHeading != undefined) {
//			popupWindow.setTitle(firstHeading.innerHTML);
//		} else {
			popupWindow.setTitle('');
//		}	
	} catch (err) {
		// error determining height - just set a default
		//alert(err);
		//Ext.getCmp('portletDetailPopupWindow').setHeight(300);
	}
}

/**
 * Submits a form by it's unique ID (note that this is not the form name)
 * @param 
 */
function submitForm(formId) {
	submitFormObject(Ext.get(formId));
}

/**
 * Removes obfuscation from URLs encoded in a web page. This is used
 * to disguise URLs from visage so they are not modified on the way through.
 * This is to work around a Visage bug where any references to atrest.cgi
 * are corrupted, so need to be obfuscated so Visage does not see them.
 */
function unmaskUrl(startValue) {
	try {
		var unmaskedUrl = startValue.replace(/@DEL@/g, '');
		return unmaskedUrl;
	} catch (e) {
		// not able to convert - just return origianl value
		return startValue;
	}
}

/**
 * Removes any masking from form actions to allow them to be submitted
 */
function unmaskFormActions() {
	var allForms = document.getElementsByTagName("form");
	for (var i=0; i<allForms.length; i++) {
		var currentForm=allForms[i];
		var currentAction = currentForm.action;
		if (currentAction instanceof String) {
			currentForm.action=unmaskUrl(currentForm.action);
		}
	}
}