function deleteItem(form_name, list) {
   var form 	 = eval('document.'+form_name);
   var lb		 = eval('form.'+list);
   lb.options[lb.selectedIndex]=null;
}

function deleteHabitacion(form_name, list) {
   var form 	 = eval('document.'+form_name);
   var lb		 = eval('form.'+list);
   form.sqlDeleteHabitacion.value = 'DELETE FROM tb_tarifasfechashabitaciones WHERE idItemsHabitaciones ='+lb.options[lb.selectedIndex].value;
   lb.options[lb.selectedIndex]=null;
   form.borrar.disabled = 'disabled';
}

function editItem(form_name, list, idTarifaFecha, f1, f2) {
	var form 	 = eval('document.'+form_name);
	var lb		 = eval('form.'+list);
	var idTF	 = eval('form.'+idTarifaFecha);
	var fechaD 	 = eval('form.'+f1);
	var fechaH 	 = eval('form.'+f2);
	var ind 	 = lb.options[lb.selectedIndex].value;
	idTF.value 	 = ind.substr(0,ind.indexOf(","));
	ind = ind.slice(ind.indexOf(",")+1,ind.length);
	fechaD.value = ind.substr(0,ind.indexOf(","));
	ind = ind.slice(ind.indexOf(",")+1,ind.length);
	fechaH.value = ind;
}

function NewItem(theList,itemAdd) {
   theList.options[theList.length]= new Option(itemAdd.text, itemAdd.value, false, false);
}

function fillFechaUpdate(form_name,idHotel,idTarifa) {
   var form 	 = eval('document.'+form_name);
   //window.alert("entró por aqui");
   form.sqlTarifasFechas.value = crearcadenaFechas('INSERT INTO tb_tarifasfechas VALUES ',form.lbFechas,idHotel,idTarifa);
   //window.alert(form.sqlTarifasFechas.value);
}

function fillHabitacionesUpdate(form_name,list) {
   var form 	 = eval('document.'+form_name);
   var lb		 = eval('form.'+list);
   var result	 = '';
   for (var i = 0; i < lb.length; i++) 
	   	result += lb.options[i].value+';';
		
	form.sqlHabitaciones.value = result;
	//window.alert(form.sqlHabitaciones.value);
}

function fillTarifaFechaUpdate(form_name,i,j) {
   var form 	 = eval('document.'+form_name);
   form.sqlTarifasFechas.value = crearcadenaTarifasFechas(form,'UPDATE tb_tarifasfechashabitaciones SET precio=',i,j,' WHERE idTarifaFecha=',' AND idItemsHabitaciones=');
   //window.alert(form.sqlTarifasFechas.value);
}

function fillRecordUpdate(form_name,idHotel) {
   var form 	 = eval('document.'+form_name);
   
   form.sqlHabitaciones.value = crearcadena('INSERT INTO tb_hotel_habitaciones VALUES ',form.habitaciones,idHotel);
   form.sqlAreaHab.value = crearcadena('INSERT INTO tb_hotel_areahab VALUES ',form.itemsAreaHab,idHotel);
   form.sqlRest.value = crearcadena('INSERT INTO tb_hotel_restaurants VALUES ',form.itemsRest,idHotel);
   form.sqlRecreacion.value = crearcadena('INSERT INTO tb_hotel_recreacion VALUES ',form.itemsRecreacion,idHotel);
   form.sqlOtrasAreas.value = crearcadena('INSERT INTO tb_hotel_otrasareas VALUES ',form.itemsOtrasAreas,idHotel);
   
   return true;
}
function moveaddHabitacion(form_name, list1, list2) {
	
   	var form 	 = eval('document.'+form_name);
	var cb   	 = eval('form.'+list1);
	var lb   	 = eval('form.'+list2);
	var edit 	 = false;
	var txtValue = cb.options[cb.selectedIndex].text;
	var val      = cb.options[cb.selectedIndex].value;

	for (var i = 0; i < lb.length; i++) {
	   if (lb.options[i].value == cb.options[cb.selectedIndex].value) {
		    lb.options[i].value = val;
			lb.options[i].text  = txtValue;
			edit = true;
		    break;
	   }  
	}

	if (!edit) {
		lb.options[lb.length] = new Option(txtValue, val, false, false);
	}
}

function moveadd(form_name, list1, list2, cant) {
	
   	var form 	 = eval('document.'+form_name);
	var cantidad = eval('form.'+cant);
	var cb   	 = eval('form.'+list1);
	var lb   	 = eval('form.'+list2);
	var edit 	 = false;
	var txtValue = cantidad.value+'>'+cb.options[cb.selectedIndex].text;
	var val      = cb.options[cb.selectedIndex].value+','+cantidad.value;

	for (var i = 0; i < lb.length; i++) {
	   var ind = lb.options[i].value;
	   if (ind.substr(0,ind.indexOf(",")) == cb.options[cb.selectedIndex].value) {
		    lb.options[i].value = val;
			lb.options[i].text  = txtValue;
			edit = true;
		    break;
	   }  
	}

	if (!edit) {
		lb.options[lb.length] = new Option(txtValue, val, false, false);
	}
	cantidad.value = 0;
	form.cb.setfocus();

}
function moveaddFechas(form_name, lb1, f1, f2, idTarifaFecha) {
	
   	var form 	 = eval('document.'+form_name);
	var idTF	 = eval('form.'+idTarifaFecha);
	var fechaD 	 = eval('form.'+f1);
	var fechaH 	 = eval('form.'+f2);
	var lb   	 = eval('form.'+lb1);
	if (idTF.value == 0 || idTF.value == '') {
	  window.alert("Debe introducir un número en el Identificador");
	  return false;
	}
	if (fechaD.value == '' || fechaH.value == '') {
	  window.alert("Debe introducir las fechas correctamente");
	  return false;
	}
	var edit 	 = false;
	var txtValue = idTF.value+', '+fechaD.value+', '+fechaH.value;
	var val      = idTF.value+', '+fechaD.value+', '+fechaH.value;

	for (var i = 0; i < lb.length; i++) {
	   var ind = lb.options[i].value;
	   if (ind.substr(0,ind.indexOf(",")) == idTF.value) {
		    lb.options[i].value = val;
			lb.options[i].text  = txtValue;
			edit = true;
		    break;
	   }  
	}

	if (!edit) {
		lb.options[lb.length] = new Option(txtValue, val, false, false);
	}
	
	idTF.value 		= null;
	fechaD.value 	= null;
	fechaH.value	= null;
}

function moveall(form_name, name_list1, name_list2) {
   	var form = eval('document.'+form_name);
	var lb1 = form.elements[name_list1];
	var lb2 = form.elements[name_list2];
	
	for (var i = 0; i < lb1.length; i++) {
	   		NewItem(lb2,lb1.options[i]);				        
	     	deleteItem(lb1,i);
			i--; 
	}
}

function crearcadenaTarifasFechas(form,txtSQL1,cantI,cantJ,txtSQL2,txtSQL3)
{  
  var update_str = '';
  var idTF = 0;
  var idHab = 0;
  var pr = 1;

  for (var j=1; j<cantJ; j++) {
	  idHab = (eval('form.'+'idHabit'+j)).value;
	  for (var i=1; i<5; i++) {
		  //window.alert(pr);
		  idTF = (eval('form.'+'idTarifaFecha'+pr)).value;
		  update_str+= txtSQL1+(eval('form.precio'+pr)).value+txtSQL2+idTF+txtSQL3+idHab+';';
		  pr++;
	  }
  }
  //window.alert(update_str);
  return update_str;
}

function crearcadenaFechas(txtSQL,list,idHotel,idTarifa)
{  
  var cant = list.length;
  var insert_str = '';
  var ind = '';
  if (cant > 0)
    var insert_str = txtSQL;

  for (var i=0; i<cant; i++) {
	ind = list.options[i].value
	if (i == 0) {
		insert_str += "(" + ind.substr(0,ind.indexOf(",")) + "," + idTarifa + ",";
		ind = ind.slice(ind.indexOf(",")+1,ind.length);
		insert_str += "#"+ind.substr(0,ind.indexOf(","))+"#,";
		ind = ind.slice(ind.indexOf(",")+1,ind.length);
		insert_str += "#"+ind+"#)";	  
	} else {
		insert_str += ',(' + ind.substr(0,ind.indexOf(",")) + ',' + idTarifa + ',';
		ind = ind.slice(ind.indexOf(",")+1,ind.length);
		insert_str += "#"+ind.substr(0,ind.indexOf(","))+"#,";
		ind = ind.slice(ind.indexOf(",")+1,ind.length);
		insert_str += "#"+ind+"#)";	
	}
  }
  
  insert_str += ';';
  return insert_str;
}

function crearcadena(txtSQL,list,idHotel)
{  
  var cant = list.length;
  var insert_str = '';
  if (cant > 0)
    var insert_str = txtSQL;

  for (var i=0; i<cant; i++) {
	if (i == 0)
	  insert_str += '(' + idHotel + ',' + list.options[i].value +')';
	else
	  insert_str += ',(' + idHotel + ',' + list.options[i].value +')';
  }
  
  insert_str += ';';
  return insert_str;
}

var sql_box_locked = false;
var only_once_elements = new Array();

function selectContent( element, lock, only_once ) {
    if ( only_once && only_once_elements[element.name] ) {
        return;
    }

    only_once_elements[element.name] = true;

    if ( lock  ) {
        return;
    }

    element.select();
}

/**
 * Displays an confirmation box before to submit a "DROP DATABASE" query.
 * This function is called while clicking links
 *
 * @param   object   the link
 * @param   object   the sql query to submit
 *
 * @return  boolean  whether to run the query or not
 */
function confirmLinkDropDB(theLink, theSqlQuery)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(confirmMsgDropDB + '\n' + confirmMsg + ' :\n' + theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLinkDropDB()' function

function confirmLink(theLink, confirmMsg)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
	
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(confirmMsg);
    if (is_confirmed) {
        if ( typeof(theLink.href) != 'undefined' ) {
            theLink.href += '&is_js_confirmed=1';
        } else if ( typeof(theLink.form) != 'undefined' ) {
            theLink.form.action += '?is_js_confirmed=1';
        }
    }

    return is_confirmed;
} // end of the 'confirmLink()' function

function confirmAction(theMessage)
{
    // TODO: Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(theMessage);

    return is_confirmed;
} // end of the 'confirmAction()' function


function emptyCheckTheField(theForm, theFieldName)
{
    var isEmpty  = 1;
    var theField = theForm.elements[theFieldName];
    // Whether the replace function (js1.2) is supported or not
    var isRegExp = (typeof(theField.value.replace) != 'undefined');

    if (!isRegExp) {
        isEmpty      = (theField.value == '') ? 1 : 0;
    } else {
        var space_re = new RegExp('\\s+');
        isEmpty      = (theField.value.replace(space_re, '') == '') ? 1 : 0;
    }

    return isEmpty;
} // end of the 'emptyCheckTheField()' function


function checkTableEditForm(theForm, fieldsCnt)
{
    // TODO: avoid sending a message if user just wants to add a line
    // on the form but has not completed at least one field name

    var atLeastOneField = 0;
    var i, elm, elm2, elm3, val, id;

    for (i=0; i<fieldsCnt; i++)
    {
        id = "field_" + i + "_2";
        elm = getElement(id);
        if (elm.value == 'VARCHAR' || elm.value == 'CHAR') {
            elm2 = getElement("field_" + i + "_3");
            val = parseInt(elm2.value);
            elm3 = getElement("field_" + i + "_1");
            if (isNaN(val) && elm3.value != "") {
                elm2.select();
                alert(errorMsg1);
                elm2.focus();
                return false;
            }
        }

        if (atLeastOneField == 0) {
            id = "field_" + i + "_1";
            if (!emptyCheckTheField(theForm, id)) {
                atLeastOneField = 1;
            }
        }
    }
    if (atLeastOneField == 0) {
        var theField = theForm.elements["field_0_1"];
        alert(errorMsg0);
        theField.focus();
        return false;
    }

    return true;
} // enf of the 'checkTableEditForm()' function


function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
    }

    return true;
}

function setCheckboxes( container_id, state ) {
    var checkboxes = document.getElementById(container_id).getElementsByTagName('input');

    for ( var i = 0; i < checkboxes.length; i++ ) {
        if ( checkboxes[i].type == 'checkbox' ) {
            checkboxes[i].checked = state;
        }
    }

    return true;
} // end of the 'setCheckboxes()' function


// added 2004-05-08 by Michael Keck <mail_at_michaelkeck_dot_de>
//   copy the checked from left to right or from right to left
//   so it's easier for users to see, if $cfg['ModifyAtRight']=true, what they've checked ;)
function copyCheckboxesRange(the_form, the_name, the_clicked)
{
    if (typeof(document.forms[the_form].elements[the_name]) != 'undefined' && typeof(document.forms[the_form].elements[the_name + 'r']) != 'undefined') {
        if (the_clicked !== 'r') {
            if (document.forms[the_form].elements[the_name].checked == true) {
                document.forms[the_form].elements[the_name + 'r'].checked = true;
            }else {
                document.forms[the_form].elements[the_name + 'r'].checked = false;
            }
        } else if (the_clicked == 'r') {
            if (document.forms[the_form].elements[the_name + 'r'].checked == true) {
                document.forms[the_form].elements[the_name].checked = true;
            }else {
                document.forms[the_form].elements[the_name].checked = false;
            }
       }
    }
}


function setCheckboxColumn(theCheckbox){
    if (document.getElementById(theCheckbox)) {
        document.getElementById(theCheckbox).checked = (document.getElementById(theCheckbox).checked ? false : true);
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = document.getElementById(theCheckbox).checked;
        }
    } else {
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = (document.getElementById(theCheckbox +'r').checked ? false : true);
            if (document.getElementById(theCheckbox)) {
                document.getElementById(theCheckbox).checked = document.getElementById(theCheckbox + 'r').checked;
            }
        }
    }
}


function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
} // end of the 'setSelectOptions()' function


function insertValueQuery() {
    var myQuery = document.sqlform.sql_query;
    var myListBox = document.sqlform.dummy;

    if(myListBox.options.length > 0) {
        sql_box_locked = true;
        var chaineAj = "";
        var NbSelect = 0;
        for(var i=0; i<myListBox.options.length; i++) {
            if (myListBox.options[i].selected){
                NbSelect++;
                if (NbSelect > 1)
                    chaineAj += ", ";
                chaineAj += myListBox.options[i].value;
            }
        }

        //IE support
        if (document.selection) {
            myQuery.focus();
            sel = document.selection.createRange();
            sel.text = chaineAj;
            document.sqlform.insert.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (document.sqlform.sql_query.selectionStart || document.sqlform.sql_query.selectionStart == "0") {
            var startPos = document.sqlform.sql_query.selectionStart;
            var endPos = document.sqlform.sql_query.selectionEnd;
            var chaineSql = document.sqlform.sql_query.value;

            myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
        } else {
            myQuery.value += chaineAj;
        }
        sql_box_locked = false;
    }
}

/**
  * listbox redirection
  */
function goToUrl(selObj, goToLocation) {
    eval("document.location.href = '" + goToLocation + "pos=" + selObj.options[selObj.selectedIndex].value + "'");
}

/**
 * getElement
 */
function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e];
    }
    return document.getElementById(e);
}

/**
  * Refresh the WYSIWYG-PDF scratchboard after changes have been made
  */
function refreshDragOption(e) {
    myid = getElement(e);
    if (myid.style.visibility == 'visible') {
        refreshLayout();
    }
}

/**
  * Refresh/resize the WYSIWYG-PDF scratchboard
  */
function refreshLayout() {
        myid = getElement('pdflayout');

        if (document.pdfoptions.orientation.value == 'P') {
            posa = 'x';
            posb = 'y';
        } else {
            posa = 'y';
            posb = 'x';
        }

        myid.style.width = pdfPaperSize(document.pdfoptions.paper.value, posa) + 'px';
        myid.style.height = pdfPaperSize(document.pdfoptions.paper.value, posb) + 'px';
}

/**
  * Show/hide the WYSIWYG-PDF scratchboard
  */
function ToggleDragDrop(e) {
    myid = getElement(e);

    if (myid.style.visibility == 'hidden') {
        init();
        myid.style.visibility = 'visible';
        myid.style.display = 'block';
        document.edcoord.showwysiwyg.value = '1';
    } else {
        myid.style.visibility = 'hidden';
        myid.style.display = 'none';
        document.edcoord.showwysiwyg.value = '0';
    }
}

/**
  * PDF scratchboard: When a position is entered manually, update
  * the fields inside the scratchboard.
  */
function dragPlace(no, axis, value) {
    if (axis == 'x') {
        getElement("table_" + no).style.left = value + 'px';
    } else {
        getElement("table_" + no).style.top  = value + 'px';
    }
}


function goFiltrarHoteles(selObj) {

   window.location.href = "restaurantes.php?where="+selObj.options[selObj.selectedIndex].value;;
}

function goById(selObj,pageGo,paramPage) {
   
   //window.alert('Entró por la segunda: '+selObj.options[selObj.selectedIndex].value);
   if(pageGo != '') {
     window.location.href = pageGo+selObj.options[selObj.selectedIndex].value+paramPage;
   } else {
     window.location.href = selObj.options[selObj.selectedIndex].value;
	 //window.alert('Entró por la segunda: '+selObj.options[selObj.selectedIndex].value);
   }
}
