var xmlHttp_search;
var g_type_data;
var change_query;
var last_query_id;

var page_completed=0;
page_completed=0;

function loading_states(){
	$("#div_select_state").html('<select name="select_state" id="select_state" disabled><option>Loading...</option></select>')
}

function states_us_dis(){
	$("#div_select_state").html('<select name="select_state" id="select_state" disabled><option>Refine by State/Province</option></select>')
}

function states_nonus_dis(){
	$("#div_select_state").html('<select name="select_state" id="select_state" disabled><option>Refine by Country</option></select>')
}


function query_object_class(){
	if($('#usorcan1').is(':checked')) this.usorcan=1;
	if($('#usorcan2').is(':checked')) this.usorcan=2;
	this.select_profile=$('#select_profile').val();
	this.select_subcategory=$('#select_subcategory').val();
	this.select_state=$('#select_state').val();
	this.refine_vehicle=$('#refine_vehicle').val();
	this.refine_language=$('#refine_language').val();
	this.refine_rate=$('#refine_rate').val();
	this.refine_specialty=$('#refine_specialty').val();
	this.refine_experience=$('#refine_experience').val();
	this.refine_affiliation=$('#refine_affiliation').val();
	this.refine_location=$('#refine_location').is(':checked');
	this.location_from=$('#location_from').val();
	this.location_within=$('#location_within').val();
}

function f_select_profile(){
	if(page_completed==1){
		f_disable_refine()
		$("#search_forname").val("")
		$('#select_subc').val("")
		if($('#select_profile').val()!="" && $('#select_profile').val()!=undefined && $('#select_profile').val()!="Select category"){
			//$('#profile_icon').html('');
			//set_states();
			$('#submit1').attr('class','browse_input');

			f_enable_refine(false)
			f_enable_refine_location()
			//loading_states()
		}else{
			//$('#profile_icon').html('<img src="images/browse/flecha.gif" />');
			$('#submit1').attr('class','browse_input_dis');
			f_disable_refine()
		}
	    f_change_query();
	}
}


function f_select_subcategory(){
	f_disable_refine()
	if($('#select_subc').val()!=""){
		f_enable_refine(false)
		loading_states()
	}
    f_change_query();
}

function f_select_state(){
    f_change_query();
    f_enable_refine(true);
    if($("#select_state").val()!="" && $("#hidden_usorcan").val()=="1"){
    	$("#div_refine_location").show()
    }else{
    	f_disable_refine_location()
    	$("#div_refine_location").hide()
    }
}

function f_enable_refine_location(){
	$('#location_within').attr('disabled',false);
	$('#location_from').attr('disabled',false);
	$('#location_from').val('Zip Code');

}

function f_disable_refine_location(){

	$('#location_within').val(1);
	$('#location_from').val('');
	$('#location_within').attr('disabled',true);
	$('#location_from').attr('disabled',true);
	$('#location_from').val('Zip Code');
}

function f_disable_refine(){
	$('#refine_vehicle').attr("disabled",true);
	$('#refine_language').attr("disabled",true);
	$('#refine_rate').attr("disabled",true);
	$('#refine_specialty').attr("disabled",true);
	$('#refine_experience').attr("disabled",true);
	$('#refine_affiliation').attr("disabled",true);
	$('#refine_vehicle').val('');
	$('#refine_language').val('');
	$('#refine_rate').val('');
	$('#refine_specialty').val('');
	$('#refine_experience').val('');
	$('#refine_affiliation').val('');
	$('#refine_location').attr("disabled",true);
	$('#refine_location').attr("checked",false);

	$('#div_refine_vehicle').hide();
	$('#div_refine_rate_ext').hide();
	$('#div_refine_experience_ext').hide();
	$('#div_refine_specialty').hide();


	$('#div_refine').hide();
	$('#div_refine_location').hide();
	f_disable_refine_location();
}

function f_enable_refine(is_subcategory){
	var is_subc
	if(is_subcategory) is_subc=1;
	else is_subc=0;

	if(!is_subcategory || $("#select_subc").val()!=""){
		$.ajax({
			url:"get_data_search.php",
			type:"POST",
			dataType:"json",
			data: {action:"get_refine",select_profile:$("#select_profile").val(),select_subcategory:$("#select_subc").val(),is_subc:is_subc,usorcan:$("#hidden_usorcan").val(),select_state:$("#select_state").val()},
			success:function(data){
				var refine_count=0;
				if(!is_subcategory){
					if(data.subcategories==1){
						$('#div_select_subc').html(data.subcategories_html)
						$('#div_select_subc').show()
					}else{
						$('#div_select_subc').hide()
						$('#select_subc').attr("disabled",true)
					}
				}
				if(data.languages==1){
					$('#div_refine_language').html(data.languages_html)
					$('#div_refine_language').show()
					refine_count++;
				}else{
					$('#div_refine_language').hide()
					$('#refine_language').attr("disabled",true)
				}
				if(data.transportation==1){
					$('#refine_vehicle').attr("disabled",false)
					$('#refine_vehicle').val()
					$('#div_refine_vehicle').show()
					refine_count++;
				}else{
					$('#div_refine_vehicle').hide()
					$('#refine_vehicle').attr("disabled",true)
				}
				if(data.affiliations==1){
					$('#refine_affiliation').attr("disabled",false)
					$('#refine_affiliation').val()
					$('#div_refine_affiliations').show()
					refine_count++;
				}else{
					$('#div_refine_affiliations').hide()
					$('#refine_affiliation').attr("disabled",true)
				}
				if(data.specialties==1){
					$('#div_refine_specialty').html(data.specialties_html)
					$('#div_refine_specialty').show()
					refine_count++;
				}else{
					$('#div_refine_specialty').hide()
					$('#refine_specialty').attr("disabled",true)
				}
				if(data.rate==1){
					$('#refine_rate').attr("disabled",false)
					$('#refine_rate').val()
					$('#div_refine_rate_ext').show()
					refine_count++;
				}else{
					$('#div_refine_rate_ext').hide()
					$('#refine_rate').attr("disabled",true)
				}
				if(data.experience==1){
					$('#refine_experience').attr("disabled",false)
					$('#refine_experience').val()
					$('#div_refine_experience_ext').show()
					refine_count++;
				}else{
					$('#div_refine_experience_ext').hide()
					$('#refine_experience').attr("disabled",true)
				}
				if(refine_count==0){
					$('#div_refine').hide();
				}else{
					$('#div_refine').show();
				}
				$("#div_select_state").html(data.state)
				/*if($('#usorcan1').is(':checked')){
					$('#refine_location').attr('disabled',false);
					$('#div_refine_location').show();
				}*/
			}
		})
	}
	if(!is_subcategory) loading_states();

}

function clear_search(){
	f_disable_refine();

	$('#select_profile').val("");
	$('#select_state').val("");

	$('#select_profile').attr('disabled',false);
	$('#select_state').attr('disabled',true);

	$('#profile_icon').html('<img src="images/browse/flecha.gif" />');
	$('#category_icon').html('');
	if($('#usorcan1').is(":checked")){
		$('#div_select_state').html('<select name="select_state" id="select_state" onchange="f_select_state()" disabled><option>Select State/Province</option></select>');
		$('#text_refine_state').html('Refine by state:');
	}else{
		$('#div_select_state').html('<select name="select_state" id="select_state" onchange="f_select_state()" disabled><option>Select Country</option></select>');
		$('#text_refine_state').html('Refine by country:');
	}
	$('#submit1').attr("class",'browse_input_dis');
}

function check_form(){

	if(change_query==1) return false;
	if($('#select_profile').val()=="" || $('#select_profile').val()=="Select Category" || $('#select_profile').val()=="Select category") return false;
	return true;
}

function check_form2(){
	if($('#refine_location').is(':checked')){
		var x=parseInt($('#location_from').val()).toString();
		if(x=='NaN'){
			if($('#location_from').val().toString().length==7){//Canada
				return false;
			}else{
				alert('Please enter a valid Zip Code');
				return true;
			}
		}
		if(($('#location_from').val()=="" ||
			($('#location_from').val()<10000 || $('#location_from').val()>99999)
			)) {
			alert('Please enter a valid Zip Code');
			return true;
		}else{
			return false;
		}
	}
}


function f_change_query(){
    change_query=0;
    if(check_form()) $('#submit1').attr("class",'browse_input');
    else $('#submit1').attr("class",'browse_input_dis');
}

function switch_to_visual_browse(){
	v_query_object=new query_object_class();
	url='switch_browse.php';
	url=url+"?"+Ext.urlEncode({select_profile:v_query_object.select_profile,
							   select_subcategory:v_query_object.select_subcategory,
                               select_state:v_query_object.select_state,
                               usorcan:v_query_object.usorcan,
                               refine_specialty:v_query_object.refine_specialty,
							   query_id:last_query_id,
							   to:'visual_browse'
								})
	document.location=url;
}
