window.addEvent('domready', function(){
	if($('regstep2')) $hide('regstep2');
	if($('btnregnext')) $show('btnregnext');
	
	if($('btnregnext')) $('btnregnext').addEvent('click', function(e){ checkRegForm('step1', e); });
	if($('btnregister')) $('btnregister').addEvent('click', function(e){ checkRegForm('step2', e); });
	if($('btnregprev')) $('btnregprev').addEvent('click', function(e){ registerNext(this); });
	
	if($('ddlbrowse')) $('ddlbrowse').addEvent('change', function(e){ document.frmbrowse.submit(); });
});

Number.extend({
		/*
		Property: numberFormat
			Format a number with grouped thousands.
 
		Arguments:
			decimals, optional - integer, number of decimal percision; default, 2
			dec_point, optional - string, decimal point notation; default, '.'
			thousands_sep, optional - string, grouped thousands notation; default, ','
 
		Returns:
			a formatted version of number.
 
		Example:
			>(36432.556).numberFormat()  // returns 36,432.56
			>(36432.556).numberFormat(2, '.', ',')  // returns 36,432.56
		*/
 
		numberFormat : function(decimals, dec_point, thousands_sep) {
			decimals = Math.abs(decimals) + 1 ? decimals : 2;
			dec_point = dec_point || '.';
			thousands_sep = thousands_sep || ',';
 
			var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[2] as decimals
			var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
			return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
					(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
		}
 
 
});
String.extend({
	toQueryStringObj: function(){
		var obj = {};
		$A(this.replace(/(^.*\?)|(#.*$)/g,'').split('&')).each(function(param){
			param = param.split("=");
			obj[decodeURIComponent(param[0])] = decodeURIComponent(param[1]);
		});
		return obj;
	}
});


/* show hide */
function $show(id){ $(id).removeClass('hide'); }
function $hide(id){ $(id).addClass('hide'); }
function $showhide(id){	if(!$(id).hasClass('hide')) $hide(id); else $show(id); }
//selects
function getValue(selectID){ return $(selectID).options[$(selectID).selectedIndex].value; }

//functions
function registerNext(el){
	if(el.value == 'Next'){ $show('regstep2'); $hide('regstep1'); }
	else { $hide('regstep2'); $show('regstep1'); }
}
function expandClick(el){
	var label = el.getText()=='Read More'? 'Collapse' : 'Read More';
	el.setText(label);
}


function checkRegForm(section, event){
	
	var success = true;
	
	//check step1 of form
	$ES('.step1', 'regstep1').each(function(el){
		el.removeClass('error');
		if(el.value.trim()==''){
			el.addClass('error');
			success = false;
		}
	});
	
	//go to next page
	if(success && section=='step1'){
		registerNext($('btnregnext'))
		return;
	}
	if(!success){
		var e = new Event(event).stop();
		alert('Please fill in all fields highlighted in red');
	}	
}