﻿var hostServer = 'http://www.ivote.vn/';
function loadXmlHttp(url, id) {
	var f = this;
	f.xmlHttp = null;

	if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
		f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
		else if (/(object)|(function)/.test(typeof createRequest))
		f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
	else {
		f.xmlHttp = null;
	}
	if(f.xmlHttp != null){
		f.el = document.getElementById(id);
		f.xmlHttp.open("GET",url,true);
		f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
		f.xmlHttp.send(null);
	}
}

function autoLoad2(page,tag,Id) {
	alert(page);
	loadXmlHttp.prototype.stateChanged=function () {
	if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
		this.el.innerHTML = this.xmlHttp.responseText;
	}
	
	var requestTime = function(){
	new loadXmlHttp(page + '?t=' + new Date().getTime() + '&Id=' + Id, tag);
	setInterval(function(){new loadXmlHttp(page + '?t=' + new Date().getTime() + '&Id=' + Id , tag);}, 60000);
	}
	/*
	if (window.addEventListener) 
	 window.addEventListener('load', requestTime, false);
	else if (window.attachEvent)
	 window.attachEvent('onload', requestTime);
	*/
}

function autoLoad(page,tag,Id) {
	
	setInterval(function(){
		rs = page.search(/center=1/);
		if(rs > 0) {
			urlPhp = page + '&Id=' + Id;
		} else {
			urlPhp = page + '?Id=' + Id;
		}
		
		var data = $.ajax({
			   type: "POST",
			   url: urlPhp,
			   async: false
			 }).responseText;
			 
			 $('#' + tag).html(data);
	}, 10000);
}
function autoLoad3(page,tag,Id) {
	rs = page.search(/center=1/);
	if(rs > 0) {
		urlPhp = page + '&Id=' + Id;
	} else {
		urlPhp = page + '?Id=' + Id;
	}
	var data = $.ajax({
		   type: "POST",
		   url: urlPhp,
		   async: false
		 }).responseText;
		 $('#' + tag).html(data);
		
}

function checkComment() {
	comment = document.getElementById("comment").value;
	if(comment == "") {
		alert("Bạn chưa nhập ký tự nào");
		return false;
	}
	if(comment.length > 200 ) {
		alert("Bạn chỉ được nhập tối đa 200 ký tự");
		return false;
	} else {
		document['comment'].submit();
	}
	
}
function getMsgonline(latestId) {
	if(latestId != '') {
		var data = $.ajax({
		   type: "POST",
		   url: server + "/index.php?action=getnewnotice&type=public&id=" + latestId,
		   async: false
		 }).responseText;
		 return data;
	}	 
	return ''; 
}
function addOption(n) {
	for(i = n; i <= 20; i++) {
		if(document.getElementById(i).style.display == "none") {
			document.getElementById(i).style.display = "block";
			showOption(i);
			break;
		}
	}
}
function showOption(id) {
	var str = '';
	str += '<div  style="float: left;border:1px solid black;width:80%">';
	str +='<div style="float: left; width: 100px;"><input type = "text" id ="keyword' + id + '" size = "3" name = "keyword'+id+'"> </div>';
	str +='<div style="float: left;width: 180px"> <input type = "text" id = "title' + id +'" name = "title' + id + '"> </div>';
	str +='<div style="float: left; width: 300px"> <textarea name = "desc' + id + '"></textarea> </div>';
	str +='<div style="float: left; width: 250px"> <input type = "file" name ="picture' + id + '"> </div>';
	str +='<div style="float: left; width: 180px"> <input type = "text" name = "link' + id + '"> </div>';
	str +='<div><input type = "hidden" name = "count[]" value = "' + id + '" ><input type = "hidden" name = "c" value = "' + i +'" ></div></div>';
	str +='<div  style="float: left;border:1px solid black;"><a href="#" onclick ="remove2(' + id + ');>Remove</a></div>';
	document.getElementById(id).innerHTML = str;
}
function remove2(id) {
	document.getElementById(id).innerHTML = "";
}
function checkCode() {
	var code = document.getElementById('code').value;
	if(code == "") {
		alert("Yêu cầu nhập mã số");
		document.getElementById('code').focus();
		return false;
	}
	var out = $.ajax({
	   type: "POST",
	   url: "http://www.ivote.vn/checkCode.php",
	   data: "code=" + code,
	   async: false
	 }).responseText;
	//$('format').value = 'VOTE '+ code.toUpperCase() + ' X Y';
	document.getElementById('showFormat').innerHTML = code;
	document.getElementById('isCodeExist').innerHTML = out;
}
function updateTitle() {
	var name = $("name").value;
	var pollId = $("pollId").value;
	if(name != "" && pollId != "") {
		showDiv('title');
		hideDiv('frmTitle');
		showDiv('lkTitle');
		$('title').innerHTML = name;
		new Ajax.Updater('title', 'updateByAjax.php?type=title&name=' + name + '&pollId='+ pollId);
	} else {
		alert("Yêu cầu nhập tên bình chọn!");
		$("name").focus();
	}
}
function stopPoll(id,flag) {
	var status;
	var message;
		if(flag == 1) {
			status = 'stopPoll';
			message = "Bạn có chắc muốn dừng cuộc bầu chọn này không?";
		} else {
			status = 'contPoll';
			message = "Bạn có chắc muốn tiếp tục cuộc bầu chọn này không?";
		}
	if(!confirm(message)) {
		return false;
	} else {	
	if(id != "") {
		var out = $.ajax({
	   type: "POST",
	   url: hostServer + "updateByAjax.php",
	   data: "type=" + status + "&pollId=" + id,
	   async: false
		}).responseText;
		document.getElementById('status').innerHTML = out;
		if(flag == 1) {
			document.getElementById('stop').innerHTML = '<button class="submitBtn" onclick = "return stopPoll(' + id + ',0);"><span>Tiếp tục cuộc bầu chọn</span></button>';
		} else {
			document.getElementById('stop').innerHTML = '<button class="submitBtn" onclick = "return stopPoll(' + id + ',1);"><span>Kết thúc cuộc bầu chọn</span></button>';
		}
	 }
	 }
}
function updateFeatured(id) {
	var id2 = "f_" + id;
	var s;
	//alert(id2);
	if($("#" + id2).is(":checked") == true) {
		s = 1;
	} else {
		s = 0;
	}
	//new Ajax.Updater("status", "updateByAjax.php?type=updateFeatured&pollId="+ id + "&status=" + s );
	var data = $.ajax({
		   type: "POST",
		   url: hostServer + "updateByAjax.php?type=updateFeatured&pollId="+ id + "&status=" + s,
		   async: false
		 }).responseText;
		 //alert(hostServer + "updateByAjax.php?type=updateFeatured&pollId="+ id + "&status=" + s);
		 //$('#' + tag).html(data);
}

function updateCat(id) {
	var catId = "cat_" + id;
	var name = $(catId).value;
	var updateId = "name_" + id;
	if(name != "") {
		new Ajax.Updater(updateId, "updateByAjax.php?type=updateCat&name=" + name + "&id="+ id);
	}
}

function changeStatus(id,s) {
	var id2 = 'status_' + id;
	new Ajax.Updater(id2, "updateByAjax.php?type=changeStatus&pollId=" + id + '&status=' + s);
}

function showText(input,show) {
	var inputId = document.getElementById(input);
	var showText = document.getElementById(show);
	$(document).ready(function(){
    
    $(inputId).blur(function () {
         if(inputId.value != '') {
			document.getElementById(show).innerHTML = inputId.value;
		 }
    });

  });
}

function loadStyle() {
$(document).ready(function(){
		$('table tbody tr:nth-child(odd)').addClass('alt');
		$('table tbody tr td:first-child').addClass('first');
		$('table tbody tr td:nth-child(2)').addClass('second');
		$('table tbody tr td:last-child').addClass('last');
	});
}

function selecttext(id) {
      $('#' + id).select();
}

function showOptY(id) {
	if(id == 1) {
		$('#showOpY').show();
		$('#opYdesc').show();
	} else {
		$('#showOpY').hide();
		$('#opYdesc').hide();
	}
	//alert($('#opty').is(":checked"));
}
