/*

POZOJ
(cc) s1e, majx, ~GAF

--------------------------------------
JAHVESKRIPT - common subroutines
--------------------------------------

*/



// We call the notice constructor at pageload.
window.onload = function()
{
	if ( document.getElementById && document.createElement )
	{
		_notice.init();
		_cart.init();
		_searchinit();
	}
}


function _searchinit()
{
	var searchBox = document.getElementById('modSearchBox_Word');
	if ( searchBox )
	{
		searchBox.onfocus = function()
		{
			if ( this.value == 'Naslov knjige ali avtor' ) this.value = '';
		}
		
		searchBox.onblur = function()
		{
			if ( this.value == '' ) this.value = 'Naslov knjige ali avtor';
		}
	}
}


// Function manipulates the global notice container, which presents us what is happening
// with dynamic xmlhttp sessions.
function change_notice(text)
{
	var notice_container_id = 'modNotice';
	var notice_container = findobj(notice_container_id);
	var text = document.createTextNode(text);

	// We check if the container exists as an element.
	if ( notice_container )
	{
		// We check if he already has child nodes in it.
		if ( notice_container.hasChildNodes )
		{
			// If he has, we check if we have to replace a node or del it..
			if ( text )
			{
				// We replace it.
				notice_container.replaceChild(text, notice_container.childNodes[0]);
			}
			else
			{
				// del it.
				notice_container.delChild(notice_container.childNodes[0]);
			}
		}
		// Else we only append a new text node.
		else 	notice_container.appendChild(text);
	}


}


function addEvent(obj, evType, fn, useCapture)
{
	if (obj.addEventListener)
	{
    	obj.addEventListener(evType, fn, useCapture);
    	return true;
  	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else 
	{
    	alert("Handler could not be attached");
	}
}


function showhide_init_text(obj, text)
{
	if ( obj )
	{
		var value = String(obj.value);
		if ( value == text ) obj.value = "";
		if ( value == "" ) obj.value = text;
	}
}


// Function finds an object with the specified id, and returns it.
function findobj(n, d) {
	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=findobj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// Function returns a random number within given boundaries.
function getRandomNum(lbound, ubound) {
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}


// Function un/hides the div with the given id, and sets it text to the second parameter.
function popup_div(id, text)
{
	var div = findobj(id);

	if ( div ) {
		// We empty the div.
		div.innerHTML = '';

		if ( text != '' )
		{
			// Create text node.
			var t = document.createTextNode(text);
			// Append the text node.
			div.appendChild(t);

			if ( div.style.display == 'none' ) {
				// Toggle visibility.
				showhide(id);
			}
		}
		else {
			if ( div.style.display != 'none' ) {
				// Toggle visibility.
				showhide(id);
			}
		}
	}
}


// Function aids in encoding special slovenian characters by encoding them.
function SloCharHack(str) {
	s = new String(str);

	s = s.replace(/¾/g,"zssss");
	s = s.replace(/®/g,"ZSSSS");
	s = s.replace(/¹/g,"sssss");
	s = s.replace(/©/g,"SSSSS");
	s = s.replace(/è/g,"cssss");
	s = s.replace(/È/g,"CSSSS");
	s = s.replace(/æ/g,"cjsss");
	s = s.replace(/Æ/g,"CJSSS");
	s = s.replace(/ð/g,"djsss");
	s = s.replace(/Ð/g,"DJSSS");

	return s;
}


// Function returns a random character.
function getRandomChar(number, lower, upper, other, extra) {
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnopqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	if (number == true)
	charSet += numberChars;
	if (lower == true)
	charSet += lowerChars;
	if (upper == true)
	charSet += upperChars;
	if (other == true)
	charSet += otherChars;
	return charSet.charAt(getRandomNum(0, charSet.length));
}


// Function creates a random string of the given length.
function generateRndStr(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther) {
	var rc = "";
	if (length > 0)
	rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
	for (var idx = 1; idx < length; ++idx) {
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
	}
	return rc;
}


// Function is a shortcut with only one parameter, to get a random string of a variable length.
function getRndStr(length) {
	return generateRndStr(length, '', 0, 1, 0, 0, 1, 1, 1, 0);
}


// Function decodes an url encoded stringor.
function decode(str) {
	if ( str )
	{
		var result = "";

		for (var i = 0; i < str.length; i++) {
			if (str.charAt(i) == "+") result += " ";
			else
			result += str.charAt(i);
		}

		return unescape(result);
	}
	else return false;
}



// The notice object.
// Used for manipulation with the sidewide present object of information,
// that lets the user know what is going on.
_notice = {

	// Variables.
	notice_container_id : 'modNotice',
	notice_container : '',
	default_text : 'V pripravljenosti.',
	reset_time : 2000,
	s_flag : 0,


	
	hide_container : function() {
		this.notice_container.style.visibility = 'hidden';
	},

	// Function that resets the notice text after a certain interval
	reset : function()
	{
		this.change(this.default_text);
		// We hide the notifier.
		setTimeout("_notice.hide_container()", 1000);
		this.s_flag = 0;		
	},


	// Main function that actually commits changes to the notice object, and setups timers to reset the
	// notice to the default after a certain time.
	change : function(text)
	{
		if ( this.notice_container )
		{
			// We display the container.
			this.notice_container.style.visibility = 'visible';
			// Setup the text node.
			var text = document.createTextNode(text);

			// We check if he already has child nodes in it.
			if ( this.notice_container.hasChildNodes )
			{
				// If he has, we check if we have to replace a node or del it..
				if ( text )
				{
					// We replace it.
					this.replace(text);
				}
				else
				{
					this.del();
				}
			}
			// Else we only append a new text node.
			else 	this.insert(text);

			// Here we check if we have permission to setup a timeout call,
			// that will reset the notice to the default text.
			if ( this.s_flag != 1 )
			{
				this.s_flag = 1;
				setTimeout("_notice.reset()", this.reset_time);
			}
		}
	},


	// Function inserts a brand new text node into the container.
	insert : function(node)
	{
		this.notice_container.appendChild(node);
	},


	// Function replaces the existing text node inside the container.
	replace : function(replacement)
	{
		this.notice_container.replaceChild(replacement, this.notice_container.childNodes[0]);
	},


	// Function dels the text node inside the notification container.
	del : function()
	{
		this.notice_container.deleteChild(this.notice_container.childNodes[0]);
	},


	// Constructor.
	init : function()
	{
		if ( document.getElementById(this.notice_container_id) )
		{
			this.notice_container = document.getElementById(this.notice_container_id);
			this.s_flag = 0;
		}
		else this.s_flag = 1;
	}
}
