// JavaScript Document
/*
---
description:     Overlay

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - Overlay
...
*/
var Overlay = new Class({
	
	Implements: [Options,Events],
	
	options:  {
		id: 'overlay',
		color: '#000',
		duration: 500,
		opacity: 0.5,
		zIndex: 5000/*,
		onClick: $empty,
		onClose: $empty,
		onHide: $empty,
		onOpen: $empty,
		onShow: $empty
		*/
	},
	
	initialize: function(container,options) {
		this.setOptions(options);
		this.container = document.id(container);
		this.overlay = new Element('div',{
			id: this.options.id,
			opacity: 0,
			styles: {
				position: 'absolute',
				background: this.options.color,
				left: 0,
				top: 0,
				'z-index': this.options.zIndex
			},
			events: {
				click: function() {
					this.fireEvent('click');
				}.bind(this)
			}
		}).inject(this.container);
		this.tween = new Fx.Tween(this.overlay,{ 
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity',
			onStart: function() {
				this.overlay.setStyles({
					width: '100%',
					height: this.container.getScrollSize().y
				});
			}.bind(this),
			onComplete: function() {
				this.fireEvent(this.overlay.get('opacity') == this.options.opacity ? 'show' : 'hide');
			}.bind(this)
		});
	},
	open: function() {
		this.fireEvent('open');
		this.tween.start(this.options.opacity);
		return this;
	},
	close: function() {
		this.fireEvent('close');
		this.tween.start(0);
		return this;
	}
});


window.addEvent('domready', function() {
<!--datepicker-->
	var todaysdate = new DatePicker('#todaysdate', { pickerClass: 'datepicker_dashboard', format: 'm/d/Y', toggleElements: '.date_toggler', inputOutputFormat: 'm/d/Y'});
	var hearingdate = new DatePicker('#hearingdate', { pickerClass: 'datepicker_dashboard', format: 'Y-m-d', allowEmpty: true, toggleElements: '.date_toggler2', inputOutputFormat: 'Y-m-d'});

<!--auto complete-->
    new Autocompleter.Request.JSON('docname', '/order/docname.php', {
        'postVar': 'search'
    });
<!--swap fields-->	
	$('county1').addEvent('change', function(){
	new Request.HTML({
		url: '/order/court.php',
		method: 'get',
		update: 'courtdiv',
		data: 'value' 											  
		}).get({'val':this.get('value')});						 
	});	
 <!--remove formcheck from selected fields--->
		$('local').addEvent('click', function(e){	
			//////out of california////
			if(this.get('checked')==true){
				$('county1').setProperty('disabled',true); 
				$('court1').setProperty('disabled', true);	
				$('court').setProperty('disabled', false);			
				$$('tr[id^=local]').setStyle('display','none');
				$('outofcacourt').setStyle('display','table-row');
			}else{
				$('county1').setProperty('disabled', false);
				$('court1').setProperty('disabled', false);	
				$('court').setProperty('disabled', true);			
				$$('tr[id^=local]').setStyle('display','table-row');
				$('outofcacourt').setStyle('display','none');		
			}
		});
		///address add and remove
		var count=1;
		//Add Address Button Properties
		$('addaddress').addEvent('click', function(e){	
		count=count+1;
			 if(count==1){
				$('removeaddress').setStyle('display', 'none');
			 }else if(count==2){
			 	$$('tr[id^=addtr2-]').setStyle('display','table-row');
				$$('input[id^=inputadd2-]').setProperty('disabled',false);
				$('addaddress').setStyle('display', 'inline');
				$('removeaddress').setStyle('display', 'inline');
			 }else{
			 	$$('tr[id^=addtr3-]').setStyle('display','table-row');
				$$('input[id^=inputadd3-]').setProperty('disabled',false);
				$('addaddress').setStyle('display', 'none');
				$('removeaddress').setStyle('display', 'inline');			 
			 	count=3;
			 }
		});	
		//Remove Address Button Properties
		$('removeaddress').addEvent('click', function(e){	
			count=count-1;
			if(count==2){
			 	$$('tr[id^=addtr3-]').setStyle('display','none');
				$$('input[id^=inputadd3-]').setProperty('disabled',true);
				$('removeaddress').setStyle('display', 'inline');	
				$('addaddress').setStyle('display', 'inline');
			 }else{

				$('addaddress').setStyle('display', 'inline');
				$('removeaddress').setStyle('display', 'none');
				$$('tr[id^=addtr2-]').setStyle('display','none');
				$$('input[id^=inputadd2-]').setProperty('disabled',true);
			 }
		});		
	//// Doctype input when changed to Summons Type Document
	//// The drop down input for Serve Entity will show 
	$('doctype').addEvent('change', function(){
		if(this.get('value')=='X'){
			$('serveentity').setStyle('display','table-row');
			$('serveentityid').setProperty('disabled',false);
		}else{
			$('serveentity').setStyle('display','none');
			$('serveentityid').setProperty('disabled',true);
		}	
	});	
	//// Serve Entity drop down when choosing Other
	//// All of the checkboxes for type of Serve Entity is shown
	$('serveentityid').addEvent('change', function(){
		if(this.get('value')=='O'){
			$$('tr[id^=serveentityoption]').setStyle('display','table-row');
			$$('input[id^=entity1-]').setProperty('disabled',false);
		}else{
			$$('tr[id^=serveentityoption]').setStyle('display','none');
			$('entity').setProperty('class', " "); 
			$$('input[id^=entity1-]').setProperty('disabled',true);
		}
	
	});
	//// Switching from Adding another subject button to Submit Button
	$('yessubmitorder').addEvent('click', function(){
		if(this.get('checked')==true){
			$('anothersubject').setStyle('display','none');
			$('anothersubject').setProperty('disabled',true);
			$('submitorder').setStyle('display','block');
			$('submitorder').setProperty('disabled',false);
		}else{
			$('anothersubject').setStyle('display','block');
			$('anothersubject').setProperty('disabled',false);			
			$('submitorder').setStyle('display','none');
			$('submitorder').setProperty('disabled',true);
		}	
	});			
	
////////////////////////////////////	
////     fancy uploader        ////
///////////////////////////////////
var fupload = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
		// we console.log infos, remove that in production!!
		verbose: true,
		
		// url is read from the form, so you just have to change one place
		url: $('orderform').action,
		
		// path to the SWF file
		path: '/_includes/js/fancyuploader/Swiff.Uploader.swf',
		
		// remove that line to select all files, or edit it, add more items
		typeFilter: {
			'Accepted(*.jpg, *.jpeg, *.gif, *.png, *.tiff, *.doc, *.docx, *.txt, *.wps, *.zip,*.rar,*.pdf)': '*.jpg; *.jpeg; *.gif; *.png; *.tiff,*.doc; *.docx; *.txt; *.wps,*.zip; *.rar;*.pdf'
		},
		
		// this is our browse button, *target* is overlayed with the Flash movie
		target: 'demo-browse',
		
		// graceful degradation, onLoad is only called if all went well with Flash
		onLoad: function() {
			$('demo-status').removeClass('hide'); // we show the actual UI
			$('demo-fallback').destroy(); // ... and hide the plain form
			
			// We relay the interactions with the overlayed flash to the link
			this.target.addEvents({
				click: function() {
					return false;
				},
				mouseenter: function() {
					this.addClass('hover');
				},
				mouseleave: function() {
					this.removeClass('hover');
					this.blur();
				},
				mousedown: function() {
					this.focus();
				}
			});

			// Interactions for the 2 other buttons
			
			$('demo-clear').addEvent('click', function() {
				fupload.remove(); // remove all files
				return false;
			});

			$('demo-upload').addEvent('click', function() {
				fupload.start(); // start upload
				return false;
			});
		},
		
		// Edit the following lines, it is your custom event handling
		
		/**
		 * Is called when files were not added, "files" is an array of invalid File classes.
		 * 
		 * This example creates a list of error elements directly in the file list, which
		 * hide on click.
		 */ 
		onSelectFail: function(files) {
			files.each(function(file) {
				new Element('li', {
					'class': 'validation-error',
					html: file.validationErrorMessage || file.validationError,
					title: MooTools.lang.get('FancyUpload', 'removeTitle'),
					events: {
						click: function() {
							this.destroy();
						}
					}
				}).inject(this.list, 'top');
			}, this);
		},
		
		/**
		 * This one was directly in FancyUpload2 before, the event makes it
		 * easier for you, to add your own response handling (you probably want
		 * to send something else than JSON or different items).
		 */
		onFileSuccess: function(file, response) {
			/*var json = new Hash(JSON.decode(response, true) || {});
			
			if (json.get('status') == '1') {
				file.element.addClass('file-success');
				file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
			} else {
				file.element.addClass('file-failed');
				file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
			}*/
		},
		
		/**
		 * onFail is called when the Flash movie got bashed by some browser plugin
		 * like Adblock or Flashblock.
		 */
		onFail: function(error) {
			switch (error) {
				case 'hidden': // works after enabling the movie and clicking refresh
					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
					break;
				case 'blocked': // This no *full* fail, it works after the user clicks the button
					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
					break;
				case 'empty': // Oh oh, wrong path
					alert('A required file was not found, please be patient and we fix this.');
					break;
				case 'flash': // no flash 9+ :(
					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
			}
		}
		
	});
	
   // Preload Images
img1 = new Image(16, 16);  
img1.src="/_assets/spinner.gif";

img2 = new Image(220, 19);  
img2.src="/_assets/loader-bar.gif";

/////////////overlay for processing/////////////
var overlay = new Overlay(document.body,{
	id: 'overlay',
	color: '#000',
	duration: 300,
	opacity: .8,
	onClick: function(){
		//this.close();
	},
	onOpen: function(){
		processing_bg = new Element('div',{
			'id': 'processing_bg',
			'opacity': 100,
			'styles': {
				'position': 'absolute',
				'background': "url('/_assets/processing_bg.png') no-repeat",
				'width': '300px',
				'height': '150px',
				'left': '500px',
				'top': '500px',
				'z-index': 6000
			}
		}).inject(document.body);
		processing_loader = new Element('div',{
			'id': 'processing_loader',
			'opacity': 100,
			'styles': {
				'position': 'absolute',
				'background': "url('/_assets/processingloader.gif') no-repeat",
				'width': '300px',
				'height': '150px',
				'left': '500px',
				'top': '500px',
				'z-index': 6000
			}
		}).inject(document.body);				
	},
	onClose: function(){
		$('processing_loader').dispose();
		$('processing_bg').dispose();
		
	}
	
});
/////////overlay for buttons when completed//////////
var overlay_option = new Overlay(document.body,{
	id: 'overlay_option',
	color: '#000',
	duration: 300,
	opacity: .8,
	onClick: function(){
		//this.close();
	},
	onOpen: function(){
		processing_bg = new Element('div',{
			'id': 'processing_bg',
			'opacity': 100,
			'styles': {
				'position': 'absolute',
				'background': "url('/_assets/processing_bg.png') no-repeat",
				'width': '300px',
				'height': '150px',
				'left': '500px',
				'top': '500px',
				'z-index': 6000
			}
		}).inject(document.body);	
		processing_text = new Element('div',{
			'id': 'processing_text',
			'opacity': 100,
			'styles': {
				'position': 'absolute',
				'width': '300px',
				'height': '150px',
				'left': '500px',
				'top': '500px',
				'z-index': 7000
			}
		}).inject(document.body);										
	},
	onClose: function(){
		$('processing_bg').dispose();
		$('processing_text').dispose();
		
	}	
	
});				
var myscroll = new Fx.Scroll(window, {
	wait: false,
	duration: 300,
	offset: {'x': -200, 'y': -250},
	transition: Fx.Transitions.Quad.easeInOut
});
var count=1;
$('subjectnumber').set('text',count);
var OrderForm=new FormCheck('orderform',{
	//submitByAjax: false,
	//submit:false,
	onSubmit: function (){
		//alert();
	},
	onValidateSuccess : function(){
		// Show the spinning indicator when pressing the submit button...
		//$('ajax_loading').setStyle('display','block');
		var sendform=$('orderform').set('send', { 
			onRequest: function(){
				overlay.open();
				fupload.start();
			},
			onSuccess: function(response) {
				myscroll.toElement('processing_bg');
				overlay.close();
				//Open the overlay for button selection
				overlay_option.open();
				if($('anothersubject').getProperty('disabled')==false){
					$('processing_text').set('html','<div id="addanotheroption"><h3>Please select</h3><br/><a href="#" id="addanothersubject_again">Add Another Subject</a> | <a href="#" id="iamdone">Done</a></div>');
					$('ajax_loading').setStyle('display','none');	
					$('addanothersubject_again').addEvent('click', function(e){	
						overlay_option.close();
						myscroll.toElement('serveentity');
						count=count+1;
						$('addingmoremessage').setStyle('display','block');
						$('subjectnumber').set('html',count);
					});
					$('iamdone').addEvent('click', function(e){	
						overlay_option.close();
						document.location.href='/?sec=order&sub=pendingorders';
					});		
				}else{
					$('processing_text').set('html','<div id="addanotheroption"><h3>Order Complete</h3><br /><a href="#" id="iamdone">View Orders</a></div>');
					$('iamdone').addEvent('click', function(e){	
						overlay_option.close();
						document.location.href='/?sec=order&sub=pendingorders';
					});					
				}
			}
		});
		// Send the form.
		sendform.send();
     }, 
    onValidateFailure : function(){ 
			$('processing_text').set('html','<div id="addanotheroption">We do appologize for the inconvenience, we were unable to process your order at this time.  Please contact administrator.</div>');
			$('ajax_loading').setStyle('display','none');	
    } 
});

			
});

	
