var formblock;
var forminputs;
var view_was_clicked;
var click_count = 0;

function prepare() {
	formblock= document.getElementById('ad_list_form');
	forminputs = formblock.getElementsByTagName('input');
}

function select_all(name, value) {
	prepare();
	for (i = 0; i < forminputs.length; i++) {
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) {
			if (value == '1') {
				forminputs[i].checked = true;
			} else {
				forminputs[i].checked = false;
			}
		}
	}
}

function toggle_search_results_photos(display_photos) {
	// get the original URL
	var url = document.URL;
 	var url_split = document.URL.split("?");
 	var url_main = url_split[0];
	
	var param_list = url_split[1];
	if(url_split[1] != undefined){
		var params = param_list.split("&");
		var num_params = params.length;
		var x = 0;
		var new_url = "";
		var new_param = "";
		var pop_at = 0;
		var found = 0;
		
		for(x = 0; x < num_params; x++){
			if(params[x].match('display_photos')){
				if(x == 0){
					new_param = '?display_photos=' + display_photos;
					var new_url = url_main+new_param;
					found = 1;
				}else {
					params[x] = "display_photos="+display_photos;
					new_url = params.join("&");
					new_url = "?" + new_url;
					found = 1;
				}
			}
		}
		
		if(found == 0){
			if(num_params < 1){
				new_url = new_url+"?display_photos="+display_photos;
			} else {
				new_url = url_main+"?"+params.join("&")+"&display_photos="+display_photos;
			}
		}
	} else {
		var new_url = url_main+"?display_photos="+display_photos;
	}
	window.location=new_url;
}

function selectRow(row, ad_id) {
	/*
	if(!view_was_clicked) {
		if(row.className == 'selected') {
			row.className = 'highlight';
			$('check_'+ad_id).checked = false;
		} else {
			row.className = 'selected';
			$('check_'+ad_id).checked = true;
		}
	}
	//click_count++;
	view_was_clicked = false;
	*/
}

function onMouse( row) {
	if(row.className != 'selected') {
		if(row.className == 'highlight') {
			row.className = '';
		} else {
			row.className='highlight';
		}
	}
}

function toggleHighlight(row_name) {
	view_was_clicked = true;
}

function confirmOnlineAds(chkbox_name, status_str) {
	// if a checkbox is checked
	var x = document.getElementsByName(chkbox_name);
	var total = 0;
	for(var idx = 0; idx < x.length; idx++){
		if(x[idx].checked == true){
			total += 1;
		}
	}
	
	if(total > 0){
		var confirm_val = window.confirm("You are going to take some ads " + status_str + ". Please confirm.");
		if(confirm_val != true)
			return false;
		else return true;
	} else {
		window.alert("Please select some ads you would like to take " + status_str);
		return false;
	}
}

function submitDistanceForm(session, calc_distances) {
	if(session != true){
		window.location = "/user/login/redirectPage/search";
	}else {
		// get the original URL
		var url = document.URL;
	 	var url_split = document.URL.split("?");

	 	var url_main = url_split[0];
		
		var param_list = url_split[1];

		if(url_split[1] != undefined){
			var params = param_list.split("&");
			var num_params = params.length;
			var x = 0;
			var new_url = "";
			var new_param = "";
			var pop_at = 0;
			var found = 0;
			
			for(x = 0; x < num_params; x++){
				if(params[x].match('calc_distances')){
					if(x == 0){
						new_param = '?calc_distances=' + calc_distances;
						var new_url = url_main+new_param;
						found = 1;
					}else {
						params[x] = "calc_distances="+calc_distances;
						new_url = params.join("&");
						new_url = "?" + new_url;
						found = 1;
					}
				}
			}
			
			if(found == 0){
				if(num_params < 1){
					new_url = new_url+"?calc_distances="+calc_distances;
				} else {
					new_url = url_main+"?"+params.join("&")+"&calc_distances="+calc_distances;
				}
			}
		} else {
			var new_url = url_main+"?calc_distances="+calc_distances;
		}
		window.location=new_url;
	}
}

function checkCompare(click_count) {
	var objCheckBoxes = document.forms['ad_list_form'].elements['ads[]'];
	var click_count = 0;

	if(objCheckBoxes) {
		var countCheckBoxes = objCheckBoxes.length;
		//if there is only one check box, countCheckBoxes will be undefined
		//and the for loop will not run, so we will see if that one
		//check box is checked manually
		if(countCheckBoxes) {
			for(var i = 0; i < countCheckBoxes; i++) {
				if (objCheckBoxes[i].checked) {
					click_count++;
				}
			}
		} else {
			if (objCheckBoxes.checked) {
				click_count++;
			}
		}
	}
	
    if(click_count > 3) {
        return confirm('Only three boats can be compared at one time. Only the first three will be shown. Do you want to continue?');
    }
    if(click_count < 1) {
        alert('One or more boats must be chosen for comparison.');
        return false;
    }
}

function confirmDelete() {
	var objCheckBoxes = document.forms['ad_list_form'].elements['ads[]'];
	var click_count = 0;

	if(objCheckBoxes) {
		var countCheckBoxes = objCheckBoxes.length;
		//if there is only one check box, countCheckBoxes will be undefined
		//and the for loop will not run, so we will see if that one
		//check box is checked manually
		if(countCheckBoxes) {
			for(var i = 0; i < countCheckBoxes; i++) {
				if (objCheckBoxes[i].checked) {
					click_count++;
				}
			}
		} else {
			if (objCheckBoxes.checked) {
				click_count++;
			}
		}
	}
	
    if(click_count > 0) {
        return confirm('Are you sure you want to delete the selected saved ads?');
    } else {
		alert('No saved ads were selected to delete. Please select one or more ads below and try again.');
		return false;
	}
}

function checkMarkAsSold() {
	var objCheckBoxes = document.forms['ad_list_form'].elements['ads[]'];
	var click_count = 0;

	if(objCheckBoxes) {
		var countCheckBoxes = objCheckBoxes.length;
		//if there is only one check box, countCheckBoxes will be undefined
		//and the for loop will not run, so we will see if that one
		//check box is checked manually
		if(countCheckBoxes) {
			for(var i = 0; i < countCheckBoxes; i++) {
				if (objCheckBoxes[i].checked) {
					click_count++;
				}
			}
		} else {
			if (objCheckBoxes.checked) {
				click_count++;
			}
		}
	}
	
    if(click_count < 1) {
		alert('No ads were selected to mark as sold. Please select one or more ads below and try again.');
		return false;
	}
	
	return true;
}
