var	TFlg=0;	
var	TTim='';
var	selcnt=0;
var invflg=false;
var invst="fldlnk";
var invstx="fldlnkx";

function byId(id){return (document.getElementById(id));};

// возвращает cookie если есть или undefined
function getCookie(name) {
    var matches = document.cookie.match(new RegExp(
      "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
    ))
    return matches ? decodeURIComponent(matches[1]) : undefined
}
 
// уcтанавливает cookie
function setCookie(name, value, props) {
    props = props || {}
    var exp = props.expires
    if (typeof exp == "number" && exp) {
        var d = new Date()
        d.setTime(d.getTime() + exp*1000)
        exp = props.expires = d
    }
    if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }
 
    value = encodeURIComponent(value)
    var updatedCookie = name + "=" + value
    for(var propName in props){
        updatedCookie += "; " + propName
        var propValue = props[propName]
        if(propValue !== true){ updatedCookie += "=" + propValue }
    }
    document.cookie = updatedCookie
 
}
 
// удаляет cookie
function deleteCookie(name) {
    setCookie(name, null, { expires: -1 })
}

function doAjax(query){
    var http=false;
    try {http=new XMLHttpRequest();} catch (e1){
    try {http=new ActiveXObject("Msxml2.xmlhttp");} catch (e2){
    try {http=new ActiveXObject("Microsoft.xmlhttp");} catch (e3){http=false;}}}
    http.onreadystatechange=function(){if(http.readyState==4){
		/*alert(http.responseText);*/ eval(http.responseText);
	}};
    http.open("POST",query,true);
    http.send(null);
};

function keepsession(){
    doAjax('keepsession.php');
    keeptimer=setTimeout("keepsession()",600000);
};
keeptimer=setTimeout("keepsession()",600000);

function onlogout() { window.open('logout.php','_frm'); };

function ondelmsg(msgid) {
	doAjax('delmsg.php?msgid='+msgid);
};

function delbdr() {
	for (i=0; i<81; i++) {
		byId("fld"+i).style.borderStyle="none";
		//byId("fld"+i).style.borderColor="";
	};
};

function sel(k,v) {
	if (GameArr[k]==0) {
		GameArr[k]=v;
		selcnt++;
		byId('fld'+k).innerHTML='<table border="0" class="tb2"><tr><td class="td2"><a class="fldlnk1" href="javascript:desel('+k+')">'+v+'</a></table>';
		
		if (selcnt==81) {
			byId('sndbtn').disabled=false;
		};
	};
};

function hidesel(event,k,ln) {
	if (byId('a_'+k+'_'+ln).className==invst) {
		byId('a_'+k+'_'+ln).className=invstx;
	} else {
		byId('a_'+k+'_'+ln).className=invst;
	};
};

function desel(k) {
	if (GameArr[k]!=0) {
		GameArr[k]=0;
		selcnt--;
		var st='<table border="0" cellspacing="1" class="tb3" align="center">';
		for (var i3=0; i3<3; i3++) {
			st += '<tr>';
			for (var j3=0; j3<3; j3++) {
				st += '<td class="tb3_'+((i3*3+j3)%2)+'"><a id="a_'+k+'_'+(i3*3+j3+1)+'" class="'+invst+'" href="javascript:sel('+k+','+(i3*3+j3+1)+')" oncontextmenu="hidesel(event,'+k+','+(i3*3+j3+1)+')">';
				st += i3*3+j3+1;
				st += '</a></td>';
			};
			st += '</tr>';
		};
		st += '</table></center>';
		byId('fld'+k).innerHTML=st;
		byId('sndbtn').disabled=true;
	};
};

function sndrez() {
	if (selcnt==81) {
		byId('sndbtn').disabled=true;
		st='';
		for (i=0; i<81; i++) {
			st+=GameArr[i];
		};
		doAjax('gamefin.en.php?tflg='+TFlg+'&fld='+st+'&rnd='+Math.random());
	};
};

function svgame() {
	st='';
	for (i=0; i<81; i++) {
		st+=GameArr[i];
		if (GameArr[i]==0) {
			for (j=1; j<10; j++) {
				if  (byId('a_'+i+'_'+j).className=="fldlnkx") {
					st+='1';
				} else {
					st+='0';
				};
			};
		};
	};
	doAjax('gamesave.en.php?tflg='+TFlg+'&fld='+st+'&rnd='+Math.random());
};

function rsgame() {
	doAjax('gamerestore.en.php?tflg='+TFlg+'&rnd='+Math.random());
};

function gamerestore(arr) {
	delbdr();
	for (i=0; i<81; i++) {
		val=arr.shift();
		if (val=='x') {
			//
		} else {
			if (val>0) {
				if (byId('fld'+i).innerHTML.length>1) {
					try { desel(i); } catch (e2){ };
				};
				try { sel(i,val); } catch (e2){  };
			} else if (val==0) {
				if (byId('fld'+i).innerHTML.length>1) {
					try { desel(i); } catch (e2){ };
				};
				for (j=1; j<10; j++) {
					val1=arr.shift();
					if (val1==0) {
						try { byId('a_'+i+'_'+j).className="fldlnk"; } catch (e2){ };
					} else {
						try { byId('a_'+i+'_'+j).className="fldlnkx"; } catch (e2){ };
					};
				};
			};
		};
	};
};

function chginv() {
	invflg=! invflg;
	if (invflg) {
		invst="fldlnkx";
		invstx="fldlnk";
		setCookie("SudokuInv", "on", { expires: 3000000} )
	} else {
		invst="fldlnk";
		invstx="fldlnkx";
		deleteCookie("SudokuInv")
	};
	for (i=0; i<81; i++) {
		for (j=1; j<10; j++) {
			try {
				if (byId('a_'+i+'_'+j).className=="fldlnk") {
					byId('a_'+i+'_'+j).className="fldlnkx";
				} else {
					byId('a_'+i+'_'+j).className="fldlnk";
				}
			} catch (e2){ };
		};
	};
};

function ShowFieldX() {

stx='field.php?tflg='+TFlg+'&fldid='+FId+'&rnd='+Math.random();

var st = '<br><br><center>';
st += '<div oncontextmenu="return false;" style="position:relative; width:400px; height:400px; background:transparent url(\'files/loading.gif\') no-repeat center center"><div style="position:absolute; top:0px; left:0px;"><img src=\''+stx+'\' width="396" height="396"></div>';
di=2; k=0; td=0;
for (i=0; i<9; i++) {
	if ((i==3)||(i==6)) { di+=2; td=(td+1)%2; };
	dj=2;
	for (j=0; j<9; j++) {
		if ((j==3)||(j==6)) { dj+=2; td=(td+1)%2; };
		if (GameArr[k]==0) {
			st += '<div class="td2 td1_'+td+'" id="fld'+k+'" style="position:absolute; width:43px; height:43px; left:'+(i*43+di)+'px; top:'+(j*43+dj)+'px;">';
		
					st += '<table border="0" cellspacing="1" class="tb3" align="center">';
					for (i3=0; i3<3; i3++) {
						st += '<tr>';
						for (j3=0; j3<3; j3++) {
							st += '<td class="tb3_'+((i3*3+j3)%2)+'"><a id="a_'+k+'_'+(i3*3+j3+1)+'" class="fldlnk" href="javascript:sel('+k+','+(i3*3+j3+1)+')" oncontextmenu="hidesel(event,'+k+','+(i3*3+j3+1)+')">';
							st += i3*3+j3+1;
							st += '</a></td>';
						};
						st += '</tr>';
					};
					st += '</table>';
		
			st += '</div>';
		} else {
			st += '<div class="td2 td1_'+td+'" id="fld'+k+'" style="position:absolute; width:43px; height:43px; left:'+(i*43+di)+'px; top:'+(j*43+dj)+'px;">';
			st += '</div>';
			selcnt++;
		};
		k++;
	};
};

st += '</div>';

if (TFlg==1) {
	st += '<table class="xsmall" width="400"><tr><td align="left">Started at '+TTim+' GMT</td><td align="right"><input type="checkbox" id="invflg" onclick="chginv()"> Invert the prompts</td></tr></table>';
	st += '<br><br><input style="width:175px;" id="sndbtn" type="button" value="&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;" onclick="sndrez()" disabled>';
	st += '&nbsp;&nbsp;&nbsp;<input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Restart&nbsp;&nbsp;&nbsp;" onclick="window.location.reload()">';
	st += '<br><br><input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Save&nbsp;&nbsp;&nbsp;" onclick="svgame()">';
	st += '&nbsp;&nbsp;&nbsp;<input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Restore&nbsp;&nbsp;&nbsp;" onclick="rsgame()">';
};


if (TFlg==0) {
	st += '<table class="xsmall" width="400"><tr><td align="left"></td><td align="right"><input type="checkbox" id="invflg" onclick="chginv()"> Invert the prompts</td></tr></table>';
	st += '<br><input style="width:175px;" id="sndbtn" type="button" value="&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;" onclick="sndrez()" disabled>';
	st += '&nbsp;&nbsp;&nbsp;<input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Restart&nbsp;&nbsp;&nbsp;" onclick="window.location.reload()">';
	st += '<br><br><input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Save&nbsp;&nbsp;&nbsp;" onclick="svgame()">';
	st += '&nbsp;&nbsp;&nbsp;<input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Restore&nbsp;&nbsp;&nbsp;" onclick="rsgame()">';
	st += '<br><br><input style="width:175px;" type="button" value="&nbsp;&nbsp;&nbsp;Change the layout&nbsp;&nbsp;&nbsp;" onclick="doAjax(\'resetgame.php\')">';
}

st += '</center>';



byId('GameFld').innerHTML=st;
if (getCookie("SudokuInv")=="on") { byId("invflg").click(); };
};

