function GetByID(id) {
	if (document.all) {
		return document.all[id];
	} else {
		return document.getElementById(id);
	}
}

if (!Array.prototype.inArray) {
   Array.prototype.inArray=function(val) {
      for (key in this) {
         if (this[key]===val) {
            return true; // If you want the key of the matched value, change "true" to "key"
         }
      }
      return false;
   }
}

function RTRIM(tcString){
	var lnI = tcString.length-1;
	while (tcString.charAt(lnI)==' ' && lnI-->=0) {/* lnI--; */}
	return tcString.substring(0, lnI+1);
}
function LTRIM(tcString){
	var lnI = 0;
	while (tcString.charAt(lnI)==' ' && lnI++ < tcString.length-1) {/* lnI++; */}
	return tcString.substring(lnI, tcString.length);
}
function ValidCode(txtCode) {
	var llOk = true;
	with (txtCode) {
		value=LTRIM(RTRIM(value));
		if (value!='') {
			var num=parseInt(value);
			if (isNaN(num)) {
				alert(msg_2);
				value='';
				focus();
				llOk = false;
			} else if (value.length != 7 && value.length != 9) {
				alert(msg_3);
				focus();
				llOk = false;
			}
		}
	}
	return (llOk);
}
function ValidSubmit(theForm)
{
	with (theForm) {
		cId.value=LTRIM(RTRIM(cId.value));
		Company.value=LTRIM(RTRIM(Company.value));
		Person.value=LTRIM(RTRIM(Person.value));
		Trademark.value=LTRIM(RTRIM(Trademark.value));
		City.value=LTRIM(RTRIM(City.value));
		Street.value=LTRIM(RTRIM(Street.value));
		ClientId.value=LTRIM(RTRIM(ClientId.value));
		ClientPassw.value=LTRIM(RTRIM(ClientPassw.value));
		if (ClientId.value == '') { alert(msg_4); ClientId.focus(); return false; }
		if (ClientPassw.value == '') { alert(msg_5); ClientPassw.focus(); return false; }
		if (''+cId.value+Company.value+Person.value+Trademark.value == '') { alert(msg_6); cId.focus(); return false; }
		do_search.value = 'do'
	}
	return true;
}
function ShowHelp() {
	var wnd = open("help.htm", "_blank", "width=300,height=100,status=no,toolbar=no,menubar=no,location=no");
	return true;
}
function submit_link(string,companyid){
	with(document.forms["m1"]) {
		page.value = string;
		compnyid.value = companyid;
		submit();
	}
	return true;
}

function open_link(variableNames,variableValues){
	if (AJAX) {
		openAJAX(variableNames,variableValues);
	} else {
		var paramString = '';
		for(i=0; i<variableNames.length; i++) {
			paramString += escape(variableNames[i])+'='+escape(variableValues[i])+'&';
		}
		paramString = paramString.substring(0, (paramString.length-1));
		document.location='?'+paramString;
	}
	return true;
}


function olink(variable,req_type){
	if (!req_type) {
		req_type = 'get';
	}
	if (AJAX) {
		olinkAJAX(variable,req_type);
	} else {
		document.location = '?x='+variable;
	}
	return true;
}

function submitInfo(tcAction) {

	with (document.forms['info']) {

		action = tcAction;
		ClientId.value = LTRIM(RTRIM(document.forms[0].ClientId.value));
		ClientPassw.value = LTRIM(RTRIM(document.forms[0].ClientPassw.value));

		if (ClientId.value != '' && ClientPassw.value != '') submit();
		else alert(msg_7);

	}
}


function Preview(cPreviewId)
{ with(document.forms["frmPreview"]) {
	cId.value=cPreviewId;
	ClientId.value=parent.sql.document.m1.ClientId.value;
	ClientPassw.value=parent.sql.document.m1.ClientPassw.value;
	submit();
}
window.status="";
return true;
}
function showMsg() {
	el1 = GetByID("div1");
	el1.style.visibility = "hidden";
	el2 = GetByID("div2");
	el2.style.visibility = "visible";
	return true;
}
function bgon(tr) { if (tr && tr.className) tr.className = 'bgon'; }
function bgoff(tr, nRow) { if (tr && tr.className) tr.className = 'bg'+nRow ; }
function kalba(lang){
	document.m1.lang.value=lang;
	document.m1.submit();
}
/*
function show_main_info(companyid){
if (AJAX) {
openAJAX(new Array('page','companyid'),new Array('main_info',companyid));
} else {
with(document.forms['m1']){
companyid.value = companyid;
page.value = 'main_info';
submit();
}
}
}*/
function startSearch()
{
	window.moveTo(1,1);
	window.resizeTo(760,570);
	window.focus();

	if (document.forms.length > 0) {
		document.forms[0].ClientId.value = opener.parent.sql.document.m1.ClientId.value;
		document.forms[0].ClientPassw.value = opener.parent.sql.document.m1.ClientPassw.value;

		document.forms[0].view.value = "2";
		document.forms[0].target = "art135468337";
		document.forms[0].submit();
	}
	return true;
}


function submitArticles()
{ if (confirm('Nuo 2004.11.01 d. straipsniai mokami.\n\n                   Prašome patvirtinti,\nkad Jūs pasirinkote gauti MOKAMĄ informaciją apie įmonę'))
if (window.frames['art135468337'].document.forms.length>0) {
	var frmSelection = window.frames['art135468337'].document.forms[0];

	with (document.forms['m1']) {

		articleid.value = "";

		for (var i=0; i<frmSelection.length; i++)
		if (frmSelection.elements[i].type=='checkbox' && frmSelection.elements[i].checked && frmSelection.elements[i].name != 'chkAll')
		articleid.value += (frmSelection.elements[i].name.substring(3, frmSelection.elements[i].name.length)+',');

		if (articleid.value != "") {

			view.value = "3";
			target = "_blank";
			submit();
			return true;
		}
	}
}
}
function nextPage()
{
	with (document.forms[0]) {

		if (parseInt(CurrentPage.value,10)+1 > parseInt(parent.document.forms['frmMemo'].LastPage.value,10) ) {

			ClientId.value=parent.document.forms[0].ClientId.value;
			ClientPassw.value=parent.document.forms[0].ClientPassw.value;
			view.value = "2";

			target = '';
			submit();
			return true;
		}
		else history.forward();
	}
}

function submitOrder(tcDescending)
{ with (document.forms[0]) {
	ClientId.value=parent.document.forms[0].ClientId.value;
	ClientPassw.value=parent.document.forms[0].ClientPassw.value;
	view.value = "2";

	desc.value = tcDescending;

	fromid.value = "";
	CurrentPage.value = "1";
	parent.document.forms['frmMemo'].LastPage.value = "0";

	target = '';
	submit();
	return true;
}
}
function checkOne(toCheck, tlDirect)
{
	if ( !toCheck.checked && tlDirect) {
		toCheck.form.chkAll.checked = false;
	}
	var loRow = GetByID('row'+toCheck.name.substring(3, toCheck.name.length));
	if (loRow && loRow.className) {
		if ( loRow.className == 'bg0' ){
			loRow.style.backgroundColor = ( (toCheck.checked) ? '#a6caf0' : '#FFFFFF' )
		} else {
			loRow.style.backgroundColor = ( (toCheck.checked) ? '#a6caf0' : '#F0F0F0' );
		}
	}

}
function checkAll(toCheck)
{
	for (var i=0; i<toCheck.form.length; i++)
	if (toCheck.form.elements[i].type=='checkbox' && toCheck.form.elements[i].name != toCheck.name) {
		toCheck.form.elements[i].checked = toCheck.checked;
		checkOne(toCheck.form.elements[i], false);
	}
}

function recheckAll()
{
	if (document.forms.length>0)
	with (document.forms[0]) {

		if ( parseInt(CurrentPage.value,10) > parseInt(parent.document.forms['frmMemo'].LastPage.value,10) )
		parent.document.forms['frmMemo'].LastPage.value = CurrentPage.value;

		for (var i=0; i<document.forms[0].length; i++)
		if (elements[i].type=='checkbox' && elements[i].name != 'chkAll')
		checkOne(elements[i], false);
	}
}
function wait_window(show) {
	var wind;
	if (show) {
		wind = GetByID("container_wait");
		wind.style.display = "block";
		wind = GetByID("container");
		wind.style.display = "none";
	} else {
		wind = GetByID("container_wait");
		wind.style.display = "none";
		wind = GetByID("container");
		wind.style.display = "block";
	}
}

var old_elem="";
var time_ticket=false;

function show_info(elem) {
	el = GetByID(elem);
	if (old_elem != "") hide_time();
	if (time_ticket) {
		clearTimeout(time_ticket);
		time_ticket = false;
	}
	el.style.display = "block";
}

function hide_info(elem) {
	old_elem = elem;
	time_ticket=setTimeout("hide_time()", 500);
}

function hide_time() {
	old_el = GetByID(old_elem);
	old_el.style.display = "none";
	if (time_ticket) {
		clearTimeout(time_ticket);
		time_ticket = false;
	}
}

function openSearch(forma) {
	if (AJAX) {
		openSearchAJAX(forma);
		return false;
	} else {
		forma.submit();
		return true;
	}
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60; //valandos
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {

	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function setSpanVisibility(element) {
	var n = GetByID(element);
	if (n.style.display == 'none'){
		n.style.display = 'inline';
	}else{
		n.style.display = 'none';
	}
	document.images[element+'Img'].src = (n.style.display=="inline"?'/img/minus.gif':'/img/plus.gif');
}

var dienos = new Array(7);
dienos[0] = new Array(3);
dienos[0]['lt'] = "Sekmadienis";
dienos[0]['en'] = "Sunday";
dienos[0]['de'] = "Sonntag";

dienos[1] = new Array(3);
dienos[1]['lt'] = "Pirmadienis";
dienos[1]['en'] = "Monday";
dienos[1]['de'] = "Montag";

dienos[2] = new Array(3);
dienos[2]['lt'] = "Antradienis";
dienos[2]['en'] = "Tuesday";
dienos[2]['de'] = "Dienstag";

dienos[3] = new Array(3);
dienos[3]['lt'] = "Trečiadienis";
dienos[3]['en'] = "Wednesday";
dienos[3]['de'] = "Mittwoch";

dienos[4] = new Array(3);
dienos[4]['lt'] = "Ketvirtadienis";
dienos[4]['en'] = "Thursday";
dienos[4]['de'] = "Donnerstag";

dienos[5] = new Array(3);
dienos[5]['lt'] = "Penktadienis";
dienos[5]['en'] = "Friday";
dienos[5]['de'] = "Freitag";

dienos[6] = new Array(3);
dienos[6]['lt'] = "Šeštadienis";
dienos[6]['en'] = "Saturday";
dienos[6]['de'] = "Samstag";

function startclock(lang)
{
	var thetime=new Date();

	var nhours=thetime.getHours();
	var nmins=thetime.getMinutes();
	var nsecn=thetime.getSeconds();
	var nday=thetime.getDay();
	var nmonth=thetime.getMonth();
	var ntoday=thetime.getDate();
	var nyear=thetime.getYear();
	var AorP=" ";

nmonth+=1;	
	
if (nmonth<10) nmonth="0"+nmonth;
if (ntoday<10) ntoday="0"+ntoday;
 
if (nsecn<10)
 nsecn="0"+nsecn;
 
if (nmins<10)
 nmins="0"+nmins;


 nday = dienos[nday][lang];

if (nyear<=99)
  nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
 nyear+=1900;

document.clockform.clockspot.value=nday+", "+nyear+" "+nmonth+" "+ntoday+", "+nhours+":"+nmins+":"+nsecn;
 
setTimeout("startclock('"+lang+"')",1000);
} 

function hov(loc,cls){
   if(loc.className)
      loc.className=cls;
}

var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var inp = document.createElement('input');
	inp.style.width="200px";
	inp.type="text";
	fakeFileUpload.appendChild(inp);
	var but = document.createElement('input');
	but.value='Browse...';
	but.className='btn';
	but.type='button';
	fakeFileUpload.appendChild(but);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].getAttribute('noscript')) continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		if (x[i].value)
			x[i].onchange();
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

function mark(el){
	if( el.className == 'head_button'){
		el.style.color = "#7ab7da";
	}
}

function unmark(el){
	if( el.className == 'head_button'){
		el.style.color = "#ffffff";
	}
}

function setSize(){
	window.resizeTo(500, 300);
}

function noNews(){
	var n = GetByID('News');
	n.style.display = 'none';
}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function quicksearch(form){
	if (form.qs_input.value == '') {
		alert('Laukas neturi būti tuščias');
		return false;
	}
	if (form.qs_input.value.length < 2) {
		alert('Paieškos kriterijus negali būti trumpesnis nei 2 simboliai');
		return false;
	}
	form.screen_resolution.value = screen.width+'x'+screen.height;
	return true;
}
/*
<?php
$DB = NewADOConnection(ADODB_DB_SERVER);
$DB->Connect(ADODB_HOST, ADODB_SCRIPT_USER, ADODB_SCRIPT_PWD, ADODB_DATABASE);
$res = $DB->Execute("SELECT code FROM phone_city_codes ORDER BY length(code) desc");
$kodai = '';
while (!$res->EOF) {
	$kodai .= "'".$res->fields['code']."',";
	$res->MoveNext();
}
$res = $DB->Execute("SELECT code FROM countries_tel_codes");
$kodai_inter = '';
while (!$res->EOF) {
	$kodai_inter .= "'".$res->fields['code']."',";
	$res->MoveNext();
}
$DB->Close();
$kodai = trim($kodai,',');
$kodai_inter = trim($kodai_inter,',');
?>

function validatePhone(fieldas) {
	rez = validatePhoneText(fieldas.value);
	if (rez === false) {
		return false;
	} else {
		fieldas.value = rez;
		return true;
	}
}

function validatePhoneText(num2) {
	var num = new String(num2);
	var rez = new String(num2);

	num = num.replace(/[a-z,\.\(\)\s\-]/gi,'');
	if (num.length == 0) {
		rez = '';
		return '';
	}

	if (num.length < 8 && num.length != 4) {
		alert ('Numeris per trumpas');
		return false;
	}
	var re = /^[\d\+]{6,25}$/i;
	if (!re.test(num)) {
		alert ('Klaidingas numeris');
		return false;
	}

	if (num.length == 4) {
		rez = num;
		return rez;
	}
	var lietuviskas = false;
	var new_num = new String('');
	if (num.length == 8) {
		new_num += '+370 ';
		lietuviskas = true;
	} else {
		if (num.length == 9 && num[0] == '8') {
			new_num += '+370 ';
			num = num.replace('8','');
			lietuviskas = true;
		} else {
			var international = new Array(<?php echo $kodai_inter;?>);
			var kode_int_ok = false;
			for (key in international) {
				if (num.indexOf('+'+international[key]) == 0) {
					new_num += '+'+international[key]+' ';
					num = num.replace('+'+international[key],'');
					kode_int_ok = true;
					if (international[key] == '370') {
						lietuviskas = true;
					}
					break;
				}
			}

			if (!kode_int_ok && num[0] == '+') {
				alert ('Nepavyko nustatyti teisingo tarptautinio kodo!');
				return false;
			}
		}
	}
	if (lietuviskas && num.length != 8) {
		alert ('Numeris netinkamo ilgio.\nLietuviški numeriai su miesto kodu turi būti 8 simbolių ilgio');
		return false;
	}
	if (!lietuviskas) {
		new_num += num;
		rez = new_num;
		return rez;
	}
	if (num.indexOf('6') == 0 || num.indexOf('900') == 0 || num.indexOf('800') == 0 || num.indexOf('700') == 0 || num.indexOf('901') == 0) {
		var kodas = num.substr(0,3);
		new_num += kodas+' ';
		num = num.replace(kodas,'');
	} else {
		var city_codes = new Array(<?=$kodai?>);
		var kode_ok = false;
		for (key in city_codes) {
			if (num.indexOf(city_codes[key]) == 0) {
				new_num += city_codes[key]+' ';
				num = num.replace(city_codes[key],'');
				kode_ok = true;
				break;
			}
		}
		if (!kode_ok) {
			alert ('Nepavyko nustatyti teisingo miesto kodo!');
			return false;
		}
	}
	new_num += num;
	rez = new_num;
	return rez;
}
*/
