function form_MainSearch(auto_id) {
	this.DOMConstruct('form_MainSearch', auto_id);
}
form_MainSearch.prototype = new SK_FormHandler({
	name: 'main_search',
	
	fields: {
		search_with_photo_only: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		search_online_only: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		username: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		sex: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		match_sex: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		ethnicity: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		relationship: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		location: {
			construct: function($input){
					if ($input!=undefined) {
						this.$container_node = $input.parents(".field_mileage:eq(0)");
					}
					
					var handler = this;
					
					this.$tab_prototype = this.$(".tab_container .prototype_node")
						.clone()
						.removeClass("prototype_node");
						
					this.$(".tab_container .prototype_node").remove();	
					
							
					this.prepareTabs();	
				},
			validate: function(value, required) {},
			focus: function() {},
			$container_node: {},
			tab_prototype: {},
			tabs: {},
			active_tab: undefined,
			$: function($expr){
					return this.$container_node.find($expr);
				},
			reset: function($node){
					$("input, select", $node).each(function(i, obj){
						if ($(obj).is("select")) {
							obj.selectedIndex = 0;
						} else if($(obj).is("input[type=checkbox]")) {
							$(obj).attr("checked", false);
						} else {
							$(obj).val("");
						}
					});
				},
			resetTab: function(tab){
					this.reset(tab["content"]);
					
					if (tab.name == "tab1") {
						var $cont = tab["content"];
						$cont.find("[name*=state_id]").parents("tr:eq(0)").hide();
						$cont.find("[name*=city_id]").parents("tr:eq(0)").hide();
						$cont.find("[name*=custom_location]").parents("tr:eq(0)").hide();
					} else if(tab.name == "tab2") {
						tab["content"].find(".country").hide();
					}
				},
			ajaxCall: function(params, callback){
					var handler = this;
					params.action = "process_mileage";		
					$.ajax({
								url: "http://www.catchyourmatch.net/field_responder.php",
								method: "post",
								dataType: "json",
								data: params,
								success: function(result){
								 	callback(result);
								}
						});
				},
			prepareTabs: function(){
					var handler = this;
					
							
					var $tab1 = this.$(".tab_1");
					
					var state_value = $tab1.find("select[name*=state_id]").val();
					var city_value = $tab1.find("input[name*=city_id]").val();
					var radius_first_value = $tab1.find("input[name*=radius_first]").val();
					var custom_location_value = $tab1.find("input[name*=custom_location]").val();
					
					$tab1.find("[name*=country_id]").change(function(){
							var $parent = $(this).parents(".content_container");
							
							$parent.find("[name*=state_id]").parents("tr:eq(0)").hide();
							$parent.find("[name*=city_id]").parents("tr:eq(0)").hide();
							$parent.find("[name*=city_id]").val("");
							$parent.find("[name*=radius_first]").val("");
							$parent.find("[name*=custom_location]").val("");
							$parent.find("[name*=custom_location]").parents("tr:eq(0)").hide();
							if ($(this).val()=="") return;
							handler.ajaxCall({country_id: $(this).val()},function(result){
								
								if (result.length > 0) {
									var $field = $parent.find("[name*=state_id]");
									
									
									$field.empty();
									$field.append("<option value=\"\">" + SK_Language.text("%profile_fields.select_invite_msg.113") + "</option>");
									$field.unbind();
									$field.parents("tr:eq(0)").show();
									handler.fillSelect($field,result);
									$parent.find("[name*=custom_location]").parents("tr:eq(0)").hide();
									
									$field.change(function(){
										
										handler.ajaxCall({country_id: $parent.find("[name*=country_id]").val(), state_id: $(this).val()}, function(result){
											
											$parent.find("[name*=city_id]").val("")
													.parents("tr:eq(0)").hide();
											$parent.find("[name*=radius_first]").val("");		
													
											$parent.find("[name*=city_id]").parent().find(".suggest_cont").remove();	
											
											if (result.state_id){
												
												if(city_value) {
													$parent.find("[name*=city_id]").val(city_value);
													city_value= false;
												}
												
												if(radius_first_value) {
													$parent.find("[name*=radius_first]").val(radius_first_value);
													radius_first_value= false;
												}
												else {
													$parent.find("[name*=radius_first]").val("");
												}
												
												$parent.find("[name*=city_id]").parents("tr:eq(0)").show();
												handler.suggest($parent.find("[name*=city_id]"),result.state_id);
												
											}
																										
										});
									});
									
									if (state_value) {
										$field.find("option[value=" + state_value + "]").attr("selected", "selected");
										state_value = false;
										$field.change();
									}
								}
								else {
								
									if (custom_location_value) {
										$parent.find("[name*=custom_location]").val(custom_location_value);
										custom_location_value = false;
									}
								
									$parent.find("[name*=custom_location]").parents("tr:eq(0)").show();
									$parent.find("[name*=state_id]").parents("tr:eq(0)").hide();
								}
							});	
					});
								
					this.addTab("tab1", SK_Language.text("%forms._fields.mileage.tab1"),$tab1, $tab1.hasClass("active"));
				
					var $tab2 = this.$(".tab_2");
					
					var $region_cont_prototype = $tab2.find(".region_cont").clone();
					$tab2.find(".region_cont").remove();
								
					var regions = {"Afr":"Africa","Ant":"Antarctic Region","Arc":"Arctic Region","Asi":"Asia","CAC":"Central America and the Caribbean","Eur":"Europe","MEa":"Middle East","NAm":"North America","Ocn":"Oceania","SAm":"South America","SEA":"Southeast Asia"};
					
					$.each(regions, function(code, name){
						var $region_cont = $region_cont_prototype.clone();
						var $region = $region_cont.find(".region");
						$region.text(name);
						
						var $region_val_cont = $tab2.find(".tab_values .region_value");
						
						var active = ($region_val_cont.find(".region_id:contains(" + code + ")").length > 0);
						
						if (active) {
							$region.addClass("active");
						}	
													
						$region.toggle(
						function(){
								
							$region_cont = $(this).parent();
							var $country_list = $region_cont.find(".country_list").hide();
							if ( $country_list.find(".country:visible").length <= 0 )
							{
								handler.ajaxCall({region_id: code},function(result){
								
									var $country_prot = $country_list.find(".country").clone().removeClass("prototype_node");				
									
									$.each(result, function(code, name){
										var active_country = ($region_val_cont.find(".country:contains("+code+")").length > 0);
										
										var $country = $country_prot.clone();
										
										if (active_country) {
											$country.find(":checkbox").attr("checked", true);
										}
										
										$country.find("label").text(name).attr("for",code);
										$country.find(":checkbox").attr("id",code).val(code);
										$country_list.append($country);
									});
									$country_list.slideDown("normal");
									
								});
							}
							else {
								$country_list.slideDown("normal");
							}
													
													
						},
						function(){
							$region_cont = $(this).parent();
							var $country_list = $region_cont.find(".country_list");
							$country_list.slideUp("normal");
						});
						
						$tab2.find(".tab_content").append($region_cont);
					});
					
					this.addTab("tab2", SK_Language.text("%forms._fields.mileage.tab2"), $tab2, $tab2.hasClass("active"));
					
					
					
					
					this.addTab("tab3",SK_Language.text("%forms._fields.mileage.tab3"), this.$(".tab_3"), this.$(".tab_3").hasClass("active"));
		
				},
			$suggest_cont_prototype: undefined,
			showSuggest: function($node, suggests){
					var handler = this;
					
					var removeSuggest = function(){
						$node.parent().siblings(".suggest_cont").remove();
					}
					
					if (this.$suggest_cont_prototype ==undefined) {
						this.$suggest_cont_prototype = this.$(".suggest_cont").remove().clone();
					}		
					
					var $suggest_cont = this.$suggest_cont_prototype.clone();
					
					removeSuggest();
					
					if (suggests.length <= 0) {
						return;
					}
					
					var itemHover = function($item){
						$item.parent().find(".suggest_item").removeClass("hover");
						$item.addClass("hover");
					}
					
					$.each(suggests, function(i, item){
					
						var $item_node = $suggest_cont.find(".prototype_node").clone().removeClass("prototype_node").css("display","block");
						var $parent_node = $suggest_cont.find(".prototype_node").parent();
						
						$item_node.html(item.suggest_label);
						
						$item_node.mouseover(function(){
							itemHover($item_node);
						});
						
						$item_node.click(function(){
							$node.val(item.name);
							removeSuggest();
							$node.focus();
						});
						
						$parent_node.append($item_node);
						
					});
					
					$node.unbind("keypress");
					$node.keypress(function(eventObject){
						
						$selected_item = $node.parent().find("div.suggest_cont ul li.hover");
						if ( $selected_item.length == 0 ) {
							$selected_item = $node.parent().find("div.suggest_cont ul li:visible:eq(0)");
							itemHover($selected_item);
							return;
						}
						
						switch(eventObject.keyCode){
							case 40:
								itemHover($selected_item.next(".suggest_item"));
								break;
							case 38:
								itemHover($selected_item.prev(".suggest_item"));
								break
							case 13:
								itemHover($selected_item.prev(".suggest_item"));
								if ($selected_item.length > 0) {
									$selected_item.click();
									return false;	
								}
								break
						}
					});
					
					$node.unbind("blur");
					$node.blur(function(){
						window.setTimeout(removeSuggest,200);
					});
								
					$node.parent().after($suggest_cont);
					$suggest_cont.css("width",$node.outerWidth()).show();
					
				},
			suggest: function($node, state_id){
					var handler = this;
					var timeout;
					var last_str;	
					
					
					$node.unbind();
					$node.keyup(function(eventObject){
						
						var $field = $(this);
						
						var getCityList = function(str, state_id) {
							if (!$.trim(str)) {
								last_str = "";
								handler.showSuggest($node, []);
								return;
							}	
						
							var key = eventObject.which;
							if ( last_str == str || key==13) {
								return;
							}
							
							var params ={
								str : str,
								state_id: state_id,
								action: "location_get_city"
							};
							$.ajax({
										url: "http://www.catchyourmatch.net/field_responder.php",
										method: "post",
										dataType: "json",
										data: params,
										success: function(result){
											last_str = str;
											handler.showSuggest($node, result, eventObject);
										}
								});
						}
						
						var suggestGetList = function(){
							var str = $field.val();
							getCityList(str, state_id);
						}
						
						if (timeout != undefined) {
							window.clearTimeout(timeout)
						}
						timeout = window.setTimeout(suggestGetList,300);
						return false;
					});
				},
			fillSelect: function($select, data){
					$.each(data,function(i, item){
						var $option = $("<option>" + item.name + "</option>");
						$option.attr("value", item.value);
						$select.append($option);
					});
				},
			activateTab: function(tab){
					var handler = this;
					var $cont = this.$(".content_container");
					
					if (this.active_tab!=undefined) {
						this.active_tab["tab"].removeClass("active");
					}
					tab["tab"].addClass("active");
								
					$cont.fadeOut("fast",function(){
						if (handler.active_tab!=undefined) {
							$cont.find(".tab_" + handler.active_tab["name"]).hide();
							handler.active_tab["content"] = $cont.find(".tab_" + handler.active_tab["name"]);
							handler.resetTab(handler.active_tab);
						}
						
						if ($cont.find(".tab_" + tab["name"]).length > 0) 
						{
							$cont.find(".tab_" + tab["name"]).fadeIn("fast");
							$cont.fadeIn("fast");
						} 
						else 
						{
							var $active_content = tab["content"].find(".tab_content");
							$active_content.addClass("tab_" + tab["name"]);
							$cont.append($active_content).fadeIn("fast");
							
							$active_content.find("select[name*=country_id]").change();
						}
						handler.active_tab = tab;
					});
				
				},
			addTab: function(name, label, $content, active){
					var handler = this;
					this.tabs[name] = {
						name: name,
						tab: this.$tab_prototype.clone(),
						content: $content.clone(true),
						label: label
					};
					$content.remove();
								
					var $tab = this.tabs[name];
					var $tab_node = $tab["tab"]
					if (active) {
						handler.activateTab($tab);
					}
					
					$tab_node.click(function(){
						if (!$(this).hasClass("active")) {
							handler.activateTab($tab);			
						}
					});
					$tab_node.text(label);
					$tab_node.appendTo(this.$(".tab_container"));
				}
		},
		height: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		birthdate: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		religion: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		body_type: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		eye_color: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		hair_color: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		have_children: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		language: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		education: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		income: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		smoke: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		drink: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		interests: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		},
		english_level: {
			construct: function($input, form_handler) {},
			validate: function(value, required) {},
			focus: function() {}
		}
	},
	actions: {
		search: {
		fields: {"search_with_photo_only":false,"search_online_only":false,"username":false,"sex":false,"match_sex":false,"ethnicity":false,"relationship":false,"location":false,"height":false,"birthdate":false,"religion":false,"body_type":false,"eye_color":false,"hair_color":false,"have_children":false,"language":false,"education":false,"income":false,"smoke":false,"drink":false,"interests":false,"english_level":false}
		}
	},
	default_action: 'search'
});

