function ConfirmLangChange(Link)
{
	ChangeLang = confirm(MSG_ChangeLang);
	if(ChangeLang)
		window.location.href = Link.href;
	
	return ChangeLang;
}

function LangChange(Link)
{
	window.location.href = Link.href;
}

function ConfirmPersonDelete(elem)
{
	DeletePerson = confirmPersonDelete(MSG_DeletePerson, function () {
			deleteCurrentRow(elem);
	});
}

function CalculatePriceMSG()
{
	CalculatePerson = calculatePrice(MSG_PleaseWait, function () {
	});
}

function ConfirmOldPersonNotInsured()
{
	CalculatePerson = confirmOldPersonNotInsured(MSG_OldPersonNotInsured, function () {
	});
}

// алиас для getElementById
function _(ElementID)
{
	return document.getElementById(ElementID);
}

function ElementHide(ElementID)
{
	_(ElementID).style.display = "none";
	return true;
}

function ElementShow(ElementID)
{
	_(ElementID).style.display = "block";
	return true;
}

function ScrollDocumentToElement(ElementID)
{
	var ScrollToY = 0;
	
	Element = _(ElementID);
	
	while(Element != null)
	{
		ScrollToY += Element.offsetTop;
		Element = Element.offsetParent;
	}
	
	window.scrollTo(0, ScrollToY);
	
	return true;
}

function LayerShowHide(ElementID)
{
	Element = _(ElementID);
	
	if(Element.style.display != "block")
	{
		Element.style.display = "block";
	}
	else
	{
		Element.style.display = "none";
	}
}

var CurrentFilterOpenedID = -1;
function ShowFiltersPanel(Element)
{
	if(CurrentFilterOpenedID == Element.id)
	{
		_(CurrentFilterOpenedID).className = '';
		_('Filters_' + CurrentFilterOpenedID + '_handler').style.display = 'none';
		
		CurrentFilterOpenedID = -1;
	}
	else
	{
		if(CurrentFilterOpenedID != -1)
		{
			_(CurrentFilterOpenedID).className = '';
			_('Filters_' + CurrentFilterOpenedID + '_handler').style.display = 'none';
		}
		
		Element.className = 'selected_filter';
		_('Filters_' + Element.id + '_handler').style.display = 'block';
		_('Field_' + Element.id + '_main').focus(self);
		if(_('Field_' + Element.id + '_main').tagName == 'INPUT')
			_('Field_' + Element.id + '_main').select();
		
		CurrentFilterOpenedID = Element.id;
	}
}

function OCTASCConfirmationTextChange(Element)
{
	_('OCTASCConfirmationTextButton').disabled = !Element.checked;
}

function OCTASCConfirmationTextSubmit()
{
	_('OCTASCConfirmationTextForm').submit();
}

function OpenEditor(ElementID)
{
	w = 700;
	h = 700;
	s = 0;
	r = 0;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;
	
	url = "/admin/editor.php?ElementID=" + ElementID;
	
	target = "_blank";
	
	window.open(url, target, "toolbar=0,scrollbars=" + s + ",resizable=" + r + ",left=" + l + ",top=" + t + ",width=" + w + ",height=" + h);
}

function ShowWaitCursor()
{
	for(i = 0; i < document.getElementsByTagName('fieldset').length; i++)
	{
		document.getElementsByTagName('fieldset')[i].style.cursor = 'wait';
	}
	document.body.style.cursor = 'wait';
}

function CorrectPolicyDateBegin(Element, MinDate, MaxDate)
{
	// min
	mDateSplitted = MinDate.split('/');
	MinDate = mDateSplitted[2] + mDateSplitted[1] + mDateSplitted[0];
	
	cDateSplitted = Element.value.split('/');
	CurrDate = cDateSplitted[2] + cDateSplitted[1] + cDateSplitted[0];
	
	if(MinDate > CurrDate)
		Element.value = mDateSplitted[0] + '/' + mDateSplitted[1] + '/' + mDateSplitted[2];
	
	// max
	mDateSplitted = MaxDate.split('/');
	MaxDate = mDateSplitted[2] + mDateSplitted[1] + mDateSplitted[0];
	
	cDateSplitted = Element.value.split('/');
	CurrDate = cDateSplitted[2] + cDateSplitted[1] + cDateSplitted[0];
	
	if(MaxDate < CurrDate)
		Element.value = mDateSplitted[0] + '/' + mDateSplitted[1] + '/' + mDateSplitted[2];
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function getBrowserLocal() {
    BrowserDetect.init();
		return BrowserDetect.browser;
}



function CheckClientCenterName(Element, SpanElementID, RadioEmenetID)
{
	if(_(RadioEmenetID).checked)
	{
		if(Element.value != 0)
		{
			_(SpanElementID).style.color = 'black';
			return true;
		}
		else
		{
			_(SpanElementID).style.color = 'red';
			return false;
		}
	}
}

function CheckClientCenterNameKASKO(Element, SpanElementID)
{
	if(Element.value != 0)
	{
		_(SpanElementID).style.color = 'black';
		return true;
	}
	else
	{
	  _(SpanElementID).style.color = 'red';
		return false;
	}
}

function CheckDeliveryAddress(Element, SpanElementID, RadioEmenetID, IgnoreCheckbox)
{
	if(_(RadioEmenetID).checked || IgnoreCheckbox)
	{
		if(Element.value.length > 0)
		{
			_(SpanElementID).style.color = 'black';
			return true;
		}
		else
		{
			_(SpanElementID).style.color = 'red';
			return false;
		}
	}
	
	Element.value = (Element.value + '').toUpperCase();
}

function ChangePolicyPrice(Element)
{
	//PolicyPriceWithCurrency = PolicyPrice[Element.value].split(' ');
	//_('PolicyPriceHolder').innerHTML = (PolicyPriceWithCurrency[0] * 1.05) + ' ' + PolicyPriceWithCurrency[1];
	//_('PolicyPriceInternetHolder').innerHTML = PolicyPriceWithCurrency[0] + ' ' + PolicyPriceWithCurrency[1];
}


function ShowSkNaudaDetailed(TextareaID)
{
	w = 600;
	h = 600;
	s = 1;
	r = 0;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;
	
	url = "/admin/viewSkNauda.php?ElementID=" + TextareaID;
	
	target = "_blank";
	
	window.open(url, target, "toolbar=0,scrollbars=" + s + ",resizable=" + r + ",left=" + l + ",top=" + t + ",width=" + w + ",height=" + h);
}

function EditElements(SystemTreeID, ObjectKey)
{
	Form = document.getElementById('TablefiltersControlForm');
	
	w = 1000;
	h = 600;
	s = 1;
	r = 0;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;
	
	target = "_blank";
	
	for(i = 0; i < Form.length; i++)
	{
		if(Form[i].type == 'checkbox')
		{
			if(Form[i].checked)
			{
				window.open("./?SingleWindowEdit&SystemTreeID=" + SystemTreeID + "&Mode=EditObject&ObjectID=" + Form[i].name + "&ObjectKey=" + ObjectKey + '&Editable', target, "toolbar=0,scrollbars=" + s + ",resizable=" + r + ",left=" + l + ",top=" + t + ",width=" + w + ",height=" + h);
			}
		}
	}
	return false;
}


function DeleteElements(SystemTreeID, ObjectKey)
{
	Form = document.getElementById('TablefiltersControlForm');
	IDs = '0';
	if(confirm('Vai tiešām dzēst?'))
	{
		for(i = 0; i < Form.length; i++)
		{
			if(Form[i].type == 'checkbox')
			{
				if(Form[i].checked)
				{
					IDs += ',' + Form[i].name;
				}
			}
		}
		if(IDs == '0')
			alert('Jums ir jāizvelas vismaz vienu objektu vispirms!');
		else
			window.location = "./?SystemTreeID=" + SystemTreeID + "&Mode=DeleteObject&ObjectID=" + IDs + "&ObjectKey=" + ObjectKey;
	}
	return false;
}

function closeOneWinForm(hiddenVal, formName)
{
  var newAction = _('ReturnURL').value;
  document.forms.editform.action = newAction;
  window.location = _('ReturnURL').value;
  //_('editform').submit();
}
function ReturnToFirst(){
  if (LanguageID == "1"){
    location.replace("/lv/");
  } else {
    location.replace("/ru/");
  }
}

function Form_ChangePaymentType()
{
	document.getElementById('edit_form').submit();
}
