var request = false;
try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
	    try {
	      request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
	      request = false;
	    }
	}
}
if (!request)
  alert("Error initializing XMLHttpRequest!");
var request2 = false;
try {
	request2 = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request2 = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
	    try {
	      request2 = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
	      request2 = false;
	    }
	}
}

function ShowBlock ( id, show ) {
	if ( show )
		document.getElementById ( id ).style.display = 'block';
	else
		document.getElementById ( id ).style.display = 'none';
}

function OutputPrice ( value ) {	
	var v1 = value % 1000;
	var v2 = 0;
	if ( value > 1000 )
		v2 = Math.floor ( value / 1000 );
	else if ( value == 1000 )
		v1 = '1 000';
	else
		v2 = 0;

	if ( v2 && v1 < 10 )
		v1 = '00' + v1;
	else if ( v2 && v1 < 100 )
		v1 = '0' + v1;
	if ( v1 && v2 )
		return v2 + ' ' + v1;
	else if ( v1 )
		return v1;
	else
		return 0;
}

function ToBasket ( id, count, cost ) {
	request.open ( "POST", "/includes/ajax-add-to-basket.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = ToBasketResult;
	var params = 'id=' + id + '&count=' + count + '&cost=' + cost;
	request.send( params );
}

function ToBasketResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {		
			var data = request.responseText.split ( '||' );
			var bt = document.getElementById ( 'basket_text' );
			if ( bt ) {
				bt.innerHTML = data[1];
			}
				document.getElementById ( 'overlay' ).style.display = 'block';
				document.getElementById ( 'message-form' ).style.display = 'block';
				//document.getElementById ( "msg-title" ).innerHTML = 'Корзина';
				document.getElementById ( "msg-text" ).innerHTML = data[0];
				//document.getElementById ( "msg-reload" ).value = 1;
				//document.location.reload (1);
				var str = document.location.href;
				var str = str.substr ( 0, str.length - 1 );
				document.location.href = str + 'basket/';
		}
	}
}

function CalcBasketPrice () {
	var inp, cost, ch, c_html;
	var total = 0;
	for ( k = 1; k <= total_objs; k++ ) {
		var obj_total = 0;		
		inp = document.getElementById ( 'obj' + k );
		cost = document.getElementById ( 'obj_cost' + k );
		c_html = document.getElementById ( 'obj_cost_html' + k );
		var obj_cost = cost.value;

		if ( c_html ) c_html.innerHTML = OutputPrice ( inp.value * obj_cost );		

		if ( inp && cost ) {
			if ( inp.value > 0 ) {
				total += inp.value * obj_cost;				
			}
		}
	}	
	
	var tinp = document.getElementById ( 'total_cost' );
	if ( tinp )
		tinp.innerHTML = OutputPrice ( total );
}

function ChangeBasketCount ( num, mode ) {
	var inp = document.getElementById ( 'obj' + num );
	if ( inp ) {
		if ( mode == 1 )
			inp.value = inp.value*1 + 1;
		else {
			inp.value = inp.value*1 - 1;
			if ( inp.value < 0 ) inp.value = 0;			
		}
		CalcBasketPrice ();
	}
}

function CalcBasketPrice2 () {
	var inp, cost, ch, c_html, c_html2;
	var total = 0;
    var total2 = 0;
	for ( k = 1; k <= total_objs; k++ ) {
		var obj_total = 0;		
		inp = document.getElementById ( 'obj' + k );
		cost = document.getElementById ( 'obj_cost' + k );
		c_html = document.getElementById ( 'obj_cost_html' + k );
		c_html2 = document.getElementById ( 'obj_cost_html2' + k );
		var obj_cost = cost.value;

		if ( c_html ) c_html.innerHTML = OutputPrice ( inp.value * obj_cost );		
        if ( c_html2 ) c_html2.innerHTML = OutputPrice ( inp.value * obj_cost );
		
		if ( inp && cost ) {
			if ( inp.value > 0 ) {
				total += inp.value * obj_cost;
			    total2 += inp.value * 1;
			}
		}
	}
	var tinp = document.getElementById ( 'total_cost' );
	if ( tinp )
		tinp.innerHTML = OutputPrice ( total );
		
	var tinp2 = document.getElementById ( 'total_cost2' );
	if ( tinp2 )
		tinp2.innerHTML = OutputPrice ( total );

	var tinp3 = document.getElementById ( 'total_cost3' );
	if ( tinp3 )
		tinp3.innerHTML = OutputPrice ( total2 );
		
	var tinp4 = document.getElementById ( 'total_cost4' );
	if ( tinp4 )
		tinp4.innerHTML = OutputPrice ( total );
	
	var tinp5 = document.getElementById ( 'total_cost5' );
	if ( tinp5 )
		tinp5.innerHTML = OutputPrice ( total );
	
	var tinp6 = document.getElementById ( 'total_cost6' );
	if ( tinp6 )
		tinp6.innerHTML = OutputPrice ( total );
		
	var tinp7 = document.getElementById ( 'total_cost7' );
	if ( tinp7 )
		tinp7.innerHTML = OutputPrice ( total );
		
	var tinp8 = document.getElementById ( 'total_cost8' );
	if ( tinp8 )
		tinp8.innerHTML = OutputPrice ( total );
}

function ChangeBasketCount2 ( num, mode ) {
	var inp = document.getElementById ( 'obj' + num );
	var inp2 = document.getElementById ( 'obj2' + num );
	var inp3 = document.getElementById ( 'obj3' + num );	
	var inp4 = document.getElementById ( 'obj4' + num );
	
	if ( inp && inp2 && inp3 && inp4 ) {
		if ( mode == 1 ) {
			inp.value = inp.value*1 + 1;
			inp2.innerHTML = OutputPrice ( inp.value );
			inp3.innerHTML = OutputPrice ( inp.value );
			inp4.innerHTML = OutputPrice ( inp.value );
			}
		else {
			inp.value = inp.value*1 - 1;
			if ( inp.value < 0 ) {
			inp.value = 0;	
			inp2.innerHTML = OutputPrice ( inp.value );
			inp3.innerHTML = OutputPrice ( inp.value );
			inp4.innerHTML = OutputPrice ( inp.value );
            }
		    inp2.innerHTML = OutputPrice ( inp.value );
			inp3.innerHTML = OutputPrice ( inp.value );
			inp4.innerHTML = OutputPrice ( inp.value );
		}
		CalcBasketPrice2 ();
	}
}

function DelBasketItem ( num ) {
	var id, obj_id;
	var id_val = 0; var obj_id_val = 0;
	obj_id = document.getElementById ( 'obj_id_' + num );		
	if ( obj_id ) {
		obj_id_val = obj_id.value;
	}	
	if ( obj_id_val  ) {
		request.open ( "POST", "/includes/ajax-del-from-basket.php", true );
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
		request.onreadystatechange = DelFromBasketResult;
		var params = 'id=' + obj_id_val;
		request.send( params );
	}
}

function DelFromBasketResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var res = request.responseText;						
			document.location.reload (1);
		}
	}
}

function SetSort ( type, dir ) {
	request.open ( "POST", "/includes/ajax-set-sort.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = SetSortResult;
	var params = 'type=' + type + '&dir=' + dir;
	request.send( params );
}
function SetSortResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var res = request.responseText;		
			var ua = navigator.userAgent.toLowerCase();
			var isOpera = (ua.indexOf("opera")  != -1); 
			if ( ! isOpera )
				document.location.href = '';
			else
				document.location.reload ();
		}
	}
}
function Register () {
	HideAllForms();

	request.open ( "POST", "/includes/ajax-reg.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = RegisterResult;
	var params = 'login=' + document.reg_form.login.value + '&fio=' + document.reg_form.fio.value + '&email=' + document.reg_form.email.value + '&password=' + document.reg_form.password.value;
	request.send( params );
}
function RegisterResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var data = request.responseText;	
			document.getElementById ( 'overlay' ).style.display = 'block';
			document.getElementById ( 'message-form' ).style.display = 'block';
			//document.getElementById ( "msg-title" ).innerHTML = 'Регистрация';
			document.getElementById ( "msg-text" ).innerHTML = data;
		}
	}	
}
function HideMsgBlock () {
	document.getElementById ( 'overlay' ).style.display = 'none';
	document.getElementById ( 'message-form' ).style.display = 'none';
	var url = document.getElementById ( "msg-url" ).value;
	if ( document.getElementById ( "msg-reload" ).value == 1 && url != '' )
		document.location.href = url;
	else if ( document.getElementById ( "msg-reload" ).value == 1 )
		document.location.reload (1);
}

function Enter () {
	request.open ( "POST", "/includes/ajax-enter.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = EnterResult;
	var params = 'login=' + document.auth_form.login.value + '&password=' + document.auth_form.password.value;	
	request.send( params );
}
function EnterResult () {
	HideAllForms ();
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var data = request.responseText;			
			var test = data.substr ( 0, 1 );	
			if ( test == '0' ) {
				document.getElementById ( 'is_enter' ).value = 1;
			}
			document.getElementById ( 'overlay' ).style.display = 'block';
			document.getElementById ( 'message-form' ).style.display = 'block';
			//document.getElementById ( "msg-title" ).innerHTML = 'Авторизация';
			document.getElementById ( "msg-text" ).innerHTML = data.substr ( 1, data.length - 1 );
		}
	}	
}
function RestorePass () {
	request.open ( "POST", "/includes/ajax-restore-pass.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = RestorePassResult;
	var params = 'login=' + document.pass_form.login.value + '&email=' + document.pass_form.email.value;	
	request.send( params );
}
function RestorePassResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var data = request.responseText;						
			document.getElementById ( 'overlay' ).style.display = 'block';
			document.getElementById ( 'message-form' ).style.display = 'block';
			//document.getElementById ( "msg-title" ).innerHTML = 'Восстановление пароля';
			document.getElementById ( "msg-text" ).innerHTML = data;
		}
	}	
}
function SendCallBack () {
	HideAllForms ();
	request.open ( "POST", "/includes/ajax-callback.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = CallBackResult;
	var params = 'name=' + document.callback_form.name.value + '&phone=' + document.callback_form.phone.value;	
	request.send( params );
}
function CallBackResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var data = request.responseText;						
			document.getElementById ( 'overlay' ).style.display = 'block';
			document.getElementById ( 'message-form' ).style.display = 'block';			
			document.getElementById ( "msg-text" ).innerHTML = data;
			document.location.href = "/zakazzvonka/";
		}
	}	
}
function ProcessPayment ( ) {
	var fio = document.order_form.fio.value;
	var email = document.order_form.email.value;
	var phone = document.order_form.phone.value;
	var address = document.order_form.address.value;
	var comment = document.order_form.text.value;
	var disc = document.order_form.disc.value;
	var total_cost = document.getElementById ( 'total_cost' ).innerHTML;
	var msg = document.order_form.msg.value;
	if ( !fio || !email || !phone || !address ) {
		document.getElementById ( 'overlay' ).style.display = 'block';
		document.getElementById ( 'message-form' ).style.display = 'block';
		//document.getElementById ( "msg-title" ).innerHTML = 'Отправка заказа';
		document.getElementById ( "msg-text" ).innerHTML = msg;
		return;
	}

	document.order_form.action = '/basket/payment/';
	document.order_form.submit();
}
function SendOrder () {
	var fio = document.order_form.fio.value;
	var email = document.order_form.email.value;
	var phone = document.order_form.phone.value;
	var address = document.order_form.address.value;
	var comment = document.order_form.text.value;
	var disc = document.order_form.disc.value;
	var total_cost = document.getElementById ( 'total_cost' ).innerHTML;
	var msg = document.order_form.msg.value;
	if ( !fio || !email || !phone || !address ) {
		document.getElementById ( 'overlay' ).style.display = 'block';
		document.getElementById ( 'message-form' ).style.display = 'block';
		//document.getElementById ( "msg-title" ).innerHTML = 'Отправка заказа';
		document.getElementById ( "msg-text" ).innerHTML = msg;
		return;
	}
	request.open ( "POST", "/includes/ajax-order.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = SendOrderResult;
	var params = 'fio='+fio+'&email='+email+'&phone='+phone+'&address='+address+'&text='+comment+'&total_cost='+total_cost+'&disc='+disc;
	var k = 0;
	for ( var i = 0; i < total_objs; i++ ) {
		params += '&names['+k+']=' + document.getElementById ( 'obj_name'+(i+1) ).value;
		params += '&ids['+k+']=' + document.getElementById ( 'obj_id_'+(i+1) ).value;
		params += '&costs['+k+']=' + document.getElementById ( 'obj_cost'+(i+1) ).value;
		params += '&counts['+k+']=' + document.getElementById ( 'obj'+(i+1) ).value;
		params += '&types['+k+']=obj';
		k++;		
	}
	request.send( params );
//	document.location.href = "http://riccosvet.ru/thanks/";	
}
function SendOrderResult () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var data = request.responseText;
			document.location.reload (1);
			document.location.href = "http://riccosvet.ru/thanks/";	
			document.getElementById ( 'overlay' ).style.display = 'block';
			document.getElementById ( 'message-form' ).style.display = 'block';
			//document.getElementById ( "msg-title" ).innerHTML = 'Отправка заказа';
			document.getElementById ( "msg-text" ).innerHTML = data;
			document.getElementById ( "msg-reload" ).value = 1;
			document.getElementById ( "msg-url" ).value = '/thanks/';
		}
	}	
}
function ShowSubGroup ( id ) {
	document.getElementById ( 'group' + id ).style.display = 'block';
	document.getElementById ( 'group-img-r' + id ).style.display = 'none';
	document.getElementById ( 'group-img-l' + id ).style.display = 'inline';
}
function HideSubGroup ( id ) {
	document.getElementById ( 'group' + id ).style.display = 'none';
	document.getElementById ( 'group-img-r' + id ).style.display = 'inline';
	document.getElementById ( 'group-img-l' + id ).style.display = 'none';
}
function ShowAuthForm () {
	if ( document.getElementById ( 'is_enter' ).value == 1 ) {
		document.getElementById ( 'overlay' ).style.display = 'block';
		document.getElementById ( 'message-form' ).style.display = 'block';		
		document.getElementById ( "msg-text" ).innerHTML = 'Вы уже авторизованы на сайте.';
	}
	else {
		document.getElementById ( 'auth-form' ).style.display = 'block';
		document.getElementById ( 'reg-form' ).style.display = 'none';
		document.getElementById ( 'pass-form' ).style.display = 'none';
		document.getElementById ( 'call-form' ).style.display = 'none';
	}
}
function ShowRegForm () {
	document.getElementById ( 'auth-form' ).style.display = 'none';
	document.getElementById ( 'reg-form' ).style.display = 'block';
	document.getElementById ( 'pass-form' ).style.display = 'none';
	document.getElementById ( 'call-form' ).style.display = 'none';
}
function ShowPassForm () {
	document.getElementById ( 'auth-form' ).style.display = 'none';
	document.getElementById ( 'reg-form' ).style.display = 'none';
	document.getElementById ( 'pass-form' ).style.display = 'block';
	document.getElementById ( 'call-form' ).style.display = 'none';
}
function ShowCallForm () {
	document.getElementById ( 'auth-form' ).style.display = 'none';
	document.getElementById ( 'reg-form' ).style.display = 'none';
	document.getElementById ( 'pass-form' ).style.display = 'none';
	document.getElementById ( 'call-form' ).style.display = 'block';
}
function HideAllForms () {
	document.getElementById ( 'auth-form' ).style.display = 'none';
	document.getElementById ( 'reg-form' ).style.display = 'none';
	document.getElementById ( 'pass-form' ).style.display = 'none';
	document.getElementById ( 'call-form' ).style.display = 'none';
}

function ShowBrandsBox () {
	var div = document.getElementById ( 'brands-box' );
	var div2 = document.getElementById ( 'styles-box' );
	var div3 = document.getElementById ( 'types-box' );
	var span = document.getElementById ( 'brands-span' );
	var span2 = document.getElementById ( 'styles-span' );
	var span3 = document.getElementById ( 'types-span' );
	if ( !div ) return;
	if ( div.style.display != 'block' ) {
		div.style.display = 'block';
		div2.style.display = 'none';
		div3.style.display = 'none';
		span.className = '';
		span2.className = 'box-link';
		span3.className = 'box-link';
	}	
}

function ShowStylesBox () {
	var div = document.getElementById ( 'styles-box' );
	var div2 = document.getElementById ( 'brands-box' );
	var div3 = document.getElementById ( 'types-box' );
	var span = document.getElementById ( 'styles-span' );
	var span2 = document.getElementById ( 'brands-span' );
	var span3 = document.getElementById ( 'types-span' );
	if ( !div ) return;
	if ( div.style.display != 'block' ) {
		div.style.display = 'block';
		div2.style.display = 'none';
		div3.style.display = 'none';
		span.className = '';
		span2.className = 'box-link';
		span3.className = 'box-link';
	}	
}

function ShowTypesBox () {
	var div = document.getElementById ( 'types-box' );
	var div2 = document.getElementById ( 'styles-box' );
	var div3 = document.getElementById ( 'brands-box' );
	var span = document.getElementById ( 'types-span' );
	var span2 = document.getElementById ( 'brands-span' );
	var span3 = document.getElementById ( 'styles-span' );
	if ( !div ) return;
	if ( div.style.display != 'block' ) {
		div.style.display = 'block';
		div2.style.display = 'none';
		div3.style.display = 'none';
		span.className = '';
		span2.className = 'box-link';
		span3.className = 'box-link';
	}	
}

function ChangeObjCount ( type ) {
	var div = document.getElementById ( 'object-count-value' );
	if ( !div ) return;
	if ( type > 0 )
		div.innerHTML = div.innerHTML*1 + 1;
	else {
		var val = div.innerHTML*1 - 1;
		if ( val < 1 )
			val = 1;
		div.innerHTML = val;
	}	
}

function ShowHideStyles ( id ) {
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
	el_id = document.getElementById ( 'cat-id-'+id );
	if ( isIE ) {
		if ( el_id.style.display != 'block' )
			el_id.style.display = 'block';
		else
			el_id.style.display = 'none';
	}
	else {
		if ( el_id.style.display != 'table-row' )
			el_id.style.display = 'table-row';
		else
			el_id.style.display = 'none';
	}
	request.open ( "POST", "/includes/ajax-set-sel-type.php", true );
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	request.onreadystatechange = ShowHideStylesRes;
	var params = 'id=' + id;
	request.send( params );
}
function ShowHideStylesRes () {
	if (request.readyState == 4) {
	 	if (request.status == 200) {			
			var res = request.responseText;						
		}
	}
}
function GetNewCaptcha ( elem ) {	
	request2.open ( "POST", "/includes/ajax-get-captcha.php", true );
	request2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
 	request2.onreadystatechange = function () { SetNewCaptcha ( elem ) };
	var params = "";
	request2.send( params );
}
function SetNewCaptcha ( elem ) {
	if (request2.readyState == 4) {
	 	if (request2.status == 200) {
		 	var file = request2.responseText;	
			if ( elem )
				elem.src = file;
		}
	 }		
}

function SwapImage ( img_num ) {

	var img = document.getElementById ( 'bigimg' );
	var pdf = document.getElementById ( 'big-img-pdf' );
	//var img2 = document.getElementById ( 'big-img' );
	//var img3 = document.getElementById ( 'big-img-td' );
	var sel_img = document.getElementById ( 'sel-images' );
	var color_con = document.getElementById ( 'color_count' );
	//var text1 = document.getElementById ( 'bigimg_text1' );
	//var text2 = document.getElementById ( 'bigimg_text2' );
	var img_val = document.getElementById ( 'img_val' + img_num );
	var img_pdf = document.getElementById ( 'img_pdf' + img_num );
	var text_val = document.getElementById ( 'img_text' + img_num );
	var img_div = document.getElementById ( 'img_div' + img_num );
	var img_width = document.getElementById ( 'img_width' + img_num );
	if ( !img || !img_div || (!img_val && !img_pdf) || !text_val ) return;
	if ( img_val ) {
		img.style.display = 'block';
		pdf.style.display = 'none';
		img.src = img_val.value;
		img.alt = text_val.value;
		img.title = text_val.value;
		img.style.width = img_width.value + 'px';
	}
	else {
		img.style.display = 'none';
		pdf.style.display = 'block';
		pdf.innerHTML = '<a href="' + img_pdf.value + '">Скачать файл</a>';
	}
	//img2.style.width = img_width.value + 'px';
	//img3.style.width = img_width.value + 'px';
	sel_img.style.width = img_width.value - 20 - 2 + 'px';
	all_color = color_con.value +2;
	//if ( text1 )	
	//	text1.innerHTML = text_val.value;
	//if ( text2 )
	//	text2.innerHTML = text_val.value;
	for ( var i = 1; i < all_color; i++ ) {
		img_div_ = document.getElementById ( 'img_div' + i );
		if ( img_div_ )
			img_div_.className = 'small-img';
	}
	if ( img_div )
		img_div.className = 'small-img-sel';
}
