// Copyright 2002, MicroPatent, LLC.
// The contents of this page are the property of MicroPatent, LLC and
// cannot be reproduced without the permission of the owner.
// This copyright extends to all text, html, asp, javascript, and xml contained herein.
///////////////////////////////////////////////////////////////////////////////
function FormFilterKeys( oForm, fnValidate ) {
	var chrCode = self.event ? self.event.keyCode : null;

	try {
		if( chrCode == 13 && oForm ) {
			if( fnValidate != null && typeof(fnValidate) == 'function' ) {
				if( !fnValidate() )	{
					self.event.returnValue = false;
					return false;
				}
			}
				
			oForm.submit();
			self.event.returnValue = false;
			return false;
		}
	}
	catch( e ) {
	}	
	
	return true;
}

////////////////////////////////////////////////////////////////////////////////////
function getXmlCharData( sValue ) {
	if( !sValue || sValue == '' )
		return '';
		
	if( typeof(sValue) != 'string' )
		sValue = sValue.toString();

	return sValue.replace(/([<>&"])/g, function($1) { 
			switch ($1) {
				case '<' : return '&lt;'; 
				case '>' : return '&gt;';
				case '&' : return '&amp;';
				case '"' : return '&quot;';
				default  : return $1;
			}
		}
	);
}

////////////////////////////////////////////////////////////////////////////////////
function isFormSuccCtrl( oCtrl ) {
	if( !oCtrl || !oCtrl.name || oCtrl.name=='' || oCtrl.disabled )
		return false;
	
	if( oCtrl.tagName.toUpperCase() == 'INPUT' ) {
		switch( oCtrl.type.toUpperCase() ) {
			case 'CHECKBOX':
			case 'RADIO':
				return oCtrl.checked;
		}			
	}
	
	return true;
}

////////////////////////////////////////////////////////////////////////////////////
function getFormXml( id ) {
	var oForm = document.forms( id );
	var xml = '<attributes>';
	if( oForm ) {
		for( var i = 0; i < oForm.elements.length; i++ ) {
			var oCtrl = oForm.elements(i);
			if( isFormSuccCtrl( oCtrl ) )  {
				if( oCtrl.tagName.toUpperCase() == 'SELECT' && oCtrl.options ) {
					for( var o = 0; o < oCtrl.options.length; o++ ) {
						var oElm = oCtrl.options[o];
						if( oElm && oElm.selected )
							xml += '<attribute key=\'' + oCtrl.name + '\'>' + getXmlCharData( oElm.value ) + '</attribute>'
					}
				}
				else {
					xml += '<attribute key=\'' + oCtrl.name + '\'>' + getXmlCharData( oCtrl.value ) + '</attribute>'
				}
			}	
		}
	}
	return xml + '</attributes>';
}

////////////////////////////////////////////////////////////////////////////////////
function isFormDirty( id ) {
	var oForm = document.forms( id );
	if( oForm ) {
		var i, j;
		for( i = 0; i < oForm.elements.length; i++ ) {
			var oElm  = null;
			var oCtrl = oForm.elements(i);
			switch ( oCtrl.tagName ) {
				case 'SELECT':
					for( j = 0; j < oCtrl.options.length; j++ ) {
						oElm = oCtrl.options(j);
						if( oElm.selected != oElm.defaultSelected )
							return true;
					}
				break;
				default:
					if ( oCtrl.tagName == 'INPUT' && ( oCtrl.type == 'checkbox' || oCtrl.type == 'radio' ) ) {
						if( oCtrl.checked != oCtrl.defaultChecked )
							return true;
					}
					else {
						if( oCtrl.value != oCtrl.defaultValue )
							return true;
					}
				break;	
			}
		}
	}
	return false;
}

