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

function dismiss_header_notice( url ) {
	Effect.BlindUp('notice', {duration:.5, delay:0});
	
	// Send ajax request setting the dismissal session var
	new Ajax.Request( url ,
	  {
	    method:'get'
	  });
}

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

function confirm_cp_delete(url) {
	if (confirm("Are you sure you want to permanently DELETE this user's Contestant Portfolio?")) {
		window.location = url;
	}
}

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

// User submits the application form
function submit_application() {
	
	var form = $('account_application');
	
	if ( check_application_form( form ) ) {
		
		$('submit_button_id').hide();
		$('loading_message_id').show();
		
		form.submit();
	} 
}

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

// User submits the Edit Account Form
function submit_edit_account() {
	
	var form = $('edit_account');
	
	if ( check_edit_account_form( form ) ) {
		
		$('submit_button_id').hide();
		$('loading_message_id').show();
		
		form.submit();
	} 
}

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

// User submits the Forgot Password form
function submit_forgot_password() {
	var form = $('forgot_password');
	form.submit();
}

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

function goto_application() {
	
	var form = $('audition_application');
	form.submit();
}

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

function init_application( country_id ) {
	update_application_country();
}

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

function update_application_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_edit_account_form(ref) {

	return checkform( ref );
}

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

function check_application_form(ref) {
	
	var country = $('select_country_id');
	var country_val = country.options[ country.selectedIndex ].value;

	// 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_email_confirm_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;
		}
	}
	
	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 check_cp_form(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;}

	// split the required fields
	var reqfields=document.getElementById('required').value.split(',');
	var errorClass = "form_div_error";
	
	// Clean up old mess
	$('cp_form_error_msg').hide();
	for(var i=0;i<reqfields.length;i++)
	{
		var fieldname = reqfields[i];
		if (fieldname.indexOf(":") != -1) {
			fieldname = fieldname.split(":")[1];
		}
		
		var f=document.getElementById("div_" + fieldname );
		
		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 cur = reqfields[i];
		
		if (cur.indexOf(":") != -1) {
			var f=document.getElementById(cur.split(":")[0]);
		} else {
			var f=document.getElementById(cur);
		}
		
		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 && !cp_isEmailAddr(f.value)){cp_add_error(cur, 'Email address is invalid')}
				
				if(f.value=='' && f.id!='email'){cp_add_error(cur)}							
				
										
			break;
			case 'textarea':
				if(f.value==''){cp_add_error(cur)}							
			break;
			
			case 'password':
				if(f.value==''){cp_add_error(cur)}
				
				// password must be four characters
				if(f.value.length < 4){cp_add_error(cur, 'Password must be at least 4 characters')}
				
			break;
			
			case 'checkbox':
				if(!f.checked){cp_add_error(cur)}							
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cp_add_error(cur)}							
			break;
		}// end switch
	}// end for
	
	
	return !errorActivated;
	
	
	function cp_isEmailAddr(str) 
	{
	    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	
	function cp_add_error(o,msg)
	{
		// Check if there is no error message
		if(!errorActivated)
		{
			// create errormessage and insert before submit button
			errorActivated = true;

			flag_form_errors( o, msg, errorClass );

		} 
	}
}

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

function flag_form_errors(o, msg, errorClass) {
	
	var fieldname = o;
	if (fieldname.indexOf(":") != -1) {
		fieldname = fieldname.split(":")[1];
	}
	fieldname = $("div_" + fieldname);
	
	if (!fieldname) return false;
	
	if (fieldname.className.indexOf(errorClass) == -1) fieldname.className = fieldname.className + " " + errorClass;
	
	$('cp_form_error_msg').show();
	Effect.BlindDown('cp_form_error_msg',{duration:.5});

	if (msg != '' && msg != undefined) {
		alert(msg);
	}
}

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

function account_navigate_to( confirm_exit, url ) {
	
	if (confirm_exit) {
		if (confirm("Are you sure you want to exit the Contestant Portfolio process? You will lose any unsaved data.")) {
			window.location = url;
		}
	} else {
		window.location = url;
	}
}

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

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
}

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