/**
 * Ten plik jest częścią pakietu SOWA-WWW.
 *
 * Plik JavaScript z kilkoma potrzebnymi w serwisie funkcjami.
 *
 * @package SOWA-WWW 
 * @copyright SOKRATES-Software (http://www.sokrates.pl)
 * 
 * $Id: funcs.js 165 2010-08-19 06:13:04Z szumek $
 */

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

/**
 * Pobiera pozycję karetki w kontrolce
 * @param {Object} control Obiekt kontrolki edycyjnej
 * @return int
 */
function doGetCaretPosition(control) {
	if (control.selectionStart == undefined) {
		// Sposób wręcz karygodny, ale podobno "polecany" przez Microsoft :P
		control.focus();
		var tmpt = control.value;
		var st = '~~`~';
		var range = document.selection.createRange();
		range.text = st;
		var pos = control.value.search(st);
		control.value = tmpt;
		return pos;
	}
	else {
		return control.selectionStart;
	}
}

/**
 * Ustawia pozycję karetki w kontrolce
 * @param {Object} ctrl Obiekt kontrolki edycyjnej
 * @param {int} pos 
 */
function doSetCaretPosition(ctrl, pos) {
	if(ctrl.setSelectionRange) {
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

/**
 * Wstawia tekst do kontrolki w aktualnej pozycji karetki
 * @param {Object} control Obiekt kontrolki edycyjnej
 * @param {String} txt Tekst do wyświetlenia
 */
function insertAtCaret(control, txt) {
	control.focus();
	if (control.selectionStart == undefined) {
		var range = document.selection.createRange();
		range.text = txt;
		range.moveStart('character', txt.length);
	}
	else {
		var t=control.value;
		var p=control.selectionStart;
		control.value = t.substring(0,p)+txt+t.substring(p);
		control.setSelectionRange(p+txt.length,p+txt.length);
	}
}

/**
 * Wstawia znak specjalny do ostwatnio uzywanej  kontrolki.
 * @param {String} char znak do wstawienia
 */
function insertSpecChar(char) {
	if (!LastFocused) {
		var tt = document.getElementsByTagName('textarea');
		if (!tt.length) tt = document.getElementsByTagName('input')
		
		if (tt.length > 0) LastFocused = tt[0]
	}
	
	if (LastFocused) {
		insertAtCaret(LastFocused, char)
		/*
		var cp=doGetCaretPosition(LastFocused)
		var v=LastFocused.value
		LastFocused.value = v.substring(0,cp)+char+v.substring(cp)
		doSetCaretPosition(LastFocused,cp+1)
		LastFocused.focus()
		*/
	}
}

/**
 * Wrapper dla wieloprzeglądarkowości pobierania obiektu o podanym ID
 * @param id identyfikator id elementu
 * @return HTMLElement
 */
function getElementById(id) {
	return is_nav ? document.getElementById(id) : document.all[id]; 
}


/**
 * Ustawia wyświetlanie elementu blokowego (diva)
 * @param {Object} div Obiekt kontrolki
 * @param {bool} visible Czy ma byc widoczny.
 */
function set_display(div, visible) {
	if (visible) {
		div.style['visibility'] = 'visible';
		div.style['display'] = 'block';
	}
	else {
		div.style['visibility'] = 'hidden';
		div.style['display'] = 'none';		
	}
}

/**
 * Przełącza widzialność DIVa. Jeśli podany będzie drugi argument, to miast przełączać po prostu ustawi.
 * @param {String} id Identyfikator diva
 */
function toggleDIV(id) {
	var div = getElementById(id);
	if (div) {
		if (div.style['visibility'] == 'visible') 
			set_display(div, false);
		else
			set_display(div,  true);
	}
}

/** Przełącza bar znaków specjalnych */
function toggleSC() { toggleDIV('specchar-bar'); }

function switchCheckbox(id) {
	var obj = getElementById(id);
	obj.checked = !obj.checked;
}

function ZmienENS(sel) {
	var val = sel.options[sel.selectedIndex].value;
	var obj = getElementById('ff');

	obj.ens.value = val;
}

function Konto(KatID, id, opstring, prefix) {
  if (!prefix) prefix = "./";
  location.href = prefix + 'sowacgi.php?KatID=' + KatID + '&typ=acc&id=' + id + opstring;
}

function save_query(KatID, queryID) {
	location.href = 'sowacgi.php?KatID=' + KatID + '&typ=acc&id=zapytania&op=A&q='+queryID
}

function cancel_order(KatID, idw, agenda) {
	if ( confirm(xMain_cancelOrder)) {
		window.location = 'sowacgi.php?KatID='+KatID+'&typ=acc&id=zamowienia&op=S&idw='+idw+'&agenda='+agenda;
	}
}

function prolong_order(KatID, idw, agenda) {
	var days = parseInt (getElementById('xxx'+idw).value);
	if (isNaN(days)) {
		alert(xMain_enterDays);
	}
	else if (days > 365 || days < 1) {
		alert (xMain_wrongRange);
	}
	else if ( confirm(xMain_oProlong_pref+' '+days+' '+xMain_oProlong_suff)) {
		window.location = 'sowacgi.php?KatID='+KatID+'&typ=acc&id=zamowienia&op=P&idw='+idw+'&agenda='+agenda+'&dni='+days;
	}
}

function cancel_reservation(KatID, sn) {
	if ( confirm(xMain_cancelReserve) ) {
		window.location = 'sowacgi.php?KatID='+KatID+'&typ=acc&id=rezerwacje&op=U&sn='+sn;
	}
}

function prolong_lending(KatID, sn) {
	if ( confirm(xMain_prolongLending) ) {
		window.location = 'sowacgi.php?KatID='+KatID+'&typ=acc&id=wypozyczenia&op=T&sn='+sn;
	}
}

function ckk_showImage(KatID,rid,bid,bw) {
	var img = getElementById('ckk_blob_'+rid);
	if (img) {
		href = 'sowacgi.php?KatID='+KatID+'&typ=getblob&record_id='+rid+'&blob_type=ckk&blob_id='+encodeURI(bid)+'&width='
		img.setAttribute('src', href+bw);
		img.setAttribute('bid', bid);
		
		img_min = getElementById('ckk_blob_'+rid+'_min');
		if (img_min) {
			img_min.setAttribute('src', href+'50');
			img_min.setAttribute('bid', bid);			
		}
	}
}

function ckk_getBlob(KatID,rid,bt,bid) {
	if (!bid && bt=='ckk') {
		var img = getElementById('ckk_blob_'+rid);
		bid = img.getAttribute('bid');
		bt = 'jpg';
	}
	
	href = 'sowacgi.php?KatID='+KatID+'&typ=getblob&record_id='+rid+'&blob_type='+bt+'&blob_id='+encodeURI(bid);
	open(href)
}
