// JavaScript Document
var error_total = 0;

function check_input(check_who){
	var form_item = check_who;
	
	if(form_item.value == ''){
		//form_item.value='This is a required field.';
		add_error(form_item);	
	}
	else if(form_item.value != ''){
		subtract_error(form_item);
	}
	
	calculate_errors();
	
}

function calculate_errors(){
	if(error_total > 0){
		//document.getElementById('commit').disabled = true;
		document.getElementById('error_div').style.display = 'block';
		if(error_total == 1){
			document.getElementById('case').innerHTML = '';
			document.getElementById('total_errors').innerHTML = '';			
		}
		else{
			document.getElementById('case').innerHTML = 's';
			document.getElementById('total_errors').innerHTML = error_total;
		}
	}
	else if(error_total == 0){
		document.getElementById('error_div').style.display = 'none';
		//document.getElementById('commit').disabled = false;	
	}	
}

function add_error(add_who){
	var form_item = add_who;
	if(form_item.className == ''){
		form_item.className='form_error';
		error_total++;
	}	
}

function subtract_error(subtract_who){
	var form_item = subtract_who;	
	if(form_item.className == 'form_error'){
		form_item.className='';
		if(error_total>0){
			error_total--;
		}
	}
}

function clear_input(clear_who){
	var form_input = clear_who;
	document.getElementById(form_input).value='';
	if(document.getElementById(form_input).className != ''){
		subtract_error(document.getElementById(form_input));
	}
}

function reset_select(reset_who){
	var form_input = reset_who;
	document.getElementById(form_input).selectedIndex = 0;
	if(document.getElementById(form_input).className != ''){
		subtract_error(document.getElementById(form_input));
	}
}

function hide_div(hide_who){
	var form_input = hide_who;
	if(document.getElementById(form_input).style.display=='block'){
		document.getElementById(form_input).style.display='none';
	}
}

function check_select(check_who){
	var form_input = check_who;
	if(document.getElementById(form_input).selectedIndex == 0){		
		add_error(document.getElementById(form_input));
		calculate_errors();
	}
	else{
		subtract_error(document.getElementById(form_input));
		calculate_errors();
	}	

}

function check_host(){	
	reset_select('advertiser_type');
	clear_input('other_advertiser_type');
	hide_div('other_advertiser_div');
	reset_select('reservations');
	clear_input('other_reservations');
	hide_div('other_reservations_div');

	if(document.getElementById('options_div').className == 'options_error'){
		document.getElementById('options_div').className='';
		if(error_total>0){
			error_total--;				
		}
	}

	calculate_errors();
}

function check_advertiser(){
//	reset_select('company_type');
//	clear_input('other_company_type');
//	hide_div('other_company_div');

	if(document.getElementById('options_div').className == 'options_error'){
		document.getElementById('options_div').className='';
		if(error_total>0){
			error_total--;				
		}
	}
	
	calculate_errors();
}

function check_form(){
	valid_form = true;
	
	check_input(document.getElementById('company'));
	check_input(document.getElementById('contact'));
	check_input(document.getElementById('website'));
	check_input(document.getElementById('phone'));
	check_input(document.getElementById('email'));
	
	check_select('location');
	if(document.getElementById('location').selectedIndex == 10){
		check_input(document.getElementById('other_location'));
	}	
	
	if(document.getElementById('select_type_host').checked || document.getElementById('select_type_advertiser').checked){
		
		if(document.getElementById('select_type_host').checked){
			reset_select('advertiser_type');
			clear_input('other_advertiser_type');
			hide_div('other_advertiser_div');
			reset_select('reservations');
			clear_input('other_reservations');
			hide_div('other_reservations_div');
			
			check_select('company_type');
			if(document.getElementById('company_type').selectedIndex == 7){
				check_input(document.getElementById('other_company_type'));
			}
		}
		
		if(document.getElementById('select_type_advertiser').checked){
			reset_select('company_type');
			clear_input('other_company_type');
			hide_div('other_company_div');
			
			check_select('advertiser_type');
			
			if(document.getElementById('advertiser_type').selectedIndex == 1){
				check_select('reservations');
				
				if(document.getElementById('reservations').selectedIndex == 23){
					check_input(document.getElementById('other_reservations'));
				}	
				
			}
			if(document.getElementById('advertiser_type').selectedIndex == 6){
				check_input(document.getElementById('other_advertiser_type'));
			}			
			
		}
		
		if(document.getElementById('options_div').className == 'options_error'){
			document.getElementById('options_div').className='';
			if(error_total>0){
				error_total--;				
			}
		}
		
		calculate_errors();

	}
	else{
		if(document.getElementById('options_div').className == ''){
			document.getElementById('options_div').className='options_error';
			error_total++;
		}
		calculate_errors();
	}
	
	if(error_total == 0){
		return true;	
	}
	else{
		return false;			
	}
}

function check_host_form(){
	valid_form = true;
	
	check_input(document.getElementById('company'));
	check_input(document.getElementById('contact'));
	check_input(document.getElementById('website'));
	check_input(document.getElementById('phone'));
	check_input(document.getElementById('email'));
	
	check_select('location');
	if(document.getElementById('location').selectedIndex == 10){
		check_input(document.getElementById('other_location'));
	}
			
	check_select('company_type');
	if(document.getElementById('company_type').selectedIndex == 7){
		check_input(document.getElementById('other_company_type'));
	}
	
//	reset_select('company_type');
//	clear_input('other_company_type');
//	hide_div('other_company_div');
		
		calculate_errors();
	
	if(error_total == 0){
		return true;	
	}
	else{
		return false;			
	}
}

function check_advertiser_form(){
	valid_form = true;
	
	check_input(document.getElementById('company'));
	check_input(document.getElementById('contact'));
	check_input(document.getElementById('website'));
	check_input(document.getElementById('phone'));
	check_input(document.getElementById('email'));
	
	check_select('location');
	if(document.getElementById('location').selectedIndex == 10){
		check_input(document.getElementById('other_location'));
	}	
	
//			reset_select('advertiser_type');
//			clear_input('other_advertiser_type');
//			hide_div('other_advertiser_div');
//			reset_select('reservations');
//			clear_input('other_reservations');
//			hide_div('other_reservations_div');
			
			check_select('advertiser_type');
			
			if(document.getElementById('advertiser_type').selectedIndex == 1){
				check_select('reservations');
				
				if(document.getElementById('reservations').selectedIndex == 23){
					check_input(document.getElementById('other_reservations'));
				}	
				
			}
			if(document.getElementById('advertiser_type').selectedIndex == 6){
				check_input(document.getElementById('other_advertiser_type'));
			}			
		
		calculate_errors();

	
	if(error_total == 0){
		return true;	
	}
	else{
		return false;			
	}
}

