function axiWellLoad(elem, baseUrl, width) {
   elem.well = new Array();
   for (var i=0; i < width; i++) {
      elem.well[i] = new Image();
      elem.well[i].src = baseUrl+"&ImgIndex="+i;
   }
}

function ReplaceSubString( str, searchFor, replaceWith ) {
	while ( str.search(searchFor) != -1 ) {
		parmArray=str.split(searchFor);
		tempVar=parmArray.join(replaceWith) ;
		str = tempVar;
	}
	return str;
}

function gotoURL(url, aTarget){
	var webDbs;
	var webPaths;
	webDbs = document.Notes.WebDbAliases;
	webPaths = document.Notes.WebDbPaths;
	
	if (webDbs.length === undefined){
	webDbs = new Array(document.Notes.WebDbAliases)
	}
	if (webPaths.length === undefined){
	webPaths = new Array(document.Notes.WebDbPaths)
	}

//	alert(webDbs.length);
	
	for (i=0; i<webDbs.length; i++) {
		url = ReplaceSubString(url, webDbs[i].value, webPaths[i].value);
	//	alert(url);
	}
	url = ReplaceSubString(url, '#LANG#', document.Notes.QueryLanguageEncoded.value);
	
	
	switch (aTarget) {
					case "_self" :
					document.location.href=url;
					break;
				case "_blank":
					window.open(url, 'NewWindow', '');
					break;
				case "_top":
					top.document.location.href=url;
					break;
				case "_parent":
					parent.document.location.href=url;
					break;
	}
}

 function createCookie(name,value,days) {
		 if (days) {
		 		 var date = new Date();
		 		 date.setTime(date.getTime()+(days*24*60*60*1000));
		 		 var expires = "; expires="+date.toGMTString();
		 }
		 else var expires = "";
		 document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
		 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) return c.substring(nameEQ.length,c.length);
		 }
		 return null;
}

function eraseCookie(name) {
		 createCookie(name,"",-1);
}


/* --------------------------    AJAX VIEW LOAD    ------------------------------*/
/*This function is called from the onload event of data forms in WebDotNSF content modules*/
/*It loads the contents of Category Lists in the page*/

function ajaxViewLoad(){

	$(document).ready(function(){

			//trigger the ajaxCompleted function (see below) after the content returned by the ajax request has been loaded
			//NOTE: the reference to "body" doesn't have any meaning: there needs to be a selector to call the ajaxComplete function,
			//this selector must be 1) present on the page and 2) different from .viewcategory (if .viewcategory is used, the onclick events won't work)
					
			$('.viewcategory').each(function(index) {
	    			if($(this).innerHTML='Loading...'){
	    				$(this).text("Please wait...");
	    				//var view = $(this).attr("view");  'the view is currently fixed

	    				/*the following line is a compact notation for this one:
	    				var db = ($(this).attr("db")==undefined)?"":"&db="+$(this).attr("db");
	    				which means: if the attribute "db" is undefined, return nothing, else return the string "&db=value"*/
	    				var db =(!(tmp = $(this).attr("db")))?"":"&db="+tmp;
	    				
	    				var cat1 =(!(tmp = $(this).attr("cat1")))?"":"&cat1="+tmp; //same for cat1, cat2, etc.
	    				var cat2 =(!(tmp = $(this).attr("cat2")))?"":"&cat2="+tmp;
	    				var cat3 =(!(tmp = $(this).attr("cat3")))?"":"&cat3="+tmp;
	    				var cat4 =(!(tmp = $(this).attr("cat4")))?"":"&cat4="+tmp;
	    				var cat5 =(!(tmp = $(this).attr("cat5")))?"":"&cat5="+tmp;
	    				
	    				var lang="&language="+langUser;
	    				
	    				var url = webPath +"/aViewCat?openagent"+db+"&view=vCatList" + cat1 + cat2 + cat3 + cat4 + cat5+lang;
	    				$(this).load(url).text();    				
	    			}
		  	});
			return false;
			
			
			$(".viewcategory").each(function (i) {
				this.style.color="blue";
				var viewcat = $(this);
				var cat1 = viewcat.attr("cat1");
			
				if(viewcat){  //check on existance of div: if the div is set to hidden, it will be null 
					if(viewcat.innerHTML='Loading...'){		
						viewcat.text("Please wait...");
						return false;
						}
					}
				});
	});


	$.ajax({
	/*function to be executed after all ajax requests on the page have been executed*/
	complete: function(){

	/*View entry behaviour*/
		$("li.docentry").hover(function(){
			$(this).addClass('highlighted')}
			,function(){
			$(this).removeClass('highlighted')}
		);	
		
		$("li.docentry").click(function(){		
			$(this).parents("div").find("li.docentry").removeClass('selected');  //unselect the previously selected entry
			$(this).addClass('selected');	//select the current entry
			var unid = $(this).attr("name"); //get docunid
			var url = webPath +"/0/"+unid+"?opendocument";
			//alert(url);
			window.location.href = url; //load document 
		//	$(window).load(url); //load document 
		});

	/*View category behaviour*/	
		$("span.catname").toggle(   //display expand/collapse icon next to category name
			function(){$(this).children(".catexpandimage").attr("src",iconExpand);},
			function(){$(this).children(".catexpandimage").attr("src",iconCollapse);}
		);
	    
		$("span.catname").click(function(){ //expand/collapse: show or hide the category contents
				//$(this).parent().children("li.catcontents").toggle();
				$(this).parent().children("ul.doclist").toggle();
		});
	}
	});

	}
