﻿// JScript File
	function BuildDownloadDialog(HtmlContentPath,FileUrl,Title) {
		 $("#dialog").dialog({
			   bgiframe: false,
			   autoOpen: false, 
			   resizable: false,
			   height:500,
			   width:500, 
			   modal: true,
			   title: Title,
			   open: function() {
					 //display correct dialog content
							$("#dialog").html("");
							$("#dialog").load(HtmlContentPath);
					 },
			   overlay: {
				   backgroundColor: '#000',
				   opacity: 0.5
			   },
			   buttons: {
				   "Download": function() {
						if($('.ui-dialog .ui-dialog-buttonpane :checkbox').attr("checked")){
							if(FileUrl !=''){
								$("#DownloadForm").submit();
							}
							$(this).dialog('close');
						}else{
						  $('.ui-dialog .ui-dialog-buttonpane :checkbox').focus();
						}
				   }
				},
				close: function() {
					$(this).dialog('destroy');
				}
		});
	}	
	
	
		function DisplayDialogAndAcceptDownload(Url,HtmlContentPath,Title){
			var DownloadFileUrl;
			var DownloadDialogTitle;
			DownloadFileUrl = '';
			DownloadDialogTitle = '';
			
			if(Url !=''){
				DownloadFileUrl = Base64.decode(Url);
			}
			if(Title !=''){
				DownloadDialogTitle = Title;
			}
			
			BuildDownloadDialog(HtmlContentPath,DownloadFileUrl,DownloadDialogTitle);
		
			var btnDownload = $('.ui-dialog .ui-dialog-buttonpane :button');
			var intCheckboxCount = $('.ui-dialog .ui-dialog-buttonpane :checkbox').length
			var pnlFooter = $('.ui-dialog .ui-dialog-buttonpane')
			var cbxAgree = $('.ui-dialog .ui-dialog-buttonpane #cbxAgree')
				
			//add checkbox to button panel
			if(intCheckboxCount == 0){
				var checkboxstring = "<div style='width:200px;float:left; margin: .2em .4em .5em 0; padding: .1em .6em .3em .6em;'>";
				checkboxstring += "<table border='0'><tr><td>";
				checkboxstring += "<input type='checkbox' id='cbxAgree' onclick='SetupDownloadButton(this);'></input>";
				checkboxstring += "</td><td>";
				checkboxstring += "<span style='color:red;vertical-align: middle;position: relative;'>I agree to the License.</span>";
				checkboxstring += "</td></tr></table>";
				checkboxstring += "</div><div style='display:none;'><form id='DownloadForm' action='" + DownloadFileUrl + "'></form></div>"
				pnlFooter.prepend(checkboxstring); 
			}else{
				cbxAgree.attr("checked",false);
			}
			
			//disable button
			//btnDownload.attr("disabled", true);
			SetupDownloadButton(cbxAgree);
			
			
			$('#dialog').dialog('open');
		}
		
		function SetupDownloadButton(chkbox){
			var btnDownload = $('.ui-dialog .ui-dialog-buttonpane :button');
			if(chkbox.checked){
				btnDownload.attr("disabled", false);
				var cssObj = {
					'color' : '#ffffff',
					'background' : '#fdf5ce url(/theme/images/ui-bg_gloss-wave_35_f6a828_500x100.png) 50% 50% repeat-x',
					'cursor' : 'pointer'
				  }
				  btnDownload.css(cssObj);

			}else{
				btnDownload.attr("disabled", true);
				var cssObj = {
					'color' : '#cccccc',
					'background' : '#f6f6f6',
					'cursor' : 'auto'
				  }
				  btnDownload.css(cssObj);
			}
		}
		
		function include(file)  
		 {  
		   var script  = document.createElement('script');  
		   script.src  = file;  
		   script.type = 'text/javascript';  
		   script.defer = true;  
		   
		   document.getElementsByTagName('head').item(0).appendChild(script);  
		}  
		
		include("/Scripts/webtoolkit.base64.js");
