function OpenNotesSession_old(user, pass){
		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("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	if (xmlHttp){
  		xmlHttp.open("GET", "/names.nsf?login&username=" + user + "&password=" + pass, false);
  		xmlHttp.send(null);
  		var result = xmlHttp.responseText;
		var newURL = document.location.toString();
		if (newURL.indexOf('ls=1')>-1){
			document.location = newURL.split('?')[0];
		}else{
			document.location = newURL;
		}
//		document.location = document.location;
//		document.location = newURL;
	}else{
		return false;
	}
}

function OpenNotesSession(user, pass){
		$.post('/names.nsf?login', {username: user, password:pass}, function(data){
				logged=false;
				var cookies = document.cookie.split(';');
				for (i=0; i<cookies.length; i++){
					if (cookies[i].split('=')[0].indexOf('DomAuthSessId') > -1){
						logged=true;
					}
				}
				if (logged){
					alert('logged');
					document.location = document.location;
				}else{
					alert('Problem with login');
				}

		})

}


function getData_post(url, params){
	$.post(url, {parameters:params.join('&')}, function(result){
		result = result.split('!~')[1].split('~');
		alert(result[0] + ':' + result[1]);
		$.post('/names.nsf?login', {username: result[0], password:result[1]}, function(data){
				alert('in second func');
				logged=false;
				var cookies = document.cookie.split(';');
				for (i=0; i<cookies.length; i++){
					if (cookies[i].split('=')[0].indexOf('DomAuthSessId') > -1){
						logged=true;
					}
				}
				if (logged){
					alert('logged')
					document.location = document.location;
				}else{
					alert('Problem with login');
				}

		})
	})
}


function getData(url, params){
			$.ajax({async: false,
			type: "POST",
			url: url, 
			data: params.join('&'), 
			success: function(result){
				result = result.split('!~')[1].split('~');
				//alert(result[0] + ':' + result[1]);
/*				$.ajax({async: false,
						url: '/names.nsf?login', 
						data: 'username=' + result[0] + '&password=' + result[1], 
						success: function(data){
							alert('in second func');
							logged=false;
							var cookies = document.cookie.split(';');
							for (i=0; i<cookies.length; i++){
								if (cookies[i].split('=')[0].indexOf('DomAuthSessId') > -1){
									logged=true;
								}
							}
							if (logged){
								alert('logged')
								document.location = document.location;
							}else{
								alert('Problem with login');
							}
						},
						error: function(xhr, err, e){alert("inner: username=" + result[0] + "&password=" + result[1]);}
				})
*/				OpenNotesSession_old(result[0], result[1]);
			}
		})
}


function getData_old(url, params){

	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("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	if (xmlHttp){
		if (url.indexOf("?") > -1){
			url = url + "&" + params.join("&");
		}else{
			url = url + "?" + params.join("&");
		}
//		alert(url);
  		xmlHttp.open("GET",url ,false);
  		xmlHttp.send(null);
  		var result = xmlHttp.responseText;
		result = result.split('!~')[1]
		var userInfo = result.split("~");
		var response = OpenNotesSession(userInfo[0], userInfo[1]);
		if(userInfo[2] == '') {
//			alert('noMail');
		}
//		document.location = document.location;
		return response
	}else{
		return false;
	}
}

function LogOut(){
		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("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	if (xmlHttp){
  		xmlHttp.open("GET", "/names.nsf?logout");
  		xmlHttp.send(null);
	}else{
		return false;
	}
}

function doLogin(user, pass, infoDiv){
		infoDiv.css('display', 'none');
		
		if (user.val() == '') {
			user.addClass('xspWrongInputFieldEditBox');
			infoDiv.html('\u041C\u043E\u043B\u044F \u0432\u044A\u0432\u0435\u0434\u0435\u0442\u0435 \u043F\u043E\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043B\u0441\u043A\u043E \u0438\u043C\u0435 ...');
			infoDiv.show('normal', function (){user.focus();});
			return false;
		}else{
			user.removeClass('xspWrongInputFieldEditBox');
		}
		if (pass.val() == '') {
			pass.addClass('xspWrongInputFieldEditBox');
			infoDiv.html('\u041C\u043E\u043B\u044F \u0432\u044A\u0432\u0435\u0434\u0435\u0442\u0435 \u043F\u0430\u0440\u043E\u043B\u0430 ...');
			infoDiv.show('normal', function (){pass.focus();});
			return false;
		}else{
			pass.removeClass('xspWrongInputFieldEditBox');;
		}
		infoDiv.html('');
		infoDiv.hide('normal');
		$.ajax({async: false,
			type: "POST",
			url: '/names.nsf?login', 
			data: 'username=' + user.val() + "&password=" + pass.val(), 
			success: function(result){
				logged=false;
				var cookies = document.cookie.split(';');
				for (i=0; i<cookies.length; i++){
					if (cookies[i].split('=')[0].indexOf('DomAuthSessId') > -1){
						logged=true;
					}
				}
				if (logged){
					var newURL = document.location;
					newURL += '?';
					newURL = newURL.split('?')[0];
//					document.location = document.location;
					document.location = newURL;
				}else{
					infoDiv.html('\u0413\u0440\u0435\u0448\u0435\u043D \u0438-\u043C\u0435\u0439\u043B \u0438\u043B\u0438 \u043F\u0430\u0440\u043E\u043B\u0430 ...');
					infoDiv.show('normal');
				}
			}
		})

}


var Url = {
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}



			
			interest = new Array(36);
			interest[3] = "1.038";
			interest[4] = "1.042";
			interest[6] = "1.048";
			interest[7] = "1.064";
			interest[9] = "1.080";
			interest[10] = "1.088";
			interest[12] = "1.098";
			interest[15] = "1.128";
			interest[18] = "1.148";
			interest[24] = "1.188";
			interest[30] = "1.248";    
			interest[36] = "1.278";
//---------------			
			function RoundNumber(num, ndec, addz){
				if (num != "") {
					var factor = Math.pow(10, ndec);
					num = Math.round(num * factor);
					num = num / factor;
					if (addz) {
						var dot = ("" + num).indexOf(".");
						if (dot>=0) {
							var nzeros = dot + ndec + 1 - ("" + num).length;
							for (i=0; i<nzeros;i++) {
								num = num + "0";
							}
						} else {
							num = num + ".";
							for (i=0; i<ndec;i++) {
			                         num = num + "0";
			                }
			               }
			          }
			     }
			     return (num);
			}
			
//----------------			
			function calculateAll() {
				if (check()) {
					calculateMonthPayment();
				}
				return true;
			}
			
//--------------------------			
			function check() {
				var months = document.getElementById("month").value;
				if (months == "") {
					alert("\n\u041D\u0435 e \u0437\u0430\u0434\u0430\u0434\u0435\u043D \u0441\u0440\u043E\u043A \u043D\u0430 \u043A\u0440\u0435\u0434\u0438\u0442\u0430.");
					document.getElementById("month").value = "12";
					document.getElementById("month").focus();
					return false;
				}
			
				for (var i = 0; i < months.length; i++) {
					var ch = months.substring(i, i + 1);
			
					if ( ch < "0" || ch > "9" ) {
						alert("\n\u041C\u043E\u043B\u044F \u0432\u044A\u0432\u0435\u0434\u0435\u0442\u0435 \u043A\u043E\u0440\u0435\u043A\u0442\u0435\u043D \u0431\u0440\u043E\u0439 \u043C\u0435\u0441\u0435\u0446\u0438.");
						document.getElementById("month").select();
						document.getElementById("month").value = "12";
						document.getElementById("month").focus();
						return false;
					}
				}
				return true;
			}
			
//--------------			
			function calculateMonthPayment() {
				var months = document.getElementById("month").value;
				var price = document.getElementById("price").value;
				document.getElementById("SumPerMonth").value =  RoundNumber(((price * interest[months] + ( ((price * 0.83) / 100) + 2.8) * months) / months), 2, true);
				document.getElementById("SumAllMonths").value =  RoundNumber((price * interest[months] + ( ((price * 0.83) / 100) + 2.8) * months), 2, true);
				return true;
			}
			

