IE = navigator.appName=="Microsoft Internet Explorer";
NS = navigator.appName=="Netscape";
bVer = parseInt(navigator.appVersion);
var newWin=null;

function PopUp(loc_ie, name, width, height, prodname) {
	if (window.NewWindow == null || window.NewWindow.closed)
	{
		var _params = "width="+width+",height="+height+",resizable=no,status=no,scrollbars=0";
		loc_ns = loc_ie;
		if (bVer >= 4) {
				 _left = ( (screen.width-width) >>1 );
				 _top = ( (screen.height-height) >>1 );
		} else {
				 _left = ( (800-width) >>1 );
				 _top = ( (600-height) >>1 );
		}
		if (IE) _params += ",top=" + _top + ",left=" + _left;
		else if (NS) _params += ",screenX=" + _left + ",screenY=" + _top;
		else _params += ",screenX=" + _left + ",screenY=" + _top;
		newWin = window.open(loc_ie, name, _params);
		newWin.document.open();
		newWin.document.write('<html><head><title>'+prodname+'</title></head><body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0><img src='+loc_ie+' height="'+height+'" width="'+width+'" onClick="parent.self.close(); return false" style="cursor:pointer" title="Закрыть окно" alt="'+prodname+'"></body></html>');
		newWin.document.close();
		if ( newWin!=null )
				newWin.focus();
	}
	else
	{
		window.NewWindow.location.href = loc_ie;			
		window.NewWindow.focus();
	}
}

function PopUpAlert(obj, buttonid){

	var popupie = document.getElementById("popupie");
	/*var popup = document.getElementById(obj);*/
	var dark = document.getElementById("dark");
	popupie.style.display = "block";
	/*popup.style.display = "block";*/
	dark.style.display = "block";
	if (document.getElementById("confirmbuybuttonparam")){
		document.getElementById("confirmbuybuttonparam").value = buttonid;
	}
	
	
}

function HidePopUpAlert(obj){
	var dark = document.getElementById("dark");
	dark.style.display = "none";
	var popupie = document.getElementById("popupie");
	popupie.style.display = "none";
	
}

function OrderButtonSend(){
	var id = '';
	if (document.getElementById('confirmbuybuttonparam')) id = document.getElementById('confirmbuybuttonparam').value;
	if (id) if (document.getElementById(id)) checkOrderNum(document.getElementById(id));
}
function ReloadAlert(obj) {
	document.getElementById('popupie').innerHTML += "";
}

function FeedbackShowForm(){
	var waitobj = document.getElementById("fbwait");
	var buttobj = document.getElementById("fbbutt");
	var sentobj = document.getElementById("fbsent");
	sentobj.style.display = "none";
	waitobj.style.display = "none";
	buttobj.style.display = "block";
}

function SendTheFeed(obj){
	if (trim(obj.funame.value)==""){ alert('Представьтесь, пожалуйста'); return false; }
	if (obj.fumail)
		if (!emailCheck(obj.fumail.value)){ alert('Укажите, пожалуйста, корректный адрес электронной почты'); return false; }
	if (trim(obj.futext.value)==""){ alert('Введите текст сообщения'); return false; }
	if (trim(obj.fuantispam.value)==""){ alert('Введите число с картинки'); return false; }
	var waitobj = document.getElementById("fbwait");
	var buttobj = document.getElementById("fbbutt");
	var sentobj = document.getElementById("fbsent");
	var date=new Date();
	buttobj.style.display = "none";
	sentobj.style.display = "none";
	waitobj.style.display = "block";
	requrl = "/inc/sendfeedback.php?funame="+encodeURI(obj.funame.value)+"&futext="+encodeURI(obj.futext.value)+"&fuantispam="+encodeURI(obj.fuantispam.value)+"&fuid="+encodeURI(obj.fuid.value)+"&futype="+encodeURI(obj.futype.value)+"&uniq=" +date.getTime();
	if (obj.fumail) requrl = requrl+"&fumail="+encodeURI(obj.fumail.value);
	if (obj.fuphone) requrl = requrl+"&fuphone="+encodeURI(obj.fuphone.value);
	var XHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	if (XHttpRequest){
		XHttpRequest.onreadystatechange = function(){ 
			if (XHttpRequest.readyState == 4) 
				if (XHttpRequest.status == 200){
					var req = XHttpRequest.responseText;
					if (req==""){
						sentobj.style.display = "block";
						waitobj.style.display = "none";
						buttobj.style.display = "none";
						obj.futext.value = "";
						obj.fuantispam.value = "";
						document.getElementById("antispamimg").src = "/script/antispam.php?nasc=1";
					}
					else{
						if (req=="noname") alert("Представьтесь, пожалуйста");
						if (req=="nomail") alert("Укажите, пожалуйста, корректный адрес электронной почты");
						if (req=="notext") alert("Введите текст сообщения");
						if (req=="nocode") alert("Неверное число с картинки");
						FeedbackShowForm();
					}
					return true;
				}
				else{
					alert("Ошибка. Попробуйте позднее");
					buttobj.style.display = "block";
					waitobj.style.display = "none";
					sentobj.style.display = "none";
					return false;
				}
		};
		XHttpRequest.open("GET", requrl, true);
		XHttpRequest.send("null");
	}
}

function	emailCheck (emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) return false;
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++)	if (user.charCodeAt(i)>127) 	return false;
	for (i=0; i<domain.length; i++)	if (domain.charCodeAt(i)>127)	return false;
	if (user.match(userPat)==null) 	return false;
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {	for (var i=1;i<=4;i++) if (IPArray[i]>255) return false;
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) if (domArr[i].search(atomPat)==-1)	return false;
	if (checkTLD && domArr[domArr.length-1].length!=2 &&	domArr[domArr.length-1].search(knownDomsPat)==-1)			return false;
	if (len<2)		return false;
	return true;
}

function	trim(s)
{
	return 	s.replace(/^(\s*)/,"$`").replace(/(\s*)$/,"$'");
}
