
window.addEvent('domready', function() {
	if($defined($('mainform'))){
		submitmainform();
	}
	if($defined($('loginform'))){
		submitloginform();
	}
	if($defined($('tabBoxExample'))){
		viewTabs();
	}
	if($defined($('leaguesearchform'))){
		leaguesearchform();
	} 
	if($defined($('leaguepasswordform'))){
	
		leaguepasswordform();
	} 
	if($defined($('autocompleter'))){
		
	autoCompleter('searchteamname');
	}
	if($defined($('AccordionMulti'))){
		memberScores();
	}

})

function tst()
{
alert('aa');	
}

function submitloginform()
{
	$('loginform').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
	
		//Empty the log and show the spinning indicator.
		var log = $('errorslogin').empty().addClass('ajax-loading');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
			
		this.set('send', {onComplete: function(response) { 
			log.removeClass('ajax-loading');
			var myObject = JSON.decode(response);
			if(myObject.success == 1)
			{
			window.location = myObject.redirecturl;
			}
			var myvar = "";
			for(i=0; i<myObject.message.length; i++)
			{
				myvar = myvar + myObject.message[i] + "<br />";
			}
			log.set('html', myvar);

		}});
		//Send the form.
		this.send();
	});
}
// end of the mainform

function leaguesearchform()
{
	$('leaguesearchform').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
	
		//Empty the log and show the spinning indicator.
		var log = $('errors').empty();
		log.removeClass('error');	
		log.removeClass('success');
		log.addClass('inactive');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
			
		this.set('send', {onComplete: function(response) { 
			
				
			var myObject = JSON.decode(response);
			
			
			var myvar = "";
		
			if(myObject.message){
			for(i=0; i<myObject.message.length; i++)
			{
				
				myvar = myvar + myObject.message[i] + "<br />";
			}
			log.addClass('active');
			
			}
			log.addClass('error');
			log.set('html', myvar);
		
			$('foundleagues').set('html', myObject.foundhtml);

		}});
		//Send the form.
		this.send();
	});
}

function leaguepasswordform()
{
	$('leaguepasswordform').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
	
		//Empty the log and show the spinning indicator.
		var log = $('errorspass').empty().addClass('ajax-loading');
		log.addClass('inactive');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
			
		this.set('send', {onComplete: function(response) { 
			log.removeClass('ajax-loading');
			var myObject = JSON.decode(response);
			
			if(myObject.success == 1)
			{
			window.location = myObject.redirecturl;
			}
			var myvar = "";
			if(myObject.message){
			for(i=0; i<myObject.message.length; i++)
			{
				
				myvar = myvar + myObject.message[i] + "<br />";
			}
			log.addClass('active');
			log.set('html', myvar);
			}
		

		}});
		//Send the form.
		this.send();
	});
}




function joinleaguemodal(leagueurl)
{
new StickyWin.Modal.Ajax({
	url: leagueurl,

		  modalOptions: {
	    modalStyle:{
	      'background-color':'#d6e1b9',
	      'opacity':.6
	    }
	  }
	}).update();
}

function submitmainform()
{
	$('mainform').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
	
		//Empty the log and show the spinning indicator.
		var log = $('errors').empty(); //.addClass('ajax-loading');
		log.removeClass('error');	
		log.removeClass('success');
		log.addClass('inactive');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
			
		this.set('send', {onComplete: function(response) { 
	
			var myObject = JSON.decode(response);

			if(myObject.success == 1 && myObject.redirecturl)
			{
				log.addClass('success');
			if(myObject.alert){
				alert(myObject.alert)
			}
			window.location = myObject.redirecturl;
			}
			else if(myObject.success == 1)
			{
				log.addClass('success');
			}
			var myvar = "";
			for(i=0; i<myObject.message.length; i++)
			{
				myvar = myvar + myObject.message[i] + "<br />";
			}
			log.addClass('error');
			log.set('html', myvar);

		}});
		//Send the form.
		this.send();
	});
}
// end of the mainform
function viewTabs()
{
	new TabSwapper({
		  selectedClass: 'on',
		  deselectedClass: 'off',
		  tabs: $$('#tabBoxExample li.off'),
		  clickers: $$('#tabBoxExample li a'),
		  sections: $$('div.panelSet div.panel'),
		 
		  /*use transitions to fade across*/
		  smooth: true,
			smoothSize: true
		});
}

function helpmodal(help_url)
{
new StickyWin.Modal.Ajax({
	url: help_url,

		  modalOptions: {
	    modalStyle:{
	      'background-color':'#d6e1b9',
	      'opacity':.6
	    }
	  }
	}).update();
}

// TODO should combine these two..
function showimage(baseurl){
	
	var newlogo = baseurl + 'media/img/logo/' + $('member_logo').get('value');
	$('show_member_logo').set('src', newlogo);
	
	}

function showimage_sub(baseurl){
	
	var newlogo = baseurl + 'media/img/logo/' + $('sub_logo').get('value');
	$('show_sub_logo').set('src', newlogo);
	
	}

function showitem(baseurl, item, target){
	
	
	var tmp = $(item).get('value');
	var tmp = tmp.split("|");
	var newlogo = baseurl + 'media/img/team/' + tmp[1];
	$(target).set('src', newlogo);
	
	}

function MoneyFormat(Money)  
{  

var bank = Money+"";
var bankoutput=""  
var nb = 0  
var pl = 0  
var fr = ""  
var at = 0  

negative = bank.substr(0,1)  
 if(negative == "-"){ls = bank.length-1; bank=bank.substr(1,ls);}  
  
for (index=bank.length-3;index>0;index=index-3)  
{  
       
value=bank.substr(index,3)  
  
bankoutput=","+value+bankoutput  
nb++  
}  
pl=nb*3  
at=bank.length-pl  
fr=bank.substr(0,at)  
bankoutput=fr+bankoutput  
  
if(negative == "-"){bankoutput = "-" + bankoutput} else  
{bankoutput = bankoutput}  
return bankoutput  
   
}
function autoCompleter(url)
{
	var inputWord = $('autocompleter');
	

new Autocompleter.Ajax.Json(inputWord, baseurl+url,  {});
}

function memberScores(){
	

	new MultipleOpenAccordion($('AccordionMulti'), {
		elements: $$('#AccordionMulti dd.stretcher'),
		togglers: $$('#AccordionMulti dt.stretchtoggle'),
		openAll: false,
		firstElementsOpen: []
	});
}

function getpointDetails(urlreq, destination){
	

	
	var req = new Request.HTML({url: urlreq , 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('destination').set('text', '');
			//Inject the new DOM elements into the results div.
			$('destination').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('destination').set('text', 'The request failed.');
		}
	});
	
	req.send();

	alert(dt.stretchtoggle);
	alert(section);
}