var isIE = window.navigator.userAgent.indexOf("MSIE")>-1;
var GlassWindow=null;
var oDiv=null;
var divHdr=null;
var divBdy=null;

function ajaxFunction()	{
	var xmlHttp;
	try {  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
	catch (e) {  // Internet Explorer
		try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
	  catch (e)	{
		  try {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
		catch (e) {
			alert("Jusu narsykle nepalaiko AJAX!");
			return false;
			}
		}
	}
	return xmlHttp;
}
function openwindow(URL) { 
	var availHeight = screen.availHeight; 
	var availWidth = screen.availWidth; 
	var x = 0, y = 0; 
	if (document.all) { 
	x = window.screenTop; 
	y = window.screenLeft; 
	} 
	else if (document.layers) { 
	x = window.screenX; 
	y = window.screenY; 
	} 
	var arguments = 'resizable=1,toolbar=1,location=1,directories=1, addressbar=1,scrollbars=1,status=1,menubar=1,top=0,left=0, screenX='+x+',screenY='+y+',width='+availWidth+',height='+availHeight; 
	newwindow = window.open(URL,'demo',arguments); 
} 
function Show1() {
	var Http = ajaxFunction();
	var company_type= document.getElementById('company_type0');
	if(company_type.checked) {
		ajaxfile='ccompany.php';
	} else {
		ajaxfile='vcompany.php';
	}
	var SendPost='';
	Http.open('post','index.php?ajaxfile='+ajaxfile, true);
	Http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	Http.onreadystatechange = function () {
		if (Http.readyState==4)
		{
			document.getElementById("order").innerHTML=Http.responseText;
		}
	}
	Http.send(SendPost);
}
function showFilial() {
    var filial_module= document.getElementById('filial_module');
    var filial= document.getElementById('filial');
    if(filial_module.checked) {
        filial.className="enamble";
    } else {
        filial.className="disable";
    }
}

function checkForm() {
	var ok=true;
	var header = '<p>';
	var header1 = '<p>';
	var name = document.getElementById('name');
	var company_type=document.getElementById('company_type1');
	var tel = document.getElementById('tele');
	var code = document.getElementById('code');
	var email = document.getElementById('email');
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var filtercode  = /^([0-9]{5,11})$/;
	var filtertel  = /^[a-zA-Z0-9\._\-\+\(\)\ ]+[0-9]{5,7}$/;
	var filtervat  = /^[A-Z\.\_]{2,3}[0-9]+$/;
	if (name.value.length<5) {
		name.focus();
		header+='Įrašykite įmonės pavadinimą!<br />';
		ok=false;
	}
		if (!filtercode.test(code.value) ) {
			code.focus();
			header+='Neteisingai įrašytas įmonės kodas!<br />';
			ok=false;
		}
	if (!filtertel.test(tel.value) || tel.value.length==0 ) {
			tel.focus();
			header+='Neteisingai įrašytas telefono numeris!<br />';
			ok=false;
		}
	if (!filter.test(email.value) || email.value.length==0) 	 {
		email.focus();
		header+='Neteisingas el. pašto adresas!<br />';
		ok=false;
	}
	if (!ok) {
		header+='</p>'
		AlertError(header,"","");
		return false;
	}
document.frmorder.submit();
}
function createCookie(name,value,seconds) {
    if (seconds) {
        var date = new Date();
        date.setTime(date.getTime()+seconds);
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var xname = name + "=";
    var sk = document.cookie.split(';');
    for(var i=0;i < sk.length;i++) {
        var tmp = sk[i];
        while (tmp.charAt(0)==' ') tmp = tmp.substring(1,tmp.length);
        if (tmp.indexOf(xname) == 0) return tmp.substring(xname.length,tmp.length);
    }
    return null;
}
function getScroll(){
    var yScroll;

    if (window.pageYOffset) {
        yScroll = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }
        return yScroll;
}
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;

	return window.undefined;
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
}
function ShowGlassWindow(show){
	if(GlassWindow==null){
		GlassWindow=document.createElement('div');
		with(GlassWindow.style){
			//стили экрана после создания
			display='none'; //невидим
			position='absolute'; //абсолютное позиционирование
			left = 0;
			top =0;
//			width = getViewportWidth()+"px";
//			height = getViewportHeight()+"px";
			width1 = getViewportWidth()+"px";
			height1 = getViewportHeight()+"px";
			width = document.body.offsetWidth;
			height = document.body.offsetHeight;
//			alert(Number(width));
			if (parseInt(width)<parseInt(width1)) {
				width=width1
			}
			if (parseInt(height)<parseInt(height1)) {
				height=height1
			}
			zIndex=1;
			background='#565656';
			filter='alpha(opacity=70)'; // IE
			opacity='0.70'; // FF
		}
		// добавлем созданный элемент в структуру документа
		document.body.appendChild(GlassWindow);

	}
	if(show){
		with(GlassWindow.style){
			// позиционируем экран в левый верхний угол экрана и площадью во весь экран браузера
			left = 0;
//			top = getScroll()+"px";
			top = "0px";

			//width = document.body.clientWidth;
			//height = document.body.clientHeight;
		}
	}
	GlassWindow.style.display=show?'block':'none';//отображаем/прячем экран
}
function ShowModalWindow(show, Header, Body, Footer){
	ShowGlassWindow(show); //показываем/скрываем экран
	// получим ссылку на форму диалога (элемент с id=modal)
//	if(oDiv==null || divBdy==null) {
	if(show) {
		oDiv=document.createElement("div");
		divHdr=document.createElement("div");
		divBdy=document.createElement("div");
		divFoo=document.createElement("div");
		document.body.appendChild(oDiv);
		oDiv.style.zIndex=2;
		//divHdr.innerHTML='<img style="vertical-align:middle"  src="info.gif">&nbsp;&nbsp;'+divHdr.innerHTML;
		//divHdr.innerHTML='<div align="right"><button class="btndeleteoff" title="Naikinti" id="delete" onmousedown="this.className=\'btndeleteon\'" onmouseup="this.className=\'btndeleteoff\'" onmouseout="this.className=\'btndeleteoff\'" onclick="ShowModalWindow(false, \'\',\'\', \'\')"></button></div>'+divHdr.innerHTML;
		divHdr.style.fontWeight='bold';
		divHdr.style.width='395px';
		divHdr.style.padding='10px';
		divHdr.style.paddingBottom='5px';
		divHdr.style.color='#990000';
		divHdr.style.background='#eff1f4';
		divHdr.style.zIndex=2;
		divBdy.style.zIndex=2;
		divFoo.style.zIndex=2;
		oDiv.appendChild(divHdr);
		divBdy.style.width='395px';
		divBdy.style.paddingLeft='10px';
		divBdy.style.paddingRight='10px';
		divBdy.style.color='#FFFFFF';
		divBdy.style.background='#eff1f4';
		oDiv.appendChild(divBdy);
		divFoo.style.fontWeight='bold';
		divFoo.style.width='395px';
		divFoo.style.padding='10px';
		divFoo.style.paddingTop='15px';
		divFoo.style.color='#990000';
		divFoo.style.background='#eff1f4';
		divFoo.style.zIndex=2;
		oDiv.appendChild(divFoo);
		oDiv.style.position="absolute";
		oDiv.style.visibility='hidden';
	}
	if(show){
		//createCookie('Position',getScroll());
		//window.scrollTo( 0, 0);
		var body = document.body;
//		width = getViewportWidth()+getScroll()+"px";
//		height = getViewportHeight()+"px";
		oDiv.style.top  = (Math.round((body.clientHeight - 450)/2))+'px';
		oDiv.style.left = Math.round((body.clientWidth/2)-200)+'px';
		oDiv.style.visibility='visible';
		divBdy.innerHTML=Body;
		divFoo.innerHTML=Footer;
		if (Header.indexOf("<")==0) {
			divHdr.innerHTML=Header;
		} else {
			divHdr.innerHTML='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td>'+Header+'</td><td align="right"><button class="btndelete" title="Uždryti" id="delete" onclick="ShowModalWindow(false, \'\',\'\', \'\')"></button></td></tr></table>';
		}
		oDiv.focus();
		document.body.style.overflow='hidden';
	}
	else {
  /*		var Position = readCookie('Position');
		if ( Position > 0 ) {
			window.scrollTo( 0, Position );
		}*/
		document.body.style.overflow='auto';
		oDiv.style.visibility='hidden';
	}
}
function AlertError(msg,title,btn) {
		if (btn.length<1) {
			btn="Taisyti";
		}
	var text='<div align="center" style="color:#FFFFFF; background-color:#a00000; padding:25px 0 25px 0;">'+msg+'</div>';
	var footer='<p align="center"><span class="buttonBorder"><input type="button" name="error" class="btnUpForm1" onclick="ShowModalWindow(false, \'\',\'\',\'\')" value="'+btn+'" /></span></p>'
		ShowModalWindow(true, title,text, footer);
}
