window.addEvent('domready', function() {
 document.id('Rib_Upload').setStyle('cursor', 'pointer');
document.id('Rib_SizeQty').setStyle('cursor', 'pointer');
  document.id('Rib_Terms').setStyle('cursor', 'pointer');
  document.id('Rib_Order').setStyle('cursor', 'pointer');
	var up = new FancyUpload2(document.id('demo-status'), document.id('demo-list'), {
	url: document.id('form-demo').action,
		path: 'scripts/Swiff.Uploader.swf',
		typeFilter: {'Images ( *.jpg, *.jpeg, *.pdf, *.tif)': '*.pdf; *.tif; *.jpg; *.jpeg;'},
		target: 'demo-browse',
		onLoad: function() {
			document.id('demo-status').removeClass('hide'); // we show the actual UI
			document.id('demo-fallback').destroy();         // ... and hide the plain form
			this.target.addEvents({
				click: function(){
					document.id('AddButton').setStyle('background-position', "0px -64px");
					return false;
				},
				mouseenter: function() {
					this.addClass('hover');
					document.id('AddButton').setStyle('background-position', "0px -64px");
				},
				mouseleave: function() {
					document.id('AddButton').setStyle('background-position', "0px -1px");
					this.removeClass('hover');
					this.blur();
				},
				mousedown: function() {
					document.id('AddButton').setStyle('background-position', "0px -32px");
					this.focus();
				}
			});
			// document.id('demo-clear').addEvents({
			// 	click: function() {
			// 		up.remove(); // remove all files
			// 		return false;	
			// 	},
			// 	mousedown: function() {
			// 		document.id('RemoveButton').setStyle('background-position', "-32px -32px");
			// 		return false;	
			// 	},
			// 	mouseleave: function() {
			// 		document.id('RemoveButton').setStyle('background-position', "-32px -1px");
			// 		return false;	
			// 	}
			// 	
			// });

			document.id('demo-upload').addEvents({
				click: function() {
					up.start(); // start upload
					try{pageTracker._trackEvent('Prices', 'FileUploadActivated');}catch(err) {}
					
					return false;	
				},
				mouseenter: function() {
					this.addClass('hover');
					document.id('UploadButton').setStyle('background-position', "-64px -64px");
				},
				mousedown: function() {
					document.id('UploadButton').setStyle('background-position', "-64px -32px");
					return false;	
				},
				mouseleave: function() {
					document.id('UploadButton').setStyle('background-position', "-64px -1px");
					return false;	
				}
				
			});
		},

		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);
		},

		onFileSuccess: function(file, response) {
			var json = new Hash(JSON.decode(response, true) || {});

			if (json.get('status') == '1') {
				file.element.addClass('file-success');
				addToJobFileList(json.get('theFileName'));
				file.info.set('html', '<strong>File was uploaded:</strong> The file on the server is:' + json.get('size'));
				document.id('FileUploadNext').setStyle('display', 'block');
			} 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
					DismissBigSpinner("To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).", "Problem");
					break;
				case 'blocked': // This no *full* fail, it works after the user clicks the button
					DismissBigSpinner("To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).", "Problem");
					break;
				case 'empty': // Oh oh, wrong path
					DismissBigSpinner("A required file was not found, please be patient and we wil endeavour to fix this.", "Problem");
					break;
				case 'flash': // no flash 9+ :(
					DismissBigSpinner("To enable the embedded uploader, install the latest Adobe Flash plugin.", "Problem");
			}
		}

});

// Rib .. Ribbons  Rib_Upload is the Alternate Upload Button On The Ribbon at ther tiop of the column
document.id('Rib_Upload').addEvent('click', function(){
	ScreenClick('Upload');
});

document.id('Rib_SizeQty').addEvent('click', function(){
	ScreenClick('SetSizeQty');
});
document.id('Rib_Terms').addEvent('click', function(){
	ScreenClick('Terms');
});
document.id('Rib_Order').addEvent('click', function(){
	ScreenClick('Buy');
});


document.id('FileUploadNext').addEvent('click', function(){//File Upload Next Button
	ScreenClick('SetSizeQty');
});

document.id('SizeQuantityNext').addEvent('click', function(){//Size And Qunatity Nex Button
	ScreenClick('Terms');
});

document.id('TermsAgreement').addEvent('click', function(){//Agree Terms Check Box.. shiow reveale next button
	if(document.id('TermsAgreement').get('checked')){
		changeSteps('Rib_Terms', 'Terms_Green');
		document.id('TermsNext').reveal({duration: 1000});

	}else{
		changeSteps('Rib_Terms', 'Terms_Orange');
		document.id('TermsNext').dissolve({duration: 1000});	
	};
});

document.id('TermsNext').addEvent('click', function(){
	ScreenClick('Buy');
}); // Terms Click

//document.id('LineItem_6').setStyle('display', 'block'); // Surcharge	
document.id('LineItem_7').setStyle('display', 'block');	


// Google Analytics Code
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
      onload: function() {
          var pageTracker = _gat._getTracker("UA-15064843-1");
          pageTracker._initData();
          pageTracker._trackPageview();
      }
  });



}); // Dom

function changeSteps(targetdiv, BGPosition){
	var StepsBGPositions = new Object;
	//target id for these is Rib_Upload
	StepsBGPositions.Upload_Green = "2px 2px";
	StepsBGPositions.Upload_Orange = "2px -23px";
	StepsBGPositions.Upload_Red = "2px -47px";
	//target id for these is Rib_SizeQty
	StepsBGPositions.SizeQty_Green = "-82px 2px";
	StepsBGPositions.SizeQty_Orange = "-82px -23px";
	StepsBGPositions.SizeQty_Red = "-82px -47px";	
	//target id for these is Rib_Terms
	StepsBGPositions.Terms_Green = "-202px 3px";
	StepsBGPositions.Terms_Orange = "-202px -22px";
	StepsBGPositions.Terms_Red = "-202px -47px";	
	//target id for these is Rib_Order
	StepsBGPositions.Order_Green = "-270px 2px";
	StepsBGPositions.Order_Orange = "-270px -23px";
	StepsBGPositions.Order_Red = "-270px -47px";
	
	document.id(targetdiv).setStyle('background-position', StepsBGPositions[BGPosition]);	
}

function DismissBigSpinner(SpinMessage, SpinMessIcon){
	document.id('BigSpinner').set('html', SpinMessage);
	switch(SpinMessIcon){
		case 'Tick':
		document.id('BigSpinner').setStyle('background-image','url(WebAssets/BigGreenTick.gif)');
		break;
		case 'Caution':
		document.id('BigSpinner').setStyle('background-image','url(WebAssets/BigOrangeCaution.gif)');
		break;
		case 'Problem':
		document.id('BigSpinner').setStyle('background-image','url(WebAssets/BigRedX.gif)');	
		break;		
	}
	(function(){ document.id('BigSpinner').setStyle('display', 'none') }).delay(500);
	(function(){ document.id('BigSpinner').set('html', 'Loading...') }).delay(500);
	(function(){ document.id('BigSpinner').setStyle('background-image','url(WebAssets/5-1.gif)') }).delay(500);	
	
}

function addToJobFileList(theFileName){
if(document.id('File_0').get('html') === ''){
document.id('File_0').set('html', theFileName);
document.id('LineItem_0').setStyle('display', 'block');
changeSteps('Rib_Upload', 'Upload_Green');
changeSteps('Rib_SizeQty', 'SizeQty_Orange');
// Row Events seeing as we are usung them
document.id('Size_0').addEvent('change', function(){//Change Size_0
updateLinePrice('0');
});
document.id('Quantity_0').addEvent('keyup', function(){
updateLinePrice('0');
});

return
}	
if(document.id('File_1').get('html') === ''){
document.id('File_1').set('html', theFileName);
document.id('LineItem_1').setStyle('display', 'block');

document.id('Size_1').addEvent('change', function(){//Change Size_1
updateLinePrice('1');
});
document.id('Quantity_1').addEvent('keyup', function(){
updateLinePrice('1');
});
return
}
if(document.id('File_2').get('html') === ''){
document.id('File_2').set('html', theFileName);
document.id('LineItem_2').setStyle('display', 'block');

document.id('Size_2').addEvent('change', function(){//Change Size_2
updateLinePrice('2');
});
document.id('Quantity_2').addEvent('keyup', function(){
updateLinePrice('2');
});
return
}
if(document.id('File_3').get('html') === ''){
document.id('File_3').set('html', theFileName);
document.id('LineItem_3').setStyle('display', 'block');

document.id('Size_3').addEvent('change', function(){//Change Size_3
updateLinePrice('3');
});
document.id('Quantity_3').addEvent('keyup', function(){
updateLinePrice('3');
});
return
}
if(document.id('File_4').get('html') === ''){
document.id('File_4').set('html', theFileName);
document.id('LineItem_4').setStyle('display', 'block');
document.id('Size_4').addEvent('change', function(){//Change Size_4
updateLinePrice('4');
});
document.id('Quantity_4').addEvent('keyup', function(){
updateLinePrice('4');
});
return
}
	
	
}//addToJobFileList

function updateLinePrice(RowID){
//	alert(document.id('File_' + RowID).get('html') + "\n" + document.id('Size_' + RowID).get('value') + "\n" + document.id('Quantity_' + RowID).get('value'));
//	alert(document.id('Origs_' + RowID).get('readonly'));
	var jsonRequest = new Request.JSON({
				   url: "AJ_Prices.php",
				method: "post",
				  data: {'Action':'GetLinePrice',
						  'RowID': RowID,
				    'TheFileName':document.id('File_' + RowID).get('html'),
				'TheDocumentSize':document.id('Size_' + RowID).get('value'),
				    'TheQtyOrigs':document.id('Origs_' + RowID).get('value'),
				    'TheQuantity':document.id('Quantity_' + RowID).get('value'),
					   'ReadOnly':document.id('Origs_' + RowID).get('readonly')
					    },
				onComplete: function(theData, theXML){
					document.id('Unit_' + RowID).set('html', theData.Unit.toFixed(2) + 'GBP');	
					document.id('Price_' + RowID).set('html', theData.Price.toFixed(2) + 'GBP');
					
					if(parseInt(theData.Origs) < 1 && document.id('Origs_' + RowID).get('readonly')){
					alert('Warning: The Server could not extract the number of Pages in the PDF File.\nThis could be because the file is either:\ncorrupt\nor the Security has been switched on.\nIf you know that the pdf is printable please enter the number of pages in the Originals Box.');				
					document.id('Origs_' + RowID).erase('readonly');
					document.id('Origs_' + RowID).focus();
					}else{
					document.id('Origs_' + RowID).set('value', theData.Origs);
					}
					if(theData.message){
						document.id('message').set('html', document.id('message').get('html') +"\n" + theData.message);
					}
					if(theData.EV){theData.EV.each(function(el) {eval(el);});};
					updateTotals();
					
				},
				onFailure: function(){
					alert("Failed");
			}
		}).send();
	
} //updateLinePrice


function updateTotals(){
	//	alert(document.id('File_' + RowID).get('html') + "\n" + document.id('Size_' + RowID).get('value') + "\n" + document.id('Quantity_' + RowID).get('value'));
	//	alert(document.id('Origs_' + RowID).get('readonly'));
		var jsonRequest = new Request.JSON({
					   url: "AJ_Prices.php",
					method: "post",
					  data: {'Action':'GetTotalsPrice',
								'TheFileName_0':document.id('File_0').get('html'),
							'TheDocumentSize_0':document.id('Size_0').get('value'),
								'TheQtyOrigs_0':document.id('Origs_0').get('value'),
								'TheQuantity_0':document.id('Quantity_0').get('value'),
								'TheFileName_1':document.id('File_1').get('html'),
							'TheDocumentSize_1':document.id('Size_1').get('value'),
								'TheQtyOrigs_1':document.id('Origs_1').get('value'),
								'TheQuantity_1':document.id('Quantity_1').get('value'),
								'TheFileName_2':document.id('File_2').get('html'),
							'TheDocumentSize_2':document.id('Size_2').get('value'),
								'TheQtyOrigs_2':document.id('Origs_2').get('value'),
								'TheQuantity_2':document.id('Quantity_2').get('value'),
								'TheFileName_3':document.id('File_3').get('html'),
							'TheDocumentSize_3':document.id('Size_3').get('value'),
								'TheQtyOrigs_3':document.id('Origs_3').get('value'),
								'TheQuantity_3':document.id('Quantity_3').get('value'),
								'TheFileName_4':document.id('File_4').get('html'),
							'TheDocumentSize_4':document.id('Size_4').get('value'),
								'TheQtyOrigs_4':document.id('Origs_4').get('value'),
								'TheQuantity_4':document.id('Quantity_4').get('value')
						    },
					onComplete: function(theData, theXML){
						document.id('Total_NET').set('html', theData.Total_NET.toFixed(2) + ' GBP');	
						document.id('Total_VAT').set('html', theData.Total_VAT.toFixed(2) + ' GBP');
						document.id('Total_GROSS').set('html', theData.Total_GROSS.toFixed(2) + ' GBP');
						if(theData.SurchargePrice){
							document.id('QandSSurchargeUnit').set('html', theData.SurchargePrice.toFixed(2) + ' GBP');
							document.id('QandSSurchargePrice').set('html', theData.SurchargePrice.toFixed(2) + ' GBP');
						}
						//alert(theData.DeliveryPrice);
						if (typeof theData.DeliveryPrice != "undefined") {
						//if(isObject(theData.DeliveryPrice)){
							document.id('QandSDeliveryUnit').set('html', theData.DeliveryPrice.toFixed(2) + ' GBP');
							document.id('QandSDeliveryPrice').set('html', theData.DeliveryPrice.toFixed(2) + ' GBP');
							
						}
						if(theData.EV){theData.EV.each(function(el) {eval(el);});};
						if(theData.message){
							document.id('message').set('html', document.id('message').get('html') +"\n" + theData.message);
						}

					},
					onFailure: function(){
						alert("Failed");
				}
			}).send();
// This Checks to see iof there is enough to continue to terms	
var SetScreenGreen = true;
if(document.id('LineItem_0').getStyle('display')==='block' && document.id('Size_0').get('value') === '--') SetScreenGreen =false;
if(document.id('LineItem_1').getStyle('display')==='block' && document.id('Size_1').get('value') === '--') SetScreenGreen =false;
if(document.id('LineItem_2').getStyle('display')==='block' && document.id('Size_2').get('value') === '--') SetScreenGreen =false;
if(document.id('LineItem_3').getStyle('display')==='block' && document.id('Size_3').get('value') === '--') SetScreenGreen =false;
if(document.id('LineItem_4').getStyle('display')==='block' && document.id('Size_4').get('value') === '--') SetScreenGreen =false;
if(SetScreenGreen){
changeSteps('Rib_SizeQty', 'SizeQty_Green');
}else{
changeSteps('Rib_SizeQty', 'SizeQty_Orange');
	}

}
function ScreenClick(selectedbutton){
	switch (selectedbutton) {
			case 'Upload': 
			document.id('colsx').reveal({duration: 1000}); // Left colsx
			document.id('PricesTable').reveal({duration: 1000});
			document.id('ordersystem').tween('width', '415'); // Right column ordersystem
			$$('.UploadScreenText').reveal({duration: 1000});
			$$('.SizeandquantityScreen').setStyle('display','none');
			document.id('Upload_Screen').reveal({duration: 1000});
			document.id('SizeAndQuantity_Screen').dissolve({duration: 1000});
			document.id('Terms_Screen').dissolve({duration: 1000});
			document.id('Order_Screen').dissolve({duration: 1000});
			
			
			break;
			case 'SetSizeQty':
			if(document.id('LineItem_0').getStyle('display')==='block' || document.id('LineItem_1').getStyle('display')==='block' ||document.id('LineItem_2').getStyle('display')==='block'||document.id('LineItem_3').getStyle('display')==='block'||document.id('LineItem_4').getStyle('display')==='block'){
				document.id('ordersystem').tween('width', '415'); // Right column ordersystem
				document.id('colsx').reveal({duration: 1000}); // Left colsx
				$$('.SizeandquantityScreen').reveal({duration: 1000});
				$$('.UploadScreenText').setStyle('display','none');
				document.id('PricesTable').dissolve({duration: 1000});
				document.id('Upload_Screen').dissolve({duration: 1000});
				document.id('SizeAndQuantity_Screen').reveal({duration: 1000});
				document.id('Terms_Screen').dissolve({duration: 1000});
				document.id('Order_Screen').dissolve({duration: 1000});
				try{pageTracker._trackEvent('Prices', 'SizeAndQunatityActivated');}catch(err) {}
			}else{
				document.id('Rib_Upload').fireEvent('click');// They Have not Uploaded anything.
				alert('Oops... You must upload you files first\nClick the upload button before moving on to \nsetting Size and Quantity.\n... thank you');
				
				
			}
			break;
			case 'Terms':
			if(document.id('Rib_SizeQty').getStyle('background-position') === '-82px 2px'){
			$$('.UploadScreenText').dissolve({duration: 1000});
			$$('.SizeandquantityScreen').dissolve({duration: 1000});
			document.id('PricesTable').dissolve({duration: 1000});
			document.id('colsx').dissolve({duration: 1000});
			document.id('ordersystem').tween('width', document.id('content').getSize().x - 30);
			document.id('Upload_Screen').dissolve({duration: 1000});
			document.id('SizeAndQuantity_Screen').dissolve({duration: 1000});
			document.id('Terms_Screen').reveal({duration: 1000});
			document.id('Order_Screen').dissolve({duration: 1000});
			try{pageTracker._trackEvent('Prices', 'TermsActivated');}catch(err) {}
			}else{
				alert('The Terms screen is not available until you have first uploaded files and set their respective sizes.\nIf you wish to see our Terms page without purchase. It can be found below the FAQ Page.');
			}
			break;
			case 'Buy':
			if((document.id('Total_GROSS').get('html') === '--') || (document.id('TermsAgreement').get('checked') === false)){
				var AlertText = "You can not proceed to the 'buy' Screen Because:\n";
				AlertText += (document.id('Rib_Upload').getStyle('background-position') == '2px 2px')?"":"No Files Were Uploaded.\n";
				AlertText += (document.id('Rib_SizeQty').getStyle('background-position') == '-82px 2px')?"":"File Sizes have Not Been Set.\n";
				AlertText += (document.id('Rib_Terms').getStyle('background-position') == '-202px 3px')?"":"You have NOT agreed to our terms.\n";
				alert("Oops!.." + AlertText);
				return
			}
			$$('.UploadScreenText').dissolve({duration: 1000});
			$$('.SizeandquantityScreen').dissolve({duration: 1000});
			document.id('PricesTable').dissolve({duration: 1000});
			document.id('colsx').reveal({duration: 1000}); // Left colsx
			document.id('ordersystem').tween('width', '415'); // Right column ordersystem
			document.id('Upload_Screen').dissolve({duration: 1000});
			document.id('SizeAndQuantity_Screen').dissolve({duration: 1000});
			document.id('Terms_Screen').dissolve({duration: 1000});
			document.id('Order_Screen').reveal({duration: 1000});
				var jsonRequest = new Request.JSON({
							   url: "AJ_Prices.php",
							method: "post",
							  data: {'Action':'GetInvoicePage',
										'TheFileName_0':document.id('File_0').get('html'),
									'TheDocumentSize_0':document.id('Size_0').get('value'),
										'TheQtyOrigs_0':document.id('Origs_0').get('value'),
										'TheQuantity_0':document.id('Quantity_0').get('value'),
										'TheFileName_1':document.id('File_1').get('html'),
									'TheDocumentSize_1':document.id('Size_1').get('value'),
										'TheQtyOrigs_1':document.id('Origs_1').get('value'),
										'TheQuantity_1':document.id('Quantity_1').get('value'),
										'TheFileName_2':document.id('File_2').get('html'),
									'TheDocumentSize_2':document.id('Size_2').get('value'),
										'TheQtyOrigs_2':document.id('Origs_2').get('value'),
										'TheQuantity_2':document.id('Quantity_2').get('value'),
										'TheFileName_3':document.id('File_3').get('html'),
									'TheDocumentSize_3':document.id('Size_3').get('value'),
										'TheQtyOrigs_3':document.id('Origs_3').get('value'),
										'TheQuantity_3':document.id('Quantity_3').get('value'),
										'TheFileName_4':document.id('File_4').get('html'),
									'TheDocumentSize_4':document.id('Size_4').get('value'),
										'TheQtyOrigs_4':document.id('Origs_4').get('value'),
										'TheQuantity_4':document.id('Quantity_4').get('value')
								    },
							onComplete: function(theData, theXML){
								theData.EV.each(function(el) {
													  eval(el);
													});	
								if(theData.message){
									document.id('message').set('html', document.id('message').get('html') +"\n" + theData.message);
								}
							try{pageTracker._trackEvent('Prices', 'BuyScreenActivated');}catch(err) {}
							},
							onFailure: function(){
								alert("Failed");
						}
					}).send();	
			break;
	}	
}




