var QueuedFiles=false;
var SubmittedDoc=false;
var LiveConnect=false;

function SDOFFtpClient()
{
  var _info = navigator.userAgent;
  var ie = (_info.indexOf("MSIE") > 0);
  var win = (_info.indexOf("Win") > 0);
  var WebUrl = document.getElementById('sdweburl').value;
  var propsfile = WebUrl + 'sd/js/thinupload.properties';
  var messagefile = WebUrl + 'droptarget.html';
  var dt = document.getElementById('sddroptarget');
  var dtWide =  dt.width;
  var dtHeight = dt.height ? dt.height : dt.offsetHeight;

	if(win)
	{

		if(ie)
		{
            document.writeln('<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"');
            document.writeln('width= "' + dtWide + '" height= "' + dtHeight + '" id="rup"');
            document.writeln('codebase="http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#version=1,4,1">');
            document.writeln('<param name="archive" value="/sd/js/ThinFTPUpload.jar">');
            document.writeln('<param name="code" value="com.thinfile.upload.ThinFTPUpload">');
            document.writeln('<param name="props_file" value="' + propsfile + '">');
            document.writeln('<param name="name" value="Spinnerdog FTP Upload">');
            document.writeln('<param name="message" value="' + messagefile + '">');
		}
		else {
            dtHeight = document.getElementById('sddroptarget').offsetHeight;
	          document.writeln('<object type="application/x-java-applet;version=1.4.1"');
	          document.writeln('width= "' + dtWide + '" height= "' + dtHeight + '" id="rup" name="rup" >');
            document.writeln('<param name="archive" value="/sd/js/ThinFTPUpload.jar">');
            document.writeln('<param name="code" value="com.thinfile.upload.ThinFTPUpload">');
            document.writeln('<param name="props_file" value="' + propsfile + '">');
            document.writeln('<param name="name" value="Spinnerdog FTP Upload">');
            document.writeln('<param name="message" value="' + messagefile + '">');
		    }

	}
	else {
		/* mac and linux */
		document.writeln('<applet ');
		document.writeln('   			archive  = "/sd/js/ThinFTPUpload.jar"');
		document.writeln('   			code     = "com.thinfile.upload.ThinFTPUpload"');
		document.writeln('   			name     = "Thin FTP Upload"');
		document.writeln('   			hspace   = "0"');
		document.writeln('   			vspace   = "0" MAYSCRIPT="yes"');
		document.writeln('   			width = "' + dtWide + '"');
		document.writeln('  			height = "' + dtHeight + '"');
		document.writeln('	   		align = "middle" id="rup" name="rup">');
    document.writeln('<param name="message" value="' + messagefile + '">');
    document.writeln('<param name="props_file" value="' + propsfile + '">');
	}


 	if(win)
	{
		document.writeln('</object>');
	}
	else
	{
		document.writeln('</applet>');
	}
}
function SDOFFormLoad()
{

	if( window.cmdForm ){
		alert( "OI EP detected" );
		}
  // insure the client has java installed
  if( ! navigator.javaEnabled() ){
    window.location = '../sd/html/NoJava.htm';
    }
  // find the form element named droptarget and insert the ftp client
  //var btn = document.getElementById('SDOFsubmit');
  //btn.disabled=true;
  if ( window.OnFormLoad ){
       OnFormLoad();
   }

}

function SDOFFormFtpUrl()
{
  var ftppass = document.getElementById( 'sdftppass' ).value;

  if( ftppass.match( /^field:/ ) ){
    var fname = ftppass.replace( /^field:/, "" );
    ftppass = document.getElementById( fname ).value;
    ftppass = ftppass.replace( /@/, '%40' );

  }

  ftppass = ftppass.replace( /^.*</, '');
  ftppass = ftppass.replace( />.*$/, '' );

  var ftpUrl = 'ftp://' +
                document.getElementById( 'sdftpuser' ).getAttribute( "value" ) +
                ':' + ftppass +
                '@' + document.getElementById( 'sdftphost' ).getAttribute( "value" ) +
                '/' + document.getElementById( 'sdftpupdir' ).getAttribute( "value" );

  return( ftpUrl );
}

function FormToQueryString( theForm ){
var qString = ""; 
var amp = ""; 
for(i=0; i<theForm.elements.length; i++){ 
if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "hidden"){ 
qString += amp+theForm.elements[i].name+"="+encodeURIComponent(theForm.elements[i].value); 
} else if (theForm.elements[i].type == "checkbox" || theForm.elements[i].type == "radio") { 
qString += amp+theForm.elements[i].name+"="+theForm.elements[i].checked; 
} else if (theForm.elements[i].type == "select-one") { 
qString += amp+theForm.elements[i].name+"="+encodeURIComponent(theForm.elements[i].options[theForm.elements[i].selectedIndex].value || theForm.elements[i].options[theForm.elements[i].selectedIndex].text ); 
}
amp = "&"; 
} 
return( qString );
}

function PostForm( theForm ){
	var xhr = false;
	if ( !window.XMLHttpRequest){
		if( window.ActiveXObject ){
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	}else{ 
		xhr = new XMLHttpRequest();
	}
	
	var data = FormToQueryString( theForm );
	xhr.onreadystatechange = function(){
		if (this.readyState == 4) {
			if( this.status == 200){
				return;
			} 
			else {
				//alert('There was a problem with the request. it returned ' + this.status + ' with state ' + this.readyState );
			}
		}
	};
	xhr.open("POST", theForm.action + ',nan' );
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", data.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(data);
}
		
function SDOFSubmit()
{
  var _info = navigator.userAgent;
  var ie = (_info.indexOf("MSIE") > 0);
  

  if ( window.FormValid ){
      if( ! FormValid() ){
        return(0);
      }
   }

  if( document.getElementById( 'sdrequireuploads' ).value == 'yes' && document.getElementById( 'sdnoattachwarn' ).value == 'yes' ){
        if( ! QueuedFiles ){
            var loc = dpfindPos( document.getElementById('sddroptarget') );
            movePopup( 'WinID1', loc[0], loc[1] - 220 );
            fadeboxin( 'WinID1' );
            return( 0 );
            }
        }

  var arcfiles = document.getElementsByName( 'UseArchiveFiles' )[0];
  if( arcfiles ){ 
	if( arcfiles.checked == true ){
		document.forms[0].submit();
		return;
		}
	}
  var ftpUrl = SDOFFormFtpUrl();
  
// if the java applet is loaded then send the files
  if( document.rup ){
	  PostForm( document.forms[0] );
	  fadeboxin( 'FileUpload' );
	  document.rup.setFtpMode( document.getElementById( 'sdftpmode' ).value );
	  document.rup.setUrl( ftpUrl );
	  document.rup.jsStartUpload();
	}else{ 
		uploadCompleted(); 
		}


  if( BrowserDetect.OS == 'Mac' && BrowserDetect.browser == 'Safari' ){
	document.forms[0].submit();
	} 
  


}

function startUpload()
{
    document.rup.jsStartUpload();
    return false;
}

function filesQueued()
{
  //var btn = document.getElementById('SDOFsubmit');
  //btn.disabled=false;
  QueuedFiles = true;
  

}

function uploadCompleted()
{
	
	if( QueuedFiles ){
	fadeboxout( 'FileUpload' );
	}
	
	//alert( "Waiting to submit second time" );
	if( document.forms[0] ){
		document.forms[0].submit(); 
	}
		

}

function uploadCancelled()
{
    var btn = document.getElementById('btn_send');
    btn.disabled=true;
}

function fileUploaded(s)
{
   //var flist = document.getElementById('flist');
   //flist.value += escape(s) + "\n";
}
function JSAppletInitialized(s)
{
	LiveConnect = true;
}
function Finished()
{
	if( document.forms[0].sdOIMessage ){
		document.forms[0].sdOIMessage.value = "finished";
	}
	
	window.close();
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
