/**
 * js公共函数文件
 */

/**
 * 地址下拉列表改变时查询并显示下一级地址信息
 * 
 * @param obj
 *            点出的下拉框对象
 * @param lev
 *            点击的下拉框的级别。1:省，2:市...
 * @return
 */
function doChange(lev) {
	var strID = "";
	switch (lev) {
	case "0":
		strID = "come";
		break;
	case "1":
		strID = $("#province").val();
		break;
	case "2":
		strID = $("#city").val();
		break;
	case "3":
		strID = $("#district").val();
		break;
	default:
		strID = "";
	}

	if (strID != 0 && strID != "" || strID != '0') {
		switch (lev) {
		case "0":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				window.clipboardData.setData("text",data.options);
				$("#province").html(data.options);
			});
			$("#city").html("<option value=''>----</option>");
			$("#district").html("<option value=''>----</option>");
			$("#street").html("<option value=''>----</option>");
			break;
		case "1":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#city").html(data.options);
			});
			$("#district").html("<option value=''>----</option>");
			$("#street").html("<option value=''>----</option>");
			break;
		case "2":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#district").html(data.options);
			});
			$("#street").html("<option value=''>----</option>");
			break;
		case "3":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#street").html(data.options);
			});
			break;
		default:
			break;
		}
	} else {
		$(obj).nextAll().html("<option value=''>----</option>");
	}
}
//一个页面上需要两个相同的三级下拉框
function doChange1(lev) {
	var strID = "";
	switch (lev) {
	case "0":
		strID = "come";
		break;
	case "1":
		strID = $("#province1").val();
		break;
	case "2":
		strID = $("#city1").val();
		break;
	case "3":
		strID = $("#district1").val();
		break;
	default:
		strID = "";
	}

	if (strID != 0 && strID != "" || strID != '0') {
		switch (lev) {
		case "0":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#province1").html(data.options);
			});
			$("#city1").html("<option value=''>----</option>");
			$("#district1").html("<option value=''>----</option>");
			$("#street1").html("<option value=''>----</option>");
			break;
		case "1":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#city1").html(data.options);
			});
			$("#district1").html("<option value=''>----</option>");
			$("#street1").html("<option value=''>----</option>");
			break;
		case "2":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#district1").html(data.options);
			});
			$("#street1").html("<option value=''>----</option>");
			break;
		case "3":
			$.getJSON("http://www.99pto.com/ajax/areaSelect.html?sid=" + strID
					+ "&level=" + lev + "&jsoncallback=?", function(data) {
				$("#street1").html(data.options);
			});
			break;
		default:
			break;
		}
	} else {
		$(obj).nextAll().html("<option value=''>----</option>");
	}
}
function fillAddress() {
	var province = $("#province").val();
	var city = $("#city").val();
	var district = $("#district").val();
	var street = $("#street").val();
	$.getJSON("http://www.99pto.com/ajax/fillAddress.html?pid=" + province
			+ "&cid=" + city + "&did=" + district + "&sid=" + street + "&jsoncallback=?", function(data) {
		$("#streetexplicit").html(data);
	});	
	
}
// 提取cookie
function getCookie(cookie_name) {
	var allcookies = document.cookie;
	var cookie_pos = allcookies.indexOf(cookie_name);

	// 如果找到了索引，就代表cookie存在，
	// 反之，就说明不存在。
	if (cookie_pos != -1) {
		// 把cookie_pos放在值的开始，只要给值加1即可。
		cookie_pos += cookie_name.length + 1;
		var cookie_end = allcookies.indexOf(";", cookie_pos);

		if (cookie_end == -1) {
			cookie_end = allcookies.length;
		}
		var value = unescape(allcookies.substring(cookie_pos, cookie_end));
	}
	return value;
}



// 提取cookie
function getCookie2(cookie_name) {
	var allcookies = window.document.cookie;
	var cookie_pos = allcookies.indexOf(cookie_name);

	// 如果找到了索引，就代表cookie存在，
	// 反之，就说明不存在。
	if (cookie_pos != -1) {
		// 把cookie_pos放在值的开始，只要给值加1即可。
		cookie_pos += cookie_name.length + 1;
		var cookie_end = allcookies.indexOf(";", cookie_pos);

		if (cookie_end == -1) {
			cookie_end = allcookies.length;
		}
		var value = unescape(allcookies.substring(cookie_pos, cookie_end));
	}
	return value;
}

// 设定Cookie值
function SetCookie(name, value) {
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if (expires != null)
		expdate.setTime(expdate.getTime() + (expires * 1000));
	document.cookie = name + "=" + escape(value)
			+ ((expires == null) ? "" : ("; expires=" + expdate.toGMTString()))
			+ ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "");
}

/**
 * 弹出点击车次的结果
 */
var trains = Array();
var popTrainNumber = function() {
	var trainnumber = arguments[0];
	var url = "/public/alipay/poptrainnumber.php";
	$("#poptrainnumber").show();
	// 设置DIV的高度
	$("#poptrainnumber").css("top",
			document.documentElement.scrollTop + 210 + "px").css(
			"left",
			document.body.scrollWidth / 2 - $("#poptrainnumber").width() / 2
					+ "px");
	$("#popin")
			.html(
					"<div style='width:100%;text-align:center;margin-top:10px;padding-bottom:10px;font-size:12px;'>正在查询，请稍候...</div>");
	if (typeof trains[trainnumber] == "undefined") {
		$.post(url, {
			checi : trainnumber
		}, function(data) {
			$("#popin").html(data);
			trains[trainnumber] = data;
		});
	} else {
		$("#popin").html(trains[trainnumber]);
	}
}

/**
 * 列车类型筛选
 */
var filterTrainType = function() {
	var obj = arguments[0];
	var val = $(obj).val();
	$(".t0").hide();
	$(".t" + val).show();
	return false;
}

/**
 * 提取链接地址里的参数
 * 
 * @param key
 *            string
 * @returm string
 */
String.prototype.queryString = function(key) {
	var sValue = this.match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"));
	return sValue ? sValue[1] : sValue
}