		/* 		developed by webous agency 
				http://webous.com/
				author: val petruchek
				e-mail: val@webous.com
				date: 	July 12, 2007								*/

var Cell = new Array();
function AskWord(dir, num)
	{//dir is hor|ver, num is number
	text = (dir=="hor") ? HorWords[num].def : VerWords[num].def;
	len = (dir=="hor") ? HorWords[num].len : VerWords[num].len;
	input = (dir=="hor") ? HorWords[num].input : VerWords[num].input;
	newinput = prompt(text+" (букв: "+len+")",input);
	if (newinput && (newinput != input))
		{
		newinput = newinput.toUpperCase()
		newinput = newinput.replace(/Ё/g,"Е");		
		newinput = newinput.replace(/Й/g,"И");		
		if (dir=="hor")	
			HorWords[num].input = newinput;
		else
			VerWords[num].input = newinput;
		DisplayWords();
		}
	}
function DisplayWords()
	{
	Answered = 0; Total = 0; Error = false;
	//nullify Cell
	for (i=1;i<=Rows;i++)
		for (j=1;j<=Cols;j++)
			Cell[i][j] = '';
	for (num in HorWords)
		{
		word = HorWords[num];
		for (i=0;i<word.len;i++)
			Cell[word.x][word.y+i] += word.input.charAt(i);
		if ( (word.input == "") || (word.input.length != word.len))
			document.getElementById('hor_word_'+num).className = "word";
		else{
			document.getElementById('hor_word_'+num).className = "word_answered";
			Answered ++;
			}
		Total ++;		
		}
	for (num in VerWords)
		{
		word = VerWords[num];
		for (j=0;j<word.len;j++)
			Cell[word.x+j][word.y] += word.input.charAt(j);
		if ( (word.input == "") || (word.input.length != word.len))
			document.getElementById('ver_word_'+num).className = "word";
		else{
			document.getElementById('ver_word_'+num).className = "word_answered";
			Answered ++;
			}
		Total ++;		
		}
	for (i=1;i<=Rows;i++)
		{
		for (j=1;j<=Cols;j++)
			{
			if ( (Cell[i][j].length == 2) && (Cell[i][j].charAt(0)==Cell[i][j].charAt(1)) )
				Cell[i][j] = Cell[i][j].charAt(0);
			elem = document.getElementById('c_'+i+'_'+j);
			if (elem)
				{
				elem.innerHTML = Cell[i][j];
				if (Cell[i][j].length == 2)
					{
					elem.innerHTML = "<span class='cell_error'>"+Cell[i][j]+"</span>";
					Error = true;
					}
				}
			}
		}
	if (Answered != Total)
		document.getElementById('status').innerHTML = 'Заполнено слов: '+Answered+' из '+Total;
	else
		document.getElementById('status').innerHTML = '<span style="color:green"> Заполнены все слова ('+Answered+')</span> ';

	if (Error)
		document.getElementById('status').innerHTML += '<br /><span style="color:red">Есть ошибки пересечения!</span>';
	else
		document.getElementById('status').innerHTML += '<br />&nbsp;';
	document.getElementById('checkbutton').disabled = (Answered == 0);
	document.getElementById('notify_about_button').className = (Answered == 0) ? "answer_visible" : "answer_hidden";
	}
function CheckAnswers(sid)
	{
	data = "sid="+escape(sid);
	for (num in HorWords)
		data += "&hw["+num+"]="+escape(HorWords[num].input);
	for (num in VerWords)
		data += "&vw["+num+"]="+escape(VerWords[num].input);
	loadXMLDoc("check",data);
	ButtonEnabled(false);
	}
function getBodyScrollTop()
	{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	}
function loadXMLDoc(url, data) 
	{
    if (window.XMLHttpRequest) 
		{// branch for native XMLHttpRequest object
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
  		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(data);
		return true;
    	}
	else{
		if (window.ActiveXObject)
			{// branch for IE/Windows ActiveX version
	        req = new ActiveXObject("Microsoft.XMLHTTP");
    	    if (req) 
				{
        	    req.onreadystatechange = processReqChange;
		        req.open("POST", url, true);
		  		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				req.send(data);
				return true;
        		}
	    	}
		else{
			return false;
			}
		}	
	}
function processReqChange() 
	{
    // only if req shows "complete"
    if (req.readyState == 4) 
		{
        // only if "OK"
        if (req.status == 200)
			{
            document.getElementById('checkanswersdiv').innerHTML = req.responseText;
			Position('checkanswersdiv',Math.round((getWidth()-400)/2),getBodyScrollTop()+100);
        	}
		else{
            alert("Возник проблем:\n" + req.statusText + "\n\n" + req.responseText);
			ButtonEnabled(true);
        	}	
		}
	}
function Position(element,left,top)
	{
	if (document.all)
		{
		document.all[element].style.left = left+"px";
		document.all[element].style.top = top+"px";
		}
	else{
		if (document.layers)
			{
			document.layers[element].left = left;
			document.layers[element].top = top;
			}
		else{
			document.getElementById(element).style.left = left+"px";
			document.getElementById(element).style.top = top+"px";
			}
		}
	}
function getWidth()
	{
	width = (document.all) ? document.body.clientWidth : window.innerWidth;
	return Math.max(width,400);
	}
function CloseAnswers()
	{
	Position('checkanswersdiv',-500,-500);
	ButtonEnabled(true);
	}
function ButtonEnabled(buttonstatus)
	{
	thebutton = document.getElementById('checkbutton');
	thebutton.value = (buttonstatus) ? "Проверить ответы" : "Проверяю ответы ...";
	thebutton.disabled = !buttonstatus;
	}
