isInitialized = false;
var PopUp = Class.create();
PopUp.prototype = {
	
	options: {width: '', height: '', html: '', additionalInit: ''}, 
	
	initialize: function(options){
    	
    	try {options.element.blur()} catch(err){};
		
		if ( !isInitialized ) {
			isInitialized = true;	
			thisPopUp = this;			
			Object.extend(this.options, options || {});
			
			this.markUp(this.popupHTML);
			this.setContent();
			this.setButtons();
			this.setDim();
			
			eval(this.options.additionalInit);
			
			this.show();		
			
			try {new Draggable(this.holder, {starteffect: null, endeffect: null})} catch(err){};
			try {this.form.focusFirstElement()} catch(err){};
		}
	},
	
	markUp: function(popupHTML){
			
		if (window.ActiveXObject && ! window.XMLHttpRequest) {  // IE6
			this.options.shadow = false;
			$$('select').each( function(i){Element.hide(i)});
		}
		$$('object').each( function(i){i.style.visibility = "hidden";});		// Hide video and Flash 
		try {
			$('videoPlayer').style.visibility = "hidden";
			
		} catch(err){};
		 
		if (!this.options.shadow) {
			new Insertion.Top(document.body, this.containerHTML);
			new Insertion.Bottom($('popupHolder'), popupHTML);
		} else {
			new Insertion.Top(document.body, this.containerWithShadowHTML);
			new Insertion.Bottom($('figueOfShadow'), popupHTML);
		}
		new Insertion.Top(document.body, this.overlayHTML);	
		
		this.holder = $('popupHolder');
		this.popup = $('popup');
		this.overlay = $('overlayer');
		
		try { 
			this.form = $$('#' + this.popup.id + ' form')[0];
			this.form.setAttribute("action", this.options.form.action);
			this.form.setAttribute("name", this.options.form.name);
			this.form.setAttribute("id", this.options.form.id);				
			this.form.setAttribute("method", this.options.form.method || "get");
			this.form.onkeypress = stopRKey;				
		} catch(err){};
	},
	
	setContent: function() {
		new Insertion.Top($('popupContent'), this.options.html);	
	},
	
	setDim: function(){	
		if (this.options.height){
			$('buttonBar').style.position = 'absolute';
			this.popup.style.height = this.options.height + 'px';			
		}
		if (this.options.width){
			this.popup.style.width = this.options.width + 'px';
		}
		var a = document.body.scrollTop + 50;
		var b = document.getElementsByTagName('html')[0].scrollTop + 50;
		this.holder.style.top = ((a > b)? a : b) + 'px';
		
		try {
			this.holder.style.left = ((document.body.scrollWidth - 70 - (this.options.width ? this.options.width:0 )) /2 )+ 'px';	
			this.overlay.style.height = document.body.scrollHeight + 'px';
		} catch (err){}
		
	},
	
	setButtons: function(){
		if($('closePopup')) Event.observe($('closePopup'), 'click', this.onclickClose);			
		if($('ok')) Event.observe($('ok'), 'click', this.onclickOk);
	},
	
	show: function() {
		this.holder.show();
		Effect.Appear(this.overlay, {duration:0.3, to:0.2});
	},
	
	hide: function() {
		this.holder.hide();
		Effect.Fade(this.overlay, {duration:0.3});		
	},	

	remove: function() {
		this.hide();
		
		setTimeout("[thisPopUp.overlay, thisPopUp.holder].each(Element.remove)", 300);
		isInitialized = false;
		
		$$('select').each( function(i){Element.show(i)});  // IE6
		$$('object').each( function(i){i.style.visibility = "visible";});// Show video and Flash	
		try {
			$('videoPlayer').style.visibility = "visible";
		} catch(err) {};
	},
	
	onclickClose: function(){
		var that = thisPopUp;
		that.remove();
	},
	
	onclickCancel: function() {
	   var that = thisPopUp;
	   that.remove();
	   eval(that.options.onclickCancel);
	},
	
	onclickOk: function(){
		var that = thisPopUp;
		that.remove();
		eval(that.options.onclickOk);
	},
		
	onSubmit: function(){
		var that = thisPopUp;
		that.remove();
		eval(that.options.onSubmit);
	},
			
		
	popupHTML:
	
		'	<div id="popup" class="popup">' +
		'		<div id="popupContent"></div>' +
		'		<div id="buttonBar" class="buttonBar">' +
		'			<a type="button" class="button" value="Ok" name="ok" id="ok" />' +
		'		</div>' +
		'		<img id="closePopup" style="position:absolute;top:4px; right:4px;" src="/base/image/iconCloseRed.gif" />' +							
		'	</div>',
	
	overlayHTML: '<div id="overlayer" style="display:none"></div>',
	
	containerHTML: '<div id="popupHolder"></div>',
	
	containerWithShadowHTML:
	
		'<div id="popupHolder"  class="withShadow" style="display:none">'+
		'<table class="shadow" >'+
        '    <tr><td class="top_left"></td><td class="top_"></td><td class="top_right"></td></tr>'+
        '    <tr>'+
        '        <td class="left_"><div class="left_"><div></td>'+
        '        <td style="background-color:#fff">' +
        '			<div id="figueOfShadow" style="position:relative; background-color:#fff; height:border: 0px solid red"></div>'+
        '		</td>'+
        '        <td class="right_"><div class="right_"><div></td>'+
        '    </tr>'+
        '    <tr ><td class="bottom_left"></td><td class="bottom_"></td><td class="bottom_right"></td></tr>'+
        '</table>'+
        '</div>'
};

var Warning = Class.create();
Object.extend(Warning.prototype, PopUp.prototype);
Object.extend(Warning.prototype, {
	
	options: { width: 450, height: 150 },  //Default
	
	setContent: function() {
		new Insertion.Bottom($('popupMessage'), this.options.msg);			
	},

	popupHTML:
	
		'	<div id="popup" class="popup">' +
		'		<div id="popupContent" style="margin:0;border:red solid 0px">' +
		'			<img style="position:relative; top:40px;left:40px;" src="/base/image/iconWorning.gif" alt="!"/>' + 		
		'			<div id="popupMessage" style="margin:0px 50px 20px 100px;border:red solid 0px"></div>' +
		'		</div>' +
		'		<div id="buttonBar" class="buttonBar">' +
		'			<input type="button" class="button" value="Ok" name="ok" id="ok" />' +
		'		</div>' +	
		'	</div>'

});

var Confirm = Class.create();
Object.extend(Confirm.prototype, PopUp.prototype);
Object.extend(Confirm.prototype, {
	
	options: { width: 450, height: 150, html: '' },  //Default
	
	setContent: function() {
		new Insertion.Bottom($('popupMessage'), this.options.msg);			
	},
	
	setButtons: function(){
		if($('closePopup')) Event.observe($('closePopup'), 'click', this.onclickClose);			
		if($('ok')) Event.observe($('ok'), 'click', this.onclickOk);
		if($('cancel')) Event.observe($('cancel'), 'click', this.onclickCancel);		
	},
	
	onclickCancel: function(){
        var that = thisPopUp;
        eval(that.options.onclickOk.toString() + "(false)" );
        that.remove();        
    },
	
	onclickOk: function(){
		var that = thisPopUp;
		eval(that.options.onclickOk.toString() + "(true)" );
		that.remove();		
	},

	
	popupHTML:
	
		'	<div id="popup" class="popup">' +
		'		<div id="popupContent" style="margin:0;border:red solid 0px">' +
		'			<img style="position:relative; top:40px;left:40px;" src="/base/image/iconWorning.gif" alt="!"/>' + 		
		'			<div id="popupMessage" style="margin:0px 50px 20px 100px;border:red solid 0px"></div>' +
		'		</div>' +
		'		<div id="buttonBar" class="buttonBar">' +
		'			<input type="button" style="width:60px" class="button" value="Ok" name="ok" id="ok" />' +
		'			<input type="button" style="width:60px" class="button" value="Cancel" name="cancel" id="cancel" />' +
		'		</div>' +	
		'	</div>'
});

var ConfirmQuotaWarning = Class.create();
Object.extend(ConfirmQuotaWarning.prototype, PopUp.prototype);
Object.extend(ConfirmQuotaWarning.prototype, {
	
	options: { width: 450, height: 150, html: '' },  //Default
	
	setContent: function() {
		new Insertion.Bottom($('popupMessage'), this.options.msg);			
	},
	
	setButtons: function(){
		if($('closePopup')) Event.observe($('closePopup'), 'click', this.onclickClose);			
		if($('ok')) Event.observe($('ok'), 'click', this.onclickOk);		
	},
	
	onclickOk: function(){
		var that = thisPopUp;
		eval(that.options.onclickOk.toString() + "(true)" );
		that.remove();		
	},

	
	popupHTML:
	
		'	<div id="popup" class="popup">' +
		'		<div id="popupContent" style="margin:0;border:red solid 0px">' +
		'			<img style="position:relative; top:40px;left:40px;" src="/base/image/iconWorning.gif" alt="!"/>' + 		
		'			<div id="popupMessage" style="margin:0px 50px 20px 100px;border:red solid 0px"></div>' +
		'		</div>' +
		'		<div id="buttonBar" class="buttonBar">' +
		'			<input type="button" style="width:60px" class="button" value="Ok" name="ok" id="ok" />' +
		'		</div>' +	
		'	</div>'
});

var Wizard = Class.create();
Object.extend(Wizard.prototype, PopUp.prototype);
Object.extend(Wizard.prototype, {
	
	options: { width: 600, height: 300, submit: true, onNextCallback: ''},  //Default	
		
	setContent: function() {
		new Insertion.Top($('popupContent'), this.options.html); // <<<<<<
	},
	
	updateContent: function() {
		
	},
	
	setButtons: function(){
		Event.observe($('closePopup'), 'click', this.onclickClose);			
		Event.observe($('next'), 'click', this.onclickNext);
		Event.observe($('previous'), 'click', this.onclickPrevious);

	},
	
	onclickNext: function(){
		var that = thisPopUp;
		var isValidated;
		
		eval("isValidated =" + that.options.validation);
		eval(that.options.onNextCallback);
				
		if ( isValidated && that.options.submit ){	
			that.form.submit();
			that.remove();
		}
	},	
		
	onclickFinish: function(){
		var that = thisPopUp;
		var isValidated;
		
		eval("isValidated =" + that.options.validation);

		if ( isValidated ){			
			that.form.submit();
			that.remove();
		}
	},
	
	onclickPrevious: function(){
		var that = thisPopUp;		
		eval(that.options.onclickPrevious);
	},

	popupHTML: 
	
		'	<div id="popup" class="popup">' +
		'	<form id="wizardForm">' +
		'		<div id="popupContent"></div>' +
		'		<div id="buttonBar" class="buttonBar right">' +
		'			<input type="button" style="display:auto" class="button" value="Previous" name="previous" id="previous" />' +
		'			<input type="button" style="margin-right: 2em; display:auto" class="button" value="Next" name="next" id="next" />' +
		'		</div>' +	
		'		<img id="closePopup" style="position:absolute;top:4px; right:4px;" alt="[x]" title="close" src="/base/image/iconCloseRed.gif" />' +
		'	</form>' +										
		'	</div>'
});

function showWarning(msg, xtype, rtnFunctionName) {
	if (xtype == 0)
		new Warning( {msg: msg, shadow: "true" } );
	else if (xtype == 1){
		new Confirm( {msg: msg, shadow: "true", onclickOk: rtnFunctionName, onclickClose: rtnFunctionName } );
	}
};

function showWarningQuota(msg, rtnFunctionName) {
		new ConfirmQuotaWarning( {msg: msg, shadow: "true", onclickOk: rtnFunctionName } );
};

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}