/**********************************************************
***********************************************************
*  Copyright 2004 (c) Aumediage S.P.R.L. All Rights Reserved.								*
*  This is a commercial software product, please visit												*
*  http://www.aumediage.net for more information. or send email to					*
*  aumediage@hotmail.com																						*
*  See http://www.aumediage.net for Commercial License Agreement				*
*  All Copyright statements must always remain in place in all files at all times	*
*		***** PLEASE NOTE: THIS IS NOT FREE SOFTWARE	*****						*
*				*****	IT MUST BE LICENSED FOR ALL USE	*****								*
*  HISTORY																												*
*	Name			Date							Action											Version			*
*	GdB				05/11/2003			Creation										1.00					*
*																																	*
************************************************************
***********************************************************/


/*******************************************************
** *****************************************************
** *****************************************************
 GENERIC FUNCTION  FUNCTIONS
** *****************************************************
** ****************************************************/
function getFileExtension(sFileName){
		return sFileName.replace(/.*[\.]/, '')
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function getFileName(sPath){
		return sPath.replace(/.*[\/]/, '')
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function checkFolderPath(sPath, bIsFile){
	var iLength = sPath.length-1;
	var iLastSlash = sPath.lastIndexOf('/');
	if (iLength == iLastSlash)
		return (bIsFile)?sPath.substring(0, iLength) : sPath;
	else
		return (bIsFile)?sPath : sPath+'/';
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function getFileIcon(sFileName){try{
	var sFileExtension = getFileExtension(sFileName)
	switch(sFileExtension.toLowerCase()){
		case 'asp':
		case 'jcs':
			bReturnValue = 'asp';	break;
		case 'bmp':
		case 'png':
		case 'gif':
			bReturnValue = 'gif';	break;
		case 'jpg':
			bReturnValue = 'jpg';	break;
		case 'htm':
		case 'html':
			bReturnValue = 'html';break;
		case 'vbs':
			bReturnValue = 'vbs';	break;
		case 'js':
			bReturnValue = 'js';	break;
		case 'asa':
			bReturnValue = 'asa';	break;
		case 'xml':
			bReturnValue = 'xml';	break;
		case 'xsl':
			bReturnValue = 'xsl';	break;
		case 'htc':
		case 'wsh':
			bReturnValue = 'wsh';	break;
		case 'txt':
			bReturnValue = 'txt';	break;
		case 'bat':
			bReturnValue = 'bat';	break;
		default:
			bReturnValue = 'undefined';break;
	}
	return ('file_'+bReturnValue)
}catch(e){handleClientError('getFileIcon', e);}}

/*******************************************************
** *****************************************************
** *****************************************************
THIS CREATES A OBJECT TO GET AN IMAGE FILE DETAIL
** *****************************************************
** ****************************************************/
function oMediaDetails(thisFile){
	this.sFilePath		= thisFile;
	aryDetails				= getImageDetails(thisFile);
	this.sImageType	= aryDetails[0];
	this.iWidth			= aryDetails[1];
	this.iHeight			= aryDetails[2];
	this.iDepth			= aryDetails[3];
	this.fileSize			= aryDetails[4];
	this.bFileExists		= aryDetails[5];
	this.iFileType		= aryDetails[6];
}
/*******************************************************
THIS GET BYTES FROM A FILE'S STREAM
*******************************************************/
function GetBytes(oFso, oFile, sFilePath, offset, bytes){	try{
		var retValue			= '';	
		var lngSize			= oFile.Size;
		var oTextStream	= oFso.OpenTextFile(sFilePath, 1)
		if (offset > 0)
			sBuffer				= oTextStream.Read(offset - 1);
		retValue				= (bytes == -1)? oTextStream.Read(lngSize) : oTextStream.Read(bytes) ;
		oTextStream.Close();
		return retValue;
}catch(e){handleClientError('GetBytes', e);}}
/*******************************************************
THIS GET IMAGE DETAILS
*******************************************************/
function getImageDetails(sFilePath){try{
		var g_sImageType	= '(unknown)';
		var g_iHeight; var g_iWidth; var g_iDepth; var lngMarkerSize;
		var oFso				= new ActiveXObject("Scripting.FileSystemObject");
		var bFileExists		=  (oFso.FileExists(sFilePath))?true:false;
		var oFile				= oFso.GetFile(sFilePath);
		var iFileType			= '(unknown)';
		var lngSize			= oFile.Size;
		var sFileType		= GetBytes(oFso, oFile, sFilePath, 0, 3);
		var sPngExt			= String.fromCharCode(8240) + String.fromCharCode(80) + String.fromCharCode(78);
		var sGifExt			= 'GIF';
		var sBmpExt			= String.fromCharCode(66) + String.fromCharCode(77);
		//alert(sFileType)
		if (sFileType == sGifExt){
			//alert('GIF')
			g_sImageType		= 'GIF';
			g_iWidth				= lngConvert(GetBytes(oFso, oFile, sFilePath, 7, 2));
			g_iHeight				= lngConvert(GetBytes(oFso, oFile, sFilePath, 9, 2));
			g_iDepth				= Math.pow( 2,(GetBytes(oFso, oFile, sFilePath, 11, 1).charCodeAt(0) & 7) + 1 );
			iFileType				= 2;
		}
		else if (sFileType.substr(0, 2) == sBmpExt){
			g_sImageType		= "BMP";
			g_iWidth				= lngConvert(GetBytes(oFso, oFile, sFilePath, 19, 2));
			g_iHeight				= lngConvert(GetBytes(oFso, oFile, sFilePath, 23, 2));
			g_iDepth				= Math.pow(2, GetBytes(oFso, oFile, sFilePath, 29, 1).charCodeAt(0));
			iFileType				= 2;
		}
		else if (sFileType == sPngExt){
			g_sImageType		= "PNG";
			g_iWidth				= lngConvert2(GetBytes(oFso, oFile, sFilePath, 19, 2));
			g_iHeight				= lngConvert2(GetBytes(oFso, oFile, sFilePath, 23, 2));
			g_iDepth				= GetBytes(oFso, oFile, sFilePath, 25, 2);
			iFileType				= 2;
			switch (g_iDepth.substr(g_iDepth.length-1).charCodeAt(0)){
				case 0:g_iDepth = Math.pow( 2, g_iDepth.substr(0, 1).charCodeAt(0));retValue = true;break;
				case 2:g_iDepth = Math.pow( 2, g_iDepth.substr(0, 1).charCodeAt(0) * 3);retValue = true;break;
				case 3:g_iDepth = Math.pow( 2, g_iDepth.substr(0, 1).charCodeAt(0));retValue = true;break;
				case 4:g_iDepth = Math.pow( 2, g_iDepth.substr(0, 1).charCodeAt(0) * 2);retValue = true;break;
				case 6:g_iDepth = Math.pow( 2, g_iDepth.substr(0, 1).charCodeAt(0) * 4);retValue = true;break;
				default:g_iDepth = -1;break;
			}
		}
		else{
			var sBuffer			= GetBytes(oFso, oFile, sFilePath,0,-1);
			var iBufferSize		= sBuffer.length;
			var sToFind			= String.fromCharCode(255) + String.fromCharCode(216) + String.fromCharCode(255);
			var iIndexFound	= sBuffer.indexOf(sToFind);
			var bExitLoop		= false;
			if (iIndexFound = 0){throw 'No config found';}
			var iCursorPos		= iIndexFound + 3;
			while (bExitLoop == false && iCursorPos < iBufferSize){
				while ( sBuffer.substr(iCursorPos-1,1).charCodeAt(0) == 255 && iCursorPos <= iBufferSize){
					iCursorPos	= iCursorPos + 1;
				}			
				if ( sBuffer.substr(iCursorPos-1,1).charCodeAt(0) < 192 || sBuffer.substr(iCursorPos-1,1).charCodeAt(0) > 195 ){
					lngMarkerSize = lngConvert2(sBuffer.substr(iCursorPos,2));
					iCursorPos	= iCursorPos + lngMarkerSize  + 1;
				}
				else{
					bExitLoop		= true;
				}
			}
			if (bExitLoop == false){throw 'No config found';}
			else{
				g_sImageType	= "JPG";
				g_iHeight			= lngConvert2(sBuffer.substr(iCursorPos+3,2));
				g_iWidth			= lngConvert2(sBuffer.substr(iCursorPos+5,2));
				g_iDepth			= Math.pow( 2 , sBuffer.substr(iCursorPos+7,1).charCodeAt(0) * 8);
				iFileType			= 2;
			}
		}
		oFile							= null; 
		oFso							= null;
		var aryReturn			= new Array();
		aryReturn[0]			= (g_sImageType)?g_sImageType:-1;
		aryReturn[1]			= (g_iWidth)?g_iWidth:-1;
		aryReturn[2]			= (g_iHeight)?g_iHeight:-1;
		aryReturn[3]			= (g_iDepth)?g_iDepth:-1;
		aryReturn[4]			= (lngSize)?lngSize:-1;
		aryReturn[5]			= (bFileExists);
		aryReturn[6]			= (iFileType)?iFileType:-1;
		
		return aryReturn;
}catch(e){
	handleClientError('getImageDetails', e);
	var aryReturn = new Array();
	aryReturn[0] = -1;
	aryReturn[1] = -1;
	aryReturn[2] = -1;
	aryReturn[3] = -1;
	aryReturn[4] = -1;
	aryReturn[5] = bFileExists;
	aryReturn[6] = -1;
	return aryReturn;
}}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function lngConvert(strTemp){
	return Math.round( strTemp.substr(0, 1).charCodeAt(0) + strTemp.substr(strTemp.length-1).charCodeAt(0) * 256)
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function olngConvert2(strTemp){//replaced by vbScript
	return Math.round( (doAsc(doRight(strTemp, 1))) + (doAsc(doLeft(strTemp, 1))*256) )
}
/**********************************************************
***********************************************************
*  Copyright 2004 (c) Aumediage S.P.R.L. All Rights Reserved.								*
*  This is a commercial software product, please visit												*
*  http://www.aumediage.net for more information. or send email to					*
*  aumediage@hotmail.com																						*
*  See http://www.aumediage.net for Commercial License Agreement				*
*  All Copyright statements must always remain in place in all files at all times	*
*		***** PLEASE NOTE: THIS IS NOT FREE SOFTWARE	*****						*
*				*****	IT MUST BE LICENSED FOR ALL USE	*****								*
*  HISTORY																												*
*	Name			Date							Action											Version			*
*	GdB				05/11/2003			Creation										1.00					*
*																																	*
************************************************************
***********************************************************/
