
datagrid_selected = 'url(include/img/datagrid_selected.gif)';
datagrind_roll_over = 'url(include/img/datagrid_roll_over.gif)';
datagrind_default = '#F8F8F8';
wait_message_for_widgets = '<table width="100%" height="100%"><tr><td class="content" align="center" valign="middle">Por favor espere...</td></tr></table>';

function forceAsignerUser(user_id)
{
	var asigner_user_index = 0;
	asigner_user_selector = document.getElementById('asigner_user');
	for (i = 0; i < asigner_user_selector.length; ++i) {
		if (asigner_user_selector.options[i].value == user_id) {
			asigner_user_index = i;
			break;
		}
	}
	if (asigner_user_index != 0) {
		asigner_user_selector.selectedIndex = asigner_user_index;
		asigner_user_selector.disabled = true;
	}
}

function displayMessage(message) {
	document.getElementById('message_text').innerHTML = message;
	//document.getElementById('message').style.width = getPageWidth();
	document.getElementById('message').style.height = getPageHeight();
	document.getElementById('message_container').style.marginTop = getScrollY();
	self.window.blur();
	self.window.focus();
	document.getElementById('message').style.visibility = 'visible';
	document.getElementById('message_container').style.visibility = 'visible';
}

function hideMessage() {
	document.getElementById('message_container').style.visibility = 'hidden';
	document.getElementById('message').style.visibility = 'hidden';
	document.getElementById('message_text').innerHTML = '';
}

function displayOperationMessage(message) {
	document.getElementById("operationMessage").innerHTML = message;	
	document.getElementById("operationMessage").style.display = 'block';	
	//setTimeout("cleanOperationMessage()", 5000);
}
function displayOperationMessageRed(message) {
	document.getElementById("operationMessage").innerHTML = message;	
	//document.getElementById("operationMessage").style.display = 'block';	
	//setTimeout("cleanOperationMessage()", 5000);
}

function cleanOperationMessage() {
	document.getElementById("operationMessage").style.display = 'none';	
}

function cleanDiv(div_id) {
	document.getElementById(div_id).innerHTML = '';
}

function tryDeletePhoto(input_name, image_name) {
	showLoading();
	obj.deleteCarPhoto(image_name, function(result) {
		eval(result);
		hideLoading();
	});
}

function tryDelete(page, gridSelectionsArray, gridValuesArray) {
	showLoading();
	var selectedIndex = 0;
	itemsToDelete = new Array();
	for (var i = 0; i < gridSelectionsArray.length; ++i) {
		if (gridSelectionsArray[i] == true) {
			itemsToDelete[selectedIndex] = gridValuesArray[i];
			++selectedIndex;
		}
	}
	obj.deleteRecords(page, itemsToDelete, function(result) {
		eval(result);
		hideLoading();
	});

}

function trySave() {
	launchSubmit_general_form();
}

function tryCancel(page) {
	for (image_changed in images_to_save) {
		if (images_to_save[image_changed][0] != '') {
			showLoading();
			obj.cancelUploadedFiles(images_to_save, function(result) {
				hideLoading();
			});
		}
	}
	getURL("?page=" + page);
}

function deletePreviousUpload(file_to_delete, thumbnails)
{
	showLoading();
	
	obj.cancelUploadedFiles([[file_to_delete, thumbnails]], function(result) {
		hideLoading();
	});
}

function tryEdit(gridSelectionsArray, gridValuesArray, editionPage) {
	selectedIndex = tryOperation(gridSelectionsArray, gridValuesArray);
	if (selectedIndex >= 0)
		getURL(editionPage + "id=" + gridValuesArray[selectedIndex]);
}

function tryEditRecord(recordId, editionPage) {
	getURL(editionPage + "id=" + recordId);
}

function tryPrint(gridSelectionsArray, gridValuesArray, editionPage) {
	openNewWindow(editionPage);
}

function tryPrintRecord(recordId, editionPage) {
	openNewWindow(editionPage + "id=" + recordId);
}

function tryOperation(gridSelectionsArray, gridValuesArray) {
	var selectedItems = 0;
	var selectedIndex = 0;
	for (var i = 0; i < gridSelectionsArray.length; ++i) {
		if (gridSelectionsArray[i] == true) {
			++selectedItems;
			selectedIndex = i;
		}
	}
	if (selectedItems == 0) {
		displayMessage("Favor de seleccionar un elemento.");
		return -1;
	}
	if (selectedItems > 1) {
		displayMessage("Las operaciones Agregar, Consultar y Modificar solo permiten un elemento a la vez.<br><br>Favor de deseleccionar los elementos que sobran.");
		return -1;
	}
	return selectedIndex;
}

function standardTableRowHandler(tableRow, rowIndex, gridSelectionsArray, highLight) {
	if (highLight == 0) {
		if (gridSelectionsArray[rowIndex]) {
			tableRow.style.background = datagrid_selected;
		}
		else {
			tableRow.style.background = datagrind_default;
		}
	} else if (highLight == 1) {
		if (!gridSelectionsArray[rowIndex]) {
			tableRow.style.background = datagrind_roll_over;
		}
	} else if (highLight == 2) {
		if (gridSelectionsArray[rowIndex]) {
			tableRow.style.background = datagrind_roll_over;
			gridSelectionsArray[rowIndex] = false;
		}
		else {
			tableRow.style.background = datagrid_selected;
			gridSelectionsArray[rowIndex] = true;
		}
    }
}

function getURL(pageURL) {
	document.location.href = pageURL;
}


function openNewWindow(pageURL) {
	window.open(pageURL, "", "toolbar=YES,menubar=YES,location=YES,directory=NO,width=1000,height=500,resizable=YES,scrollbars=YES,status=YES");
}

function generateStandardGridConnector(table_name, offset, extra_conditions, sorting, target_div, filtering) {
	showLoading();
	obj.generateStandardGridConnector(table_name, offset, extra_conditions, sorting, target_div, filtering, function(result) {
		eval(result);
		hideLoading();
	});
}

function changeStandardGridFilterStatus(table_id, enable_disable)
{
	showLoading();
	obj.changeStandardGridFilterStatus(table_id, enable_disable, function(result) {
		eval(result);
		hideLoading();
	});
}


function getCalendarSelector(start_date, target_input, custom_vars) {
	if (isNaN(custom_vars[1]) || custom_vars[1] == "")
	{
		displayMessage("Favor de Seleccionar la sucursal antes de seleccionar la Hora y Fecha.");
		return;
	}
	document.getElementById('calendar_selector_' + target_input).innerHTML = wait_message_for_widgets;
	showLoading();
	obj.getScheduler(start_date, target_input, custom_vars, function(result) {
		document.getElementById('calendar_selector_' + target_input).innerHTML = result;
		hideLoading();
	});
}

function getFileUploader(target_input, current_value, record_id, thumbnails) {
	showLoading();
	document.getElementById('file_uploader_' + target_input).innerHTML = wait_message_for_widgets;
	obj.getFileUploader(target_input, current_value, record_id, thumbnails, function(result) {
		document.getElementById('file_uploader_' + target_input).innerHTML = result;
		hideLoading();
	});
}

function getDateSelector(start_date, target_input) {
	showLoading();
	document.getElementById('date_time_selector_' + target_input).innerHTML = wait_message_for_widgets;
	obj.getDateSelector(start_date, target_input, function(result) {
		document.getElementById('date_time_selector_' + target_input).innerHTML = result;
		hideLoading();
	});
}

function getLongDateSelector(start_date, target_input, years_before ,years_after, default_selected) {
	showLoading();
	document.getElementById('long_date_time_selector_' + target_input).innerHTML = wait_message_for_widgets;
	obj.getLongDateSelector(start_date, target_input, years_before ,years_after, default_selected, function(result) {
		document.getElementById('long_date_time_selector_' + target_input).innerHTML = result;
		hideLoading();
	});
}

function getComboBoxEditor(target_input, selected_value, group_name, params_for_refreshing) {
	showLoading();
	document.getElementById('combobox_editor_' + target_input).innerHTML = wait_message_for_widgets;
	obj.getComboBoxEditor(target_input, selected_value, group_name, params_for_refreshing, function(result) {
		eval(result);
		hideLoading();
	});
}

function comboBoxEditorMoveRow(input_id, selected_row, direction) {
	selected_row = parseInt(selected_row);
	direction = parseInt(direction);
	
	comboBoxEditorUpdateArray(input_id);
	eval('var temp_options = array_of_options_' + input_id + '[' + selected_row + ']; array_of_options_' + input_id + '[' + selected_row + '] = array_of_options_' + input_id + '[' + (selected_row + direction) + ']; array_of_options_' + input_id + '[' + (selected_row + direction)+ '] = temp_options;');
	refreshComboBoxEditor(input_id)
}

function refreshComboBoxEditor(input_id) {
	var array_of_options = eval('array_of_options_' + input_id );
	content = '';
	for (var i = 0; i < array_of_options.length; ++i)  {
		if (array_of_options[i][0] != -1) {
			content += '<table border="0" cellspacing="0" cellpadding="2" width="100%" >';
			content += '<tr>';
			content += '<td align="center"><input type="text" id="combobox_editor_' + input_id +'_row_' + i + '" name="combobox_editor_' + input_id +'_row_' + i + '" value="' + array_of_options[i][2] + '"></td>';
			content += '<td>';
	
			if (i > 0)
				content += '<a href="javascript:void(0)" onclick="comboBoxEditorMoveRow( \'' + input_id + '\', ' + i + ', -1)" title="Subir"><img src="include/img/move_up.png" width="16" height="16" /></a> ';
			else
				content += '<img src="include/img/move_up_disabled.png" width="16" height="16" /> ';
			
			if (i < array_of_options.length - 1)
				content += '<a href="javascript:void(0);" onclick="comboBoxEditorMoveRow( \'' + input_id + '\', ' + i + ', +1)" title="Bajar"><img src="include/img/move_down.png" width="16" height="16" /></a>';
			else
				content += '<img src="include/img/move_down_disabled.png" width="16" height="16" />';
	
			content += ' <a href="javascript:void(0);" onclick="comboBoxEditorRemoveRow( \'' + input_id + '\', ' + i + ')" title="Eliminar"><img src="include/img/delete.png" width="16" height="16" /></a>';
			
			content += '<td></tr>';
			content += '</table>';
		}
	}
	document.getElementById('row_container_' + input_id).innerHTML = content;
}

function addComboboxOption(input_id) {
	comboBoxEditorUpdateArray(input_id);
	eval('array_of_options_' + input_id + '[array_of_options_' + input_id + '.length] = [array_of_options_' + input_id + '.length, \'0\', \'\'];');
	refreshComboBoxEditor(input_id);
}

function comboBoxEditorRemoveRow(input_id, selected_row) {
	comboBoxEditorUpdateArray(input_id);
	eval('array_of_options_' + input_id + '[' + selected_row + '][0] = -1 ;');
	refreshComboBoxEditor(input_id);
}

function comboBoxEditorSave(input_id, group_name, selected_value, params_for_refreshing) {
	comboBoxEditorUpdateArray(input_id);
	var array_of_options = eval('array_of_options_' + input_id );
	
	obj.saveComboBoxEdition(input_id, array_of_options, group_name, selected_value, params_for_refreshing, function(result) {
		eval(result);
		hideLoading();
	});
}

function comboBoxEditorUpdateArray(input_id) {
	for (var i = 0; i < eval('array_of_options_' + input_id  + '.length'); ++i)  {
		if (eval('array_of_options_' + input_id  + '[' + i + '][0] != -1'))
			eval('array_of_options_' + input_id + '[' + i + '][2] = \'' + document.getElementById('combobox_editor_' + input_id +'_row_' + i).value + '\'');
	}	
}

function comboBoxEditorSort(input_id, direction) {
	comboBoxEditorUpdateArray(input_id);
	var temp_operator;
	if (direction == "ASC") temp_operator = "<"; else temp_operator = ">";
	var sorting_finished = false;
	while (!sorting_finished)
	{
		sorting_finished = true;
		for (var i = 0; i < eval('array_of_options_' + input_id  + '.length') - 1; ++i)  {
			if (eval('array_of_options_' + input_id  + '[' + i + '][2] ' + temp_operator + 'array_of_options_' + input_id  + '[' + (i + 1) + '][2]')) {
				eval('var var_temp = array_of_options_' + input_id + '[' + i + '];');
				eval('array_of_options_' + input_id + '[' + i + '] = array_of_options_' + input_id + '[' + (i + 1) + '];');
				eval('array_of_options_' + input_id + '[' + (i + 1) + '] = var_temp;');
				sorting_finished = false;
			}
		}
	}
	refreshComboBoxEditor(input_id);
}

function getPageWidth() {
	var x;
	var test1 = document.body.scrollWidth;
	var test2 = document.body.offsetWidth;
	if (test1 > test2) {// all but Explorer Mac
		x = document.body.scrollWidth;
	}
	else { // Explorer Mac; //would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
	}
	return x;
}

function getPageHeight() {
	var y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) { // all but Explorer Mac
		y = document.body.scrollHeight;
	}
	else { // Explorer Mac; //would also work in Explorer 6 Strict, Mozilla and Safari
		y = document.body.offsetHeight;
	}
	return y;
}

function getScrollX(){
	var x;
	if (self.pageYOffset) {// all except Explorer
		x = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {// Explorer 6 Strict
		x = document.documentElement.scrollLeft;
	}
	else if (document.body) {// all other Explorers
		x = document.body.scrollLeft;
	}
	return x;
}

function getScrollY(){
	var y;
	if (self.pageYOffset) { // all except Explorer
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {	// Explorer 6 Strict
		y = document.documentElement.scrollTop;
	}
	else if (document.body) { // all other Explorers
		y = document.body.scrollTop;
	}
	return y;
}

positionIsReady = false;
isLoading = false;

function showLoading() {
	isLoading = true;
	if (positionIsReady)
		document.getElementById('loading').style.display = 'block';
}

function hideLoading() {
	document.getElementById('loading').style.display = 'none';
	isLoading = false;
}

function updateLoaderPosition(e) {
	var tempX = 0;
	var tempY = 0;
	
	if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
		tempX = e.pageX;
		tempY = e.pageY;
	}
	
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	
	document.getElementById('loading').style.top  = (tempY + 10) + 'px';
	document.getElementById('loading').style.left = (tempX + 15) + 'px';

	if (!positionIsReady && isLoading) {
		positionIsReady = true;
		document.getElementById('loading').style.display = "block";
	}
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
