var isIE = window.navigator.userAgent.indexOf("MSIE")>-1;
var GlassWindow = null;
var Dialog = null;
var htmlRegForm = null;
var htmlEnterForm = null;
var waitIcon = null

function pass_restore()
{
	popup('restore.php')

}
function popup(url) 
{
 var width  = 500;
 var height = 200;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function ShowRegForm(show){
	if(htmlRegForm == null){
		ShowModalWindow(show);
		setWaitIcon(Dialog);
		var loader = GetAJAXLoader();
		if(loader === null){ return;}
		loader.onreadystatechange = function(){
			try {
				if (loader.readyState == 4) {
					if (loader.status == 200) {
						htmlRegForm = loader.responseText;
						clearContent(Dialog);
						ShowRegForm(true);
					}
				}
			}
			catch( e ) { }
		}
		loader.open("GET", '/scripts/getregform.php?r='+Math.random(), true);
		loader.send(null);
		return false;
	}
	Dialog.innerHTML = htmlRegForm;
	ShowModalWindow(show);
	return false;
}
function ShowEnterForm(show){
	if(htmlEnterForm == null){
		ShowModalWindow(show);
		setWaitIcon(Dialog);
		var loader = GetAJAXLoader();
		if(loader === null){ return;}
		loader.onreadystatechange = function(){
			try {
				if (loader.readyState == 4) {
					if (loader.status == 200) {
						htmlEnterForm = loader.responseText;
						clearContent(Dialog);
						ShowEnterForm(true);
					}
				}
			}
			catch( e ) { }
		}
		loader.open("GET", '/scripts/getentergform.php?r='+Math.random(), true);
		loader.send(null);
		return  false;
	}
	Dialog.innerHTML = htmlEnterForm;
	ShowModalWindow(show);
	return false;
}
function clearContent(obj){
	while(obj.childNodes[0]) obj.removeChild(obj.childNodes[0]);
}
function setWaitIcon(obj){
	clearContent(obj);
	if(waitIcon == null){
		waitIcon = document.createElement('IMG');
		with(waitIcon) { src = '/imgs/loading.gif'; width=100; height=60; alt='Ждите'; align='middle'; }
	}
	Dialog.appendChild(waitIcon);
}

function ShowGlassWindow(show){
	if(GlassWindow==null){
		GlassWindow=document.createElement('DIV');
		GlassWindow.className = 'glass';
		with(GlassWindow.style){
			if(isIE){
				backgroundColor = '#000';
				filter="progid:DXImageTransform.Microsoft.Alpha(Opacity=80, Style=0)";
			}
			else backgroundImage = 'url(/imgs/alpha40.png)';
		}
		document.body.appendChild(GlassWindow);
	}
	if(show){
		var s = getDocumentSize();
		with(GlassWindow.style){ left = top = 0; width = s[0]+'px'; height = s[1]+'px'; }
	}
	GlassWindow.style.display = show ? 'block' : 'none';
}
function ShowModalWindow(show){
	ShowGlassWindow(show);
	if(Dialog === null) Dialog=document.getElementById('modal');
	if(!Dialog){
		 Dialog = document.createElement('DIV');
		 Dialog.className='modal';
		 Dialog.id='modal';
		 document.body.appendChild(Dialog);
	}
	if(show){
		Dialog.style.display='block';
		CenterWindow(Dialog);
	}
	else Dialog.style.display='none';
}
function CenterWindow(win){
	var sz = getElementPosition(win);
	var c = getClientCenter();
	win.style.left = (c[0]-sz.width/2)+'px';
	win.style.top  = (c[1]-sz.height/2)+'px';
	win.focus();
}

/* POSITION FUNCTIONS */
function getElementPosition(elem){
    var w = elem.offsetWidth, h = elem.offsetHeight, l = 0, t = 0;
    while (elem)    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {"left":l, "top":t, "width": w, "height":h};
}

function getDocumentSize(){
	return [
	document.body.scrollWidth > document.body.offsetWidth ? document.body.scrollWidth : document.body.offsetWidth,
	document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight
	];
}
function getClientSize(){
	if(document.compatMode=='CSS1Compat') return [document.documentElement.clientWidth, document.documentElement.clientHeight];
	else return [document.body.clientWidth, document.body.clientHeight];
}
function getDocumentScroll(){
	return [
	self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) 
		|| (document.body && document.body.scrollLeft),
	self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) 
		|| (document.body && document.body.scrollTop)
	];
}

function getClientCenter(){
	var sizes = getClientSize();
	var scrl = getDocumentScroll();
	return [parseInt(sizes[0]/2)+scrl[0], parseInt(sizes[1]/2)+scrl[1]];
}

function GetAJAXLoader(){
	var req=null;
   	if (typeof(window.XMLHttpRequest)!='undefined') { try { req = new XMLHttpRequest(); } catch (e){ alert("Ошибка при получении AJAX загрузчика"); } }
   	else if (typeof(window.ActiveXObject)!='undefined') {
       	try { req = new ActiveXObject('Msxml2.XMLHTTP');} catch (e){
            try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e){ alert("Ошибка при получении AJAX загрузчика"); }
		}
	}
	else alert("Браузер не поддерживает AJAX");
	return req;
}

/* REGFORM FUNCTIONS */
function switchRegBlock(part){
	var regPPart = document.getElementById('PPart');
	var regJPart = document.getElementById('JPart');
	regPPart.style.display = part=='P' ? 'block' : 'none';
	regJPart.style.display = part=='J' ? 'block' : 'none';
	CenterWindow(Dialog);
}

function CheckRegForm(f){
	if (isEmpty(f.login.value)){ alert("Укажите ваш логин!"); f.login.focus(); return false; }
	if (isEmpty(f.email.value) || !isEmail(f.email.value) || f.email.value.length< 8) { alert("Вы неправильно ввели E-mail!"); f.email.focus(); return false; }
	if (isEmpty(f.YP_PHONE.value)){ alert("Укажите ваш телефон!"); f.YP_PHONE.focus(); return false; }
	var SFORM = GetRadioValue(f.SFORM);
	if(SFORM=='P'){
		if (isEmpty(f.YP_NAME_P.value)) { alert("Вы не указали Ф.И.О."); f.YP_NAME_P.focus(); return false; }
	}
	else{
		if (isEmpty(f.YP_NAME_J.value)) { alert("Вы не указали название организации"); f.YP_NAME_J.focus(); return false; }
		if (isEmpty(f.YP_INN.value)) { alert("Вы не указали ИНН"); f.YP_INN.focus(); return false; }
		if (isEmpty(f.YP_BANK.value)) { alert("Вы не указали название банка"); f.YP_BANK.focus(); return false; }
		if (isEmpty(f.YP_ACC.value)) { alert("Вы не указали номер расчетного счета"); f.YP_ACC.focus(); return false; }
		if (isEmpty(f.YP_CACC.value)) { alert("Вы не указали номер корр. счета"); f.YP_CACC.focus(); return false; }
		if (isEmpty(f.YP_BIC.value)) { alert("Вы не указали БИК банка"); f.YP_BIC.focus(); return false; }
		if (isEmpty(f.YP_KPP.value)) { alert("Вы не указали БИК банка"); f.YP_KPP.focus(); return false; }
	}
	RegUser(f);
	return false;
	f.submit();
}
function RegUser(f){
	var url = window.location.href;
	var post = 'login='+encodeURIComponent(f.login.value)+'&email='+encodeURIComponent(f.email.value)+'&YP_PHONE='+encodeURIComponent(f.YP_PHONE.value);
	var SFORM = GetRadioValue(f.SFORM);
	if(SFORM=='P'){
		post += '&SFORM=P&YP_NAME='+encodeURIComponent(f.YP_NAME_P.value);
	}
	else{
		post += '&SFORM=J&YP_NAME='+encodeURIComponent(f.YP_NAME_J.value);
		post += '&YP_INN='+encodeURIComponent(f.YP_INN.value);
		post += '&YP_BANK='+encodeURIComponent(f.YP_BANK.value);
		post += '&YP_ACC='+encodeURIComponent(f.YP_ACC.value);
		post += '&YP_CACC='+encodeURIComponent(f.YP_CACC.value);
		post += '&YP_BIC='+encodeURIComponent(f.YP_BIC.value);
		post += '&YP_KPP='+encodeURIComponent(f.YP_KPP.value);
	}
	var loader = GetAJAXLoader();
	if(loader===null){ return;}
	loader.onreadystatechange = function(){
		try {
			if (loader.readyState == 4) {
				if (loader.status == 200) {
//					alert(loader.responseText);
					try{ eval(loader.responseText); } catch(e) {};
				}
			}
		}
		catch( e ) { }
	}
	loader.open("POST", url, true);
	loader.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	loader.setRequestHeader("Ajax-Request", 'registration');
	loader.send(post);
}
/* ENTER FUNCTIONS */

function CheckEnterForm(f){
	if (isEmpty(f.login.value)){ alert("Укажите ваш логин!"); f.login.focus(); return false; }
	if (isEmpty(f.password.value)){ alert("Укажите пароль!"); f.password.focus(); return false; }
	EnterUser(f);
	return false;
	f.submit();
}
function EnterUser(f){
	var url = window.location.href;
	var post = 'login='+encodeURIComponent(f.login.value)+'&password='+encodeURIComponent(f.password.value);
	if(f.saveme.checked) post += '&saveme=1';
	var loader = GetAJAXLoader();
	if(loader===null){ return;}
	loader.onreadystatechange = function(){
		try {
			if (loader.readyState == 4) {
				if (loader.status == 200) {
					try{ eval(loader.responseText); } catch(e) {};
				}
			}
		}
		catch( e ) { }
	}
	loader.open("POST", url, true);
	loader.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	loader.setRequestHeader("Ajax-Request", 'login');
	loader.send(post);
}



/* COMMON FUNCTIONS  */
function GetRadioValue(o){
	if(typeof(o.value)!='undefined') return o.value;
	if(typeof(o.length)=='undefined') return '';
	for(var i=0; i<o.length; i++) if(o[i].checked) return o[i].value;
	return '';
}

function trim(str){
	return str.length>0 ? str.replace( /(^\s+|\s+$)/, "" ) : "";
}

function isEmpty (txt){ return trim(txt)==''; }

function DrawEmail(login, stub, server, name){
	email  = login+'@'+server;
	url = 'mailto:'+email;
	if(!name) name=email;
	document.write('<a href="'+url+'">'+name+'</a>');
}

function isEmail(email) {
	var arr1 = email.split("@");
	if (arr1.length != 2) return false;
	else if (arr1[0].length < 1) return false;
	var arr2 = arr1[1].split(".");
	if (arr2.length < 2) return false;
	else if (arr2[0].length < 1) return false;
	return true;
}

function XOR(){
	var n = arguments.length;
	var formname = n==0 ? 'del_form' : arguments[0];
	var f = document.forms[formname];
	var name = n<2 ? 'ids[]' : arguments[1];
	for(var i=0; i<f.elements.length; i++){
		var e = f.elements.item(i); 
		if(e.tagName == 'INPUT' && e.name == name) e.checked = e.checked ? false : true;
	}
	return false;
}

