// This var must be set to TRUE. Build_site_validate.js looks for this value to determine how to display errors //
var createASite = true;

function siteFeaturesGet() {
	var siteFeatures = document.getElementById("service");
	return (siteFeatures.options[siteFeatures.selectedIndex].value);
}

function siteTypeGet() {
	var siteType = document.getElementById("site_type");
	var returnValue = siteType.options[siteType.selectedIndex].text;
	if (returnValue == 'Organization')
		return ('Org');
	else
		return (returnValue);			
}

function siteServiceOptionGet() {
	var serviceOption = document.getElementById("service_option");
	return (serviceOption.options[serviceOption.selectedIndex].text);			
}

function adOptionGet() {
	return ($('ad_option').selectedIndex);
}		

function diskSizeGet() {
	return ($('disk_space_qty').selectedIndex);		
}			

function billingMethodGet() {			
	if (document.build_site_form.service_option.disabled)
		return ('annually');
	else
		return $('service_option').value;
}

function yearOrMonth() {
	var currentSelection = siteServiceOptionGet();
	
	if (document.build_site_form.service_option.disabled)
		return ('year');
	else {
		if (billingMethodGet() == 'annually')
			return ('year');
		else
			return ('month');
	}
}

function totalGet() { 
	var currentSiteType = siteTypeGet().toLowerCase();
	var currentSelection = currentSiteType+'_'+billingMethodGet();
	var disk_space_price = ($('disk_space_qty').selectedIndex * prices[3][currentSelection]);
	
	if (($('ad_option').selectedIndex == 0) || ($('ad_option').disabled))
		adOptionPrice = 0;
	else
		adOptionPrice = prices[4][currentSelection];
	
	if ($('service_option').disabled)
		billingOptionPrice = 0;
	else
		billingOptionPrice = prices[2][currentSelection];

	return ((billingOptionPrice + adOptionPrice + disk_space_price).toFixed(2)); 	
}	

function rebuildBilling() {
	var currentSelection = $('service_option').selectedIndex;

	var sType = siteTypeGet().toLowerCase();
	var billingMethod = "";
	
	document.build_site_form.service_option.length = 0;
	
	op = document.createElement("option");
	op.text = 'Monthly - $' + prices[2][sType + '_monthly'] + ' / month';
	op.value = 'monthly';
	document.build_site_form.service_option.options.add(op);
	
	op = document.createElement("option");
	op.text = 'Annually - $' + prices[2][sType + '_annually'] + ' / year';
	op.value = 'annually';
	document.build_site_form.service_option.options.add(op);							
	
	if (siteFeaturesGet() == 'pay') {
		$('btn_submit_basic').hide();					
		$('btn_submit_plus').show();				
		document.build_site_form.service_option.disabled = false;
		document.build_site_form.ad_option.disabled = false;	
	} else {
		$('btn_submit_plus').hide();
		$('btn_submit_basic').show();				
		document.build_site_form.service_option.disabled = true;
		document.build_site_form.ad_option.disabled = true;		
	}
	
	if ($('disk_space_qty').selectedIndex == 0)
		$('disk_space').value = 'free';
	else {
		$('disk_space').value = 'pay';
		$('btn_submit_basic').hide();					
		$('btn_submit_plus').show();
	}				
	if (currentSelection >= 0)
 		$('service_option').selectedIndex = currentSelection;

	// populate values for posting //
	$('plus_billing_option').value = billingMethodGet();
	$('disk_space_option').value = billingMethodGet();
	$('ad_option_option').value = billingMethodGet();
}

function rebuildAdOptions(selected) {
	var currentBillingMethod = billingMethodGet();
	var currentSelected = adOptionGet();
	var sType = siteTypeGet().toLowerCase();
	
	for (i = document.build_site_form.ad_option.options.length - 1; i >= 0; i--) {
		document.build_site_form.ad_option.remove(i);
	}
	
	op = document.createElement("option");
	op.text = 'Display ads on my site - FREE';
	op.value = 'free';
	document.build_site_form.ad_option.options.add(op);
	
	op = document.createElement("option");
	op.text = 'No ads (or let me use my own) ' + prices[4][sType+ '_' + billingMethodGet()] + ' / ' + yearOrMonth();
	op.value = 'pay';
	document.build_site_form.ad_option.options.add(op);
	
	if (currentSelected != -1)
		$('ad_option').selectedIndex = currentSelected;	
}			

function rebuildSpaceOptions() {
	var currentBillingMethod = billingMethodGet();
	var currentSelected = diskSizeGet();
	var sType = siteTypeGet().toLowerCase();
	
	for (i = document.build_site_form.disk_space_qty.options.length - 1; i >= 0; i--) {
		document.build_site_form.disk_space_qty.remove(i);
	}
	
	op = document.createElement("option");
	
	if (document.build_site_form.service_option.disabled)
		op.text = '5 megabytes - FREE'
	else
		op.text = '30 megabytes - FREE'
	op.value = 0;
	document.build_site_form.disk_space_qty.options.add(op);
	
	for (x=1; x!=6; x++) {
		op = document.createElement("option");
		op.text = 'Add ' + (x * 20) + ' megabytes - $' + (x * prices[3][sType + '_' + billingMethodGet()]).toFixed(2) + ' / ' + yearOrMonth(); 
		op.value= x;
		document.build_site_form.disk_space_qty.options.add(op);			
	}		
	if (currentSelected != -1)
		$('disk_space_qty').selectedIndex = currentSelected;	
}		

function updateTotal() {
	var totalCost = new Number(totalGet());
	
	$('total').innerHTML = "Your Total: $" + totalCost.toFixed(2);
}
	
function showHideLeague() {
	var currentSiteType = siteTypeGet();
	
	if (currentSiteType == "League") {
		$('league_admin_header').show();
		$('org_admin_header').hide();
		$('league_administration').show();
		$('primary_contact_text').update('I am the primary contact for this league.');
	}
	else if (currentSiteType == "Org") {
		$('league_admin_header').hide();
		$('org_admin_header').show();
		$('league_administration').show();
		$('primary_contact_text').update('I am the primary contact for this organization.');
	}	
	else
		$('league_administration').hide();	
}

function calculateAll() {
	rebuildBilling();
	rebuildAdOptions();
	rebuildSpaceOptions();
	updateTotal();
}

function create_a_site_init() {
	rebuildBilling();
	calculateAll();
	showHideLeague();
	cityHelp();
	zipHelp();
	stateProvinceHelp();
	chooseSport();
	additonalRegistrationInfo();
	showCoppa();
}

function showCoppa() {
	if ($('coppa').value == 'true') {
		$('parentsPermission').show();
	}
}
function chooseSport() {
	if ($('sport').value != '') {
		var nodeList = $('sport_info_primary_sport').getElementsByTagName('option');
		var nodes = $A(nodeList);
		nodes.each(function(node){
				if (node.innerHTML.toLowerCase() == $('sport').value.toLowerCase()) {
					$('sport_info_primary_sport').value = node.value;	
				}
			});
	}  
	if ($('sport').value == 'llb')
		$('sport_info_primary_sport').value = "138";
}

function cityHelp(w) {
	if (w == 1) {
		if ($('account_city').value == 'City') {
			//$('account_city').style.color = '##000000';
			$('account_city').value = '';
		}
	} else {
		if ($('account_city').value == '') {
			//$('account_city').style.color = '##444444';
			$('account_city').value = 'City';
		}
	}
}

function zipHelp(w) {
	if (w == 1) {
		if ($('account_zip').value == 'Postal Code') {
			//$('account_zip').style.color = '##000000';
			$('account_zip').value = '';
		}
	} else {
		if ($('account_zip').value == '') {
			//$('account_zip').style.color = '##444444';
			$('account_zip').value = 'Postal Code';
		}
	}
}

function stateProvinceHelp(w) {
	if (w == 1) {
		if ($('account_state_text').value == 'State / Province') {
			//$('account_state_text').style.color = '##000000';
			$('account_state_text').value = '';
		}
    } else {
		if (($('account_state_text').value == '') || (($('account_country').value == 100)) && (($('account_state_text').value == ''))) {
			//$('account_state_text').style.color = '##444444';
			$('account_state_text').value = 'State / Province';
		}
	}
}		

function toggleSiteTypeGraphic() {
	if ($('siteTypeGraphicImage').style.display == 'none')
		$('siteTypeGraphicImage').show();
	else
		$('siteTypeGraphicImage').hide();
}
	

/* CLIENT SIDE VALIDATION */
function validate_me(e, m) {
	var returnValue = false;
	
	if ($('site_type').selectedIndex > 0)
		var leagueOrOrg = true;
	else
		var leagueOrOrg = false;
		
	switch (e) {
		case 'account_first_name' : if ($(e).value == '') { 
										$(e + "_error").update("Enter first name."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;
									
		case 'account_last_name' : if ($(e).value == '') { 
										$(e + "_error").update("Enter last name."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;			
									
		case 'account_email_1' 	:  if (m == null) {
										if ($F(e).match(/^([\w\.\-])+\@(([\w\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null) { 
											$(e + "_error").update("Enter valid email address."); 
											$(e + "_error").show();
											returnValue = true;
										} else 
											$(e + "_error").hide();
									} else {
										$(e + "_error").update(m);
										$(e + "_error").show();
										returnValue = true;										
									}
									break;
									
		case 'account_password_1':				
		case 'passwords'	     :  var errorMsg = '';
									if (m != null)
										errorMsg = m;
									else {
										if ($('account_password_1').value.length == 0)
											errorMsg = 'You must enter a password.'
										else if ($('account_password_1').value != $('account_password_2').value)
											errorMsg = 'Your passwords do not match!';
										else if ($('account_password_1').value.length < 6)
											errorMsg = 'Your password must be more than 6 characters.';
									}	
									if (errorMsg != '') {
										$('account_password_error').update(errorMsg);
										$('account_password_error').show();
										returnValue = true;
									} else
										$('account_password_error').hide();
									break;	
									
		case 'account_display_name' : if ($(e).value == '') { 
										$('display_name_container').hide();
										$(e + "_error").update("Please enter a display name."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;	
									
									
		case 'account_country'	: if ($(e).selectedIndex == 0) {
										$(e + "_error").update("Please select a country."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 	
									break;
														
		
		/* START : LEAGUE OR ORG */					
				
		case 'league_info_name'	:  if ((($(e).value == '') || ($(e).value == ' '))&& (leagueOrOrg == true)) {
										$(e + "_error").update("Enter contact name."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;		
									
		case 'league_info_email' 	: if (($F(e).match(/^([\w\.\-])+\@(([\w\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null) && (leagueOrOrg == true)) { 
										$(e + "_error").update("Enter valid email address."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;					
									
									
		case 'league_info_phone'	: if (($F('league_info_phone').match(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/) == null)  && (leagueOrOrg == true)) {
										$(e + "_error").update("Enter a valid phone number."); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 
									break;		
									
		case 'league_info_signups'  : if (($(e).selectedIndex == 0)  && (leagueOrOrg == true)) {
										$(e + "_error").update("When do registrations start?"); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 	
									break;

		case 'league_info_signup_count'  : if (($(e).selectedIndex == 0)  && (leagueOrOrg == true)) {
										$(e + "_error").update("How many individuals register?"); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 	
									break;										
									
		case 'league_info_signup_fee': if (($(e).selectedIndex == 0)  && (leagueOrOrg == true)) {
										$(e + "_error").update("How do you collect fees?"); 
										$(e + "_error").show();
										returnValue = true;
									} else 
										$(e + "_error").hide(); 	
									break;							
									
		case 'league_info_signup_fees_team' :if (($(e).selectedIndex == 0) && (leagueOrOrg == true) && ([2,3].include($('league_info_signup_fee').value))) {
										$(e + '_error').update("Please select an option."); 
										$(e + '_error').show();
										returnValue = true;
									}
									else 
										$(e + '_error').hide(); 
									break;  
		
		case 'league_info_signup_fees_individual' : if (($(e).selectedIndex == 0)  && (leagueOrOrg == true) && ([1,3].include($('league_info_signup_fee').value))) {
										$(e + '_error').update("Please select an option."); 
										$(e + '_error').show();
										returnValue = true;
									}
									else 
										$(e + '_error').hide(); 
									break;  																				
		
		/* END : LEAGUE OR ORG */		
									
		case 'birthday'				: if( ($('account_birthday_day').selectedIndex == 0) || ($('account_birthday_month').selectedIndex == 0) || ($('account_birthday_year').selectedIndex == 0)) {
										$('account_birthday_error').update("Enter a valid date of birth."); 
										$('account_birthday_error').show();
										returnValue = true;
									}
									else 
										$('account_birthday_error').hide(); 
									break; 

		case 'account_gender'		: if($(e).selectedIndex == 0) {
										$(e + '_error').update("Please select your gender."); 
										$(e + '_error').show();
										returnValue = true;
									}
									else 
										$(e + '_error').hide(); 
									break; 
									
		case 'sport_info_primary_sport' : if($(e).selectedIndex == 0) {
										$(e + '_error').update("Please select your sport."); 
										$(e + '_error').show();
										returnValue = true;
									}
									else 
										$(e + '_error').hide(); 
									break; 
		
		case 'sportDemographics'	: errorText = "";
									  count = 0;
									  if($('sport_info_age').selectedIndex == 0) {
										errorText = 'age group';
										count = count + 1;
									  }
									  if($('sport_info_gender').selectedIndex == 0) {
									  	if (count > 0) {
									  		if ($('sport_info_skill').selectedIndex != 0) 
									  			errorText = errorText + ' and ';
									  		else 
									  			errorText = errorText + ', ';
									  	}
									  	count = count + 1;
									  	errorText = errorText + 'gender';
									  }
									  if($('sport_info_skill').selectedIndex == 0) {
									  	if (count > 0)
									  		errorText = errorText + ' and ';
									  	count = count + 1;
									  	errorText = errorText + 'level';
									  }
									  
									  if (errorText.length > 0 ) {
									  	$('sport_demographics_error').update("Please select a valid " + errorText + '.');
									  	$('sport_demographics_error').show();
									  	returnValue = true;
									  } else 
									  	$('sport_demographics_error').hide(); 
									break; 
									
		case 'site_info_name'		: if ($(e).value == '') {
										$('site_name_container').hide();
										$(e + '_error').update("Please enter a team name."); 
										$(e + '_error').show();		
										returnValue = true;									
									} else 
										$(e + '_error').hide();
									break;
									
		case 'site_terms_of_service' : if ($('site_terms_of_service').checked == false) {
										$(e + '_error').update("You must agree to the terms."); 
										$(e + '_error').show();		
										returnValue = true;									
									} else 
										$(e + '_error').hide();
									break;							
		case 'parent_email'			: 	$('parentsPermission').show();
										returnValue = true;
										break;
										
		case 'parent_email_address' :  errorMsg = '';
										if ($('coppa').value == 'true') {
											if ($F('parent_email').match(/^([\w\.\-])+\@(([\w\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == null)
												errorMsg = 'Please enter a valid email address.';
											else if ($('parent_email').value == $('account_email_1').value) 
												errorMsg = 'Please enter your parents email address.';
											if (errorMsg != '') {
												$('parent_email_error').update(errorMsg);
												$('parent_email_error').show();
												returnValue = true;
											} else 
												$('parent_email_error').hide();
										}
										break;
		
		case 'city_state_zip'	    : if ((($('account_state_text').value == '') || ($('account_state_text').value == 'State / Province'))&& ($('account_state_select').value == '') || ($('account_city').value == 'City') || ($('account_zip').value == 'Postal Code') || (($('account_zip').value.length != 5)) && ($('account_country').value == 1)) {
										$(e + '_error').update("Please enter a valid location."); 
										$(e + '_error').show();		
										returnValue = true;									
									} else {
										if ($('account_state_text').value != '')
											$('contact_state').value = $('account_state_text').value;
										else
											$('contact_state').value = $('account_state_select').selectedValue;
											
										$('contact_city').value = 	$('account_city').value;
										$('contact_zip').value = 	$('account_zip').value;
										$(e + '_error').hide();
									}
									break;									
		case 'site_info_address'    :  if (m == null ) {
										 if ($('site_info_address').value == '') {
											$('site_name_container').hide();
											$(e + '_error').update("Please enter a valid site address."); 
											$(e + '_error').show();		
											returnValue = true;									
										 } else 
											$(e + '_error').hide();
										} else {
											$('site_name_container').hide();
											$(e + '_error').update(m);
											$(e + '_error').show();	
											returnValue = true;
										}
										break; 											
		default					: break;
	}					
	toggleShowErrors();
}

function formHasErrors() {
	var count = 0;
	$$('.error').each(function(item) {
		if (item.style.display == '')
			count = count + 1;
	})
	
	$$('span.unavailable').each(function(item) {
		if (item.style.display != 'none')
			count = count + 1;
	})		
	
	if (count > 0)
		return (true);
	else
		return (false);
}

function toggleShowErrors() {
	if (formHasErrors())
		$('errorsOnForm').show();
	else 
		$('errorsOnForm').hide();
}

function formValidates() {
	if ($('new_account').value == 'true') 		
		fieldsToValidate = ['account_first_name', 'account_last_name', 'account_email_1', 'account_country', 'account_display_name', 'passwords', 'league_info_name', 'league_info_email', 'league_info_phone', 'birthday', 'account_gender', 'sport_info_primary_sport', 'sportDemographics', 'site_info_name', 'league_info_signup_count', 'league_info_signup_fee', 'league_info_signups', 'site_terms_of_service', 'city_state_zip', 'league_info_signup_fees_team', 'league_info_signup_fees_individual', 'parent_email_address'];
	else
		fieldsToValidate = ['city_state_zip', 'account_country', 'league_info_name', 'league_info_email', 'league_info_phone', 'sport_info_primary_sport', 'sportDemographics', 'site_info_name', 'league_info_signup_count', 'league_info_signup_fee', 'league_info_signups', 'league_info_signup_fees_team', 'league_info_signup_fees_individual', 'parent_email_address'];
		
	fieldsToValidate.each(function(item) { 
		validate_me(item);	
	})		
	if (formHasErrors())
		return (false);
	else
		return (true);
}

function primaryContactAutofill() {
	if ($('primaryContact').checked) {
		$('league_info_name').value = $('account_first_name').value + ' ' + $('account_last_name').value;
		if ($('league_info_name').value == ' ')
			$('league_info_name').value = '';
		$('league_info_email').value = $('account_email_1').value;
	}
	$('league_info_name_error').hide();
	$('league_info_email_error').hide();
}

function additonalRegistrationInfo() {		
		//$('league_info_signup_count').value = ;
		switch ($('league_info_signup_fee').value) { 
		case '1' : $('league_info_signup_fees_team_group').hide(); $('league_info_signup_fees_individual_group').show(); break;
		case '2' : $('league_info_signup_fees_individual_group').hide(); $('league_info_signup_fees_team_group').show(); break;
		case '3' : $('league_info_signup_fees_individual_group').show(); $('league_info_signup_fees_team_group').show(); break;
	    default : $('league_info_signup_fees_team_group').hide(); $('league_info_signup_fees_individual_group').hide(); break;
	}
}
	