//**********************************************************************************************************************
//  Module Global Functions
//**********************************************************************************************************************
	var sRootUrl 	= null;
	var sModule		= null;
	var iUserID		= null;
	var iAgencyID	= null;
	var iCategoryID	= null;
	var iAlbumID	= null;
	var sAppletKey	= null;
	
	
	function Init(pRootUrl, pModule, pUserID, pPictureGalleryKey, pAgencyID, pCategoryID, pAlbumID){
		sRootUrl	= pRootUrl;
		sModule		= pModule;
		iUserID		= pUserID;
		sAppletKey	= pPictureGalleryKey;
		iAgencyID	= pAgencyID
		iCategoryID	= pCategoryID;
		iAlbumID	= pAlbumID;
		InitAjaxDebugDataElements();
	}

//******************************
//  Page GalleryPics Functions
//******************************

		//===========================
		// Page Vars
		//===========================
		var iGalleryPics_CategoryID = null;
		var iGalleryPics_AlbumID	= null;
		var sAction 				= null;
		
		//===========================
		// Page Functions
		//===========================
		function AddAlbum(pCategoryID){
			//hide the previous validation errors
			ShowValidationErrors("none");
			
			iGalleryPics_CategoryID = pCategoryID;
			iGalleryPics_AlbumID	= null;
			
			ShowDiv('divAddGalleryAlbum');
			HideDiv('divGalleryAlbum');
			
			if(CreateXMLHttpRequest()){
				RequestCategoryDetails(pCategoryID);
			}
		}
		
		function GalleryAlbum(pAlbumID){
			//hide the previous validation errors
			ShowValidationErrors("none");
			
			iGalleryPics_AlbumID = pAlbumID;
			
			if (IsBlockElementHidden('divGalleryAlbum')) {
				ShowDiv('divGalleryAlbum');
				HideDiv('divAddGalleryAlbum');
			}
			
			//Show the details of the current selected album
			if(CreateXMLHttpRequest()){
		    	RequestAlbumDetails(iGalleryPics_AlbumID);	
		    }
		}
		
		//Save Album
		function SaveAlbum(pAction) {
			//hide the previous validation errors
			ShowValidationErrors("none");
			
			if(CreateXMLHttpRequest()){
		    	RequestAlbumValidationAndSave(pAction);	
		    }
		}
		
		function RequestAlbumValidationAndSave(pAction){
			sAction = pAction;
			// build Url
			var _sUrl = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_ajax';
			// build param string to send by Post
			var _sPost = "&action="		+ 'album_' + pAction;
			_sPost += '&AlbumID=' 		+ iGalleryPics_AlbumID;
			_sPost += '&CategoryID=' 	+ iGalleryPics_CategoryID;
			_sPost += '&AgencyID='		+ iAgencyID;
			_sPost += '&UserID=' 		+ iUserID;
			_sPost += '&Title=' 		+ escape(document.getElementById(pAction + 'Album').value);
			_sPost += '&Visible=' 		+ '1';
			
			SetAjaxDebugData (_sUrl + _sPost);
			
			// Execute the request
			oHttpRequest.onreadystatechange = SetValidationErrors;
			oHttpRequest.open('post', _sUrl, true);
			oHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			oHttpRequest.setRequestHeader("Content-length", _sPost.length);
			oHttpRequest.send(_sPost);	
		}
		
		function SetValidationErrors(){
			if (oHttpRequest.readyState == 4) {
				if (oHttpRequest.status == 200) {
					
					// Put response into xmlobject
					var _xmldoc = oHttpRequest.responseXML;
					var _item_value = "";
					
					//check the response if there where any errors
					var _nbr_errors = 0;
					// Get the number of errors
					_nbr_errors = _xmldoc.documentElement.getElementsByTagName('nbr_errors').item(0).firstChild.nodeValue;
				
					if (_nbr_errors > 0){
						// Loop through the returned validation errors and highlight the errors in the template			
						var _data = _xmldoc.documentElement.getElementsByTagName('error');
					
						if(_data.length > 0){
							for (i=0; i < _data.length; i++){
								var _ValidationCode = "";
								
								try{
									_ValidationCode = _data[i].getElementsByTagName('code').item(0).firstChild.nodeValue;
								}catch(e){
									_ValidationCode = "";
								}
								
								//create the tips for this error
								var _sMessage = '';
								try{
									_sMessage = _data[i].getElementsByTagName('message').item(0).firstChild.nodeValue;
								}catch(e){}
								
								try{
									_sMessage += ' ' + _data[i].getElementsByTagName('proposition').item(0).firstChild.nodeValue;
								}catch(e){}
															
								_sShortErrorNote = _data[i].getElementsByTagName('short_note').item(0).firstChild.nodeValue;
								
								switch (_ValidationCode){
									case "ALBUM_TITLE_MISSING"	:	//Set the Tips message
																	var _oTips = document.getElementById('errTips' + sAction + 'Album');
																	_oTips.title = _sShortErrorNote + ' :: ' + _sMessage;
																	//show the validation errors to the user
																	ShowValidationErrors(sAction);	
																	break;
									case "ALBUM_TITLE_EXISTS" :		//Set the Tips message
																	var _oTips = document.getElementById('errTips' + sAction + 'Album');
																	_oTips.title = _sShortErrorNote + ' :: ' + _sMessage;
																	//show the validation errors to the user
																	ShowValidationErrors(sAction);	
																	break;
								}
							}
						}
					} else {
						
						try{
							_item_value = _xmldoc.documentElement.getElementsByTagName('Title').item(0).firstChild.nodeValue;
						}catch(e){
							_item_value = "";
						}
						
						//document.getElementById('divTitle').innerHTML = _item_value;
						document.getElementById('EditAlbum').value = _item_value;
						
						if (_item_value == ""){
							iGalleryPics_AlbumID = null;
							AddAlbum(iGalleryPics_CategoryID);
						}
						//we have to refresh the tree
						document.getElementById('iframePGTree').src = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_tree';

					}
				}
			}
		}
		
		function ShowValidationErrors(pArea){
			var _oField = null;
			var _oTips	= null;
			
			switch (pArea){
				case "none": 	_oField = document.getElementById('EditAlbum');
								_oField.className = _oField.className.replace(' validationError', '');
								_oField.className = _oField.className.replace('validationError', '');
								_oTips = document.getElementById('errTipsEditAlbum');
								_oTips.width = 0;
								_oTips.height = 0;
								_oField = document.getElementById('AddAlbum');
								_oField.className = _oField.className.replace(' validationError', '');
								_oField.className = _oField.className.replace('validationError', '');
								_oTips = document.getElementById('errTipsAddAlbum');
								_oTips.width = 0;
								_oTips.height = 0;
								break;
								
				case "Add":		_oField = document.getElementById('AddAlbum');
								_oField.className += " validationError";
								_oTips = document.getElementById('errTipsAddAlbum');
								_oTips.width = 16;
								_oTips.height = 16;
								RefreshTips();
								break;
								
				case "Edit":	_oField = document.getElementById('EditAlbum');
								_oField.className += " validationError";
								_oTips = document.getElementById('errTipsEditAlbum');
								_oTips.width = 16;
								_oTips.height = 16;
								RefreshTips();
								break;
			}
		}
		
		function RefreshTips(){
			var Tips2 = new Tips($$('.Tips2'), {
				maxTitleChars: 75, 
				initialize:function(){
					this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
				},
				onShow: function(toolTip) {
					this.fx.start(1);
				},
				onHide: function(toolTip) {
					this.fx.start(0);
				}
			});
		}
		
		//Get details of the album
		function RequestAlbumDetails(pAlbumID){
			// build Url
			var _sUrl = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_ajax';
			// build param string to send by Post
			var _sPost = "&action=album_details";
			_sPost += '&AlbumID=' + pAlbumID;
			
			SetAjaxDebugData (_sUrl + _sPost);
			
			// Execute the request
			oHttpRequest.onreadystatechange = SetAlbumDetails;
			oHttpRequest.open('post', _sUrl, true);
			oHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");  
			oHttpRequest.setRequestHeader("Content-length", _sPost.length);
			oHttpRequest.send(_sPost);
		}
		
		function SetAlbumDetails(){
			if (oHttpRequest.readyState == 4){
				if (oHttpRequest.status == 200) {
				
					// Put response into xmlobject
					var _xmldoc = oHttpRequest.responseXML;
					var _item_value = "";
	
					// Data = ID of the catgory
					try{
						_item_value = _xmldoc.documentElement.getElementsByTagName('CategoryID').item(0).firstChild.nodeValue;
					}catch(e){
						_item_value = "";
					}
					iGalleryPics_CategoryID = _item_value;
					
					// Data = Title of the Album
					try{
						_item_value = _xmldoc.documentElement.getElementsByTagName('Title').item(0).firstChild.nodeValue;
					}catch(e){
						_item_value = "";
					}
					document.getElementById('EditAlbum').value = _item_value;
					document.getElementById('divTitle').innerHTML = _item_value;	
			
					try{
						_item_value = _xmldoc.documentElement.getElementsByTagName('AlbumID').item(0).firstChild.nodeValue;
					}catch(e){
						_item_value = "";
					}

					if (_item_value != ""){
						var _hrefItem = document.getElementById('ShowAlbumPictures');
						_hrefItem.href = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_view&AlbumID=' + _item_value + '&mainnav=rsnf&subnav=fun';
					}
				}
			}	
		}
		
		//Delete Album
		function DeleteAlbum() {
			if (confirm("Wollen Sie dieses Album wirklich löschen?")){
				if (iGalleryPics_AlbumID > 0){
					if(CreateXMLHttpRequest()){
				    	RequestAlbumValidationAndDelete();	
				    }
				}
			}
		}
		
		function RequestAlbumValidationAndDelete(){
			// build Url
			var _sUrl = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_ajax';
			// build param string to send by Post
			var _sPost = "&action="		+ 'album_delete';
			_sPost += '&AlbumID=' 		+ iGalleryPics_AlbumID;
			_sPost += '&CategoryID=' 	+ iGalleryPics_CategoryID;
			_sPost += '&AgencyID='		+ iAgencyID;
			_sPost += '&UserID=' 		+ iUserID;
			_sPost += '&Visible=' 		+ '0';
			
			SetAjaxDebugData (_sUrl + _sPost);
			
			// Execute the request
			oHttpRequest.onreadystatechange = SetValidationErrors;
			oHttpRequest.open('post', _sUrl, true);
			oHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			oHttpRequest.setRequestHeader("Content-length", _sPost.length);
			oHttpRequest.send(_sPost);	
		}
		
		//get details of the category
		function RequestCategoryDetails(pCategoryID) {
			// build Url
			var _sUrl = sRootUrl + 'index.php?module=' + sModule + '&page=gallerypics_ajax';
			// build param string to send by Post
			var _sPost = "&action=category_details";
			_sPost += '&CategoryID=' + pCategoryID;
			
			SetAjaxDebugData (_sUrl + _sPost);
			
			// Execute the request
			oHttpRequest.onreadystatechange = SetCategoryDetails;
			oHttpRequest.open('post', _sUrl, true);
			oHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");  
			oHttpRequest.setRequestHeader("Content-length", _sPost.length);
			oHttpRequest.send(_sPost);
		}
		
		function SetCategoryDetails() {
			if (oHttpRequest.readyState == 4){
				if (oHttpRequest.status == 200) {
					// Put response into xmlobject
					var _xmldoc = oHttpRequest.responseXML;
					var _item_value = "";
	
					// Data = ID of the catgory
					try{
						_item_value = _xmldoc.documentElement.getElementsByTagName('Title').item(0).firstChild.nodeValue;
					}catch(e){
						_item_value = "";
					}
					
					document.getElementById('divTitle').innerHTML = _item_value;
					//iGalleryPics_CategoryID = _item_value;
				}
			}
		}
		
		//Upload
		function JStart() {
		}
		
		function JFinish() {
			window.location = sRootUrl + 'index.php?module=rsnf&page=gallerypics_view&AlbumID=' + iAlbumID + '&mainnav=rsnf&subnav=fun';
		}
		
		function GetAppletValues()
		{
			var _sApplet = null;
			_sApplet = '<APPLET codebase="' + sRootUrl + 'includes/java/JImageUpload/" archive="JImageUpload.jar" code="jimageupload/JImageUploadApplet.class" width="579" height="461" MAYSCRIPT>' + 
	    					'<Param name="key" value="' + sAppletKey + '">' +
	    					'<Param name="UserID" value="0">' +
	    					'<Param name="CustomerID" value="' + iUserID + '">' +
	    					'<Param name="AgencyID" value="' + iAgencyID + '">' +
	    					'<Param name="CategoryID" value="' + iGalleryPics_CategoryID + '">' +
	    					'<Param name="AlbumID" value="' + iGalleryPics_AlbumID + '">' +
	    					'<Param name="BasePath" value="picture_gallery/">' +
	    					'<Param name="JStart" value="">' +
	    					'<Param name="JFinish" value="JFinish();">' +
					   '</APPLET>';
			return _sApplet;	   
		}		

		function UploadPictures(){
			if ((iGalleryPics_CategoryID != null) && (iGalleryPics_AlbumID != null)){
				window.location = sRootUrl + 'index.php?module=rsnf&page=gallerypics_upload&category=' + iGalleryPics_CategoryID + '&album=' + iGalleryPics_AlbumID + '&mainnav=rsnf&subnav=fun';
			}else{
				alert("Sie müssen zuerst ein Album auswählen!");
			}
			
		}
