var __cisAppVersionId = "1283439543600";

var onrefreshes = new Array();
function doRefresh() {
	for (var i = 0; i<onrefreshes.length;i++) {
		onrefreshes[i]();
	}
}

var onloads = new Array();
window.onload=function() {
	for ( var i = 0 ; i < onloads.length ; i++ ) {
		onloads[i]();
	}
	
	doRefresh();
};

function submitForm(formName,validate) {
	var execSubmit=true;
	if (validate) {
		execSubmit=validateForm(formName);
	}
	
	if (execSubmit) {
		var form=document.forms[formName];
		if (form.onsubmit) {
			if (form.onsubmit()) {
				form.submit();
			}
		} else {
			form.submit();
		}
	}
}

function resetSearchForm(formName, prefix) {
	var f = document.forms[formName];
	for(var i=0;i<f.elements.length;i++) {
		var e = f.elements[i];
		if(e.name && e.name.startsWith(prefix)) {
			if(e.type.equals('text'))
				e.value = '';
			else if(e.type.equals('select-one'))
				e.selectedIndex = 0;
		}
	}
}

function printSwf(id,url,width,height,bgcolor) {
	widthStr="";
	heightStr="";
	if (width) widthStr=' width="'+width+'"';
	if (height) heightStr=' height="'+height+'"';
	if (!bgcolor) bgcolor="#ffffff";
	document.write('<div style="z-index:2;">');
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" '+widthStr+' '+heightStr+' id="'+id+'" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+url+'" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="'+bgcolor+'" />');
	document.write('<embed swLiveConnect="true" src="'+url+'" quality="high" '+widthStr+' '+heightStr+' bgcolor="'+bgcolor+'" name="'+id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
	document.write('</div>');
};

function jq(myid,prefix) {
	if (!prefix) prefix="#";
	return prefix+myid.replace(/:/g,"\\:").replace(/\./g,"\\.").replace(/\@/g,"\\@");
};

function Cis() {
	this._externalFilesLoadeds_ = new Array();
	this._loadJsLoadedFiles_ = new Array();
	this._loadJsLoadingFiles_ = new Array();
	
	this.fileWasLoaded = function (filename) {
		return this._loadJsLoadedFiles_[filename];
	};
	this.processLoadCallbacks = function(filename) {
		var callbacksArray = this._loadJsLoadingFiles_[filename];
		for(var i=0;i<callbacksArray.length;i++)
			callbacksArray[i]();
		this._loadJsLoadedFiles_[filename] = true;
	};
	this.addLoadCallback = function (filename, callback) {
		if( (typeof this._loadJsLoadingFiles_[filename])=='undefined')
			this._loadJsLoadingFiles_[filename] = new Array();
		var callbacksArray = this._loadJsLoadingFiles_[filename];
		callbacksArray[callbacksArray.length++] = callback;
		return callbacksArray.length==1;
	};
};

Cis.prototype.require = function(filename) {
	this.loadjscssfile(filename,"js");
};

Cis.prototype.loadjscssfile = function(filename, filetype){
	if(!this._externalFilesLoadeds_[filename]) {
		if (filetype=="js"){ //if filename is a external JavaScript file
			var fileref=document.createElement('script')
			fileref.setAttribute("type","text/javascript")
			fileref.setAttribute("src", filename)
		}
		else if (filetype=="css"){ //if filename is an external CSS file
			var fileref=document.createElement("link")
			fileref.setAttribute("rel", "stylesheet")
			fileref.setAttribute("type", "text/css")
			fileref.setAttribute("href", filename)
		}
		if (typeof fileref!="undefined") {
			document.getElementsByTagName("head")[0].appendChild(fileref)
			this._externalFilesLoadeds_[filename] = true;
		}
	}
};

Cis.prototype.loadCss = function(filename) {
	this.loadjscssfile(filename, "css");
};

(function() {
	if(typeof(__cisAppVersionId)=='undefined')
		__cisAppVersionId = "";
})();

Cis.prototype.loadJs = function(filename, callback) {
	var localCallback = ((typeof callback)=='function') ? callback : function(){};
	if(this.fileWasLoaded(filename))
		localCallback();
	else if(this.addLoadCallback(filename, localCallback)) {
		if(__cisAppVersionId!="") {
			var data = (filename.indexOf("?_=")!=-1) ? null : "_=" + __cisAppVersionId;  
			$.ajax({
				type: "GET",
				url: filename,
				cache: true, 
				data: data,
				success: function(){
					cis.processLoadCallbacks(filename);
				},
				dataType: "script"
			});
		}
		else { 
			$.getScript(filename, function(){
				cis.processLoadCallbacks(filename);
			});
		}
	}
};

Cis.prototype.setHidden = function(form, name, value) {
	var f;
	if(typeof(form)=='object') f = form;
	else if(typeof(form)=='string'){
		f = document.getElementById(form);
		if( typeof(form)!='object')
			f = document.forms[form];
	}
	
	if(typeof(f)=='object') {
		if(typeof(f.elements[name])=='undefined') {
			var h = document.createElement('input');
			h.setAttribute("type", "hidden");
			h.setAttribute("name", name);
			h.setAttribute("value", value);
			f.appendChild(h);
		} else {
			f.elements[name].value = value;
		}
	} else {
		console.error("Form " + form + " not found");
	}
};

var cis = new Cis();


var _openDialogTitle = "";

function objectInfo(id) {
	var url="/cis/common/object/show.inc?id="+id;
	openDialog("Info",url);
}

function openDialog(title, url, width, height) {
	_openDialogTitle = title;
	$.ajax({
		url: url,
		cache: false,
		dataType: "html",
		success: function(html){
			_showDialog(title, html, width, height);
		}, error: function( xreq, textStatus, error) {
			alert('Error: ' + textStatus + ': ' + error);
			console.error('Error: ' + textStatus + ': ' + error);
		}
	});
}

function setContentDialog(url) {
	$.ajax({
		url: url,
		cache: false,
		dataType: "html",
		success: function(html){
			if($('#cisInternalDialog').data("dialog")==undefined)
				_showDialog(_openDialogTitle, html);
			else
				$('#cisInternalDialog').html(html);
		}, error: function( xreq, textStatus, error) {
			alert('Error: ' + textStatus + ': ' + error);
			console.error('Error: ' + textStatus + ': ' + error);
		}
	});
}

function closeDialog() {
	_onCloseDialog();
}

var _current_dialog;

function _onCloseDialog() {
	$('#cisInternalDialog').data("dialog").destroy();
}

function _showDialog(title, data, width, height) {
	if(typeof(width)=='undefined') width = "auto";
	if(typeof(height)=='undefined') height = "auto";
	cis.loadCss("/cis/lib/jquery-ui-1.6rc6/themes/conductiva/ui.core.css?_=1283439543600");
	cis.loadCss("/cis/lib/jquery-ui-1.6rc6/themes/conductiva/ui.theme.css?_=1283439543600");
	cis.loadCss("/cis/lib/jquery-ui-1.6rc6/themes/conductiva/ui.dialog.css?_=1283439543600");
	cis.loadJs("/cis/lib/jquery-ui-1.6rc6/ui.core.min.js?_=1283439543600");
	if(navigator.userAgent.indexOf("MSIE 6")>=0)
		cis.loadJs("/cis/lib/jquery-1.3.1/bgiframe/jquery.bgiframe.js?_=1283439543600");
	cis.loadJs("/cis/lib/jquery-ui-1.6rc6/ui.dialog.min.js?_=1283439543600", function() {
		$("#cisInternalDialog").dialog({
			bgiframe: true,
			title: title, 
			width: width, 
			height: height, 
			modal: true,
			bgiframe: true, 
			autoOpen: false,
			close: function() {
				_onCloseDialog();
			}
		});

		$('#cisInternalDialog').html(data);
		$('#cisInternalDialog').data("dialog").open();
		
	});
	
}

var __div;
var __functionToRun;

function openLayer(divId, url, functionToRun) {
	__div = document.getElementById(divId);
	__functionToRun = functionToRun;
	
	$.ajax({
		url: url,
		cache: false,
		dataType: "html",
		success: function(html){
			_showLayer(html);
		}, error: function( xreq, textStatus, error) {
			alert('Error: ' + textStatus + ': ' + error);
			console.error('Error: ' + textStatus + ': ' + error);
		}
	});
}

function _showLayer(data) {
	__div.innerHTML = data;
	__div.style.display = "block";
	
	if(typeof(__functionToRun)=='function')
		__functionToRun();
	else if(typeof(__functionToRun)=='string') 
		eval(__functionToRun);
}

function closeLayer(divId) {
	document.getElementById(divId).style.display = "none";
}


function setContentLayer(divId, url) {
	__div = document.getElementById(divId);
	$.ajax({
		url: url,
		cache: false,
		dataType: "html",
		success: function(html){
			__div.innerHTML = html;
		}, error: function( xreq, textStatus, error) {
			alert('Error: ' + textStatus + ': ' + error);
			console.error('Error: ' + textStatus + ': ' + error);
		}
	});
}

// alert2 is deprectated
function alert2(msg, title) {
	cisAlert(msg, title);
}

function activateReturnKey(formId) {
	$(jq(formId)+' input').live("keypress", function(e) {
		/* ENTER PRESSED*/
		if (e.keyCode == 13) {
			/* FOCUS ELEMENT */
			var inputs = $(this).parents("form").eq(0).find(":input");
			var idx = inputs.index(this);
			if (idx == inputs.length - 1) {
				$(this).parents("form").submit();
			} else {
				inputs[idx + 1].focus(); // handles submit buttons
//				inputs[idx + 1].select();
			}
			return false;
		}
	});
}

function cisAlert(msg, title, callback) {
	_cisLoadAlerts(function(){
		jAlert(msg, title, callback);
	});
}
function cisConfirm(msg, title, callback) {
	_cisLoadAlerts(function(){
		jConfirm(msg, title, callback);
	});
}
function cisPrompt(msg, value, title, callback) {
	_cisLoadAlerts(function(){
		jPrompt(msg, value, title, callback);
	});
}
function _cisLoadAlerts(callback) {
	cis.loadCss('/cis/lib/jquery-1.3.1/themes/conductiva/alerts/jquery.alerts.css?_=1283439543600');
	cis.loadJs("/cis/lib/jquery-1.3.1/alerts/jquery.alerts.js?_=1283439543600", function(){
		callback();
	});
}


function handleSubmit(formId,mustHandle) {
	if (mustHandle) {
		if (document.forms[formId].validate.value=="false") {
			$("#"+formId).validate({ onsubmit: false });
			$("#"+formId).unbind('submit').submit();
		} else {
			$("#"+formId).submit();
		}
	}
	
	return false; 
}

function alboradaAvisoLegal() {
	var url = "/page/legal";
	var width = 800, winWidth = $(window).width();
	var height = 600, winHeight = $(window).height();

	if(winWidth<width) width = winWidth-50;
	if(winHeight<height) height = winHeight-50;
	openDialog("Aviso legal", "/app/legal.ajax.jsp", width, height);
}
