// General functions

/*
	Open URL functions
 */

// Change the URL of the current page
function openURL(URL) {
	self.location.href= URL;
}

// Open a new window using URL
function openWin(URL) {
		myWin= openNewWindow(URL,"", "");
		return myWin;
}

// Open a new window using URL and generic window size for lookups
function openLookup(URL) {
	myWin= openNewWindow(URL,"","width=800,height=600,scrollbars=yes");
	return myWin;
}

// Open a new window using URL and custom size
function openMyWindow(URL,width,height) {
	myWin= openNewWindow(URL,"","width=" + width + ",height=" + height + ",status=0,toolbar=0,menubar=0,resizable=0,scrollbars=1");
}

// Open a new window using URL and generic forms size
function openForm(URL) {
	myWin= openNewWindow(URL,"","width=500,height=470,status=no,toolbar=no,menubar=no,scrollbars=yes");
	return myWin;
}

// Open a new window using URL and custom options (more than size)
function openNewWindow(URL, title, options) {
	myWin= open(URL, title, options);
	return myWin;
}

// Search for a field on each form and set its value
function set_value(field, value) {
	var field_reference;

	if(field == "") {
		alert("Error: Empty field name.");
		return;
	}

	if(window.FCKeditorAPI) {
		field_reference= FCKeditorAPI.GetInstance(field);
		field_reference.InsertHtml(value);
	} else {

		var found;

		for (i = 0, found = 0; ((i < document.forms.length) && (found == 0)); i++) {
			str="";

			for (j = 0; ((j < document.forms[i].elements.length) && (found == 0)); j++) {
				if(document.forms[i].elements[j].name == field) {
					found= 1;
					form_id= i;
					field_id= j;
				}
			}
		}

		if(found == 0) {
			alert("FIELD NOT FOUND");
		} else {
			document.forms[form_id].elements[field_id].value= value;
		}
	}
}

// Set the value of a given field of a given form
function set_field_value(form_name, field_name, val) {

	var my_form;

	if(!document.forms[form_name]) return false;
	my_form= document.forms[form_name];

	if(!my_form.elements[field_name]) return false;
	my_form.elements[field_name].value=val;

	return true;
}

// Get the value of a field
function get_value(field) {
	var ret;

	if(!document.forms.contenedor.elements[field]) return "";
	return document.forms.contenedor.elements[field].value;
}

// Is a field checked?
function get_checked(field) {
	if(!document.forms.contenedor.elements[field]) return false;
	return document.forms.contenedor.elements[field].checked;
}

function fckInsertHTML(fck_name, text)
{
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(fck_name) ;

	// Check the active editing mode.
	if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
	{
		// Insert the desired HTML.
		oEditor.InsertHtml(text) ;
	}
	else
		alert( 'You must be on WYSIWYG mode!' ) ;
}
