var cap=new Image;
var capTrig=0,emlTrig=0,nameTrig=0;
var bgdiv=null;
var rint=0;
var n=10;
var mirror=0;
var prefix="";

function getXMLHttpOb(){
    var obj=new Array("Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP");
    for(var i=0;i<obj.length;i++){try{return new ActiveXObject(obj[i]);}catch(e){}}
    try{return new XMLHttpRequest();}catch(e){};return null;
}

function showReg(){

    var n=10;
    window.scroll(0,0);
    var div=document.getElementById("regDiv");
    var width=0;
    var height=0;
    if(navigator.appName=="Microsoft Internet Explorer"){
	width=document.body.scrollWidth;
	height=screen.height;
    }else{
	width=window.innerWidth;
	height=window.innerHeight;
    }

    try{document.body.onclick=function(){return false;};}catch(e){}
    try{document.body.onmousewheel=function(){return false;};}catch(e){}
    try{window.onscroll=function(){window.scroll(0,0);return false;};}catch(e){}
	
    bgdiv=document.createElement("DIV");
    bgdiv.setAttribute("id", "modalBack");
    bgdiv.style.background="#AEAEAE";
    bgdiv.style.zIndex=900;
    bgdiv.style.position="absolute";
    bgdiv.style.top="0px";
    bgdiv.style.left="0px";
    bgdiv.style.width="100%";
    bgdiv.style.height="150%";
    bgdiv.style.filter="alpha(opacity=90)";
    bgdiv.style.MozOpacity="0.9";
    bgdiv.style.opacity="0.9";
    document.body.appendChild(bgdiv);

    var html='<form name="formReg" action="" method="post" style="margin:0px;">\
		<table border="0" align="center">\
		<tr>\
		    <td class="t-descr">Логин<\/td>\
		    <td><input type="text" name="name" value="" style="width:160px;" maxlength="64" onkeypress="nameTrig=0;"><\/td>\
		    <td style="width:16px;"><div id="ok-name">&nbsp;<\/div><\/td>\
		<\/tr>\
		<tr>\
		    <td id="e-name" class="e-msg" colspan="2" align="right" style="display:none;">этот логин уже используется<\/td>\
		<\/tr>\
		<tr>\
		    <td class="t-descr">E-mail<\/td>\
		    <td><input type="text" name="email" value="" style="width:160px;" maxlength="64" onkeypress="emlTrig=0;"><\/td>\
		    <td style="width:16px;"><div id="ok-email">&nbsp;<\/div><\/td>\
		<\/tr>\
		<tr>\
		    <td id="e-email" class="e-msg" colspan="2" align="right" style="display:none;">такой e-mail уже есть в базе<\/td>\
		<\/tr>\
		<tr>\
		    <td class="t-descr">Пароль<\/td>\
		    <td><input type="password" name="password" value="" style="width:160px;" maxlength="64"><\/td>\
		    <td style="width:16px;"><div id="ok-password">&nbsp;<\/div><\/td>\
		<\/tr>\
		<tr>\
		    <td class="t-descr">Повторите пароль<\/td>\
		    <td><input type="password" name="confirmp" value="" style="width:160px;" maxlength="64"><\/td>\
		    <td style="width:16px;"><div id="ok-confirmp">&nbsp;<\/div><\/td>\
		<\/tr>\
		<tr>\
		    <td align="center"><img src="'+prefix+'/capcha.php?rand='+Math.random()+'" width="150" height="60" alt="" id="imgCapcha"><br><a href="#" onclick="capReload();" class="a-link">обновить код<\/a><\/td>\
		    <td align="center"><input type="text" name="capcha" value="" style="text-align:center;font-size:18px;width:100px;" onkeypress="capTrig=0;" maxlength="4"><\/td>\
		    <td style="width:16px;"><div id="ok-capcha">&nbsp;<\/div><\/td>\
		<\/tr>\
		<tr>\
		    <td colspan="2" align="right" style="height:50px;"><input type="submit" name="regSubmit" value="Зарегистрироваться" style="width:140px;font-size:12px;font-family:arial;margin-top:5px;" onclick="regStart();" disabled><\/td>\
		<\/tr>\
		<\/table><\/form>';

    document.getElementById("regBody").innerHTML=html;

    div.style.top=Math.round((height/100)*15)+"px";
    div.style.left=Math.round(width/2-290)+"px";
    div.style.background="url('"+prefix+"/images/aback.png')";
    div.style.backgroundRepeat="no-repeat";
    div.style.zIndex=1000;
    div.style.display="block";

    rint=setInterval("checkReg();", 500);    
}

function capReload(){

    try{
	document.forms.fMsg.capcha.value="";
	document.forms.fMsg.capcha.focus();
	document.forms.formReg.capcha.value="";
        document.forms.formReg.capcha.focus();
    }catch(e){}

    cap.src=prefix+"/capcha.php?rand="+Math.random();
    setTimeout(function(){
        document.getElementById("imgCapcha").src=cap.src;
    }, 100);
}

function hideReg(){

    if(rint) clearInterval(rint);

    try{document.body.onclick=function(){return true;};}catch(e){}
    try{document.body.onmousewheel=function(){return true;};}catch(e){}
    try{window.onscroll=function(){return true;};}catch(e){}

    try {
        document.body.removeChild(document.getElementById("modalBack"));
	document.getElementById("regDiv").style.display="none";
    }catch(e){}
}

function regStart(){

    var d=document.forms.formReg;
    var xmlReq=getXMLHttpOb();

    var param=encodeURI("email="+d.email.value+"&password="+d.password.value+"&name="+d.name.value+"&capcha="+d.capcha.value);
    xmlReq.open("POST", prefix+"/regist.php?rand="+Math.random(), true);

    xmlReq.onreadystatechange=function(){
        if(xmlReq.readyState==4){
	    if(xmlReq.status==200){
    	        var text=xmlReq.responseText;

		if(text=="512"){
		    nameTrig=1;
		    document.forms.formReg.capcha.focus();
	    	    document.getElementById("ok-name").innerHTML="<img src=\""+prefix+"/images/err.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
		    document.getElementById("e-name").style.display="";
		    return;
		}

		if(text=="256"){
		    emlTrig=1;
		    document.forms.formReg.email.focus();
	    	    document.getElementById("ok-email").innerHTML="<img src=\""+prefix+"/images/err.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
		    document.getElementById("e-email").style.display="";
		    return;
		}

		if(text=="128"){
		    capTrig=1;
		    document.forms.formReg.capcha.focus();
	    	    document.getElementById("ok-capcha").innerHTML="<img src=\""+prefix+"/images/err.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
		    return;
		}
		
		if(text=="0"){
		
		    var html='<br><br><table border="0" style="width:80%;" align="center"><tr><td align="center">\
			<span class="s-msg">Вы успешно зарегистрированы. Пройдите на главную для авторизации.<\/span><\/td><\/tr>\
			<tr><td style="height:50px;">&nbsp;<\/td><\/tr>\
			<tr><td align="center"><button id="closeBtn" onclick="hideReg();" style="width:130px;">Закрыть - '+n+'<\/button><\/td><\/tr><\/table>';
		
		    document.getElementById("regBody").innerHTML=html;
		    setTimeout(function(){
				    if(rint) clearInterval(rint);
				    closeTimer();
				}, 0);
		    return;
		}
	    }
	}
    }

    xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlReq.setRequestHeader("Content-length", param.length);
    xmlReq.setRequestHeader("Connection", "close");
    xmlReq.send(param); 
    
    return false;
}

function closeTimer(){

    try{
        document.getElementById("closeBtn").innerHTML="Закрыть - "+n;
    }catch(e){}

    if(n<=0){
	hideReg();
    }else{
        n--;setTimeout("closeTimer();", 1000);
    }
}

function getEsc(c){

    if(c==27 && document.getElementById("regDiv").style.display!="none"){
	try{hideReg();}catch(e){}
    }
}

function checkReg(){

    var d=document.forms.formReg;
    var elem=new Array("name", "password", "capcha");

    if(document.getElementById("e-name").style.display!="none" && nameTrig==0) document.getElementById("e-name").style.display="none";
    if(document.getElementById("e-email").style.display!="none" && emlTrig==0) document.getElementById("e-email").style.display="none";

    d.email.value=d.email.value.replace(/\s/ig, '');

    if(d['email'].value.length>0 && (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(d.email.value)){

	if(emlTrig==1) return;
        document.getElementById("ok-email").innerHTML="<img src=\""+prefix+"/images/ok.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
    }else{
        document.getElementById("ok-email").innerHTML="&nbsp;";
    }

    if(d['confirmp'].value.length>3){// && d['password'].value.length<=d['confirmp'].value.length){
    
	if(d['password'].value==d['confirmp'].value){
	    document.getElementById("ok-confirmp").innerHTML="<img src=\""+prefix+"/images/ok.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
	}else{
	    document.getElementById("ok-confirmp").innerHTML="<img src=\""+prefix+"/images/err.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
	}
    
    }else{
        document.getElementById("ok-confirmp").innerHTML="&nbsp;";
    }

    for(var i=0;i<elem.length;i++){

        if(d[elem[i]].value.length>3){
	    if((elem[i]=="capcha" && capTrig==1) ||
		(elem[i]=="name" && nameTrig==1)) continue;
	    document.getElementById("ok-"+elem[i]).innerHTML="<img src=\""+prefix+"/images/ok.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\">";
        }else{
	    document.getElementById("ok-"+elem[i]).innerHTML="&nbsp;";
        }
    }

    if(d.email.value.length>3 && d.password.value.length>3 &&
	d.confirmp.value.length>3 && d.name.value.length>3 &&
	d.capcha.value.length>3 && d.password.value==d.confirmp.value){
	
	d.regSubmit.disabled=false;
    }else{
	d.regSubmit.disabled=true;
    }

}

function addMirror(){

    if(mirror>=4)return false;

    var tr=document.createElement("TR");
    var td_a=document.createElement("TD");
    var td_b=document.createElement("TD");
    
    td_a.className="text";
    td_a.innerHTML="URL("+(mirror+1)+")";
    td_b.innerHTML="<textarea cols=\"40\" rows=\"20\" style=\"width:100%;height:70px;\" name=\"fURL[]\" class=\"text\"><\/textarea>";
    
    tr.setAttribute("id", "mirror-"+mirror);
    tr.appendChild(td_a);
    tr.appendChild(td_b);

    document.getElementById("tblMirror").getElementsByTagName("TBODY")[0].appendChild(tr);
    mirror++;

    if(mirror>0)document.getElementById("removeMirror").style.display="";
    return false;
}

function removeMirror(){

    mirror--;
    document.getElementById("tblMirror").getElementsByTagName("TBODY")[0].removeChild(document.getElementById("tblMirror").getElementsByTagName("TBODY")[0].lastChild);

    if(mirror<=0)document.getElementById("removeMirror").style.display="none";
    return false;
}

function dateUpdate(){

    var t=new Date();
    var h=t.getHours();
    var m=t.getMinutes();
    var s=t.getSeconds();
    var mn=t.getMonth()+1;
    var day=t.getDate();
    
    var date=(t.getYear()+1900)+"-"+(mn<10 ? "0"+mn:mn)+"-"+(day<10 ? "0"+day:day)+" "+(h<10 ? "0"+h:h)+":"+(m<10 ? "0"+m:m)+":"+(s<10 ? "0"+s:s);

    try{
	document.forms.fEdit.fDate.value=date;
    }catch(e){}

    return;
}

function checkState(id){

    var xmlReq=getXMLHttpOb();
    xmlReq.open("GET", prefix+"/checkState.php?cid="+id+"&rand="+Math.random(), true);

    xmlReq.onreadystatechange=function(){
        if(xmlReq.readyState==4){
	    if(xmlReq.status==200){
    	        var text=xmlReq.responseText;
    	        alert(text);
		if(text==0){
		    return true;
		} else {
		    alert("Статус данной позиции изменился!");
		    return false;
		}
	    }
	}
    }

    xmlReq.send(null);
    return true;
}

function vote(v, id){

    var cnt=0;
    var xmlReq=getXMLHttpOb();

    document.getElementById("wait-"+id).style.visibility="visible";

    xmlReq.open("GET", "/vote.php?item="+id+"&vote="+v+"&rand="+Math.random(), true);
    xmlReq.onreadystatechange=function(){
        if(xmlReq.readyState==4){
	    if(xmlReq.status==200){

		setTimeout(function(){
		    document.getElementById("wait-"+id).style.visibility="hidden";}, 300);
	    
		result=xmlReq.responseText;
		if(result=="-1"){
		    alert("Вы уже оценивали эту новость!");
		} else {
		    cnt=document.getElementById("voteCount-"+id);
		    cnt.innerHTML=parseInt(cnt.innerHTML)+1;
		    document.getElementById("rValue-"+id).innerHTML=result;
		    document.getElementById("rImg-"+id).src="/rating.php?r="+result+"&rand="+Math.random();
		}
	    }
	}
    }
    xmlReq.send(null);
}	    
