// JavaScript Document
function submit_advanced(namesearch){
	if(!namesearch){
		if(document.advancedform.location.value.length < 2){
			alert("Please insert a starting location (city, state and/or zip code)");
			document.advancedform.location.focus();
		}else{
			//opentoolbar();
			showloader(); // inside initial_search.js
		}
	}else{
		if(document.advancedform.namesearch.value.length < 2){
			alert("Please insert some text to search for");
			document.advancedform.namesearch.focus();
		}else{
			document.advancedform.searchbyname.value = "yes";
			showloader();
		}
	}
}

function suggestLocation(loc){
	document.advancedform.location.value= loc;
	showloader(); // inside initial_search.js
	//document.advancedform.submit();
	//setTimeout('submit_advanced()',1000);
}

function checkAll(field){
	for (i = 0; i < field.length; i++) field[i].checked = true ;
}
function uncheckAll(field){
	for(i = 0; i < field.length; i++) field[i].checked = false ;
}
function startupChecked(field,where){
	var newloc= "";
	switch(where){
		case "certs":
			len= 15; //text preceding the record to cut out... checkbox_certs_
			newloc= document.advancedform.certifications;
			break;
		case "orgs":
			len= 14; //text preceding the record to cut out... checkbox_orgs_
			newloc= document.advancedform.org_type;
			break;
		default:
			len= 9; //text preceding the record to cut out... checkbox_
			newloc= document.advancedform.resource_type;
			break;
	}
	var newArray= new Array();
	for(i = 0; i < field.length; i++) {
		if(field[i].checked == true){
			var name= field[i].name;
			name= name.substr(len);
			newArray.push(name);
		} 
	}
	newloc.value= newArray.join(",");
}
function in_array(string, array){
	for (i = 0; i < array.length; i++){
		if(array[i] == string){
			return true;
		}
	}
	return false;
}

// var serviceArray= new Array();
// var certArray= new Array();
// var orgtypeArray= new Array();
function check_check(check,where){
	if(!where) var where= "services";
	switch(where){
		case "certs":
			len= 15; //text preceding the record to cut out... checkbox_certs_
			newloc= document.advancedform.certifications;
			break;
		case "orgs":
			len= 14; //text preceding the record to cut out... checkbox_orgs_
			newloc= document.advancedform.org_type;
			break;
		default:
			len= 9; //text preceding the record to cut out... checkbox_
			newloc= document.advancedform.resource_type;
			break;
	}
	var newArray= new Array();
	if(newloc.value != "") newArray= newloc.value.split(',');
	var name= $(check).attr("name");
	name= name.substr(len);
	if($(check).attr("checked")){
		if(!in_array(name,newArray)){
			newArray.push(name);
		}
	}else{
		var newServices= new Array();
		for(var i=0;i < newArray.length; i++){
			var ntext= newArray[i];
			if(name != newArray[i]){
				newServices.push(newArray[i]);
			}else{
				ntext+= " match";
			}
			//alert(ntext);
		}
		newArray= newServices;
	}
	newloc.value= newArray.join(",");
	//alert(document.advancedform.resource_type.value);
}
var open_tool= false;
function opentoolbar(){
	if(open_tool){
		$("#toolbar_position").animate({"left":"-633px"},500,function(){
			//$("#toolbar_close").attr("src","../images/toolbar/arrow_right.gif");
			open_tool= false;
		});
	}else{
		$("#toolbar_position").animate({"left":"0px"},500,function(){
			//$("#toolbar_close").attr("src","../images/toolbar/arrow_left.gif");
			open_tool= true;
		});		
	}
}
function isDigit(c){
	return ((c >= "0") && (c <= "9"))
}
var ziptest= false;
function checkZip(zip){ //e is event object passed from function invocation
	if(zip.value.length >= 5){
		if(zip.value.length > 5){
			zip.value = zip.value.substring(0,5);
		}else{
			evalZipcode(zip.value);
		}
	}else{
		ziptest= false;
	}
}
function evalZipcode(zip){
	var num= true;
	for(var i = 0; i < zip.length; i++){
		var c = zip.charAt(i);
		if(!isDigit(c)) num= false;
	}
	if(num && zip.length == 5){	
		//alert(zip);
		$.get("test_zipcode.php", {zip: zip},function(data){
			if(data == 'false'){
				ziptest= false;
			}else{
				ziptest= true;
			}
		});
	}else{
		ziptest= false;
	}
}
function searchnearby(zip,range){
	document.advancedform.radius_zip.value= zip;
	document.advancedform.radius.value= range;
	evalZipcode(zip);
	setTimeout('submit_advanced()',1000);
}
function gotoPage(page){
	document.repost_form.page.value= page;
	showloader('repost'); // inside initial_search.js
	document.repost_form.submit();
}
function changeSort(what){
	if(!what) var what= "name";
	if(what == "name"){
		if(document.repost_form.sortbyname.value == "desc" || document.repost_form.sortbyname.value == "no"){
			document.repost_form.sortbyname.value= "asc";
		}else{
			document.repost_form.sortbyname.value= "desc";
		}
	}else{
		if(document.repost_form.sortbyname.value != "no"){
			document.repost_form.sortbyname.value= "no";
			document.repost_form.radius_farthest.value= "yes";
		}
		if(document.repost_form.radius_farthest.value == "no"){
			document.repost_form.radius_farthest.value= "yes";
		}else{
			document.repost_form.radius_farthest.value= "no";
		}
	}
	document.repost_form.page.value= 1;
	document.repost_form.submit();
}

$(document).ready(function(){
	startupChecked($("#advancedform_services :checkbox"),'services');
	startupChecked($("#advancedform_certs :checkbox"),'certs');
	startupChecked($("#advancedform_org_types :checkbox"),'orgs');
	uncheckAll($("#advanced_search :checkbox"));
	
	var newHTML= "<div id=\"overlayLoader\">\n";
	newHTML+= "<div id=\"overlayLoader_bg\"></div>\n";
	newHTML+= "<div id=\"overlayLoaderBox\">\n";
	newHTML+= "<h1>&nbsp;&nbsp;We are processing your request.  Thank you for your patience!</h1><br/><img src=\"loader.gif\" />\n";
	newHTML+= "</div>\n";
	newHTML+= "</div>\n";
	$("body").append(newHTML);	
	
	$(document).click(function(){
		if(open_tool) opentoolbar();
	});
	$("#toolbar_position").click(function(event){
		event.stopPropagation();
	});
	$("#toolbar_head").click(function(){
		opentoolbar();
	});
	$("#toolbar_rightbar").click(function(){
		opentoolbar();
	});
	$("#toolbar_close").click(function(){
		opentoolbar();
	});
	$("#toolbar_rightbar,#toolbar_head").mouseover(function(){
		$("#toolbar_close").attr("src","../images/finding_resources/adv_search/mag_glass_over.png");
		/*if(!open_tool){
			$("#toolbar_close").attr("src","../images/toolbar/arrow_right_over.gif");
		}else{
			$("#toolbar_close").attr("src","../images/toolbar/arrow_left_over.gif");
		}*/
	}).mouseout(function(){
		$("#toolbar_close").attr("src","../images/finding_resources/adv_search/mag_glass.png");
		/*if(!open_tool){
			$("#toolbar_close").attr("src","../images/toolbar/arrow_right.gif");
		}else{
			$("#toolbar_close").attr("src","../images/toolbar/arrow_left.gif");
		}*/
	});
});
