// wrap all functions / variables within the DL object (think namespace protection)
var DL = {

	// form variables
	license:'',
	elOptions:'',


	// initialization script
	init: function() {
	
		// set a reference point for the form observer
		this.storeFormValues();
		
		// observe the form (if it exists) for any changes, react accordingly
		if ($('LicenseForm')) new Form.Observer('LicenseForm', 0.2, DL.formObserver);
	},
	
	
	storeFormValues: function() {
	
		// license type
		$$('#LicenseForm .lic').each(function(elm){
			if ($(elm).checked) DL.license = $(elm).value;
		});
		
		// EL options selected
		DL.elOptions = new Array();
		$$('#LicenseForm .elo').each(function(elm){
			if (elm.checked) DL.elOptions[parseInt(DL.elOptions.length)] = parseInt($(elm).value);
		});
	},
	
	
	
	formObserver: function(frm, params) {
	
		// get current license type
		license = '';
		$$('#LicenseForm .lic').each(function(elm){
			if ($(elm).checked) license = $(elm).value;
		});
		
		// hide any error messages (if present)
		if ($('error1')) $('error1').hide();
		if ($('error2')) $('error2').hide();
			
		if (
			(license != '') &&
			(license != DL.license)
		) {
			$('licenseWrapper').update('<img src="' + istock.cookielessUrl + '/static/images/loading_small.gif" alt="" /> Loading License...');
			$('elConfirm').update('');
			$('elConfirm').hide();
			
			// request new license from server
			var url = 'ajax_class_creator.php';
			var params2 = 'ajax_action=getLicenseJSON&ajax_class=downloadapproved&'+params;
			var myAjax = new Ajax.Request ( 
				url, 
				{
					method: 'post', 
					parameters: params2,
					onComplete: function(req) {
						var json = req.responseText.evalJSON();
						$('licenseWrapper').update(json.licenseWrapper);
					},
					onFailure: DL.ajaxFailed.bind(this)
				}
			);
		}
		
		// check for extended license options
		if (license == 'el') {
		
			// get current EL options
			elOptions = new Array();
			$$('#LicenseForm .elo').each(function(elm){
				if (elm.checked) elOptions[parseInt(elOptions.length)] = parseInt($(elm).value);
			});
			
			// iterate through the elOptions, checking if they do NOT exist in the DL.elOptions
			elOptions.each(function (item, ndx) {
				if ((DL.elOptions[ndx] == undefined) || (DL.elOptions[ndx] != item)) {
					DL.updateProvisions(params);
				}
			});
			
			// if there are no unchecked options but there are DL.options, something needs to be deleted
			if (elOptions.length < DL.elOptions.length) {
				DL.updateProvisions(params);
			}
		}
		
		// re-store form values
		DL.storeFormValues();
	},
	
	
	updateProvisions: function(params){
		$('elProvisions').update('<p><img src="' + istock.cookielessUrl + '/static/images/loading_small.gif" alt="" /> <em style="color:#999"> Loading Provisions...</style></p>');
		$('elCredits').update('<img src="' + istock.cookielessUrl + '/static/images/loading_small.gif" alt="" />');
		$('elConfirm').update('<img src="' + istock.cookielessUrl + '/static/images/loading_small.gif" alt="" style="padding:1em" />');
		$('elConfirm').show();
				
		// request new provisions
		var url = 'ajax_class_creator.php';
		var params2 = 'ajax_action=getProvisionsJSON&ajax_class=downloadapproved&'+params;
		var myAjax = new Ajax.Request ( 
			url, 
			{
				method: 'post', 
				parameters: params2,
				onComplete: function(req) {
					var json = req.responseText.evalJSON();
					$('elProvisions').update(json.provisionsContainer);
					$('elCredits').update(json.elCredits);
					$('elConfirm').update(json.elConfirm);
				},
				onFailure: DL.ajaxFailed.bind(this)
			}
		);
	},
	
	
	
	ajaxFailed: function() {
		alert('An error has occured with an AJAX request - this is not your fault.');
	},
	
	
	
	toggleButtons: function() {
	
		// make sure both buttons exists, hide one and show the other (used on final step of download page)
		if ($('downloadbutton') && $('startedbutton')) {
			$('downloadbutton').hide();
			$('startedbutton').show();
		}
	}, 
	
	toggleFreePDFButton: function() {
		if($('downloadFreePDFMsg') && $('downloadFreePDFLink')) {
			$('downloadFreePDFMsg').hide();
			$('downloadFreePDFLink').show();
		} 
	}
	
}

// run the init script when everything is loaded
Event.observe(window, 'load', function() {
	DL.init();
});

