/*************************************************************************
* 
*************************************************************************/
function makeADeal() {
	var current_selected_members = document.search_results_form.selected_members.value;

	if(current_selected_members == "") {
		alert("Sorry, you have not selected any members to make a deal with...");
		return false;
	}
	else {top.location.href = "search?action=make_a_deal&members=" + current_selected_members;}
}

/*************************************************************************
* 
*************************************************************************/
function changeResultPage(new_page) {
	var current_orderby          = document.search_results_form.orderby.value;
	var current_selected_members = document.search_results_form.selected_members.value;

	if(current_selected_members.length == "1") {current_selected_members = "";}

	window.location = "search?action=search_result_page&page=" + new_page + "&orderby=" + current_orderby + "&selected_members=" + current_selected_members;
}

/*************************************************************************
* 
*************************************************************************/
function zipOrderBy() {
	for (i=0;i<document.fap.order_by.length;i++){
if (document.fap.order_by[i].value==5){
document.fap.order_by[i].checked=true;
break; //exist for loop, as target acquired.
}
}
}

/*************************************************************************
* 
*************************************************************************/
function advZipOrderBy() {
	for (i=0;i<document.fapadvanced.order_by.length;i++){
if (document.fapadvanced.order_by[i].value==5){
document.fapadvanced.order_by[i].checked=true;
break; //exist for loop, as target acquired.
}
}
}

/*************************************************************************
* 
*************************************************************************/
function changeOrderBy(new_orderby) {
	var current_page             = document.search_results_form.page.value;
	var current_selected_members = document.search_results_form.selected_members.value;

	if(current_selected_members.length == "1") {current_selected_members = "";}

	window.location = "search?action=search_result_page&page=" + current_page + "&orderby=" + new_orderby + "&selected_members=" + current_selected_members;
}

/*************************************************************************
* 
*************************************************************************/
function check_checked() {
	var selected_members = document.search_results_form.selected_members.value;

	for(var element_index = 0 ; element_index < document.fap_compare_criteria.elements.length; element_index++){
		if(document.fap_compare_criteria.elements[element_index].type == "checkbox" && document.fap_compare_criteria.elements[element_index].name != "check_all_checkbox") {
			var next_customer_number       = document.fap_compare_criteria.elements[element_index].value;

			if(selected_members.indexOf(next_customer_number) >= 0) {document.fap_compare_criteria.elements[element_index].checked = true;}
		}
	}
}

/*************************************************************************
* 
*************************************************************************/
function checkAllOnResultPage(the_checkbox) {

	if(the_checkbox.checked) {
		for(var element_index = 0 ; element_index < document.fap_compare_criteria.elements.length; element_index++){
			if(document.fap_compare_criteria.elements[element_index].type == "checkbox" && document.fap_compare_criteria.elements[element_index].name != "check_all_checkbox") {
				document.fap_compare_criteria.elements[element_index].checked = true;
				checkUncheckMember(document.fap_compare_criteria.elements[element_index]);
            }
        }
	}
	else {
		for(var element_index = 0 ; element_index < document.fap_compare_criteria.elements.length; element_index++){
			if(document.fap_compare_criteria.elements[element_index].type == "checkbox" && document.fap_compare_criteria.elements[element_index].name != "check_all_checkbox") {
				document.fap_compare_criteria.elements[element_index].checked = false;
				checkUncheckMember(document.fap_compare_criteria.elements[element_index]);
            }
        }
	}
}

/*************************************************************************
* this function looks to see if a member has been checked or unchecked. If
* checked, then the member is added to the list of members. If unchecked,
* the member is removed from the list.
*************************************************************************/
function checkUncheckMember(the_checkbox) {

	if(the_checkbox.checked) {
		/* get the current member selections...in string mode */
		var current_selected_members_string = document.search_results_form.selected_members.value;

		/* if the string is empty, then this is the 1st selection */
		if(current_selected_members_string == "") {document.search_results_form.selected_members.value = the_checkbox.value;}
		else {
			/* else, we need to add the new selection to the collection */
			var current_selected_members_array  = current_selected_members_string.split(",");

			/* add the new selected member to the collection */
			current_selected_members_array.push(the_checkbox.value);

			/* join and set all the selected members */
			document.search_results_form.selected_members.value = current_selected_members_array.join(",");
		}
	}
	else {
		/* get the current member selections...in string mode AND array mode */
		var current_selected_members_string = document.search_results_form.selected_members.value;
		var current_selected_members_array  = current_selected_members_string.split(",");

		/* create the array that will hold the new selections...without the one that was just de-selected */
		var new_selected_members_array = new Array();

		/* iterate threw all the selected members */
		for(var array_index = 0; array_index < current_selected_members_array.length; array_index++) {
			/* get the next member */
			var next_member = current_selected_members_array[array_index];

			/* if the next member IS NOT the one that was just de-selected, add it back to the collection */
			if(next_member != the_checkbox.value) {new_selected_members_array.push(next_member);}
		}

		document.search_results_form.selected_members.value = new_selected_members_array.join(",");
	}
}

/******************************************************************************
 * Name: compare_window
******************************************************************************/
function compare_window() {
	/* window attributes */
	var w = "800";
	var h = "800";

	/* grab the width and height of the window */
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	/* grab the selected/checked members */
	var selected_members = document.search_results_form.selected_members.value;

	/* create the window */
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,resizable=no,status=no'
	win      = window.open('search?action=compare_members&selected_members=' + selected_members, 'MemberCompare', winprops);

	win.focus();
}

/******************************************************************************
 * Name: email_window
******************************************************************************/
function email_window() {
	var selected_members = document.search_results_form.selected_members.value;
	window.location.href = "search?action=email_members&selected_members=" + selected_members;
}

/******************************************************************************
 * Name: contacts_window
******************************************************************************/
function contacts_window() {
	var selected_members = document.search_results_form.selected_members.value;
	// alert("search?action=addcontacts&selected_members=" + selected_members);
        var myURL = "search?action=addcontacts&selected_members=" + selected_members;
	// alert(myURL);
	window.location.href = myURL;
}

/******************************************************************************
 * Name: map_window
******************************************************************************/
function map_window(memberid) {
	/* window attributes */
	var w = "550";
	var h = "500";

	/* grab the width and height of the window */
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	/* create the window */
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,resizable=no,status=no'
	win      = window.open('search?action=mapit&id=' + memberid, 'MemberMap', winprops);

	win.focus();
}


