var isbkg = 0;

function showdebug(e,t) {
	var oldiv = getElById('debugpop');
	if (oldiv) var div = oldiv;
	else {
		var div = document.createElement('div');
		div.id = 'debugpop';
		div.style.position='absolute';
		div.style.backgroundColor='white';
		div.style.border='3px double silver';
		div.style.padding='2px';
		document.body.appendChild(div);
	}
	div.innerHTML = t;	
	movePop(div,e,'T','M');
}

createCookie("screenY", screen.height); 

function showError(t) {
	var errmsg = document.getElementById('errmsg');
	if (errmsg) {
		document.getElementById('errmsg').innerHTML = t;
		if (getElById('errmsgcont')) openReminder('errmsgcont');
	}
	else if (t != undefined && t != '') alert(t);
}

function openReminder(el) {
	var rmd = document.getElementById(el?el:'outer');
	if (rmd.getAttribute('status') == 'on') {
		rmd.style.visibility = 'hidden';
		rmd.setAttribute('status','off');
		isbkg--;
		if (!isbkg) createBkgfill('off');
	}
	else {
		if (!isbkg++) createBkgfill('on');
		var elsize = getElemSize(rmd.childNodes[0]);
		var w = getPageParams();
		var otop = 100;
		if (otop+elsize[1] > (w[3]+w[5])) otop = (w[3]+w[5]) - elsize[1];
		if (w[5]>otop) otop = w[5]+20;
		rmd.childNodes[0].style.marginTop=parseInt(otop + (20 * (isbkg - 1)))+'px';
		rmd.childNodes[0].style.marginLeft=parseInt(10 * (isbkg-1))+'px';
		rmd.style.visibility = 'visible';
		rmd.setAttribute('status','on');
	}
}

function createBkgfill(a) {
	if (a == 'off')	document.body.removeChild(document.getElementById('bkgfill'));	
	else if (a == 'on') {
		var pagesize = getPageSize();
		var ndiv = document.createElement('div');
		ndiv.id = 'bkgfill';
		ndiv.style.height=pagesize[1]+'px';
		setOpacity(ndiv, 50);
		document.body.appendChild(ndiv);

		if (navigator.appVersion.indexOf('MSIE 6.') != -1) {			
			// IE script to cover <select> elements with <iframe>s
			var ieMat=document.createElement('iframe');
			if(document.location.protocol == "https:")
				ieMat.src="//0";
			else if(window.opera != "undefined")
				ieMat.src="";
			else
				ieMat.src="javascript:false";
			ieMat.scrolling="no";
			ieMat.frameBorder="0";
			ieMat.style.width=ndiv.offsetWidth+"px";
			ieMat.style.height=ndiv.offsetHeight+"px";
			setOpacity(ieMat,0);
			ndiv.appendChild(ieMat);		
		}
		
	}
}

function checkpas(f) {
	if (f.opas) {
		var op = clearSpaces(f.opas.value);
		if (op.length <= 0) {
			showError('Please, type in your old password.');
			return false;
		}
	}
	var p = clearSpaces(f.pas.value);
	var p2 = clearSpaces(f.pas2.value);
	var err = document.getElementById('perr');
	if (p.length < 6) {
		showError('New password must be at least 6 symbols long!');
		return false;
	}
	else if (p != p2) {
		showError('New password in both fields must be the same!');
		return false;
	}
	else return true;
}
function checkEm(f) {
	var em = f.mail;
	var p = f.pas;
	if (!chMail(em.value)) return false;
	if (p) if (f.pas.value.length < 6) return false;
	return true;
}
function chMail(m) {
	var myregexp = new RegExp("^(?:\\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}\\b)$", "i");
	var match = myregexp.exec(clearSpaces(m));
	if (match == null) return false;
	return true;		
}
function checkinfodesk(f) {
	if (clearSpaces(f.text.value).length == 0) {
		showError('Type in your message');
		return false;
	}
	if (!f.iaccept.checked) {
		showError('To continue, you have to accept the terms');
		return false;
	}
	if (parseInt(f.code.value) > 999999) {
		openReminder('infodeskbox');
		infopreview(true); 
		return false;		
	}
	else {
		showError('To continue You have to purchase a code and write down it in the Code field');
		return false;
	}	

}

function infopreview(ends) {
	var f = document.getElementById('addinfo');
	var title = f.title.value;
	if (title.length > 0) {
		title = title.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	}
	var text = f.text.value;
	if (text.length > 0) {
		text = bb_parse(text);
		text = text.replace(/\n/g,'<br />');
	}
	document.getElementById('previewtitle').innerHTML = title;	
	document.getElementById('previewtext').innerHTML = text;	
	document.getElementById('previewtext').style.overflow='auto';
	if (ends != null) {
		getElById('prwb').style.display='block';
		getElById('prwbn').style.display='none';
	}
	openReminder('infopreview');
}

function cAnketa(el,types,minl,maxl,tips) {
	var name = el.name; var ok = false; var procs = getElById('procenti');
	switch(types) {
		case 1:
			if (el.checked) ok = true;
		break;
		case 3:		
			if (clearSpaces(el.value).length >= minl && clearSpaces(el.value).length <= maxl) ok = true;
			break;
		case 2:
			if (clearSpaces(el.value).length >= minl && clearSpaces(el.value).length <= maxl) ok = true;
			if (tips == 'int') { if (el.value.match(/[^\d]/) != null) ok = false; }
			else if (tips == 'float') { if (el.value.match(/[^\d.,]/) != null) ok = false; }
			// else if (tips == 'str') { if (el.value.match(/[abcdefghijklmnopqrstuvwxyz]/) == null) ok = false; }
			break;
	}
	getElById(name+'__i').src=ok?'images/ok.gif':'images/cancel.gif';
	getElById(name+'__c').value=ok?1:'';
	setPercents();
}

function checkIzl(el) {
	var txt = el.value; txt2 = '';
	for (var i = 0; i < txt.length; i++) {
		if ('0123456789'.indexOf(txt.substr(i,1)) != -1) txt2 += txt.substr(i,1);
	}
	el.value = txt2;
	if (txt2.length >= el.getAttribute('maxlength')) getElById('fsubmit').focus();
}