function navigate2(url) {
	document.location.href = url;
}

function htmlEncode(str) {
	return str.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
}

function trim(s) {
	s = s.replace(/^\s*/,'').replace(/\s*$/, '');
	return s;
}

function getClientSize() {
     var dimensions = {width: 0, height: 0};
     if (window.innerWidth && window.innerHeight) {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     } else if (document.documentElement) {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     }
     return dimensions;
}

function dbClearSelect(id) {
	// Seçiniz option ekleniyor.
	var lst = jQuery("#" + id)
		.removeOption(/./)
		.addOption('', 'Seçiniz', true);
	return lst;
}

// tek veya çift sayfa için sayfa numarası metnini biçimlendirir
function formatPageNumber(sayfa_no, sayfa_adedi) {
	var sayfa_no_text;
	if (sayfa_adedi == 2) {
		sayfa_no_text = "" + sayfa_no + "-" + (parseInt(sayfa_no)+1); 
	} else {
		sayfa_no_text = sayfa_no;
	}
	return sayfa_no_text;
}

/**
 * DATABASE ISLEMLERI
 */
function dbFillSelect(url, params, id, valueTextFN) {
	// first clear all options
	var lst = dbClearSelect(id);

	var jsonReturn = null;

	jQuery.getJSON(url, params,
		function (jsonResponse) {
			jsonReturn = jsonResponse;

			// Diger planlari selectbox'a ekliyoruz
			for (var i = 0; i < jsonResponse.length; i++) {
				var valueText = valueTextFN(jsonResponse[i]);
				lst.addOption(valueText.value, valueText.text, false);
			}
		}
	);

	return jsonReturn;
}

function ajaxDestroyDialog(ths) {
	jQuery(ths).dialog("destroy").remove();
}

// CSS style information for modal window overlay
function getOverlayStyle() {
	return {
		backgroundColor: '#f5f5f5',
		opacity: '0.80'
	}
}

function createDialogInternal(dialogOptions, dialogHTML) {
	if (dialogOptions.modal) 
		dialogOptions.overlay = getOverlayStyle();
		dialogOptions.close = function(event, ui) {
		ajaxDestroyDialog(this);
    };

	var dlgDiv = document.createElement("DIV");
	// if a dialog ID is specified, use it
	if (dialogOptions.dialogID) {
		dlgDiv.id = dialogOptions.dialogID;
	}
	dlgDiv.className = "flora";
	dlgDiv.style.display = "none";
	//	dlgDiv.style.overflow = "hidden";
	jQuery(document.body).append(dlgDiv);
			
	var dlg = jQuery(dlgDiv);
	dlg.append(dialogHTML)
		.dialog(dialogOptions)
		.css("display", "block")
		.dialog("open");

	//TODO: this is not the correct way to launch the resize event 
	// init the layout
	if (dialogOptions.resize) dialogOptions.resize();
	// if (dialogOptions.resize) dlg.resize();
}

function ajaxModalWaitDialog(title, waitMessageHTML, width, height, additionalOptions) {
	var messageHTML = '<table border="0" cellpadding="0" cellspacing="0">' +
				   '<tr valign="middle"><td width="46"><img src="i/loading.gif" width="41" height="39" alt="" style="margin-right: 5px;"></td>' + 
				   '<td>' + waitMessageHTML + '</td></tr></table>';
 	ajaxModalDialog(title, messageHTML, width, height, additionalOptions);
}

function ajaxModalDialog(title, messageHTML, width, height, additionalOptions) {
	if (width == null) width = 320;
	if (height == null)	height = 100;		
	if (additionalOptions)
		dialogOptions = additionalOptions;
	else
		dialogOptions = {};

	// dialog options
	dialogOptions.autoOpen = false;
	dialogOptions.modal = true;
	dialogOptions.resizable = false;
	dialogOptions.position = ["center", "center"];
	dialogOptions.width = width;
	dialogOptions.height = height;
	dialogOptions.title = title;
	
	createDialogInternal(dialogOptions, messageHTML);
}

function ajaxAlert(title, messageHTML, width, height, additionalOptions) {
	if (width == null) width = 320;
	if (height == null)	height = 120;
	if (additionalOptions)
		dialogOptions = additionalOptions;
	else
		dialogOptions = {};

	// dialog options
	dialogOptions.closeOnEscape = true;
	dialogOptions.bgiframe = true;

	dialogOptions.autoOpen = false;
	dialogOptions.modal = true;
	dialogOptions.resizable = false;
	dialogOptions.position = ["center", "center"];
	dialogOptions.width = width;
	dialogOptions.height = height;
	dialogOptions.title = title;
	dialogOptions.buttons = {
		"Tamam": function(event) {
			ajaxDestroyDialog(this);
		}
	};
	
	createDialogInternal(dialogOptions, messageHTML);
}

function ajaxConfirm(title, messageHTML, okCallback, width, height, additionalOptions) {
	if (width == null) width = 320;
	if (height == null)	height = 120;
	if (additionalOptions)
		dialogOptions = additionalOptions;
	else
		dialogOptions = {};

	// dialog options
	dialogOptions.autoOpen = false;
	dialogOptions.modal = true;
	dialogOptions.resizable = false;
	dialogOptions.position = ["center", "center"];
	dialogOptions.width = width;
	dialogOptions.height = height;
	dialogOptions.title = title;
	dialogOptions.buttons = {
		"Tamam": function(event) {
			ajaxDestroyDialog(this);
			okCallback(event);
		},
		"İptal": function(event) {
			ajaxDestroyDialog(this);
		}
	};
	
	createDialogInternal(dialogOptions, messageHTML);
}

function ajaxLoad(url, data, container, waitMessage) {
	container.html('<table border="0" cellpadding="0" cellspacing="0">' +
				   '<tr valign="middle"><td width="46"><img src="i/loading.gif" width="41" height="39" alt="" style="margin-right: 5px;"></td>' + 
				   '<td>' + waitMessage + '</td></tr></table>');
	jQuery.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: "html",
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert("Form işlemi sırasında bir hata oluştu: " + textStatus + "\n\n" + errorThrown);
			// typically only one of textStatus or errorThrown
			// will have info
			// this; // the options for this ajax request
		},
		success: function(respHTML) {
			container.html(respHTML);
		}
	});
}

function ajaxDialog(url, data,
	left, top, width, height,
	modal, resizable,
	title, buttons,
	additionalOptions) {
	var dialogOptions;
	if (additionalOptions)
		dialogOptions = additionalOptions;
	else
		dialogOptions = {};
		
	// insert the cancel button
    buttons["İptal"] = function(event) {
    	ajaxDestroyDialog(this);
    };

	// dialog options
	dialogOptions.autoOpen = false;
	dialogOptions.modal = modal;
	dialogOptions.resizable = resizable;
	dialogOptions.position = [left, top];
	dialogOptions.width = width;
	dialogOptions.height = height;
	dialogOptions.title = title;
	dialogOptions.buttons = buttons;
	
	jQuery.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: "html",
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert("Form işlemi sırasında bir hata oluştu: " + textStatus + "\n\n" + errorThrown);
			// typically only one of textStatus or errorThrown
			// will have info
			// this; // the options for this ajax request
		},
		success: function(dialogHTML) {
			createDialogInternal(dialogOptions, dialogHTML);
		}
	});
}
