var k=document;
var clicked={
	x:new Array(),
	y:new Array() 
};
var oInterval="";
var $t={t:0,w:0,h:0,m:0};
var v={f:0};
var count={c:0};
var $d={d:new Array([15])};
var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
var in_process=0;

function startclock() {var today = new Date(); starttime = today.getTime();}
function endclock() {var today = new Date(); endtime = today.getTime();}
function calctime() {var time = (endtime - starttime - 0)/1000;	return time;}

Array.prototype.iDel = function(i) { //delete selected elem from array
   var arr=this; 
   var result=new Array(); 
   for(var s=0;s<arr.length;s++) {
       if(s!=i) {
           result.push(arr[s]);
       }
   }
   arr.clear();
   for(var t=0;t<result.length;t++) {
		arr[t]=result[t];
   }	
}

function set_point(varX, varY, wasS) { 
   if(in_process==0) {
	   var flag=0;
	   if(clicked.x.length==0) { 
		   clicked.x.push(varX);
		   clicked.y.push(varY);
		   var o=$('_'+varX+', '+varY);
		   o.style.backgroundColor='#FFCC00';
	   } else {
		   var f=clicked.x.length; 
		   for(var i=0;i<f;i++) {
			 if((clicked.x[i]==varX) && (clicked.y[i]==varY)) {
			   clicked.x.iDel(i); 
			   clicked.y.iDel(i);
			   var o=$('_'+varX+', '+varY); 
			   if(wasS==1){o.style.backgroundColor='#D2F2FF';} else {o.style.backgroundColor='#FFFFFF';}
			   flag=1;
			 } 
		   }
		   if(flag!=1) {
			   clicked.x.push(varX);
			   clicked.y.push(varY);
			   var o=$('_'+varX+', '+varY);
			   o.style.backgroundColor='#FFCC00';
		   }
	   } 
	   if(clicked.x.length>2) { checkR(); }
   }
}


function checkR() {
	in_process=1;
	var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			in_process=0;
			$('div').innerHTML=http.responseText;
			if(http.responseJS!=null) {
				if(http.responseJS.right==1) {
				    setR();
				    $d.d.push(http.responseJS.r);
				    $('clue_'+http.responseJS.c).style.color='red';
				}
				if(http.responseJS.right=='all') {
				    setR();
				    endclock();
				    clear_adv();
				    var end_time=calctime();
				    fnStopInterval();
				    save_time(end_time);
				    p_form(end_time);
				}
			}
		}
	} 
	http.open('post', 'content/c_tree/request.php', true);
	http.send({ x : clicked.x, y : clicked.y, v : v.f, d : $d.d});	
}

function setR() {
    var c=clicked.x.length;
	for(var f=0;f<c;f++) {
		var o=$('_'+clicked.x[f]+', '+clicked.y[f]);
        o.style.backgroundColor='#D2F2FF';
        //o.style.color='#FFFFFF';
        $eV(o, 'onclick', 'set_point('+clicked.x[f]+', '+clicked.y[f]+', 1)');
	}
	clicked.x.clear();
    clicked.y.clear();
}

/* $eV([object/string], [string], [string]) adds event handler to specified object */
function $eV(el, event, string) {
  	if(typeof el == 'string') {
  	   var el=document.getElementById(el);
  	}
  	if(isIE) {
  	    var handle="el."+event+"=function "+event+"(event) {"+string+"}";
		eval(handle);
	} else {
		var handle="el.setAttribute(\""+event+"\", \""+string+"\")";
		eval(handle);
	}
}

function start_game() {
    var d=$('rules');
    if(d!=null) {
        d.parentNode.removeChild(d);
    }
    clicked.x.clear();
    clicked.y.clear();
	$t.t=0;$t.w=0;$t.h=0;$t.m=0;
	v.f=0;
	count.c=0;	
	$d={d:new Array([15])};
	var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			$('t').innerHTML=http.responseText;
			v.f=http.responseJS;
			//alert(http.responseText);
			//startclock();
			fnStopInterval();
	        fnStartInterval();
		}
	}
	http.open('post', 'content/c_tree/print.php', true);
	http.send({ });	
}

function p_form() {
	var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			var nD=k.createElement('div');
			nD.id='f_div';
			nD.style.position='absolute';
			nD.style.top='35px';
			nD.style.left='0px';
			nD.style.zIndex='3';
			nD.innerHTML=http.responseText;
			$('t').appendChild(nD);
		}
	}
	http.open('post', 'content/c_tree/reg_form.php', true);
	http.send({  });
}

function save_results(end_time) {
    var u_name=$('u_name').value;
    var u_sname=$('u_sname').value;
    var u_phone1=$('u_phone1').value;
    var u_phone2=$('u_phone2').value;
    var u_phone3=$('u_phone3').value;
    var u_email=$('u_email').value;
    var u_phone=u_phone1+'-'+u_phone2+'-'+u_phone3;
    var u_url=$('u_url').value;
    var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			if((http.responseJS!=null) && (http.responseJS.error==1)) {
			    alert(http.responseJS.error);
			}	
			show_best10();
		}
	}
	http.open('post', 'content/c_tree/save_res.php', true);
	http.send({ best_time : end_time, u_name : u_name, u_sname : u_sname, u_phone : u_phone, u_email : u_email, u_url : u_url }); 
}

function save_time(end_time) {
    var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
		  //$('div').innerHTML=http.responseText;
		}
	}
	http.open('post', 'content/c_tree/save_time.php', true);
	http.send({ nowT : end_time });	
}

function fnStartInterval(){
    var oDate=new Date();
	$t.w=oDate.getTime();
	$t.t=$t.w;
	if(oInterval==""){
		oInterval=window.setInterval("fnRecycle()",500);
	}
	else{
		fnStopInterval();
	}
}
function fnStopInterval(){
	if(oInterval!=""){
		window.clearInterval(oInterval);
		oInterval="";
	}
}
function fnRecycle(){
    var oTimer=$('oTimer');
	$t.t=$t.t+500;
	var now=$t.t-$t.w;
	var sSeconds=Math.round(now/1000);
	if(sSeconds>=60) {
	    $t.m++;
	    $t.w=$t.w+sSeconds*1000;
	    sSeconds=0;
	}
	if($t.m>=60) {
	    $t.h++;
	    $t.w=$t.w+$t.m*60;
	    $t.m=0;
	}
	if(sSeconds<10) {
	    var $0='0';
	} else {
	    var $0='';
	}
	if($t.m<10) {
	    var $_='0';
	} else {
	    var $_='';
	}
	if($t.h<10) {
	    var $_h='0';
	} else {
	    var $_h='';
	}
	oTimer.innerHTML=$_h + $t.h + ":" + $_ + $t.m + ":" + $0 + sSeconds;
}

function clear_adv() {
	var x=$('advice_1');
	x.parentNode.removeChild(x);
	x=$('advice_2');
	x.parentNode.removeChild(x);
}

function show_best10() {
	var http=new JsHttpRequest();
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
		    var nD=k.createElement('div');
			nD.id='b_div';
			nD.style.position='absolute';
			nD.style.top='35px';
			nD.style.left='0px';
			nD.style.zIndex='3';
			nD.innerHTML=http.responseText;
			$('t').appendChild(nD);
		}
	}
	http.open('post', 'content/c_tree/show_best10.php', true);
	http.send({ });	
}

function CheckData() {
	ok = 0;
	if($('u_name').value=='') { 
		$('iname').style.color='red'; 
		ok = 1; 
	} else { 
		$('iname').style.color=''; 
		ok = 0; 
	}
	
	if($('u_sname').value=='') { 
		$('iname').style.color='red'; 
		ok = 1; 
	} else { 
		$('iname').style.color=''; 
		ok = 0; 
	}
	

	if(($('u_phone1').value=='') || ($('u_phone2').value=='') && ($('u_phone3').value=='')) { 
		$('iphone').style.color='red'; 
		ok = 1; 
	} else { 
		$('iphone').style.color=''; 
		ok = 0; 
	}
	
	if($('u_email').value=='') { 
		$('imail').style.color='red'; 
		ok = 1; 
	} else { 
		$('imail').style.color=''; 
		ok = 0; 
	}
	
	if($('u_url').value=='') { 
		$('iurl').style.color='red'; 
		ok = 1; 
	} else { 
		$('iurl').style.color=''; 
		ok = 0; 
	}
	

	var reg_mail = /[0-9a-z_\-\.]+@[0-9a-z_^.]+.[a-z]{2,3}/i;
	var mail = $('u_email').value;
	if(reg_mail.exec(mail) == null){
		$('imail').style.color='red'; ok = 1;
    }

	var reg_url = /^http:\/\/[a-z0-9\-\.]+\.[a-z]+[a-z0-9\/\.\_]+$/i;
	var url = $('u_url').value;
	if(reg_url.exec(url) == null){
		$('iurl').style.color='red'; ok = 1;
    }
 
    var reg_ph = /^[0-9]{10}$/;
	ph1 = $('u_phone1').value;
	ph2 = $('u_phone2').value;
	ph3 = $('u_phone3').value;
	var ph = ph1+ph2+ph3;
	if(reg_ph.exec(ph) == null){
		$('iphone').style.color='red'; ok = 1;
    }
    
	if (ok==0) {
		save_results();
		var d=$('f_div'); d.parentNode.removeChild(d);
	}
}



 
