/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function submit_registration() {
	
	var form = $('audition_registration');
	
	if ( check_registration_form( form ) ) {
		form.submit();
	} 
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function goto_registration() {
	
	var form = $('audition_registration');
	form.submit();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function init_registration( country_id ) {
	update_registration_country();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function update_registration_country() {
	
	var country = $('select_country_id');
	var country_val = country.options[ country.selectedIndex ].value;
	
	// United States
	var us_state = $('select_us_state_id');
	
	// Canada
	var canada_state = $('select_canada_state_id');
	
	// All other countries
	var regular_state = $('txt_state_id');
	
	// Special values:
	// United States = 223
	// Canada = 38
	
	// Now we define the state field (it's either the US drop down, Canada drop down, or generic textfield)
	switch( country_val ) {
		case '223':
			// United States
			us_state.show();
			canada_state.hide();
			regular_state.hide();
		break;
		
		case '38':
			// Canada
			us_state.hide();
			canada_state.show();
			regular_state.hide();
		break;
		
		default:
			// All other countries
			us_state.hide();
			canada_state.hide();
			regular_state.show();
		break;
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function check_registration_form(ref) {
	
	var country = $('select_country_id');
	var country_val = country.options[ country.selectedIndex ].value;
	
	var audition = $('select_audition_id');
	
	// Special values:
	// United States = 223
	// Canada = 38
	
	// Now we define the state field (it's either the US drop down, Canada drop down, or generic textfield)
	switch( country_val ) {
		case '223':
			// United States
			var state = $('select_us_state_id');
		break;
		
		case '38':
			// Canada
			var state = $('select_canada_state_id');
		break;
		
		default:
			// All other countries
			var state = $('txt_state_id');
		break;
	}
	
	// Make sure the email addresses line up.
	if ( $('txt_email_id').value != $('txt_email2_id').value ) {
		alert('Your email addresses did not match.');
		return false;
	}
	
	// Now check to make sure that user entered value for state
	if (state.type.toLowerCase() == 'text') {
		// Check generic state field for value
		if (state.value == '') {
			alert('You must enter a value for State/Region/Province');
			return false;
		}
		
	} else {
		// Check drop down for value other than the default
		if (state.options[ state.selectedIndex ].value == 'NoneSelected') {
			alert('You must select your State/Region/Province');
			return false;
		}
	}
	
	// Make sure user has selected an audition
	if ( audition ) {
		if ( audition.options[ audition.selectedIndex ].value == 'NoneSelected' ) {
			alert( 'Your must select an audition to register for.' );
			return false;
		}
	}
	
	return checkform( ref );
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function erase_input_default( form, defaultValue ) {
	if (form.value == defaultValue) {
		form.value = '';
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function jumpToDirLocation ( select, url ) {
	
	var id = select.options[ select.selectedIndex ].value;
	
	if (id == 'ALL') {
		var final_url = url;
	} else {
		var final_url = url + 'area/' + id;
	}
	
	window.location = final_url;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function checkform(ref)
{
	var errorActivated = false;
	
	// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}

	// Define error messages and split the required fields
	var errorID='errormsg';
	var errorClass='_error';
	var errorMsg='<strong>Error:</strong><br />Please enter or change the highlighted fields and try again.';
	var reqfields=document.getElementById('required').value.split(',');
	
	// Clean up old mess
	$('form_error_msg').hide();
	for(var i=0;i<reqfields.length;i++)
	{
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		var curClassName = f.className;
		var index = curClassName.indexOf(errorClass);
		
		if (index != -1) {
			f.className = curClassName.substr(0,index);
		}
	}
		
	// loop over required fields
	for(var i=0;i<reqfields.length;i++)
	{
		
		// check if required field is there
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		// test if the required field has an error, 
		// according to its type
		switch(f.type.toLowerCase())
		{
			case 'text':
			
				// email is a special field and needs checking
				if(f.id.indexOf('email') != -1 && !cf_isEmailAddr(f.value)){cf_add_error(f, 'Email address is invalid')}
				
				if(f.value=='' && f.id!='email'){cf_add_error(f)}							
				
										
			break;
			case 'textarea':
				if(f.value==''){cf_add_error(f)}							
			break;
			
			case 'password':
				if(f.value==''){cf_add_error(f)}
				
				// password must be four characters
				if(f.value.length < 4){cf_add_error(f, 'Password must be at least 4 characters')}
				
			break;
			
			case 'checkbox':
				if(!f.checked){cf_add_error(f)}							
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_add_error(f)}							
			break;
		}// end switch
	}// end for
	
	
	return !errorActivated;
	
	
	function cf_isEmailAddr(str) 
	{
	    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	
	function cf_add_error(o,msg)
	{
		if (o.className.indexOf(errorClass) == -1) o.className= o.className + errorClass;
		
		// Check if there is no error message
		if(!errorActivated)
		{
			// create errormessage and insert before submit button
			errorActivated = true;
			Element.update('form_error_msg', errorMsg);
			$('form_error_msg').show();
			
			if (msg != '' && msg != undefined) {
				alert(msg);
			}
				
		} 
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

