/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. biz
 * Email：service@phpcoo.com，phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2019/07/01 by OEdev $
*/

//获取select选框子地区$obj->当前select的名称 不含 depth
function oeloveGetChildArea($jname, $depth, $child, $text, $callback) {
	if (typeof($child) == "undefined") {$child = 0;}
	if (typeof($text) == "undefined") {$text = "";}
	if (typeof($callback) == "undefined") {$callback = "";}
	$depth = parseInt($depth);
	
	if ($depth < 4) {
		$j_obj = $("#"+$jname+$depth); //当前SELECT的OBJ
		$rootid = $j_obj.find("option:selected").val(); //当前SELECT所选的值

		if ($depth > 0) {
			if ($rootid < 1) { //加载子地区必选父地区
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"2'><option value=''>"+$text+"</option></select>"); //2
					$("#json_"+$jname+($depth+2)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+3)).html(""); //4
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+2)).html(""); //4
				}
				else {
					$("#json_"+$jname+($depth+1)).html(""); //4
				}
				return;
			}
		}
		//获取子地区
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php",
			cache: false,
			data: {m:"index", c:"area", a:"child", parentid:$rootid, depth:$depth, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$option_html = "";
				$.each($data, function($entryIndex, $obj){
					$option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
				});
				//HTML
				$child_name = $jname+""+($depth+1);
				$html_result = "";
				if ($option_html.length > 0) { //存在option
					$html_result += "<select name='"+$child_name+"' id='"+$child_name+"'";
					if ($child > 0) {
						
						if ($callback.length > 0) {
							$html_result += " onchange=\"oeloveGetChildArea('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"', '"+$callback+"')\"";
						}
						else {
							$html_result += " onchange=\"oeloveGetChildArea('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"')\"";
						}
					}
					$html_result +=">";
					$html_result += "<option value=''>"+$text+"</option>";
	
					$html_result += $option_html;
					$html_result += "</select>";
				}

				$("#json_"+$child_name).html($html_result);
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+2)).html(""); //第三级
					$("#json_"+$jname+($depth+3)).html(""); //第四级
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+2)).html(""); //第四级
				}
				
				if ($callback.length > 0) { //回调函数
					$fun = eval($callback);
					if (typeof($fun) == "function") {
						$fun.call(this);
					}
				}

			},
			error: function() { }
		});
	}

}


//获取select选框子户籍地区$obj->当前select的名称 不含 depth 9.07.01 3联动
function oeloveGetChildHomeTown($jname, $depth, $child, $text, $callback) {
	if (typeof($child) == "undefined") {$child = 0;}
	if (typeof($text) == "undefined") {$text = "";}
	if (typeof($callback) == "undefined") {$callback = "";}
	$depth = parseInt($depth);
	
	if ($depth < 3) {
		$j_obj = $("#"+$jname+$depth); //当前SELECT的OBJ
		$rootid = $j_obj.find("option:selected").val(); //当前SELECT所选的值

		if ($depth > 0) {
			if ($rootid < 1) { //加载子地区必选父地区
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"2'><option value=''>"+$text+"</option></select>"); //2
					$("#json_"+$jname+($depth+2)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+3)).html(""); //4
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+2)).html(""); //4
				}
				else {
					$("#json_"+$jname+($depth+1)).html(""); //4
				}
				return;
			}
		}
		//获取子地区
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php",
			cache: false,
			data: {
                m:"index", c:"area", a:"hometown", parentid:$rootid, depth:$depth, r:get_rndnum(8)
            },
			dataType: "json",
			success: function($data) {
				$option_html = "";
				$.each($data, function($entryIndex, $obj){
					$option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
				});
				//HTML
				$child_name = $jname+""+($depth+1);
				$html_result = "";
				if ($option_html.length > 0) { //存在option
					$html_result += "<select name='"+$child_name+"' id='"+$child_name+"'";
					if ($child > 0) {
						
						if ($callback.length > 0) {
							$html_result += " onchange=\"oeloveGetChildHomeTown('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"', '"+$callback+"')\"";
						}
						else {
							$html_result += " onchange=\"oeloveGetChildHomeTown('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"')\"";
						}
					}
					$html_result +=">";
					$html_result += "<option value=''>"+$text+"</option>";
	
					$html_result += $option_html;
					$html_result += "</select>";
				}

				$("#json_"+$child_name).html($html_result);

                
                //清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+1)).html(""); //第二级
					$("#json_"+$jname+($depth+2)).html(""); //第三级
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+1)).html(""); //第三级
				}
				
				if ($callback.length > 0) { //回调函数
					$fun = eval($callback);
					if (typeof($fun) == "function") {
						$fun.call(this);
					}
				}

			},
			error: function() { }
		});
	}

}



/**
 * 毕业院校 联动选择 7.09.01
 * @param:: string $area_pid 地区选框ID
 * @param:: string $school_pid 院校选框ID
 * @param:: string $text option默认提示
 * @return:: string ...
*/
function oeloveGetSchool($area_pid, $school_pid, $text) {
	if (typeof($text) == "undefined") {
		$text = "=请选择=";
	}
	$a_obj = $("#"+$area_pid); //area 选框对象
	$areaid = $a_obj.find("option:selected").val(); //所选地区

	$sch_obj = $("#json_"+$school_pid); //school 院校对象
	$sch_obj.html("");

	//获取院校数据
	$.ajax({
		type: "POST",
		url: _ROOT_PATH+"index.php",
		cache: false,
		data: {
			m:"index", c:"ajax", a:"getschool", areaid:$areaid, r:get_rndnum(8)
		},
		dataType: "json",
		success: function($data) {
			$option_html = "";
			if ($data.length > 0) {
				$.each($data, function($entryIndex, $obj){
					/*
					if ($obj.firstchar.length > 0) {
						$option_html += "<option value='"+$obj.schid+"'>"+$obj.firstchar+" "+$obj.name+"</option>";
					}
					else {
						$option_html += "<option value='"+$obj.schid+"'>"+$obj.name+"</option>";
					}
					*/
					$option_html += "<option value='"+$obj.schid+"'>"+$obj.name+"</option>";
				});
			}
			//HTML
			$html_result = "";
			$html_result += "<select name='"+$school_pid+"' id='"+$school_pid+"'>";
			$html_result += "<option value=''>"+$text+"</option>";
			$html_result += $option_html;
			$html_result += "</select>";

			$sch_obj.html($html_result);

		},
		error: function() { }
	});
}

