//
//  java uploader funcs
//

var inProgress = false;
var minVersionRequired_Windows = "1,5,0,0";
var minVersionRequired_Mac     = "1,4,6,0";

var ieClassId  = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"; // This is referred to as dynamic versioning.
// If the user has an older version of Plug-in than shown in the codebase attribute of the OBJECT tag,
// the user will be prompted to download the newer version. If the user has the same or a newer version, that version will run.
var ieCodeBase = "http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab";

var embedType_Windows  = "application/x-java-applet;version=1.5";
var embedType_Mac      = "application/x-java-applet;version=1.3";
var EasyUploaderID;
var GoodToGo = false;
var useAppletTag = false; // for IE, instead of object tag
var isExpressView = false;

function initJavaUploader(redirectURL) {
	inProgress = false;
	EasyUploaderID = document.getElementById('EasyUploader');
	// Java detected
	var IsMinVersion = PluginDetect.isMinVersion('Java', getMinVersion(), 'java/getJavaInfo.jar'); 
				
	if ((IsMinVersion == 1) || (IsMinVersion == 0)) {
		// Java version meets minimum requirement, launch Java Uploader
		//__doPostBack('EasyUploaderButton','');
	} else {	 // (IsMinVersion == -1) || (IsMinVersion == -2)
		// Java version is too old								
		//if (_isWindowsVista) {
			//document.location = redirectURL; 				
		//}
		__doPostBack('SimpleUploaderButton',''); // in upload_java.aspx.cs	
	}
}

function isGoodToGo()
{
	return GoodToGo;
}

function getMinVersion () {
	var _info = navigator.userAgent;
	var _Win = (_info.indexOf("Win") > 0);
	var _Mac = (_info.indexOf("Mac") > 0);
	
	if (_Win == true)
		return minVersionRequired_Windows;
	else if (_Mac == true)
		return minVersionRequired_Mac;
	else 
		return minVersionRequired_Windows;
}

function getUploader() {
	return document.getElementById('EasyUploader');
}

function getPhotos () 
{
	window.setTimeout('pickFiles()',50);
}	

function pickFiles() {
	if (isGoodToGo)
		getUploader().pickFiles();
}

function removeAll() {
	if (isGoodToGo)
		getUploader().removeAll();
}

function cancelUpload() {
	if (isGoodToGo)
		getUploader().cancelUpload();
}

function viewAsThumbnails() {
	if (isGoodToGo) {
		getUploader().viewAsThumbnails();
		return true;	
	} else {
		return false;
	}		
}

function viewAsList() {
	if (isGoodToGo) {
		getUploader().viewAsList();
		return true;	
	} else {
		return false;
	}			
}

function log(blurb)
{	
	if (isGoodToGo) {
		window.setTimeout(function(){getUploader().log(blurb);},1000); // wait one second now - the applet might not be ready
	}
}

/* applet callback functions */
function saveDirectoryCookie( uploaderDirectory ) {
	createCookie( 'uploaderDirectory', uploaderDirectory, 30 );
}
function eatDirectoryCookie() {
	return readCookie( 'uploaderDirectory' );
}
function pressUploadButton() {
	if ( userAgreedToTermsAndConditions() )
		getUploader().startUpload( GetAlbumIDfromDropDown(), GetAlbumNamefromDropDown() );
}
function stateChangeEvent(newstate)
{
	switch(String(newstate))
	{
		case 'UPLOAD_SUCCESS':
			break;
		case 'UPLOAD_FAILED':
			document.location = "../error.aspx";
			break;
		case 'INITIALIZING':
			break;
		case 'UNTRUSTED_APPLET':
			__doPostBack('SimpleUploaderButton',''); // in upload_java.aspx.cs		
			break;
		case 'PHOTO_SELECTION':
			GoodToGo = true;
			initPage(); // do init stuff in HTML page
			break; 		
		case 'UPLOADING':
			break;				
		case 'UPLOAD_CANCELLED':
			break;			 
		default:
			break;
	}
}
function onUploadComplete(uploadBatchID, encUserId) {
	var expressOrder = 0;
	if(isExpressView){
	expressOrder= 1 ;
	}
	document.location = "upload_complete.aspx?uid=" + encUserId + "&ubid=" + uploadBatchID + "&expressOrder=" + expressOrder;
}
function getAlbumID() {
	return GetAlbumIDfromDropDown();		
}
function getAlbumName() {
	return GetAlbumNamefromDropDown();					
}
function launchHelpPopUp() {
	// not used in this site
}
/* end call-backs */

function GetAlbumIDfromDropDown() {
	var albumsDropdown = document.getElementById('albumsDropdown');
	if (albumsDropdown) {
		var albumID = albumsDropdown.options[albumsDropdown.selectedIndex].value;
		if (albumID == '0') {
			return null;
		} else {
			return albumID;
		}
	} else {
		return null
	}
}

function userAgreedToTermsAndConditions()
{
	if ( GetTermsAndConditionsfromCheckbox() ) {
		return true;
	} else {
		var you_must_agree_terms_to_proceed = document.getElementById("you_must_agree_terms_to_proceed"); // hidden input id="you_must_agree_terms_to_proceed" must appear on aspx
		if (you_must_agree_terms_to_proceed)
			alert(you_must_agree_terms_to_proceed.value); 
		return false;
	}
}

function GetTermsAndConditionsfromCheckbox() {
	var termsCheckBox = document.getElementById('termsCheckBox');
	if (termsCheckBox) {
		return termsCheckBox.checked;
	} else {
		return false;
	}
}

function GetAlbumNamefromDropDown () {
	var albumsDropdown = document.getElementById('albumsDropdown');
	if (albumsDropdown) {
		var albumID = GetAlbumIDfromDropDown();			
		if (albumID == null) {
			return null
		} else {
			return albumsDropdown.options[albumsDropdown.selectedIndex].text;
		}
	} else {
		return null
	}
}

function GetUploadLimit() {
	return null;
}

function OnBeginUpload() 
{

}
	
function showUploader (language, width, height, speedTest, debugLogParam, clientName, UploadBatchInitURL, errorLoggingURL, AppContextID, EncUserID, ReducePhotosDefault) {

	var _info = navigator.userAgent; var _ns = false;

	var _WinIE = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
	var _Mac = (_info.indexOf("Mac") > 0);
	
	
	
	var debugLogFilePath = 'c:/EasyUploaderLog_' + clientName + '.txt';
	if (_Mac)
		var debugLogFilePath = '~\EasyUploaderLog_' + clientName + '.txt';
	
	if ((_WinIE == true) && (useAppletTag == true))
	{ 
		document.writeln('<applet');
		document.writeln('id="EasyUploader"');
		document.writeln('name="EasyUploader"');
		document.writeln('code="com.photochannel.easyUploader.EasyUploader.class"');
		document.write('width="');
		document.write(width);
		document.writeln('"');	
		document.write('height="');
		document.write(height);
		document.writeln('"');				
		document.write('codebase="java/"');
		document.writeln('>');
		document.writeln('<PARAM name="type" value="application/x-java-applet">');
		document.writeln('<param name="archive" value="Uploader.jar, nanoxml-lite-2.2.3.jar, Slim.jar">');
		if (debugLogParam == 'true') {
			document.writeln('<param name="DEBUG" value="true">');
			document.writeln('<param name="debugLogFile" value="' + debugLogFilePath + '">');
		} else if (debugLogParam == 'console') {
			document.writeln('<param name="DEBUG" value="true">');
		}
		document.write('<param name="java_arguments" value="-Xmx256m">');
		document.write('<param name="language" value="');
		document.write(language);
		document.writeln('">');
		document.writeln('<param name="scriptable" value="true">');
		document.writeln('<param name="MAYSCRIPT" value="true">');
		if (speedTest != 0) {
			document.write('<param name="speedTest" value="');
			document.write(speedTest);
			document.writeln('">');		
		}
		document.write('<param name="batchInitURL" value="');
		document.write(UploadBatchInitURL);
		document.writeln('">');
		document.write('<param name="errorLoggingURL" value="');
		document.write(errorLoggingURL);
		document.writeln('">');		
		document.write('<param name="appContextID" value="');
		document.write(AppContextID);
		document.writeln('">');
		document.write('<param name="userID" value="');
		document.write(EncUserID);
		document.writeln('">');
		document.write('<param name="reducePhotosDefault" value="');
		document.write(ReducePhotosDefault);
		document.writeln('">');
//
// TODO: fetch values from DB
//		
		document.writeln('<param name="UploadMaxThreads" value="2">');
		document.writeln('<param name="ImageDownscalingJpegQFactor" value="90">');
		document.writeln('<param name="ImageDownscalingMinDimensionPx1" value="1800">');
		document.writeln('<param name="ImageDownscalingMinDimensionPx2" value="1200">');
//
//
		document.writeln('</applet>'); 
	} else if (_WinIE == true) { 
		document.writeln('<object');
		document.writeln('id="EasyUploader"'); 
		document.writeln('name="EasyUploader"');
		document.write('classid="');
		document.write(ieClassId);
		document.writeln('"');		
		document.write('width="');
		document.write(width);
		document.writeln('"');	
		document.write('height="');
		document.write(height);
		document.writeln('"');				
		document.write('codebase="');
		document.write(ieCodeBase);
		document.writeln('">');
		document.writeln('<param name="code" value="com.photochannel.easyUploader.EasyUploader.class">');
		document.writeln('<param name="codebase" value="java/">');				
		document.writeln('<PARAM name="type" value="application/x-java-applet">');
		document.writeln('<param name="archive" value="Uploader.jar, nanoxml-lite-2.2.3.jar, Slim.jar">');
		if (debugLogParam == 'true') {
			document.writeln('<param name="DEBUG" value="true"/>');
			document.writeln('<param name="debugLogFile" value="' + debugLogFilePath + '"/>');
		} else if (debugLogParam == 'console') {
			document.writeln('<param name="DEBUG" value="true"/>');
		}
		document.write('<param name="java_arguments" value="-Xmx256m">');
		document.write('<param name="language" value="');
		document.write(language);
		document.writeln('">');
		document.writeln('<param name="scriptable" value="true"/>');
		document.writeln('<param name="MAYSCRIPT" value="true"/>');
		document.write('<param name="batchInitURL" value="');
		document.write(UploadBatchInitURL);
		document.writeln('">');
		document.write('<param name="errorLoggingURL" value="');
		document.write(errorLoggingURL);
		document.writeln('">');		
		document.write('<param name="appContextID" value="');
		document.write(AppContextID);
		document.writeln('">');
		document.write('<param name="userID" value="');
		document.write(EncUserID);
		document.writeln('">');
		document.write('<param name="reducePhotosDefault" value="');
		document.write(ReducePhotosDefault);
		document.writeln('">');		
		if (speedTest != 0) {
			document.write('<param name="speedTest" value="');
			document.write(speedTest);
			document.writeln('">');		
		}		
//
// TODO: fetch values from DB
//		
		document.writeln('<param name="UploadMaxThreads" value="2">');
		document.writeln('<param name="ImageDownscalingJpegQFactor" value="90">');
		document.writeln('<param name="ImageDownscalingMinDimensionPx1" value="1800">');
		document.writeln('<param name="ImageDownscalingMinDimensionPx2" value="1200">');
//
//		
		document.writeln('</object>'); 
	} else if (_Mac == true) {	
		document.writeln('<embed');
		document.writeln('id="EasyUploader"'); 
		document.writeln('name="EasyUploader"');
		document.write('type="')
		document.write(embedType_Mac);
		document.writeln('"');		
		document.writeln('pluginspage="http://java.com/getjava"');	
		document.writeln('archive="Uploader.jar, nanoxml-lite-2.2.3.jar, Slim.jar"');			
		document.writeln('code="com.photochannel.easyUploader.EasyUploader.class"');
		document.writeln('JAVA_CODEBASE="java/"');
		document.write('width="');
		document.write(width);
		document.writeln('"');	
		document.write('height="');
		document.write(height);
		document.writeln('"');	
		document.writeln('MAYSCRIPT="true"');
		if (debugLogParam == 'console') {
			document.writeln('DEBUG="true"');
		}		
		document.writeln('configuration="./config.xml"');
		document.writeln('stylesheet="./config.css"');
		document.writeln('stringTable="./string-table.xml"');				
		document.writeln('java_arguments="-Xmx256m"');				
		document.write('language="');
		document.write(language);
		document.writeln('"');		
		document.write('batchInitURL="');
		document.write(UploadBatchInitURL);
		document.writeln('"');
		document.write('errorLoggingURL="');
		document.write(errorLoggingURL);
		document.writeln('"');		
		document.write('appContextID="');
		document.write(AppContextID);
		document.writeln('"');		
		document.write('userID="');
		document.write(EncUserID);
		document.writeln('"');		
		document.write('reducePhotosDefault="');
		document.write(ReducePhotosDefault);
		document.writeln('"');	
		
		if (speedTest != 0) {		
			document.write('speedTest="');
			document.write(speedTest);
			document.writeln('"');
		}
//
// TODO: fetch values from DB
//		
		document.writeln('UploadMaxThreads="2"');
		document.writeln('ImageDownscalingJpegQFactor="90"');
		document.writeln('ImageDownscalingMinDimensionPx1="1800"');
		document.writeln('ImageDownscalingMinDimensionPx2="1200"');
//
//		
		document.writeln('></embed>'); 		
	} else {
		document.writeln('<embed');
		document.writeln('id="EasyUploader"'); 
		document.writeln('name="EasyUploader"');
		document.write('type="');
		document.write(embedType_Windows);
		document.writeln('"');		
		document.writeln('pluginspage="http://java.com/getjava"');						
		document.writeln('archive="Uploader.jar, nanoxml-lite-2.2.3.jar, Slim.jar"');
		document.writeln('code="com.photochannel.easyUploader.EasyUploader.class"');
		document.writeln('JAVA_CODEBASE="java/"');
		document.write('width="');
		document.write(width);
		document.writeln('"');	
		document.write('height="');
		document.write(height);
		document.writeln('"');	
		document.writeln('MAYSCRIPT');
		if (debugLogParam == 'true') {
			document.writeln('DEBUG="true"');
			document.writeln('debugLogFile="' + debugLogFilePath + '"');
		} else if (debugLogParam == 'console') {
			document.writeln('DEBUG="true"');
		}
		document.writeln('java_arguments="-Xmx256m"');		
		document.write('language="');
		document.write(language);
		document.writeln('"');
		if (speedTest != 0) {		
			document.write('speedTest="');
			document.write(speedTest);
			document.writeln('"');
		}
		document.write('batchInitURL="');
		document.write(UploadBatchInitURL);
		document.writeln('"');
		document.write('errorLoggingURL="');
		document.write(errorLoggingURL);
		document.writeln('"');		
		document.write('appContextID="');
		document.write(AppContextID);
		document.writeln('"');				
		document.write('userID="');
		document.write(EncUserID);
		document.writeln('"');		
		document.write('ReducePhotosDefault="');
		document.write(ReducePhotosDefault);
		document.writeln('"');			
//
// TODO: fetch values from DB
//		
		document.writeln('UploadMaxThreads="2"');
		document.writeln('ImageDownscalingJpegQFactor="90"');
		document.writeln('ImageDownscalingMinDimensionPx1="1800"');
		document.writeln('ImageDownscalingMinDimensionPx2="1200"');
//
//				
		document.writeln('></embed>'); 							
	}
	if (_info != null)
		log("navigator.userAgent: "+_info);
		
	JavaVersion = PluginDetect.getVersion('Java');	
	if (JavaVersion != null)
		log("Java version: "+JavaVersion);
}

function installJRE_WinIE(AbsoluteBasePath)
{
	document.write('<object classid="');
	document.write(ieClassId);
	document.writeln('"');
	document.write('codebase="');
	document.write(ieCodeBase);
	document.writeln('"');
	document.writeln('width="0" height="0">');
	document.writeln('<param name="type"       value="application/x-java-applet;version=1.5">');
	document.writeln('<param name="code"       value="com.sun.java.Main" >');
	document.writeln('<param name="codebase"   value="java/" >');
	document.writeln('<param name="archive"    value="download.jar" >');
	document.writeln('<param name="scriptable" value="false">');
	document.writeln('<param name="success"    value="' + AbsoluteBasePath + '/upload/upload.aspx">');
	document.writeln('<param name="bgcolor"    value="ffffff">');
	document.writeln('<span class="title"><br />');
	document.writeln('We encountered an issue while trying to automatically install Java software onto your machine. <br />');
	document.writeln('<img src="http://www.java.com/en/img/download/alert-activex.gif" width="400" height="88" border="0"><br /><br />');
	document.writeln('If you encounter an error, check the top of the browser (see image above) for a yellow bar that reads "This site might require the following ActiveX control: J2SE Runtime Environment 6 Update 1');
	document.writeln('Click here to install..."  Click the yellow bar and choose "Install ActiveX Control..." to allow installation to proceed.<br /><br /></span>');
	document.writeln('</object>');
}

// Process the result of the XPInstall.

function checkInstall(name, result)
{
   // Installation failed.

   if (result)
   {
      //alert("The installation of package " + name + " failed with " + result);
      //document.location = "upload.aspx?JREInstalled=0"; 		
   }

   // Installation successful, so try to reload current page.

   else
   {
      //window.location.reload();
      //document.location = "upload_java.aspx"; 		
   }
}

function isInstalled()
{
   var version='1.6.0_01';
   navigator.plugins.refresh(true);
   if ((navigator.mimeTypes) && (navigator.mimeTypes.length))
   {
      var str1 = 'application/x-java-applet;jpi-version=';
      var len = str1.length;
      for (i = 0; i < navigator.mimeTypes.length; ++i)
      {
         var str2 = navigator.mimeTypes[i].type;
         if ((str2.length > len) && (str2.substr(0, len) == str1))
         {
            thisver = str2.substr(len);
            if (thisver >= version)
               return(true);
         }
      }
   }
   return(false);
}


// Install the JRE with XPInstall verification.

function installJRE()
{
   // The browser supports XPInstall.

   if (InstallTrigger.enabled())
   {
      txt = "J2SE(TM) Runtime Environment 6 Update 1";
      var xpi = new Object();
      xpi[txt] = "http://java.sun.com/update/1.6.0/jre-6u1-windows-i586-jc.xpi";

      // Try to install the package and process the results.

      InstallTrigger.install(xpi, checkInstall);
   }

   // The browser does not support XPInstall.

   else
   {
      alert("XPInstall not supported by this browser.");
   }
}

/* mmmmm, cookie functions */
/* http://www.quirksmode.org/js/cookies.html */
/* http://www.bayareaveg.org/gallery2/d/6458-2/cookies.jpg */

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);
}
