// Javascript Functions

function destribution_ready() {
	if ($F('distribution') == 1) {
		var txt = '<p><strong>Are you ready for distribution?</strong></p>';
		txt += '<p>By signing up with Label Worx your record label needs to pass a basic review by our team before we will approve your distribution account. ';
		txt += 'Although this process is simple we do require that a small selection of tracks from your label are available for us preview online.</p>';
		txt += '<p>Your music should be loaded into an audio player on either your Website, MySpace, Soundcloud or Facebook pages. ';
		txt += 'If you do not have music online then please do not complete this form yet.  Please come back when you are ready.</p>';
		$j.prompt(txt);
	}
}

function format_username() {
	var username = $F('my_user');
    username = username.replace(/^\s+|\s+$/g, "").replace(/[_|\s]+/g, "-");
    $('my_user').value = username;
}

function source_fields() {
	if ($F('source') < 4) {
		Element.hide('recommended_store');
		Element.hide('recommended_label');
		Element.hide('recommended_other');
	} else if ($F('source') == 4) {
		Element.hide('recommended_store');
		Element.show('recommended_label');
		Element.hide('recommended_other');
	} else if ($F('source') == 5) {
		Element.hide('recommended_store');
		Element.hide('recommended_label');
		Element.show('recommended_other');
	} else if ($F('source') == 6) {
		Element.show('recommended_store');
		Element.hide('recommended_label');
		Element.hide('recommended_other');
	}
}

function dist_fields(rule) {
	
	if (rule == 1) {
		Element.show('stores_opt_out');
	} else {
		Element.hide('stores_opt_out');
	}
}

function hist_fields(rule) {
	
	if (rule == 0) {
		Element.show('new_1');
		Element.show('new_2');
		Element.show('both_1');
		Element.show('both_2');
		Element.show('both_3');
		Element.show('both_4');
		Element.hide('transfer_1');
		Element.hide('transfer_2');
		Element.hide('dist_1');	
		Element.hide('stores_transferring');		
	} else if (rule >= 1) {
		Element.hide('new_1');
		Element.hide('new_2');
		Element.show('both_1');
		Element.show('both_2');
		Element.show('both_3');
		Element.show('both_4');
		Element.show('transfer_1');
		Element.show('transfer_2');
		Element.show('stores_transferring');				
		
		if (rule >= 2) {
			Element.show('dist_1');	
		} else {
			Element.hide('dist_1');	
		}
	} else {
		Element.hide('new_1');
		Element.hide('new_2');
		Element.hide('both_1');
		Element.hide('both_2');
		Element.hide('both_3');
		Element.hide('both_4');
		Element.hide('transfer_1');
		Element.hide('transfer_2');
		Element.hide('dist_1');	
		Element.hide('stores_transferring');		
	}
}

function send_signup() {
	
	$('recaptcha_response').value = '';
	$('username_response').value = '';
	
	var errors = 0;
	var error_text = '';
	
	if (!$('promo').checked && !$('distribution').checked && !$('mastering').checked && !$('web').checked) {
		$('serv_border').setStyle({ border: '1px solid #FF0000' });  	errors++;
		error_text += '<li>Must select at least 1 service</li>';
	}
	
	if ($F('company') == '') {
		$('company').setStyle({ border: '1px solid #FF0000' });  		errors++;
		error_text += '<li>Company name</li>';
	}
	
	if ($F('address') == '') {
		$('address').setStyle({ border: '1px solid #FF0000' });  		errors++; 
		error_text += '<li>Address</li>';
	}
	
	if ($F('country') == 0) {
		$('country').setStyle({ border: '1px solid #FF0000' });  	    errors++; 
		error_text += '<li>Country</li>';
	}
	
	if ($F('contact_name') == '') {
		$('contact_name').setStyle({ border: '1px solid #FF0000' });    errors++; 
		error_text += '<li>Contact name</li>';
	}

	email_valid = !(!$F('contact_email').match(/^\w+([.\+\-]\w+)*@\w+([.\-]\w+)+$/));
	if ($F('contact_email') == '' || email_valid == false) {
		$('contact_email').setStyle({ border: '1px solid #FF0000' });   errors++; 
		if ($F('contact_email') == '') error_text += '<li>Contact email address</li>';
		else if (email_valid == false) error_text += '<li>Contact email address invalid</li>';
	}

	email_valid = !(!$F('main_email').match(/^\w+([.\+\-]\w+)*@\w+([.\-]\w+)+$/));
	if ($F('main_email') == '' || email_valid == false) {
		$('main_email').setStyle({ border: '1px solid #FF0000' });      errors++;
		if ($F('main_email') == '') error_text += '<li>Main company email</li>';
		else if (email_valid == false) error_text += '<li>Main company email invalid</li>';
	}

	if ($F('telephone') == '' && $F('mobile') == '') {
		$('mobile').setStyle({ border: '1px solid #FF0000' });
		$('telephone').setStyle({ border: '1px solid #FF0000' });  	    errors++;
		error_text += '<li>Telephone or mobile</li>';
	}

	if ($F('website') == '' && $F('myspace') == '') {	
		$('website').setStyle({ border: '1px solid #FF0000' });  		
		$('myspace').setStyle({ border: '1px solid #FF0000' });  		errors++;
		error_text += '<li>You must enter either a website address or MySpace page address</li>';
	}
	
	if ($F('my_user') == '' || $('my_user').value.length < 8) {
		$('my_user').setStyle({ border: '1px solid #FF0000' });  		errors++;
		if ($F('my_user') == '') error_text += '<li>Username</li>';
		else error_text += '<li>Username must be at least 8 characters</li>';
	}
	
	if ($F('my_pass') == '' || $('my_pass').value.length < 8) {
		$('my_pass').setStyle({ border: '1px solid #FF0000' });  		errors++;
		if ($F('my_pass') == '') error_text += '<li>Password</li>';
		else error_text += '<li>Password must be at least 8 characters</li>';
	}
	
	if ($F('my_conf') == '' || $F('my_pass') != $F('my_conf')) {
		$('my_conf').setStyle({ border: '1px solid #FF0000' });  		errors++;
		if ($F('my_conf') == '') error_text += '<li>Confirm Password</li>';
		else error_text += '<li>Passwords do not match</li>';
	}

	if ($F('genre_1') == 0) {
		$('genre_1').setStyle({ border: '1px solid #FF0000' });  		errors++;
		error_text += '<li>Primary genre</li>';
	}

	if ($F('genre_2') == $F('genre_1') && $F('genre_2') != 0) {
		$('genre_2').setStyle({ border: '1px solid #FF0000' });  		errors++;
		error_text += '<li>Primary & secondary genres cannot be the same</li>';		
	} else {
		$('genre_2').setStyle({ border: '1px solid #CCC' });
	}

	if ($F('source') == 0) {
		$('source').setStyle({ border: '1px solid #FF0000' });  		errors++;
		error_text += '<li>Select a source</li>';
	}

	if ($F('source') == 4 && $F('source_label') == '') {
		$('source_label').setStyle({ border: '1px solid #FF0000' });  	errors++;
		error_text += '<li>Recommended by label</li>';
	}
	
	if ($F('source') == 6 && $F('dsp_id') == 0) {
		$('source_other').setStyle({ border: '1px solid #FF0000' });  	errors++;
		error_text += '<li>Recommended by store</li>';
	}	

	if ($F('source') == 5 && $F('source_other') == '') {
		$('source_other').setStyle({ border: '1px solid #FF0000' });  	errors++;
		error_text += '<li>Recommended by other</li>';
	}

	if ($F('distribution') == 1 || $F('promo') == 1) {

		if ($F('year_established') == '') {
			$('year_established').setStyle({ border: '1px solid #FF0000' });  		errors++;
			error_text += '<li>Year Established</li>';
		}			

	}

	if ($F('distribution') == 1) {
		
		if ($RF('label_history') == 0) {
			
			if ($F('label_experience') == '') {
				$('label_experience').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Label running experience</li>';
			}
			
			if ($F('label_output') == '') {
				$('label_output').setStyle({ border: '1px solid #FF0000' });  		errors++;
				error_text += '<li>Label music output</li>';
			}
			
			if ($F('release_schedule') == '') {
				$('release_schedule').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Next 6 months releases</li>';
			}
	
			if ($F('artist_count') == '') {
				$('artist_count').setStyle({ border: '1px solid #FF0000' });  		errors++;
				error_text += '<li>Artists releasing on your label</li>';
			}
	
			if ($F('biggest_artists') == '') {
				$('biggest_artists').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Your biggest artists</li>';
			}
	
		} else if ($RF('label_history') >= 1) {
			
			if ($F('turnover') == '') {
				$('turnover').setStyle({ border: '1px solid #FF0000' });  			errors++;
				error_text += '<li>Last 12 months digital turnover</li>';
			}
	
			if ($F('release_history') == '') {
				$('release_history').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Last 6 months releases</li>';
			}
			
			if ($F('release_schedule') == '') {
				$('release_schedule').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Next 6 months releases</li>';
			}
	
			if ($F('artist_count') == '') {
				$('artist_count').setStyle({ border: '1px solid #FF0000' });  		errors++;
				error_text += '<li>Artists releasing on your label</li>';
			}
	
			if ($F('biggest_artists') == '') {
				$('biggest_artists').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Your biggest artists</li>';
			}
			
			if ($RF('label_history') >= 2) {	
				if ($F('distributor') == '') {
					$('distributor').setStyle({ border: '1px solid #FF0000' });		errors++;
					error_text += '<li>Who was your distributor</li>';			
				}
			}		
	
		} else {
			$('hist_border').setStyle({ border: '1px solid #FF0000' });  			errors++;
			error_text += '<li>Your label has</li>';		
		}
		
		if ($RF('distribution_rule') == 0) {
			
		} else if ($RF('distribution_rule') == 1) {
			
			if ($F('opt_out_stores') == '') {
				$('opt_out_stores').setStyle({ border: '1px solid #FF0000' });  	errors++;
				error_text += '<li>Do not deliver to these stores</li>';			
			}
	
		} else {
			$('dist_border').setStyle({ border: '1px solid #FF0000' });  			errors++;
			error_text += '<li>I would like Label Worx to</li>';
		}	
		
	}

	if ($F('recaptcha_response_field') == '') {
		$('recaptcha_response_field').setStyle({ border: '1px solid #FF0000' });errors++;
		error_text += '<li>Type the two words</li>';
	}	

	if (errors > 0) {
		var txt = '<div id="warning_box">Sign Up Form Not Completed Correctly</div>';
		txt += 'Please ensure you complete all required fields - <br /><br /><ul id="warning_list">' + error_text + '</ul><br />Please try again.';
		$j.prompt(txt);			
	
		Element.show('errors_reported');
		return false;
	} else {
		Element.hide('errors_reported');		
	}
	
	check_username();
	
}

function check_username() {

	/* Username Check */
	var ajax_url = 'ajax/ajax_signup.php?';

	new Ajax.Request(ajax_url, {
			parameters: {mode: 'user_check',
						 username: $F('my_user')
			},
			onFailure: function(transport){
				alert('User Check Failed');
			},
			onSuccess: function(transport){
				var response = transport.responseText || "success";
				
				if (response == 'success') {
					check_recaptcha();
				} else {
					var txt = '<div id="warning_box">Username Not Available</div>';
					txt += 'The username your have chosen is not available.<br /><br />Please select another and try again ';
					$j.prompt(txt);			
					return false;
				}

			}			
		});		

}

function check_recaptcha() {

	/* Recpatcha Check */
	var ajax_url = 'ajax/ajax_recaptcha.php?';

	new Ajax.Request(ajax_url, {
			parameters: {mode: 'recaptcha',
						 recaptcha_challenge_field: $F('recaptcha_challenge_field'),
						 recaptcha_response_field: $F('recaptcha_response_field')
			},
			onFailure: function(transport){
				alert('reCaptcha Failed');
			},
			onSuccess: function(transport){
				var response = transport.responseText || "success";

				if (response == 'success') {
					process_signup();
				} else {
					Recaptcha.reload();
					var txt = '<div id="warning_box">Sign Up Form Not Completed Correctly</div>';
					txt += 'The 2 words were incorrect.  Please try again';
					$j.prompt(txt);			
					return false;
				}
			}			
		});	

}

function process_signup() {
	
	var txt = '<div align="center"><p>Sending Sign Up Request.<p></div>';
	txt += '<div align="center" style="padding-top:10px"><img src="images/ajax-loader.gif" /></div>';
	txt += '<div align="center" style="padding-top:10px; font-size:10px;">Please Wait...</div>';
	
	$j.prompt(txt,{					  
		buttons: {}
	});
	
	/* Process Sign Up */
	var ajax_url = 'ajax/ajax_signup.php?';
	
	new Ajax.Request(ajax_url, {
			parameters: {mode: 'send_signup',
						 company: $F('company'),
						 promo: $F('promo'), 
						 distribution: $F('distribution'), 
						 mastering: $F('mastering'),
						 web: $F('web'),
						 address: $F('address'),
						 country: $F('country'),
						 contact_name: $F('contact_name'),
						 contact_email: $F('contact_email'),
						 main_email: $F('main_email'),
						 telephone: $F('telephone'),
						 mobile: $F('mobile'),
						 website: $F('website'),
						 myspace: $F('myspace'),
						 facebook: $F('facebook'),
						 twitter: $F('twitter'),
						 soundcloud: $F('soundcloud'),
						 youtube: $F('youtube'),
						 label_parent: $F('label_parent'),
						 label_subs: $F('label_subs'),
						 year_established: $F('year_established'),
						 username: $F('my_user'),
						 password: hex_md5($F('my_pass')),
						 source: $F('source'),
						 source_label: $F('source_label'),
						 source_other: $F('source_other'),
						 label_history: $RF('label_history'),
						 label_experience: $F('label_experience'),
						 label_output: $F('label_output'),
						 currency: $F('currency'),
						 turnover: $F('turnover'),
						 release_history: $F('release_history'),
						 release_schedule: $F('release_schedule'),
						 estimated_release_schedule: $F('estimated_release_schedule'),
						 artist_count: $F('artist_count'),
						 biggest_artists: $F('biggest_artists'),
						 distribution_rule: $RF('distribution_rule'),
						 opt_out_stores: $F('opt_out_stores'),
						 other_info: $F('other_info'),
						 distributor: $F('distributor'),
						 genre_1: $F('genre_1'),
						 genre_2: $F('genre_2'),
						 store_transfers: $F('store_transfers'),
						 promo_code: $F('promo_code'),
						 dsp_id: $F('dsp_id')
						 
			},
			onSuccess: function(transport){
				var response = transport.responseText || "success";
				
				$j.prompt.close();				

				if (response != 'success') {
					$j.prompt(response);
				} else {
					var txt = '<div id="yes_box">Sign Up Complete</div>';
					txt += 'Thank you for choosing to sign up with Label Worx.  Your application is now being processed.<br /><br />';
					txt += 'Before your account can be activated we need you to varify that your email account is working.<br /><br />';
					txt += 'We have sent an activation email to your contact email address.  You will need to click the link to confirm your email address is valid.<br /><br />';
					txt += 'Label Worx will then review your application and activate your account your account shortly.';
					$j.prompt(txt);	
					
					Element.hide('my_pass_bar');
					Element.hide('my_pass_text');
					
					$('myform').reset();
					
					Recaptcha.reload();
					
					setup_form();
					
				}
			}			
		});	

}

function $RF(el, radioGroup) {
	if($(el).type && $(el).type.toLowerCase() == 'radio') {
		var radioGroup = $(el).name;
		var el = $(el).form;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	}

	var checked = $(el).getInputs('radio', radioGroup).find(
		function(re) {return re.checked;}
	);
	return (checked) ? $F(checked) : null;
}

function check_user_length() {
	if ($('my_user').value.length >= 8 && $('my_user').value.length < 12) {
		var length_text = '<span style="color:#FF6600">Good</span>';
	} else if($('my_user').value.length >= 12) {
		var length_text = '<span style="color:#33FF00">Excellent</span>';
	} else if($('my_user').value.length == 0) {
		var length_text = '';
	} else {
		var length_text = '<span style="color:#CCC">Too Short</span>';
	}
		
	$('user_length').update(length_text);
	$('user_length').innerHTML;	
}

function setup_form() {
	source_fields();
	check_user_length();
	toggle_distribution();
	hist_fields($RF('label_history'));
	dist_fields($RF('distribution_rule'));
}

function toggle_distribution() {
	if ($F('distribution') == 1) {
		Element.show('distribution_options');
	} else {
		Element.hide('distribution_options');
	}
}

function remove_border(field) {
	$(field).setStyle({ border: '1px solid #F4F4F4' });	
}

function resetred(field, link_field) {
	if (link_field == null) {
	
		if ($F(field) != '') {
			if (field == 'contact_email') {
				email_valid = !(!$F('contact_email').match(/^\w+([.\+\-]\w+)*@\w+([.\-]\w+)+$/));
			} else {
				email_valid = true;
			}
			
			if (email_valid) $(field).setStyle({ border: '1px solid #CCC' });
		}

	} else {
		if ($F(field) != '' || $F(link_field) != '') {
			$(field).setStyle({ border: '1px solid #CCC' });
			$(link_field).setStyle({ border: '1px solid #CCC' });
		}
	}
}