// *******************************************
// *                                         *
// * JavaScript Library of common Functions  *
// *                                         *
// *******************************************

function writeCookie(name,value,days) {
	/*
	 *	Write a cookie on the user machine:
	 *
	 *	Arguments:
	 *
	 *		name:		Name of the cookie (for later retrieval
	 *
	 *		Value:		Text associated with the cookie
	 *					(What you want to save!)
	 *
	 *		days:		Time duration the cookie will be saved
	 *
	 *	Returns:		None
	 *
	 *	Example:
	 *				writeCookie("myCookie", "this was from me", 7);
	 */
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	/*
	 *	Retrieve by name a previously saved cookie
	 *
	 *	Arguments:
	 *
	 *		name:		Name under which the cookie was saved
	 *
	 *	Returns:
	 *
	 *		"value"		Text assoviated with the cookie
	 *	or
	 *		"null"		If te cookie was expired or not found
	 *
	 *	Example:
	 *				myCookie = readCookie("myCookie");
	 */
	var cookie = null;
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			cookie = c.substring(nameEQ.length,c.length);
			break;
		}
	}
	return cookie;
}

function checkPopUp(name, popUp, properties) {
	/*
	 *	Check the cookies 
	 *	if the pop-up was already displayed, display every third time
	 *	If not, then show the pop-up
	 *
	 *	Arguments:
	 *
	 *		name:		Name under which the cookie was saved.
	 *
	 *		popUp:		The URL to pop-up
	 *
	 *		properties:	The properties of the pop-up window
	 *
	 *	Returns:		None
	 *
	 *	Example:
	 *				checkPopUp('specialOffer', 'special-offer.html', 'width=250px height=500px resizable=yes');
	 */
	 var cookie = readCookie(name);
	 //alert ('Cookie '+name+'='+cookie);
	 if (cookie == null) {
	 	cookie = 1;
	 } else {
	 	cookie = parseInt(cookie) + 1;
		if ((cookie % 3) == 0) {
		 	// Pop-up the window!
			window.open(popUp, '_blank', properties);
		}
	 }
	// Create a cookie for the duration of the session only
	writeCookie(name, "0"+cookie, 0);
}

function getSearchAsArray() {
	/*
	 *	getSearchAsArray:
	 *
	 *	Convert location.search into an array of values indexed by name.
	 *
	 *	Returns:
	 *
	 *		results:		Array of values
	 *
	 *	Example:
	 *
	 *		if (location.search) {
	 *			var srchArray = getSearchAsArray();
	 *			var contents  = srcArray["contents"];
	 *		}
	*/
	var minNav3		=	(navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 3);
	var minIE4		=	(navigator.appName.indexOf("Microsoft") >= 0 && parseInt(navigator.appVersion) >= 4);
   // baseline DOM required for this function
	var minDOM		=	minNav3 || minIE4
	var results		=	new Array();
	if (minDOM) {
		var input	=	unescape(location.search.substr(1));
		if (input) {
			var srchArray	=	input.split("&");
			var tempArray	=	new Array();
			for (var i = 0; i < srchArray.length; i++) {
				tempArray				=	srchArray[i].split("=");
				results[tempArray[0]]	=	tempArray[1];
			}
		}
	}
	return results;
}

