// JavaScript Document
if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
	document.write('<link rel="stylesheet" type="text/css" href="/css/js-hide.css" />');
}

function show(id) {
	document.getElementById(id).style.display = 'block';
}

function show_inline(id) {
	document.getElementById(id).style.display = 'inline';
}

function show_tr(id) {
	document.getElementById(id).style.display = 'block';
}

function hide(id) {
	document.getElementById(id).style.display = 'none';
}

function init_form() {
	clear_all_elements();
	hide_all_elements();
	show_add_image();
	for (i=0; i<document.change_request_form.requested_action.length; i++) {
		document.change_request_form.requested_action[i].checked = false;
	}
	document.change_request_form.requested_action[0].checked = true;
	document.change_request_form.prev_requested_action.value = '';
}

function trim(s) {
  s = s.replace( /^\s+/g, "" );// strip leading
  return s.replace( /\s+$/g, "" );// strip trailing
}

function is_email_valid(address){
	var filter =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(address)) {
		testresults=true;
	} else {
		testresults=false;
	}
	return (testresults);
}

function validate_form() {
	var form = document.change_request_form;
	var requested_action_elements = form.requested_action;
	var requested_action = '';
	var found_error = false;
	var error_msg = '';
		
	for (i=0; i < requested_action_elements.length; i++) {
		if (requested_action_elements[i].checked) {
			requested_action = requested_action_elements[i].value;
		}
	}
		
	// Base check
	if (trim(form.email.value) == '') {
		alert("Please enter an email address.");
		return false;
	}
	
	if (form.email.value != form.email_confirm.value) {
		alert("Email addresses don't match.");
		return false;
	}
	
	if (!is_email_valid(form.email.value)) {
		alert("Please enter a valid email address.");
		return false;
	}
	
	if (trim(form.url.value) == '') {
		alert("Please enter a URL for your site");
		return false;
	}
	
	if (requested_action == 'add image') {

		var image_count = 0;
		var max_image_count = -1;
		for (i=0; i< form.elements.length; i++) {
			if (form.elements[i].name == 'userfile[]') {
				image_count++;
				if (form.elements[i].value != '') {
					max_image_count=image_count;
				}
			}
		}
		
		if (max_image_count < 0) {
			alert("Please include an image");
			return false;
		}

	} else if (requested_action == 'add or change site listing') {
		
		if (trim(form.site_name.value) == '') {
			alert("Please enter a name for your site.");
			return false;
		}

		var site_type_1, site_type_2, region;
		for (i=0; i<form.site_type_select_1.options.length; i++) {
			if (form.site_type_select_1.options[i].selected) {
				site_type_1 = form.site_type_select_1.options[i].value;
			}
		}
		for (i=0; i<form.site_type_select_2.options.length; i++) {
			if (form.site_type_select_2.options[i].selected) {
				site_type_2 = form.site_type_select_2.options[i].value;
			}
		}
		for (i=0; i<form.region.options.length; i++) {
			if (form.region.options[i].selected) {
				region = form.region.options[i].value;
			}
		}
		
		if (site_type_1 == '') {
			alert("Please select a site type.");
			return false;
		}
		
		if (((site_type_1 == 'Independent Escort') || (site_type_1 == 'Independent BDSM Provider')) && (site_type_2 == '')) {
			alert("Please select a provider type.");
			return false;
		}
		
		if (region == '') {
			alert("Please select a region.");
			return false;
		}
		
		if (form.description.value == '') {
			alert("Please enter a description for your site.");
			return false;
		}
		
	} else if (requested_action == 'remove site listing') {

		if (form.comments.value == '') {
			alert("Please let us know why you want to remove your site's listing.");
			return false;
		}

	}	
}

function check_site_type_select_2() {
	var display_site_type_select_2 = false;
	
	for (i=0; i<document.change_request_form.site_type_select_1.options.length; i++) {
		if (document.change_request_form.site_type_select_1.options[i].selected) {
			if ((document.change_request_form.site_type_select_1.options[i].value == 'Independent Escort') || (document.change_request_form.site_type_select_1.options[i].value == 'Independent BDSM Provider')) {
				display_site_type_select_2 = true;
			}
		}
	}
	
	if (display_site_type_select_2) {
		show('form_site_type_2');
		select_first_option(document.change_request_form.site_type_select_2.options);
	} else {
		hide('form_site_type_2');
		select_first_option(document.change_request_form.site_type_select_2.options);
	}
}

function change_state() {
	var requested_action_elements = document.change_request_form.requested_action;
	var requested_action = '';
	var prev_requested_action = document.change_request_form.prev_requested_action.value;
		
	for (i=0; i < requested_action_elements.length; i++) {
		if (requested_action_elements[i].checked) {
			requested_action = requested_action_elements[i].value;
		}
	}
	
	if (requested_action == prev_requested_action) {
		return;
	}
	
	clear_all_elements();
	hide_all_elements();
		
	if (requested_action == 'add image') {
		show_add_image();
	} else if (requested_action == 'add or change site listing') {
		show_add_or_change_site_listing();
	} else if (requested_action == 'remove site listing') {
		show_remove_site_listing();
	}
		

	document.change_request_form.prev_requested_action.value = requested_action;
}

function hide_all_elements() {
	hide('form_site_name');
	hide('form_site_type');
	hide('form_site_type_2');
	hide('form_region');
	hide('form_site_description');
	hide('form_comments');
	hide('form_images');

	hide('form_image_file_1');
	hide('form_image_file_2');
	hide('form_image_file_3');
	hide('form_image_file_4');
	hide('form_image_file_5');
	hide('form_image_file_6');
	hide('form_image_file_7');
	hide('form_image_file_8');
	hide('form_image_file_9');
	hide('form_image_file_10');
	hide('form_image_file_11');
	hide('form_image_file_12');
	hide('form_image_file_13');
	hide('form_image_file_14');
	hide('form_image_file_15');
	hide('form_image_file_16');
	hide('form_image_file_17');
	hide('form_image_file_18');
	hide('form_image_file_19');
	hide('form_image_file_20');

	hide('form_add_image_1');
	hide('form_add_image_2');
	hide('form_add_image_3');
	hide('form_add_image_4');
	hide('form_add_image_5');
	hide('form_add_image_6');
	hide('form_add_image_7');
	hide('form_add_image_8');
	hide('form_add_image_9');
	hide('form_add_image_10');
	hide('form_add_image_11');
	hide('form_add_image_12');
	hide('form_add_image_13');
	hide('form_add_image_14');
	hide('form_add_image_15');
	hide('form_add_image_16');
	hide('form_add_image_17');
	hide('form_add_image_18');
	hide('form_add_image_19');
	
	hide('instruction_url');
}

function clear_all_elements() {
	document.change_request_form.site_name.value = '';
	document.change_request_form.description.value = '';
	document.change_request_form.comments.value = '';

	select_first_option(document.change_request_form.site_type_select_1.options);
	select_first_option(document.change_request_form.site_type_select_2.options);
	select_first_option(document.change_request_form.region.options);	
}

function select_first_option(opt) {
	for (i=0; i<opt.length; i++) {
		opt[i].selected = false;
	}
	opt[0].selected = true;	
}

function show_add_image() {
	show_images();
	show('instruction_url');
}

function show_add_or_change_site_listing() {
	show('form_site_name');
	show('form_site_type');
	show('form_region');
	show('form_site_description');

	show_images();
}

function show_remove_site_listing() {
	show_tr('form_comments');
}

function show_images() {
	show('form_images');
	
	var image_count = 0;
	var max_image_count = 0;
	for (i=0; i< document.change_request_form.elements.length; i++) {
		if (document.change_request_form.elements[i].name == 'userfile[]') {
			image_count++;
			if (document.change_request_form.elements[i].value != '') {
				alert(document.change_request_form.elements[i].value);
				max_image_count=image_count;
			}
		}
	}
	
	if (max_image_count == 0) {
		show('form_image_file_1');
		show_inline('form_add_image_1');
	} else {
		for (i=1; i<=max_image_count; i++) {
			show('form_image_file_' + i);
		}
	
		if (max_image_count < image_count) {
			show_inline('form_add_image_' + max_image_count);	
		}
	}
}

function add_image_file_row(current_row) {
	next_row = current_row + 1;
	hide('form_add_image_'+current_row);
	show('form_image_file_'+next_row); 
	show_inline('form_add_image_'+next_row); 
}
