var reqTotal = false;// 当前可用的XMLHttpRequest对象

// DIV名称数组
var divNameArray = new Array();
divNameArray[0] = "content_commend_div";
divNameArray[1] = "content_self_div";
divNameArray[2] = "confirm_div";
divNameArray[3] = "success_div";
divNameArray[4] = "select_div";

// 初始化各个基本变量
function initValues() {
	var msg_commend_id = document.getElementById("msg_commend_id");// 推荐短信Id
	msg_commend_id.value = "";

	var is_recommend = document.getElementById("is_recommend");// 是否为推荐短信的标志
	is_recommend.value = "1";

	var phone_str = document.getElementById("phone_str");// 手机号码串
	phone_str.value = "";

	var is_addPhone = document.getElementById("is_addPhone");// 是否增加手机号码的标志
	is_addPhone.value = "0";

	var is_allowCheck = document.getElementById("is_allowCheck");// 是否允许申请验证码的标志
	is_allowCheck.value = "0";
}

// 请求获得推荐短信页面的页面内容
function getContentCommendDiv() {
	var div = document.getElementById("content_commend_div");// 推荐短信
	if (div.innerHTML == "" && !reqTotal) {
		reqTotal = CreateXMLHttpRequest();
		if (reqTotal) {
			SendGetRequest(reqTotal, "content_commend.html", true, initContentCommendDiv);
		}
	}
}

// 初始化推荐短信页面
function initContentCommendDiv() {
	if (GetRequestReadyState(reqTotal) == 0) {
		var status = GetRequestStatus(reqTotal);

		if (status == "OK") {
			content = Trim(GetTextResponse(reqTotal));

			var div = document.getElementById("content_commend_div");
			div.innerHTML = content;
		}

		reqTotal = false;// 释放XMLHttpRequest对象

		getSelectDiv();
	}
}

// 请求获得选择发送对象页面的页面内容
function getSelectDiv() {
	var div = document.getElementById("select_div");// 选择发送对象
	if (div.innerHTML == "" && !reqTotal) {
		reqTotal = CreateXMLHttpRequest();
		if (reqTotal) {
			SendGetRequest(reqTotal, "select.html", true, initSelectDiv);
		}
	} else {
		// 初始化选择发送对象页面的内容
		initArrangeDateIdInSelect();
		initArrangeTimeIdInSelect();
	}
}

// 初始化选择发送对象页面
function initSelectDiv() {
	if (GetRequestReadyState(reqTotal) == 0) {
		var status = GetRequestStatus(reqTotal);

		if (status == "OK") {
			content = Trim(GetTextResponse(reqTotal));

			var div = document.getElementById("select_div");
			div.innerHTML = content;
		}

		reqTotal = false;// 释放XMLHttpRequest对象
	}
}

// 请求获得自写短信页面的页面内容
function getContentSelfDiv() {
	var div = document.getElementById("content_self_div");// 自写短信
	if (div.innerHTML == "" && !reqTotal) {
		reqTotal = CreateXMLHttpRequest();
		if (reqTotal) {
			SendGetRequest(reqTotal, "content_self.html",true, initContentSelfDiv);
		}
	}
}

// 初始化自写短信页面
function initContentSelfDiv() {
	if (GetRequestReadyState(reqTotal) == 0) {
		var status = GetRequestStatus(reqTotal);

		if (status == "OK") {
			content = Trim(GetTextResponse(reqTotal));

			var div = document.getElementById("content_self_div");
			div.innerHTML = content;
		}

		reqTotal = false;// 释放XMLHttpRequest对象

		getSelectDiv();
	}
}

// 请求获得确认页面的页面内容
function getConfirmDiv() {
	var div = document.getElementById("confirm_div");// 确认页面
	if (div.innerHTML == "" && !reqTotal) {
		reqTotal = CreateXMLHttpRequest();
		if (reqTotal) {
			SendGetRequest(reqTotal, "confirm.html", true, initConfirmDiv);
		}
	}
}


// 初始化确认页面
function initConfirmDiv() {
	if (GetRequestReadyState(reqTotal) == 0) {
		var status = GetRequestStatus(reqTotal);

		if (status == "OK") {
			content = Trim(GetTextResponse(reqTotal));

			var div = document.getElementById("confirm_div");
			div.innerHTML = content;
		}

		reqTotal = false;// 释放XMLHttpRequest对象
	}
}

// 请求获得成功页面的页面内容
function getSuccessDiv() {
	var div = document.getElementById("success_div");// 成功页面
	if (div.innerHTML == "" && !reqTotal) {
		reqTotal = CreateXMLHttpRequest();
		if (reqTotal) {
			SendGetRequest(reqTotal, "success.html", true, initSuccessDiv);
		}
	}
}

// 初始化成功页面
function initSuccessDiv() {
	if (GetRequestReadyState(reqTotal) == 0) {
		var status = GetRequestStatus(reqTotal);

		if (status == "OK") {
			content = Trim(GetTextResponse(reqTotal));

			var div = document.getElementById("success_div");
			div.innerHTML = content;
		}

		reqTotal = false;// 释放XMLHttpRequest对象
	}
}

// 检查各个DIV，对没内容的DIV读入内容
function checkInit() {
	getContentCommendDiv();
	getContentSelfDiv();
	getConfirmDiv();
	getSuccessDiv();
}

// 根据页面参数决定显示某个DIV
function showDiv() {
	str = window.location.href;
	pos = str.indexOf("?")
	parastr = str.substring(pos + 1);

	var div = false;

	var allDivHidden = true;

	for (var i = 0; i < divNameArray.length; i++) {
		div = document.getElementById(divNameArray[i]);

		if (div != null) {
			if (divNameArray[i] == parastr) {
				div.style.display = "";
				allDivHidden = false;
			} else {
				div.style.display = "none";
			}
		}
	}

	// 没参数时，默认显示推荐短信页面
	if (allDivHidden) {
		div = document.getElementById("content_commend_div");
		div.style.display = "";

		div = document.getElementById("select_div");
		div.style.display = "";

		getContentCommendDiv();

	} else {
		if (parastr == "content_commend_div") {
			getContentCommendDiv();

			div = document.getElementById("select_div");
			div.style.display = "";
		} else if (parastr == "content_self_div") {
			getContentSelfDiv();

			div = document.getElementById("select_div");
			div.style.display = "";

			var is_recommend = document.getElementById("is_recommend");
			is_recommend.value = "0";
		} else if (parastr == "confirm_div") {
			getConfirmDiv();
		} else if (parastr == "success_div") {
			getSuccessDiv();
		}
	}
}

// 选择发送对象页面，显示发送日期
function initArrangeDateIdInSelect() {
	var arrangeDateSpan = document.getElementById("select_date_span");

	if (arrangeDateSpan != null && arrangeDateSpan.innerHTML == "") {
		var str = "<select id=\"arrange_date_id\" onclick=\"javascript:checkSelectDate()\">\n";
		for (var i = 0; i < dateIdArray.length; i++) {
			if (i == 6) {
				str += "<option value=\"" + i + "\" selected>" + dateIdArray[i] + "</option>\n";
			} else {
				str += "<option value=\"" + i + "\">" + dateIdArray[i] + "</option>\n";
			}
		}
		str += "</select>\n";

		arrangeDateSpan.innerHTML = str;
	}
}

// 选择发送对象页面，显示发送时间
function initArrangeTimeIdInSelect() {
	var arrangeTimeSpan = document.getElementById("select_time_span");

	if (arrangeTimeSpan != null && arrangeTimeSpan.innerHTML == "") {
		var str = "<select id=\"arrange_time_id\">\n";
		for (var i = 0; i < timeIdArray.length; i++) {
			str += "<option value=\"" + i + "\">" + timeIdArray[i] + "</option>\n";
		}
		str += "</select>\n";

		arrangeTimeSpan.innerHTML = str;
	}
}