// JavaScript Document
function yourClock(){
  var nd = new Date();
	d = nd.getDate();
	d = (d < 10)?"0"+d:d;
	mo = nd.getMonth()+1; //mon[nd.getMonth()];
	y = nd.getFullYear();
	mo = (mo < 10)?"0"+mo:mo;
  time = d +  "/" + mo + "/" + y; 
  document.getElementById("dateTime").innerHTML = time;
  gizmo = setTimeout("yourClock()", 1000);
}

function openWindow(f, w ,h)
{
	width=w;
	height=h;
	//----------------------------------
	Url=f;	
	//----------------------------------
	window.open(Url,"","toolbar=no,scrollbar=no,status=no,resizable=no ,width=" + width + ",height=" + height + ",top="
							+ ((window.screen.availHeight - height)/2) +",left=" + ((window.screen.availWidth - width)/2));
}

function displayContent(order, introText){
	if(document.getElementById("leaf_"+order)){
		if(document.getElementById(order).value != ""){
			cont = "<B>"+document.getElementById(order).value + "</B><DIV><I>Kích vào để chỉnh sửa nội dung câu trả lời</I></DIV>";
			showtip(cont);
		}else{
			f = (order < "06")?" cho 5 điều bạn đã làm được!":" cho 5 điều bạn mong muốn làm được!";
			cont = "<B>Chưa có câu trả lời!</B><DIV><I>Kích vào để nhập nội dung câu trả lời"+f+"</I></DIV>";
			showtip(cont);
		}
	}
}


function updateText(order){
	if(document.getElementById(order)){
		if(document.getElementById("uText").value != ""){
			document.getElementById(order).value = document.getElementById("uText").value;
			document.getElementById("leaf_"+order).src = "images/leaf_"+order+"_o.jpg";
		}
		document.getElementById("uContent").innerHTML = "";
		if(document.getElementById(order).value != ""){
			star = "images/done.png";
		}else{
			star = "images/undone.png";
		}
		document.getElementById("star"+order).src = star;
		
	}
	changePos();
}

function reText(order, introText){
	if(document.getElementById(order)){
		if(document.getElementById(order).value != ""){
			cont = "<DIV ALIGN=\"LEFT\" STYLE=\"PADDING-LEFT:40px;\"><B>"+introText+" <EM>(Tối đa 500 ký tự)</EM>:</B> </DIV><BR><TEXTAREA NAME=\"uText\" ID=\"uText\" COLS=\"35\" ROWS=\"3\" style=\"border:1px solid #999999; font-size:10pt\" onKeyUp=\"limitText('uText', 500)\"></TEXTAREA><DIV STYLE=\"text-align:right; padding-right:80px;\"><a href=\"javascript:updateText('"+order+"');\">Cập nhật</a></DIV>";	
			document.getElementById("uContent").innerHTML = cont;
			document.getElementById("uText").value = document.getElementById(order).value;
		}else{
			cont = "<DIV ALIGN=\"LEFT\" STYLE=\"PADDING-LEFT:40px;\"><B>"+introText+" <EM>(Tối đa 500 ký tự)</EM>:</B> </DIV><BR><TEXTAREA NAME=\"uText\" ID=\"uText\" COLS=\"35\" ROWS=\"3\" style=\"border:1px solid #999999; font-size:10pt\" onKeyUp=\"limitText('uText', 500)\"></TEXTAREA><DIV STYLE=\"text-align:right; padding-right:80px;\"><a href=\"javascript:updateText('"+order+"');\">Cập nhật</a></DIV>";	
			document.getElementById("uContent").innerHTML = cont;
		}
	}
	document.getElementById("arrow1").innerHTML =  "";
	document.getElementById("uText").focus();
}

function showArrow(){
	document.getElementById("arrow1").innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;<!--IMG SRC="images/arrow.gif" WIDTH="25" HEIGHT="40"-->';
	//changePos();
}

function checkTest(){
	var ok = true;
	for(i = 1; i <= 10; i++){
		cur = ((i < 10)?"0":"") + i;
		if(document.getElementById(cur).value == ""){
			ok = false;
		}
	}
	
	if(ok == false){
		alert("Bạn phải nhập đầy đủ 10 câu trả lời cho cuộc thi trước khi tiếp tục!");
		return false;
	}
	
	if(confirm("Bạn chắc chắn về những câu trả lời của mình và muốn tiếp tục!\nNếu tiếp tục thì bạn không thể quay lại chỉnh chỉnh nội dung câu trả lời nữa!") == true){
		return true;
	}else{
		return false;
	}
	
	return true;
}

var xPos = new Array(70, 120, 170, 150, 215, 310, 345, 395, 365, 405);
var yPos = new Array(409, 386, 415, 476, 487, 500, 407, 425, 485, 494);

function changePos(){
	var x = 0;
	if (self.innerHeight){
		x = self.innerWidth;
		extra = 0;
	}else if (document.documentElement && document.documentElement.clientHeight){
		x = document.documentElement.clientWidth;
		extra = 0;
	}else if (document.body){
		x = document.body.clientWidth;
		extra = 10;
	}
	x = (x < 1004)?1004:x;
	x = x/2;
	
	//document.getElementById("arrow").style.left = x + 70 + extra;
	var arrow = document.getElementById("arrow").style;
	for(i = 1; i <= 10; i++){
		cur = ((i < 10)?"0":"") + i;
		if(document.getElementById(cur).value == ""){
			arrow.left = xPos[i - 1] + x + extra;
			arrow.top  = yPos[i - 1];
			break;
		}
	}
}

var h, w;

function getImgSize(imgSrc)
{
var newImg = new Image();
newImg.src = imgSrc;
h = newImg.height;
w = newImg.width;
}

function openPic(sPicURL){
	getImgSize(sPicURL);
	//alert(h + "---" + w);
	window.open("realImage.htm?"+sPicURL+"", "", "toolbar=no,scrollbars=yes,status=yes,resizable=no,HEIGHT="+h+",WIDTH="+w+",TOP="
							+ ((window.screen.availHeight - h)/2) +",LEFT=" + ((window.screen.availWidth - w)/2));
}

function isEmail(s){if(s=="")return false;if(s.indexOf(" ")>0)return false;if(s.indexOf("@.")>0)return false;if(s.indexOf("@")==-1)return false;var i=1;var slen=s.length;if(s.indexOf(".")==-1 || s.indexOf(".")==s.length-1)return false;if(s.indexOf("..")!=-1)return false;if(s.indexOf("@")!=s.lastIndexOf("@"))return false;var str="abcdefghijklmnopqrstuvwxyz1234567890@-._ABCDEFGHIJKLMNOPQRSTUVWXYZ";for(var j=0;j<s.length;j++)if(str.indexOf(s.charAt(j))==-1)return false;return true;}

function isTelephone(str){
	var validStr = "+- ().0123456789";
	for(var i = 0; i < str.length; i++){
		if(validStr.indexOf(str.charAt(i)) == -1)return false;
	}
	return true;
}

function checkInfor(){
	if(!document.getElementById("txtHoTen").value){
		alert("Bạn chưa nhập họ và tên!");
		document.getElementById("txtHoTen").focus();
		return false;
	}
	if(!document.getElementById("txtNgaySinh").value){
		alert("Bạn chưa nhập ngày sinh (theo định dạng dd/mm/yyyy)!");
		document.getElementById("txtNgaySinh").focus();
		return false;
	}
	if(!document.getElementById("txtDienThoai").value){
		alert("Bạn chưa nhập số điện thoại!");
		document.getElementById("txtDienThoai").focus();
		return false;
	}
	
	if(!isTelephone(document.getElementById("txtDienThoai").value)){
		alert("Số điện thoại không hợp lệ!");
		document.getElementById("txtDienThoai").focus();
		return false;
	}
	
	if(!isEmail(document.getElementById("txtEmail").value)){
		alert("Địa chỉ email bạn cung cấp không hợp lệ!");
		document.getElementById("txtEmail").focus();
		return false;
	}
	
	var S = false;
	var cboObj = document.frmTest.radNguon;
	for(i=0; i<cboObj.length; i++){
		if(cboObj[i].checked == true){
			S = true;
			break;
		}
	}
	
	if((cboObj[cboObj.length - 1].checked == true) && (document.getElementById("txtKhac").value == "")){
		alert("Bạn chưa nhập nguồn thông tin rõ ràng!");
		document.getElementById("txtKhac").focus();
		return false;
	}
	
	if(S == false){
		alert("Bạn chưa chọn nguồn thông tin giúp bạn biết đến cuộc thi!");
		return false;
	}
	
	if((cboObj[cboObj.length - 1].checked == true) && (document.getElementById("txtKhac").value == "")){
		alert("Bạn chưa nhập nguồn thông tin rõ ràng!");
		document.getElementById("txtKhac").focus();
		return false;
	}
	
	return true;
}

function limitText(id, limitNum) {
    if (document.getElementById(id).value.length > limitNum) {
        document.getElementById(id).value = document.getElementById(id).value.substring(0, limitNum);
    } 
}

var iColor = new Array("#990000", "#FFFF00", "#009900", "#FF9900", "#000099");
var i = 0;
function changeColor(){
	if(document.getElementById("link-result")){
		document.getElementById("link-result").style.color = iColor[i];
	}
	i = ((i < 5)?(i + 1):0);
	setTimeout("changeColor()", 200);
}

function ShowDiv(i,s, name){
	for(j=1;j<=s;j++){
		if(j==i){
			//if(document.getElementById(name+i).style.display=="")
			//	document.getElementById(name+i).style.display="none";
			//else
				document.getElementById(name+i).style.display="";			
		}else{
			document.getElementById(name+j).style.display="none";
		}
	}
}


function openURL(strURL, w, h)
{
	width=w;
	height=h;
	window.open(strURL,"","toolbar=no,scrollbar=no,status=no,resizable=no,width=" + width + ",height=" + height + ",top="
							+ ((window.screen.availHeight - height)/2) +",left=" + ((window.screen.availWidth - width)/2));
}


//var obj, h, w;

function showPicture(imgSrc){

	var ImgWidth;

	var newImg = new Image();
	newImg.src = imgSrc;
	ImgWidth = newImg.width;
	ImgHeight = newImg.height;
	
	if(document.all("getPic")){
		var x = 0;
		if (self.innerHeight){
			x = self.innerWidth;
			extra = 0;
		}else if (document.documentElement && document.documentElement.clientHeight){
			x = document.documentElement.clientWidth;
			extra = 0;
		}else if (document.body){
			x = document.body.clientWidth;
			extra = 10;
		}
		x = (x < 1004)?1004:x;
		x = x/2;
		
		newWidth = (ImgWidth > ((x*2) - 20))?((x*2) - 20):ImgWidth;
		newHeight = (ImgWidth > ((x*2) - 20))?((ImgHeight * newWidth)/ImgWidth):ImgHeight;
		
		ImgWidth = newWidth;
		ImgHeight = newHeight;
		
		if(ImgHeight > 580){
			newHeight = 580;
			newWidth = (580 * ImgWidth)/ImgHeight;
		}
		
		var obj = document.getElementById("getPic");
		obj.style.display = "";
		obj.innerHTML = '<div style="text-align:right; background-color:#FFAA00; padding-right:10px; padding:5px;"><a href="javascript:hidePicture();">Đóng X</a></div><IMG SRC="'+imgSrc+'" WIDTH="'+newWidth+'" HEIGHT="'+newHeight+'" ID="realImg" NAME="realImg">';
		//alert(obj.innerHTML);

		//var newImg = new Image();
		/*newImg = document.all.realImg;
		h = newImg.height;
		w = newImg.width;*/
		
		obj.style.top = 15;
		obj.style.left= x - newWidth/2;
	}
}

function hidePicture(){
	if(document.all("getPic")){
		obj = document.getElementById("getPic");
		obj.style.left = -320;
		obj.style.top = -320;
		obj.style.display = "none";
	}
}
