var submitted = 0;	// to avoid clicking a button twice
var secs;
var timerID = null;
var delay = 1000;
var countDownObj;
var link;
var lock;
var pics;

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

/** close a window in the page */
function closeWindow(name) {
	var objID = name;
	var obj = document.getElementById(objID);
	obj.innerHTML = "";
}

/** get the match result or fixture
 * 0: backward
 * 1: forward
 * type: fixture or result
 * data_id: match id
 * data_td: table cell id
 */
function getGame(type, order, data_id, data_td) {
	var xmlhttp = GetXmlHttpObject();
	var td = document.getElementById(data_td);
	var id = document.getElementById(data_id);
	var serverPage = "modules/index.controller.php?action=next&id=" + id.value + "&type=" + type + "&order=" + order;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				td.innerHTML = xmlhttp.responseText;
			}
		} else {
			td.innerHTML = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td colspan="3" bgcolor="#FF9900" class="wordblack10ptSong" height="25"><img src="img/results.gif"/></td><td bgcolor="#FF9900" align="right" class="wordblack8ptSong">More&nbsp;</td></tr><tr><td width="25%" height="30" align="center" valign="bottom"><a class="ajaxLoading">&nbsp;Loading...&nbsp;</a></td></tr></table>';
		}
	}
	xmlhttp.send(null);
}

/** close a window in the page */
function closeWindowInNews(name) {
	var objID = name;
	var obj = document.getElementById(objID);
	obj.style.visibility="hidden";
	obj.innerHTML = '<div align="center"><img src="img/loading.gif"/></div>';
}

function playVideoInNews(code) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById("video_place");
	var serverPage = "ajaxRequest.php?action=video&code=" + code;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.style.visibility="";
				div.innerHTML = "<div align='center'>" + unescape(xmlhttp.responseText) + "</div>";
			}
		} else {
			div.style.visibility="";
		}
	}
	xmlhttp.send(null);
}

function playVideoInNewsFeed(div, id) {
	var xmlhttp = GetXmlHttpObject();
	var divObj = document.getElementById(div);
	var serverPage = "ajaxRequest.php?action=play_video_in_newsfeed&id=" + id;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				divObj.innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null);
}


function playPicInNews(name) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById("pic_place");
	var serverPage = "ajaxRequest.php?action=pic&name=" + name;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.style.visibility="";
				div.innerHTML = "<div align='center'>" + unescape(xmlhttp.responseText) + "</div>";
			}
		} else {
			div.style.visibility="";
		}
	}
	xmlhttp.send(null);
}

function playPicInNewsById(id) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById("pic_place");
	if(id >= pics.length || id < 0) {
		return;
	}
	var currPic = pics[id];
	var nextPic;
	if(id == pics.length - 1) {
		nextPic = 0;
	} else {
		nextPic = id+1;
	}
	var serverPage = "ajaxRequest.php?action=pic&name=" + currPic + "&next=" + nextPic;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.style.visibility="";
				div.innerHTML = "<div align='center'>" + unescape(xmlhttp.responseText) + "</div>";
			}
		} else {
			div.style.visibility="";
		}
	}
	xmlhttp.send(null);
}

function playPicInImageViewerById(id) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById("pic_place");
	if(id >= pics.length || id < 0) {
		return;
	}
	var currPic = pics[id];
	var nextPic;
	if(id == pics.length - 1) {
		nextPic = 0;
	} else {
		nextPic = id+1;
	}
	var serverPage = "ajaxRequest.php?action=pic&name=" + currPic + "&next=" + nextPic + "&nextNmae=" + pics[nextPic] + "&close=1";
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.style.visibility="";
				div.innerHTML = "<div align='center'>" + unescape(xmlhttp.responseText) + "</div>";
			}
		} else {
			div.style.visibility="";
		}
	}
	xmlhttp.send(null);
}

function addPic(name) {
	if(pics == null) {
		pics = new Array();
	}
	pics[pics.length] = name;
}

function LockButton(form, element) {
	if (!submitted) {
		submitted = 1;
	} else { 
		alert("已提交，请等待");
	}
}

function getVideoName(name, vid) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById(name);
	var obj = document.getElementById(vid);
	var serverPage = "ajaxRequest.php?action=get_video_name&vid=" + obj.value;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.innerHTML = xmlhttp.responseText;
			} else {
				div.innerHTML = "该视频不存在，请重新输入并验证";
			}
		}
	}
	xmlhttp.send(null);
}

function getVideoName2(name, vid) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById(name);
	var serverPage = "ajaxRequest.php?action=get_video_name&vid=" + vid;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.innerHTML = xmlhttp.responseText;
			} else {
				div.innerHTML = "该视频不存在，请重新输入并验证";
			}
		}
	}
	xmlhttp.send(null);
}

function getNewsName(name, id) {
	var xmlhttp = GetXmlHttpObject();
	var div = document.getElementById(name);
	var serverPage = "ajaxRequest.php?action=get_news_name&id=" + id;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.innerHTML = xmlhttp.responseText;
			} else {
				div.innerHTML = "该新闻不存在，请重新输入并验证";
			}
		}
	}
	xmlhttp.send(null);
}

function parsePostAddress(source, target) {
	var sourceObj = document.getElementById(source);
	var targetObj = document.getElementById(target);
	var str = sourceObj.value;
	if(str == "") return;
	var pattern = new RegExp("tid=");
	var m = pattern.exec(str);
	if(m == null) {
		alert('无法提取帖子编号');
	} else {
		var index = m.index;
		var id="";
		for(var i=index+4; i<str.length; i++) {
			if(str.charAt(i) == '&') {
				break;
			} else {
				id += str.charAt(i);
			}
		}
		targetObj.value = id;
	}
}

function confirmation(check, str, form) {
	var obj = document.getElementById(check);
	var answer = confirm(str);
	if(answer) {
		obj.value="1";
		form.submit();
	} else {
		obj.value="";
	}
}

/** asc = 0, increase the page number 
 * asc = 1, decrease the page number
 */
function loadTopicList(page, listDiv, asc) {
	var xmlhttp = GetXmlHttpObject();
	var pageObj = document.getElementById(page);
	var div = document.getElementById(listDiv);
	var serverPage = "ajaxRequest.php?action=load_topic_list&page=" + pageObj.value;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				div.innerHTML = xmlhttp.responseText;
				if(asc == 0) {
					pageObj.value = parseInt(pageObj.value) + 1;
				} else if(asc == 1) {
					pageObj.value = parseInt(pageObj.value) - 1;
				}
			} else {
				div.innerHTML = "该图片不存在或无法打开";
			}
		}
	}
	xmlhttp.send(null);
}

function addToTopicList(list, id, str) {
	var listObj = document.getElementById(list);
	for(var i = 0; i < listObj.options.length; i++) {
		if(id == listObj.options[i].value) {
			alert('此专题已在右侧列表中');
			return;
		}
	}
	var optionObj = document.createElement('option');
	optionObj.text = str;
	optionObj.value = id;
	listObj.options.add(optionObj);
}

function removeFromList(list) {
	var listObj = document.getElementById(list);
	if(listObj.selectedIndex == -1) {
		alert("请选择想删除的专题");
		return;
	}
	for(var i = listObj.options.length-1; i >= 0; i--) {
		if(listObj.options[i].selected == true) {
			listObj.remove(i);
		}
	}
}

function selectAll(list) {
	var listObj = document.getElementById(list);
	if(listObj.options.length == 0) {
		alert("无法提交空列表");
		return false;
	}
	for(var i = listObj.options.length-1; i >= 0; i--) {
		listObj.options[i].selected = true;
	}
	return true;
}

function startTimer(sec, label) {
	secs = sec;
	countDownObj = document.getElementById(label);
	countDown();
}

function countDown() {
    if(secs == 0) {
        if(link != "") {
            window.location = link;
        }
    } else {
        secs--;
        timerID = self.setTimeout("countDown()", delay);
        h = Math.floor(secs / 3600);
        m = Math.floor(secs / 60);
        s = secs - h * 3600 - m * 60;
        countDownObj.innerHTML = h + ":" + m + ":" + s + "后自动退出";
    }
}

//url: the file to be downloaded
//div: the place to preview the downloaded pic
//name: new file name
//nameField: the hidden field to hold the file name
function downloadPic(url, div, name, nameField) {
	var xmlhttp = GetXmlHttpObject();
	var divObj = document.getElementById(div);
	var nameFieldObj = document.getElementById(nameField);
	var serverPage = "ajaxRequest.php?action=download_pic&pic=" + url + "&name=" + name;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				divObj.innerHTML = xmlhttp.responseText;
				nameFieldObj.value = name;
			} else {
				divObj.innerHTML = "图片下载失败, 请重新下载";
				nameFieldObj.value = "";
			}
		}
	}
	xmlhttp.send(null);
}

function generateVideoCode(url, code, sampleAdd, codeDiv, picDiv, name, nameField) {
	var urlObj = document.getElementById(url);
	var codeObj = document.getElementById(code);
	var codeDivObj = document.getElementById(codeDiv);
	var picDivObj = document.getElementById(picDiv);
	var picNameObj = document.getElementById(nameField);
	var str = urlObj.value;
	if(str == "") return;
	var pattern = new RegExp("v=");
	var m = pattern.exec(str);
	if(m == null) {
		alert('无法提取视频编号');
	} else {
		var index = m.index;
		var id="";
		for(var i=index+2; i<str.length; i++) {
			if(str.charAt(i) == '&') {
				break;
			} else {
				id += str.charAt(i);
			}
		}
		codeDivObj.innerHTML = id;
		codeObj.value = id;
		var pattern2 = new RegExp(/PIC/);
		str2 = sampleAdd.replace(pattern2, id);
		downloadPic(str2, picDiv, name, nameField);
		
	}
}

function exploreVideo(topicId, div) {
	if(lock == true) return;
	lock = true;
	var divObj = document.getElementById(div);
	while(divObj.hasChildNodes()) {
		for(var i=0; i < divObj.childNodes.length; i++) {
			divObj.removeChild(divObj.firstChild);
		}
	}
	var listObj = document.getElementById('video_list[]');
	for(var i = listObj.length-1; i >= 0; i--) {
		listObj.remove(i);
	}
	var xmlhttp = GetXmlHttpObject();
	var serverPage = "ajaxRequest.php?action=get_explore_video&id=" + topicId;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if(xmlhttp.responseText != "") {
				var results = eval(xmlhttp.responseText);
				for(var i=0; i<results.length; i++) {
					divObj.appendChild(document.createElement("<br>"));
					result = results[i];
					var chk = document.createElement("INPUT");
					chk.setAttribute("type", "checkbox");
					chk.setAttribute("id", "explore_video_list[]");
					chk.setAttribute("name", "explore_video_list[]");
					chk.setAttribute("value", result[0]);
					chk.setAttribute("onchange", 'checkVideoList(\''+result[0]+'\', \''+result[2]+'\')');
					chk.checked = true;
					checkVideoList(result[0], result[2]);
					divObj.appendChild(chk);
					var img = new Image();
					img.src = result[3] + result[1];
					img.align="middle";
					divObj.appendChild(img);
					divObj.appendChild(document.createTextNode(" " + result[2]));
					divObj.appendChild(document.createElement("<br>"));
				}
			} else {
				divObj.innerHTML = "没有找到可添加的视频";
			}
		}
	}
	xmlhttp.send(null);
	lock = false;
}

function checkVideoList(id, str) {
	var listObj = document.getElementById('video_list[]');
	for(var i = 0; i < listObj.options.length; i++) {
		if(id == listObj.options[i].value) {
			listObj.remove(i);
			listObj.size--;
			if(listObj.size == 0) {
				listObj.style.visibility="hidden";
			}
			return;
		}
	}
	var optionObj = document.createElement('option');
	optionObj.text = str;
	optionObj.value = id;
	listObj.options.add(optionObj);
	listObj.size++;
	listObj.style.visibility="";
}

function selectAllWithoutChecking(list) {
	var listObj = document.getElementById(list);
	if(listObj.options.length == 0) {
		return;
	}
	for(var i = listObj.options.length-1; i >= 0; i--) {
		listObj.options[i].selected = true;
	}
}

function toggleDisplay(div) {
	obj = document.getElementById(div);
	if(obj.style.display=='none') {
		obj.style.display='';
	} else {
		obj.style.display='none';
	}
}

function generateVerifyCode(img) {
	if(lock == true) return;
	Stamp = new Date();
	lock = true;
	img.src = "ajaxRequest.php?action=verify_code_gen&time=" +Stamp;
	lock = false;	
}
