
window.onload = function () {
	document.documentElement.id = 'js';
}

function getHeight() {
	var h = document.getElementById('page');
	return h.offsetHeight;
}

function screnOffsetY() {
	var y = 0;
	y = (window.scrollY) ? window.scrollY : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	return y;
}

function popup(state) {
	if (!popup) var popup = document.getElementById('popupId');
	if (!blackness) var blackness = document.getElementById('blackness');
	blackness.style.height = getHeight()+'px';
	if (state=='show') {
		popup.style.display = 'block';
		popup.style.top = document.documentElement.clientHeight / 2 - popup.clientHeight / 2 + screnOffsetY() + 'px';
		blackness.style.display = 'block';
	} else if (state=='hide') {
		blackness.style.display = 'none';
		popup.style.display = 'none';
	}
}

var q = 1;
function addFile(e) {
	if (q<5) {
		var td = e.parentNode;
		var attach = document.createElement('div');
		attach.className = 'w-file';
		attach.innerHTML = '<input type="file" id="addphoto_photos1" class="file" name="foto[]" size="17" style="width: 165px;"/>';
		td.insertBefore(attach, e);
		q++;
	}
}

function getElementsByClass(searchClass, tag) {
	var classElements = new Array();
	if (!tag) tag = '*';
	var els = document.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


var showForm = false;
function LoginFormPV()
{
	switch(showForm)
	{
		case true:  document.getElementById('coupon').style.visibility = 'visible';
					document.getElementById('search').style.visibility = 'visible';
					document.getElementById('login').style.visibility = 'visible';
					document.getElementById('loginContaner').style.visibility = 'hidden';
					showForm = false;
					break;
		case false: document.getElementById('coupon').style.visibility = 'hidden';
					document.getElementById('search').style.visibility = 'hidden';
					document.getElementById('login').style.visibility = 'hidden';
					document.getElementById('loginContaner').style.visibility = 'visible';
					showForm = true;
					break;
	}
}

			/* ------ Authintification ------ */
function loginResponse(presp, ptext)
{	
	var divLog = document.getElementById('login');
	removeChlds('login');
			
	switch(presp.action)
	{
		case 'enter':
			if(presp.log)
			{
				LoginFormPV();
			
			
				var span = document.createElement('span');
				var spanText = document.createTextNode(presp.username + ' | ');
			
				span.appendChild(spanText);
				divLog.appendChild(span);
			
				var span2 = document.createElement('span');
				var a = document.createElement('a');
				a.href = "javascript: void(0);";
				a.onclick = function()
				{
					httpRequestPv('loginuser', {'login': document.getElementById('hLogin').value, 'passw': document.getElementById('hPassw').value, 'action': 'exit'}, 'loginResponse'); 
					return false;
				}
				var spanText2 = document.createTextNode('Выйти');
				a.appendChild(spanText2);
				span2.appendChild(a);
				divLog.appendChild(span2);
			}
			else document.getElementById('logError').innerHTML = ptext;
			break;
		case 'exit':
					var span3 = document.createElement('span');
					span3.style.marginRight = '10px';
					var span4 = document.createElement('span');
					
					var a2 = document.createElement('a');
					a2.href = "javascript: void(0);";
					a2.onclick = function()
					{
						LoginFormPV(); 
						return false;
					}
					var a2Text = document.createTextNode('Войти');
					a2.appendChild(a2Text);
					
					var a3 = document.createElement('a');
					a3.href = "/registeruser";
					var a3Text = document.createTextNode('Регистрация');
					a3.appendChild(a3Text);
					
					span3.appendChild(a2);
					span4.appendChild(a3);
					
					divLog.appendChild(span3);
					divLog.appendChild(span4);
					
					showForm = true;
					LoginFormPV();
					document.getElementById('logError').innerHTML = ' ';
			break;
		default:
	}
	//document.getElementById('logError').innerHTML = ptext;
}



