// Crea una pop up window.
// Recibe el link, el ancho y el alto.
function popup(src, wd, hg, target){
	if(wd!=null && hg!=null)
		if(target==null)
			window.open(src,'',"width="+wd+", height="+hg+", top="+(screen.height-hg)/2+", left="+(screen.width-wd)/2+", toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no");
		else
			window.open(src,target,"width="+wd+", height="+hg+", top="+(screen.height-hg)/2+", left="+(screen.width-wd)/2+", toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no");
	else
		if(target==null)
			window.open(src);
		else
			window.open(src, target);
}

// Muestra Contenido.
// Recibe como parámetro un div.
function show(div){
	var content = document.getElementById(div);
	
	if(content.style.display == '')
		content.style.display = 'none';
	else
		content.style.display = '';
		
	return div;
}

// Funciones para desplazar valores de listas
function deleteOption(object,index){
	object.options[index] = null;
}

function addOption(object,text,value){
	var defaultSelected = true;
	var selected = true;
	var optionName = new Option(text, value, defaultSelected, selected)
	object.options[object.length] = optionName;
}

function copySelected(fromObject,toObject){
	for (var i=0, l=fromObject.options.length;i<l;i++){
		if (fromObject.options[i].selected)
			addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
	}
		
	for (var i=fromObject.options.length-1;i>-1;i--){
		if (fromObject.options[i].selected)
			deleteOption(fromObject,i);
	}
}

function copyAll(fromObject,toObject){
	for (var i=0, l=fromObject.options.length;i<l;i++){
		addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
	}
	
	for (var i=fromObject.options.length-1;i>-1;i--){
		deleteOption(fromObject,i);
	}
}

// Recorre un arreglo y concatena valores con ', ' en una cadena
function arraytostr(array){
	var str='';
	for(i=0;i<array.length;i++){
		str+=array[i].value;
		if(i+1!=array.length)
			str+=', ';
	}
	return str;
}

// Usar Calendario
function displayCalendar(field_value, form_value){
	new Ajax.Updater($$('input[name='+field_value+']')[0], '../phplib/ajaxcalendar.php', { evalScripts: true, insertion: Insertion.After, method: 'post', parameters: { mode: 1, field: field_value, form: form_value } } );
}

// Rich TextArea
function startTinyMCE(){
	tinyMCE.init( {
		mode : "textareas",
		language: "es",
		width: '100%',
		height: 320,
		plugins: "fullscreen,spellchecker",
		spellchecker_languages: "+Español=es",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_disable: "help",
		theme_advanced_buttons1_add: "separator,fullscreen,spellchecker",
		theme_advanced_buttons2_add: "separator,removeformat,visualaid,separator,sub,sup,separator,charmap",
		theme_advanced_buttons3: "",
		content_css: "../css/tinymce.css",
		theme_advanced_blockformats : "p,h1"
	} );
}
