// JavaScript Document
	var debugmode = false;
	var formaction = 'data/?a=post';
	var myloadmsg = '<img src="/images/interface/load/mini_blue_load.gif" alt="please wait..." />';
	var sendonce = 0;
	var mysubtimer;
function speedtest(){$.jGrowl('Sorry, the form is taking a long time to process. Please give it just a little longer...',{sticky: true,theme:"warning"});mysubtimer = setTimeout(fail,15000);}
function fail(){$.jGrowl('The form should not take this long - it may have failed. I will give you the buttons again so you can try again',{sticky: true,theme:"error"});$('#f_actions').slideDown();$('#f_actions_notes').slideUp();}
function init(){
	$('form').show();
	/* vars */
	
	document.title = 'The MET Brisbane Supercomps';
	/* gui */
	$('#f_actions').hide();
	listcomps();
	
}
function details(formid){
	$('#j-details').html(myloadmsg).show().load('data/?a=d&i='+formid,function(){
		$cname = $('.t_title h1').html();
		if($cname){
			$('#my_eventname').val($cname);
		}else{
			$.jGrowl('I should have got more info from the server. The form might not submit...', { header:'Possible error',sticky: true,theme:'error'});	
		}
	});
}

function listcomps(){
	// load list page
	
	$('#j-list').html(myloadmsg).show().load('data/?a=list',function(){
		var n = $(".btn_choosecomp").size();
		if (n == 1){
			$.jGrowl('There is only one comp running. Attempting to auto open it for you...');	
			var formid = $('.btn_choosecomp a').attr('rel');
			if(formid){loadform(formid);$('.j_s1').text('Check out the current competition...');}
		}
		if (n > 1){

			$('.sc_row').css({cursor:'pointer'}).click(function(){
				this.blur();
				var formid = $(this).find('.btn_choosecomp a').attr('rel');
				if(formid){loadform(formid);}
				return false;
			});
		}
	});
	
}

function loadform(formid){
	
	$('#j-bonus').html('');
	$('#j-form').html(myloadmsg).show().load('data/form.asp',function(){
		
		$('#f_actions, .btn_loadbonus').slideDown();
		$('#f_actions_notes').slideUp();
		$('#j-list').slideUp().html('');
		details(formid);
		//loadbonus();
	});
}


function loadbonus(){
	$('#j-bonus').html(myloadmsg).show().load('data/form-bonus.asp',function(){$(this).slideDown();});

}

function sendform(){
	var myform = $('#myform');
	if (validated(myform)){
		// do something if the form returns true
		if (sendonce == 0){
			// trap multi send
			sendonce = 1;
			$('#f_actions').slideUp('fast',function(){
				$.jGrowl('Posting your form. Please wait...');	
				$('#f_actions_notes').html(myloadmsg).slideDown();										
			});
			str=$('form').serialize();

			mysubtimer = setTimeout(speedtest,15000);

			$.post("data/post.asp", str, function(data) {
												  
				if (data == "success"){
					$('#f_actions_notes').html('<h2 style="color:#66cc00">Success! Your entry has been sent the server.</h2><p>(The server will also try and send you a confirmation email.)</p>');
					$.jGrowl('The server has processed your entry - good luck!');	
				}else{
				// we have got something back other than success
					if (debugmode){$.jGrowl(data, { header:'DEBUG: Error report',sticky: true,theme:'error'});}
					$('#f_actions').slideDown();sendonce = 0;
					$('#f_actions_notes').html('<p class="err">There was an error reported by the server. The message is: '+data+'</p>');
					if (data == ''){}
				}
				
				clearInterval(mysubtimer);
				
			});
			
			if (debugmode){$.jGrowl(str, { header:'DEBUG: Post',sticky: true });}
			
			
		}else{
			$.jGrowl('The submit button has already been clicked once.');	
		}
		
		
	}
}
	

function validated(form){
		
		var formerror = false; var errormsg = '';
		var tmp = $('#email').val();if (tmp.length < 3){formerror=true;errormsg+='Your email does not look right.<br />';}
		var tmp = $('#mobile').val();if (tmp.length < 3){formerror=true;errormsg+='Your phone is missing numbers.<br />';}
		var tmp = $('#username').val();if (tmp.length < 3){formerror=true;errormsg+='Your name is too small.<br />';}
		
		var formarray='';var formindex = 0;
		$(':input.bonusfield, :input#email').each(function(){
			var fv = $(this).val().toLowerCase();									   
			var f=$(this).val(fv).val();	
			
			if(f){
				if (formarray.indexOf(f) > -1){formerror=true;errormsg+='duplicate bonus email: '+f+'<br />';}
				formarray+=f;apos=f.indexOf("@");dotpos=f.lastIndexOf(".");
				if (apos<1||dotpos-apos<2){formerror=true;errormsg+='invalid email address: '+f+'<br />';}	
			}
			
		});

		if (formerror == true){$.jGrowl(errormsg, {header: 'Error!', sticky: true ,theme:'error'});return false;
		}else{return true;}
	
}

// ---------------------------------------------------------

$(function(){
		init();
		/* function to clear the form values */
		$.fn.clearForm=function(){
			return this.each(function(){var type=this.type,tag=this.tagName.toLowerCase();if(tag=='form')return $(':input',this).clearForm();if(type=='text'||type=='password'||tag=='textarea')this.value='';else if(type=='checkbox'||type=='radio')this.checked=false;else if(tag=='select')this.selectedIndex=-1;});
			init();
		};
		$('form').clearForm();
		Date.format='yyyymmdd';
		
		$('.btn_listcomps').css({cursor:'pointer'}).click(function(){listcomps();$(this).slideUp();});
		$('.btn_loadbonus').css({cursor:'pointer'}).click(function(){$(this).slideUp('fast',function(){loadbonus();});});
		
		$('.btn_cancel').css({cursor:"pointer"}).click(function(){this.blur();$('form').clearForm();sendonce = 0;return false;});
		$('.btn_save').css({cursor:"pointer"}).click(function(){this.blur();sendform();return false;});

});
