if (typeof(Competition) == 'undefined'){
	var Competition = new SiteFramework();
}

Competition.form = null;
Competition.uploaderID = null;
Competition.uploadQueueID = null;
Competition.inProgress = false;

Competition.validateForm = function (element, uploaderID){
	if (this.inProgress == true) return false;

	var validationUrl;

	if (this.uploadQueueID == null){
		alert('Please select an image to upload');
		return false;
	}

	this.uploaderID = uploaderID;

	element = this.getElement(element);

	element = this.findNode(element, 'A');
	if (element == false) return false;

	validationUrl = element.href;

	this.form = this.findNode(element, 'FORM');
	if (this.form == false) return false;

	this.inProgress = true;

	new Ajax.Request(validationUrl, {
		method     : 'post',
		parameters : $(this.form).serialize(),
		evalJS     : true,
		onFailure  : function (){ alert('Ajax request failed'); Competition.inProgress = false; }
	} );
	return false;
}

Competition.validationErrors = function (errors){
	this.inProgress = false;
	this.debug(errors, 'validationErrors');
	if (typeof(this.form) == 'object'){
		this.formValidationErrors(this.form, errors);
	} else {
		this.redirect();
	}
}

Competition.doUpload = function (data){
	this.formValidationErrors(this.form, {});

	this.debug(data, 'DoUpload.data');

	var scriptData = {};
	for (var key in data.Competition){
//		scriptData['data[Competition]['+key+']'] = data.Competition[key];
		scriptData['data[Competition]['+key+']'] = $j.base64.encode(data.Competition[key].toString());
	}
	this.debug(scriptData, 'DoUpload.Decodedata');

	$j('#'+this.uploaderID).uploadifySettings('scriptData', scriptData);
	$j('#'+this.uploaderID).uploadifyUpload();

}

Competition.onUploadSelectFile = function (event, queueID, fileObj){
	if (this.uploadQueueID == null){
		this.uploadQueueID = queueID;
		return true;
	}
	return false;
}

Competition.onFileUploadComplete = function (event, queueID, fileObj, response, data){
	try {
		eval(response);
	} catch(e){ this.debug(e, 'onFileUploadComplete ERROR') }
	return true;
}

Competition.onAllUploadComplete = function (event, data){
	this.inProgress = false;
	return false;
}

Competition.onAddSuccess = function (){
	new Ajax.Updater('registration_form', SF.baseUrl+'pages/display/competition-thankyou');
	return false;
}

Competition.approveUrl = '';
Competition.winUrl = '';

Competition.initAdminList = function(tableID){
	$j('input:radio', $j('#'+tableID)).bind( 'change', function(event){
		var entryID, status, url;

		entryID = $j(this).attr('rel');
		state = $j(this).val();

		url = Competition.approveUrl+'/'+entryID+'/'+state;

		new Ajax.Request(url, {
			method: 'get',
			evalJS: true,
			onFailure: function (){ alert('Ajax request falure') }
		});

		return false;
	} );
	$j('input:checkbox', $j('#'+tableID)).bind( 'change', function(event){
		var entryID, status, url;

		entryID = $j(this).attr('rel');
		state = $j(this).attr('checked') ? 1 : 0;

		url = Competition.winUrl+'/'+entryID+'/'+state;

		new Ajax.Request(url, {
			method: 'get',
			evalJS: true,
			onFailure: function (){ alert('Ajax request falure') }
		});

		return false;
	} );
}

Competition.setApprove = function (entryID, state){
	if (state == 1){
		$j('input:radio[value="1"][rel="'+entryID+'"]').attr('checked', true);
		$j('input:radio[value="0"][rel="'+entryID+'"]').attr('checked', false);
	} else {
		$j('input:radio[value="1"][rel="'+entryID+'"]').attr('checked', false);
		$j('input:radio[value="0"][rel="'+entryID+'"]').attr('checked', true);
	}
}

Competition.setWin = function (entryID, state){
	if (state == 1){
		this.setApprove(entryID, 1);
		$j('input:radio[rel="'+entryID+'"]').attr('disabled', true);
		$j('input:checkbox[rel="'+entryID+'"]').attr('checked', true);
	} else {
		$j('input:radio[rel="'+entryID+'"]').attr('disabled', false);
		$j('input:checkbox[rel="'+entryID+'"]').attr('checked', false);
	}
}

Competition.disableWin = function (){
	$j('input:checkbox:not(:checked)').attr('disabled', true);
}

Competition.enableWin = function (){
	$j('input:checkbox:disabled').attr('disabled', false);
}
