
// ..................................................................

var httpCommon = MP_getHTTPObject();

// ..................................................................

function MP_getHTTPObject()
{
	var xmlhttp;

	/*@cc_on
	@if(@_jscript_version >= 5)
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E)
			{
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

// ..................................................................

valSearch = function(form)
{
	if(!form.query.value.length) { form.query.focus(); return false; }

	return true;
}

// ..................................................................

function getImageY(imgElem)
{
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while(tempEl != null)
	{
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}

	return yPos;
}

// ..................................................................

function getImageX(imgElem)
{
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
	while(tempEl != null)
	{
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}

	return xPos;
}

// ..................................................................

showcsform = function()
{
	x = getImageX(document.getElementById('csimage')) - 10;
	document.getElementById('csform').style.left = x + "px";
	document.getElementById('csform').style.display = "block";

	return false;
}

// ..................................................................

showqrform = function()
{
	x = getImageX(document.getElementById('csimage')) + 189;
	document.getElementById('qrform').style.left = x + "px";
	document.getElementById('qrform').style.display = "block";

	return false;
}

// ..................................................................

function showLoading(is_bankproducts)
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	document.getElementById("pleasewaitTitle").innerHTML = (is_bankproducts) ? "Обработка данных может занять около минуты.<br />Пожалуйста, подождите&#133;" : "Пожалуйста, подождите&#133;";

	document.body.style.background = 'url("/images/blank.gif") #e4e5e6 no-repeat';
	document.body.style.backgroundAttachment = 'fixed';

	var sels = document.getElementsByTagName('select');
	for(i = 0; i < sels.length; i++)
	{
		sels[i].style.visibility = "hidden";
	}

	document.getElementById("pleasewait").style.height = pageHeight + "px";
	document.getElementById("pleasewait").style.display = "block";

	document.getElementById("pleasewaitText").style.display = "block";
	document.getElementById("pleasewaitCard").style.display = "block";
}

// ..................................................................

function hideLoading()
{
	document.getElementById("pleasewait").style.display = "none";
}

// ..................................................................

function MP_getWindowSize()
{
	var x, y;
	if(self.innerHeight)
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight)
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	retval = new Object;
	retval.x = x;
	retval.y = y; 

	return retval;
}

// ..................................................................

function prepareCardnumber(n)
{
	n = als.number.read(n);

	var frac = Number(n).toFixed(2).split('.');
	var aliq = frac[0].split('');
	var result = aliq[0];
	for (var i = 1; i < aliq.length; i++)
	{
		if (i % 4 == 0) result += ' ';
		result += aliq[i];
	}

	return result;
}

// ..................................................................

function valSearch(form)
{
	if(!form.query.value.length) { form.query.focus(); return false; }

	document.location.href = "/search/?query=" + form.query.value;

	return false;
}

// ..................................................................


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.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;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function change_region(element) {    
	var myRandomNumber = Math.floor(Math.random()*1000000);
	var city=$("#city_id");  
	if(city)  city.load("/ajax/load_cities.php?region_id="+element.value+"&rnd="+myRandomNumber);
	var district=$("#district_id");
	if(district) district.load("/ajax/load_districts.php?city_id=0"+"&rnd="+myRandomNumber); 
	var office=$("#office_id");  
	if(office)  office.load("/ajax/load_offices.php?district_id=0"+"&rnd="+myRandomNumber); 
}

function change_city(element) {    
	var myRandomNumber = Math.floor(Math.random()*1000000);
	var district=$("#district_id");  
	if(district)  district.load("/ajax/load_districts.php?city_id="+element.value+"&rnd="+myRandomNumber); 
	var office=$("#office_id");  
	if(office)  office.load("/ajax/load_offices.php?district_id=0"+"&rnd="+myRandomNumber); 
}

function load_districts() {    
	var myRandomNumber = Math.floor(Math.random()*1000000);
	var district=$("#district_id");  
	if(district)  district.load("/ajax/load_districts.php?city_id=1"+"&rnd="+myRandomNumber);  
}

function change_district(element) {    
	var myRandomNumber = Math.floor(Math.random()*1000000);
	var office=$("#office_id");  
	if(office)  office.load("/ajax/load_offices.php?district_id="+element.value+"&rnd="+myRandomNumber); 
}
