// ----------------------------------------------------------------------------
// This extension to the prototype library allows browser independant access to
// the innerText property of an html element.
// ----------------------------------------------------------------------------
Element.addMethods({

	getInnerText: function(element) {
		element = $(element);
		return (element.innerText || element.textContent);
	},

	setInnerText: function(element, value) {
		element = $(element);
		if (element.innerText){
			element.innerText = value;
		}
		else{
			element.textContent = value;
		}
		return element;
	}
});

// ----------------------------------------------------------------------------
// This extension to the prototype library adds extended functionality to
// select elements.
// ----------------------------------------------------------------------------
Element.addMethods(
	'SELECT',
	{
		removeOptions: function(objSelect){
			objSelect = $(objSelect);
			var i;
			for (i=1;i<objSelect.options.length;){
				objSelect.options[0]=null;
			}
			objSelect.options.length = 0;

			/*
			var kids = objSelect.childNodes;
			var numkids = kids.length;
			alert("Select contains numkids=" + numkids);
			for (var i = 0; i < numkids; i++) {
				if (kids[i].value == value) {
					objSelect.removeChild(kids[i]);
					break;
				}
			}
		 */
			return objSelect;
		},
		addOption: function(objSelect, strVal, strText, blnSelected){
			objSelect = $(objSelect);
			var opt = Element.extend(document.createElement('option'));
			opt.value = strVal;
			opt.text = strText;
			opt.selected = blnSelected;
			objSelect.options.add(opt);
			return objSelect;
		}
	}
);

var AjaxRequestException = function(transport, exception){
	var strException = "\n";
	strException += "*****************************************************************\n";
	strException += " AJAX - EXCEPTION\n";
	strException += "*****************************************************************\n";
	strException += " Object: REQ\n";
	strException += "=================================================================\n";
	strException += "   transport.responseText: \n" + transport.responseText + "\n";
	strException += "=================================================================\n";
	strException += " Object: EXCEPTION\n";
	strException += "=================================================================\n";
	strException += "  exception.name: " + exception.name + "\n";
	strException += "  exception.message: " + exception.message + "\n";
	/*
	strException += exception.number;
	strException += exception.description;
	*/
	strException += "*****************************************************************\n";
	for (var strKey in exception) {
		strException += "  " + strKey + ": " + exception[strKey] +"\n";
	}
	alert("onException: " + strException);
};

// ========================================================================
// ========================================================================
// ========================================================================
var debugit = function (strMessage, blnClear){

	this.DebugElement= 'pnlDebug';

	var objDebugElement = $(this.DebugElement);

	if(objDebugElement === null){
		var objBody = $$('body')[0];
		var objDebugContainer = Element.extend(document.createElement('div'));
		//objDebugContainer = $(objDebugContainer);// have to extend or IE8 dies
		objDebugContainer.id = "pnlDebugger";
		objDebugContainer.setStyle({clear:'both'});

		objDebugElement = Element.extend(document.createElement('div'));
		//objDebugElement = $(objDebugElement);// have to extend or IE8 dies

		objDebugElement.id = this.DebugElement;
		objDebugElement.toggle();

		var objDebugTitle = Element.extend(document.createElement('div'));
		objDebugTitle = $(objDebugTitle);// have to extend or IE8 dies
		objDebugTitle.update("Debugger");
		objDebugTitle.observe('click',function(e){objDebugElement.toggle();});

		objDebugContainer.appendChild(objDebugTitle);
		objDebugContainer.appendChild(objDebugElement);
		objBody.insert({top:objDebugContainer });
	}
	if(objDebugElement){
		var objDebugRecord = Element.extend(document.createElement('div'));
		//objDebugRecord = $(objDebugRecord);// have to extend or IE8 dies
		//Element.Extend
		objDebugRecord.addClassName("error");
		objDebugRecord.update(strMessage);
		if(blnClear){
			objDebugElement.update("");
		}
		objDebugElement.appendChild(objDebugRecord);
	}
}; // end debugit(strMessage)


// ============================================================================
// ============================================================================
// ============================================================================
Object.extend(
	String.prototype, {
		evalScripts: function() {
			return this.extractScripts().map(
				function(script) {
					var s_ERROR = "";
					var o_res;
					try {
						o_res = eval(script);
					}
					catch (e) {
						s_ERROR += "Exception Caught:eval(script)\n";
						s_ERROR += "Exception:" + e + "\n";
						if (script.match(/[\s\t\r\n]*<!--/)){
							s_ERROR += "[There is a <!-- --> Comment in the javascript]\n";
							s_ERROR += "[IE Does not like to eval() that]\n";
						}
						for (var i in e) {
							s_ERROR += i + ' = ' + e[i] +'\n';
						}
						alert(s_ERROR + "\n--------------------------------------------------------\n" + script);
					}
					return o_res;
				}
			);
		}
	}
);


// ============================================================================
// Overrides window.onerror so that we can do better trapping/debugging in ie.
// Mozilla stores the line number in the lineNumber property and Opera appends
// it to the message property.
// ============================================================================
window.onerror = function (err, file, line) {
	var strErrorType = typeof(err);
	var s_ERROR = "";
	s_ERROR += 'The following error occured:\n';
	s_ERROR += 'Error [' + err + ']\n';
	s_ERROR += 'File  [' + file + ']\n';
	s_ERROR += 'Line  [' + line + ']\n';
	s_ERROR += 'Type  [' + strErrorType + ']\n';
	// --------------------------------------------------------------------------
	// Gather additional properties about the error
	// --------------------------------------------------------------------------
	// If typeif(err) is string the error is not an object and will have no
	// enumerable properties containig further information about the error
	// --------------------------------------------------------------------------

	if(strErrorType != "string"){
		for (var i in err) {
			s_ERROR += i + ' = ' + err[i] +'\n';
		}
	}



	if (window.onerror.caller === null){
		s_ERROR += 'CallLevel was called from the top level\n';
	}
	else{
		s_ERROR += 'CallLevel was called by another function.' + window.onerror.caller;
	}
	alert(s_ERROR);
	return true;
};


// ============================================================================
// ============================================================================
// ============================================================================
function displayTab (tab){
	// set all the tabs to not display
	$$('div.panel').invoke('hide');
	// set the selected tab to display
	if(!$(tab)){alert(tab + " does not exist on the page!");}
	$(tab).show();
}

// ============================================================================
// ============================================================================
// ============================================================================
//var _regx_Email = /^([a-zA-Z0-9_\-\.\&]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
var _regx_Email = /^([a-zA-Z][a-zA-Z0-9_\-\.\&]*){1,65}@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
function IsValidEmail(strEmail){
	//return _regx_Email.IsMatch(email)
	return strEmail.match(_regx_Email);
}

function addFormError(strMessage, objContainer){
	objContainer = objContainer? objContainer : $('pnl-errorzone');
	var objErrorRecord = Element.extend(document.createElement('div'));
 objErrorRecord = $(objErrorRecord);// have to extend or IE8 dies
	//debugit("000");
	objErrorRecord.update(strMessage);
	debugit("111");
	objContainer.appendChild(objErrorRecord);
	return 1;
}

