function mail(name, domain, style) {
	document.write('<a h'+'ref="'+'mai'+ 'lto:'+name+'@'+domain+'" style="'+style+'">'+name+'@'+domain+'</a>');
}

function trim(s) {
	return rtrim(ltrim(s));
}
 function ltrim(s) {
	return s.replace(/^\s+/, ''); 
}

function rtrim(s) {
	return s.replace(/\s+$/, ''); 
}

//--
function renderForm(f) {
	$("input#addit__vkontakte").attr("value", $('#vkontakte').val());
	$("input#addit__icq").attr("value", $('#icq').val());
	var i = $("#add_address div").length + 1;
	var x;
	$('div#add_address div').each(function(n,element){
		fn = $(this).find("input[name^='fullname_']").val();
		cn = $(this).find("input[name^='country_']").val();
		rg = $(this).find("input[name^='region_']").val();
		ct = $(this).find("input[name^='city_']").val();
		ad = $(this).find("input[name^='adds_']").val();
		zp = $(this).find("input[name^='zip_']").val();
		var is_default = 0;
		$("input#addit__address_"+(n+1)).attr("value", fn+"}={"+cn+"}={"+rg+"}={"+ct+"}={"+ad+"}={"+zp+"}={"+is_default );
	});
	var x_phone_1 = trim($("#phone_1").val());
	var x_phone_2 = trim($("#phone_2").val());
	var x_phone_3 = trim($("#phone_3").val());
	$("#phone").attr("value", '+'+x_phone_1+'('+x_phone_2+')'+x_phone_3);
	f.submit()
}

//--
function renderalForm(f) {
	if ( $("#sms_checkbox").attr("checked") ) { $("#addit__sms__checkbox").attr("checked","checked"); }
	else { $("#addit__sms__checkbox").removeAttr("checked"); }
	if ( $("#mail_checkbox").attr("checked") ) { $("#addit__mail__checkbox").attr("checked","checked");  }
	else { $("#addit__mail__checkbox").removeAttr("checked"); }
	f.submit()
}

//--
function renderabForm(f) {
	var n_trigger = -1;
	$('div#add_address div').each(function(n,element){
		var is_default = 0;
		x = $(this).find("input[name^='addressbook']").val();
		$("input#addit__address_"+(n+1)).attr("value", x);
		if ( (typeof(x) != 'undefined') && (x.length > 0) ) {
		arrqrx = x.split('}={');
			if (arrqrx.length != 7) {
			} else {
				if (arrqrx[6] == 1) {
					var x_fullname = arrqrx[0];
					var x_zip = arrqrx[5];
					var x_country = arrqrx[1];
					var x_state = ((arrqrx[2] != '') && (arrqrx[3] != '') && (arrqrx[4] != ''))?arrqrx[2]+', '+arrqrx[3]+', '+arrqrx[4]:'';
					$("input#fullname").attr("value", x_fullname);
					$("input#state").attr("value", x_state);
					$("input#zip").attr("value", x_zip);
					$("input#country").attr("value", x_country);
				}
			
			}
		}
		n_trigger = n;
	});
	if (n_trigger < 5 || n_trigger >= 0) {
		for(var i = n_trigger+1; i < 6; i++) {
			$("input#addit__address_"+(i+1)).attr("value", "");
		}
	}
	f.submit()
}

//--
function rendernonregForm(f) {
	var x_zip = $("#zip").val();
	var x_state = $("#region").val();
	var x_city = $("#city").val();
	var x_mini_address = $("#mini_address").val();
	$("#address").attr("value", x_zip+', '+x_state+', '+x_city+', '+x_mini_address);
	var x_phone_1 = $("#phone_1").val();
	var x_phone_2 = $("#phone_2").val();
	var x_phone_3 = $("#phone_3").val();
	$("#phone").attr("value", '+'+x_phone_1+'('+x_phone_2+')'+x_phone_3);
	f.submit()
}

//--
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


(function($){
  // очищаем select
  $.fn.clearSelect = function() {
	  return this.each(function(){
		  if(this.tagName=='SELECT') {
		      this.options.length = 0;
		      $(this).attr('disabled','disabled');
		  }
	  });
  }
  // заполняем select
  $.fn.fillSelect = function(dataArray) {
	  return this.clearSelect().each(function(){
		  if(this.tagName=='SELECT') {
			  var currentSelect = this;
			  $.each(dataArray,function(index,data){
				  var option = new Option(data.text,data.value);
				  if($.support.cssFloat) {
					  currentSelect.add(option,null);
				  } else {
					  currentSelect.add(option);
				  }
			  });
		  }
	  });
  }
})(jQuery);


//--
var get_string = "";

var instrument_get = $.getUrlVar('instrument');
var ig = $.cookie('cookie_instrument');
if (ig == null || ig == '') {ig = ''}

if (typeof(instrument_get) == 'undefined') {
instrument_get = '';
} else {
get_string = "?instrument=" + instrument_get;
}

var marka_get = $.getUrlVar('marka');
var mg = $.cookie('cookie_marka');
if (mg == null || mg == '') {mg = ''}

if (typeof(marka_get) == 'undefined') {
marka_get = '';
} else {
get_string += "&marka=" + marka_get;
}

var cal_get = $.getUrlVar('cal');
var cg = $.cookie('cookie_cal');
if (cg == null || cg == '') {cg = ''}

if (typeof(cal_get) == 'undefined') {
cal_get = '';
} else {
get_string += "&cal=" + cal_get;
}

var sum_get = $.getUrlVar('sum');
var sg = $.cookie('cookie_sum');
if (sg == null || sg == '') {sg = ''}

if (typeof(sum_get) == 'undefined') {
sum_get = '';
} else {
get_string += "&sum=" + sum_get;
}

var features_get = $.getUrlVar('features');
var fg = $.cookie('cookie_features');
if (fg == null || fg == '') {fg = ''}

if (typeof(features_get) == 'undefined') {
features_get = '';
} else {
get_string += "&features=" + features_get;
}

var summr_get = $.getUrlVar('summr');
if (typeof(summr_get) == 'undefined') {
summr_get = '';
} else {
get_string += "?summr=" + summr_get;
}

marka_get = decodeURI(marka_get);
marka_get = marka_get.replace(/%26/,'&');
marka_get = marka_get.replace(/\+/,' ');
marka_get = marka_get.replace(/\+/,' ');

function reviewGoods(num) {
    x = window.location.href.slice(0,window.location.href.indexOf('?'));
    location = (get_string=="") ? x + "?display=" + num : x + get_string + "&display=" + num;
}

$(document).ready(function(){
  
  $('.shop-stuff').hover(
function(){
	$(this).animate({
backgroundColor: "#FFFFFF",
border: "1px solid #5A819F"
}, 100);
},
function(){
	$(this).animate({
backgroundColor: "#F6F8FA",
border: "1px solid #b5bcc2"
}, 100);
});

var display_get = $.getUrlVar('display');
if (typeof(display_get) != 'undefined') {

}
//--
  var display_get = $.getUrlVar('summr');
  if (typeof(display_get) != 'undefined') {
  var arr = ["0", "1", "4", "5", "6", "7", "8", "12"];
  inar = jQuery.inArray(display_get, arr);
  if (inar != '-1') {
    $(".view_top div #viewmode > option[value='" + display_get + "']").attr('selected','selected');
    $(".view_bottom div #viewmode > option[value='" + display_get + "']").attr('selected','selected');
    } else {
      $(".view_top div #viewmode > option[value='0']").attr('selected','selected');
      x = window.location.href.slice(0,window.location.href.indexOf('?'));
      if (get_string=="") {
      location = x + "?summr=" + $('#viewmode').val();}
      else {location = x + get_string + "&summr=" + $('#viewmode').val();}
    }
  } else {
    $(".view_top div #viewmode > option[value='0']").attr('selected','selected');
  }
//--
$('.view_top div #viewmode').change(function(){
    var summr_get = $.getUrlVar('summr');
    if (typeof(summr_get) == 'undefined') {
    x = window.location.href.slice(0,window.location.href.indexOf('?'));
    location = (get_string=="") ? x + "?summr=" + $('.view_top div #viewmode').val() : x + get_string + "&summr=" + $('.view_top div #viewmode').val();
    }
    else {
    x = window.location.href.slice(0,window.location.href.indexOf('?'));
    location = (get_string=="") ? x + "?summr=" + $('.view_top div #viewmode').val() : x + get_string + "&summr=" + $('.view_top div #viewmode').val();
    }
});

$('.view_bottom div #viewmode').change(function(){
    var summr_get = $.getUrlVar('summr');
    if (typeof(summr_get) == 'undefined') {
    x = window.location.href.slice(0,window.location.href.indexOf('?'));
    location = (get_string=="") ? x + "?summr=" + $('.view_bottom div #viewmode').val() : x + get_string + "&summr=" + $('.view_bottom div #viewmode').val();
    }
    else {
    x = window.location.href.slice(0,window.location.href.indexOf('?'));
    location = (get_string=="") ? x + "?summr=" + $('.view_bottom div #viewmode').val() : x + get_string + "&summr=" + $('.view_bottom div #viewmode').val();
    } 
});

  // выбор произвидотеля
  function adjustMarka(){
  	var instrumentValue = $('#instrument').val();
  	var tmpSelect = $('#marka');
  	if(instrumentValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		adjustCaliber();
  	} else {
  		$.getJSON('/file_db/cascadeSelectMarka.php',{instrument:instrumentValue},function(data) {
  			tmpSelect.fillSelect(data).attr('disabled','');
  			prov(tmpSelect,15);
  			//adjustCaliber();
  		});
  	}
  };
  // выбор калибра
  function adjustCaliber(){
  	var instrumentValue = $('#instrument').val();
  	var markaValue = $('#marka').val();
  	var tmpSelect = $('#cal');
  	if(instrumentValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		//adjustFeatures();
  	} else {
  		$.getJSON('/file_db/cascadeSelectCaliber.php',{instrument:instrumentValue},function(data) {
			tmpSelect.fillSelect(data).attr('disabled','');
  			prov(tmpSelect,15);
  			prov(tmpSelect,13);
  			//adjustFeatures();
  		});
  	}
  };
  // выбор количества струн
  function adjustSum(){
  	var instrumentValue = $('#instrument').val();
  	var markaValue = $('#marka').val();
  	var tmpSelect = $('#sum');
  	if(instrumentValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		//adjustFeatures();
  	} else {
  		$.getJSON('/file_db/cascadeSelectSum.php',{instrument:instrumentValue},function(data) {
  			tmpSelect.fillSelect(data).attr('disabled','');
  			prov(tmpSelect,15);
  			//adjustFeatures();
  		});
  	}
  };
  // выбор особенностей
  function adjustFeatures(){
  	var instrumentValue = $('#instrument').val();
  	var markaValue = $('#marka').val();
  	var calValue = $('#cal').val();
  	var tmpSelect = $('#features');
  	if(instrumentValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
  	//$('#features').attr('disabled','');
  		$.getJSON('/file_db/cascadeSelectFeatures.php',{instrument:instrumentValue},function(data) {
  			tmpSelect.fillSelect(data).attr('disabled','');
  			prov(tmpSelect,15);
  		});
  	}
  };
  
    function prov(x,instr){
		if ( $('#instrument').val() == instr ) {
  			$(x).attr('disabled','disabled');
  		}
	};
  
	
	
	$('#instrument').change(function(){
    $.cookie('cookie_instrument',$('#instrument').val(), {path: "/", domain: "strunki.ru"});
  	adjustMarka();
  	adjustCaliber();
  	adjustSum();
  	adjustFeatures();
  	
	$.cookie('cookie_marka',null, {path: "/", domain: "strunki.ru"});
	$.cookie('cookie_sum',null, {path: "/", domain: "strunki.ru"});
	$.cookie('cookie_cal',null, {path: "/", domain: "strunki.ru"});
	$.cookie('cookie_features',null, {path: "/", domain: "strunki.ru"});
	
  });
  //}).change();
  
  $('#marka').change(function(){
  	$.cookie('cookie_marka',$('#marka').val(), {path: "/", domain: "strunki.ru"});
  });
  
  $('#sum').change(function(){
  	$.cookie('cookie_sum',$('#sum').val(), {path: "/", domain: "strunki.ru"});
  	if ( $('#sum').val() == 12 || $('#sum').val() == 1 || $('#sum').val() == 7 || $('#sum').val() == 8 || $('#sum').val() == 5 || ($('#sum').val() == 6 && $('#instrument').val() == 11) ) {
  		$("#cal > option[value='']").attr('selected','selected');
  		$("#cal").attr('disabled','disabled');
  	} else {
  		$("#cal > option[value='" + jQuery.cookie('cookie_cal') + "']").attr('selected','selected');
  		
  		$("#cal").attr('disabled','');
  	}
  });
  
  $('#cal').change(function(){
  	$.cookie('cookie_cal',$('#cal').val(), {path: "/", domain: "strunki.ru"});
  });
  
  $('#features').change(function(){
  	$.cookie('cookie_features',$('#features').val(), {path: "/", domain: "strunki.ru"});
  });
  
  if (instrument_get.length != 0 || ig != '' ) {
  instrument_get = (instrument_get.length == 0)?ig:instrument_get;
  $("#instrument > option[value='" + instrument_get + "']").attr('selected','selected');
  $.cookie('cookie_instrument',$('#instrument').val(), {path: "/", domain: "strunki.ru"});
  
  	var instrumentValue = $('#instrument').val();
  	
  	var tmpSelect = $('#marka');
  		$.getJSON('/file_db/cascadeSelectMarka.php',{instrument:instrumentValue},function(data) {
  			tmpSelect.fillSelect(data).attr('disabled','');
  			prov(tmpSelect,15);
  			    if (marka_get.length != 0  || mg != '' ) {
  			    marka_get = (marka_get.length == 0)?mg:marka_get;
  			    
  				$('#marka > option[value="' + marka_get + '"]').attr('selected','selected');
  				$.cookie('cookie_marka',$('#marka').val(), {path: "/", domain: "strunki.ru"});
  				}
  				
  						var tmpSelect2 = $('#sum');
  					$.getJSON('/file_db/cascadeSelectSum.php',{instrument:instrumentValue},function(data) {
  						tmpSelect2.fillSelect(data).attr('disabled','');
  						prov(tmpSelect2,15);
  						if (sum_get.length != 0  || sg != '' ) {
  						sum_get = (sum_get.length == 0)?sg:sum_get;
  							$("#sum > option[value='" + sum_get + "']").attr('selected','selected');
  							$.cookie('cookie_sum',$('#sum').val(), {path: "/", domain: "strunki.ru"});
  							if ( $('#sum').val() == 12 || $('#sum').val() == 1 || $('#sum').val() == 7 || $('#sum').val() == 8 || $('#sum').val() == 5 || ($('#sum').val() == 6 && $('#instrument').val() == 11) ) {
  							$("#cal > option[value='']").attr('selected','selected');
  							$("#cal").attr('disabled','disabled');
  							}
  						}
  						//
  						var tmpSelect1 = $('#cal');
  					$.getJSON('/file_db/cascadeSelectCaliber.php',{instrument:instrumentValue},function(data) {
  						tmpSelect1.fillSelect(data).attr('disabled','');
  						prov(tmpSelect1,15);
  						prov(tmpSelect1,13);
  						if (cal_get.length != 0  || cg != '' ) {
  						cal_get = (cal_get.length == 0)?cg:cal_get;
  						
  							$("#cal > option[value='" + cal_get + "']").attr('selected','selected');
  							$.cookie('cookie_cal',$('#cal').val(), {path: "/", domain: "strunki.ru"});
  						}
  						//
  						  				var tmpSelect3 = $('#features');
  					$.getJSON('/file_db/cascadeSelectFeatures.php',{instrument:instrumentValue},function(data) {
  						tmpSelect3.fillSelect(data).attr('disabled','');
  						prov(tmpSelect3,15);
  						if (features_get.length != 0  || fg != '' ) {
  						features_get = (features_get.length == 0)?fg:features_get;
  							$("#features > option[value='" + features_get + "']").attr('selected','selected');
  							$.cookie('cookie_features',$('#features').val(), {path: "/", domain: "strunki.ru"});
  						}
  						if ($('.lb_loading').css('opacity') == 0) { $('.lb_loading').fadeTo(1500, 1); }
  					});
  				});
  			});
  		});
  			
  		} else {
  			if ($('.lb_loading').css('opacity') == 0) { $('.lb_loading').fadeTo(1500, 1); }
  		}

$('#reset_button').bind('click', function(){

	$.cookie('cookie_instrument',null, {
	path: "/",
	domain: "strunki.ru"
	});
	$.cookie('cookie_marka',null, {
	path: "/",
	domain: "strunki.ru"
	});
	$.cookie('cookie_sum',null, {
	path: "/",
	domain: "strunki.ru"
	});
	$.cookie('cookie_cal',null, {
	path: "/",
	domain: "strunki.ru"
	});
	$.cookie('cookie_features',null, {
	path: "/",
	domain: "strunki.ru"
	});
	
	$("#instrument > option[value='']").attr('selected','selected');
	adjustMarka();
  	adjustCaliber();
  	adjustSum();
  	adjustFeatures();
});



$('.new_more').bind('click', function(){
	var x = $(this).parent().find('div#mininew_content');
	if (x.css('display') == 'none') {
		$(this).parent().find('div#mininew_content').slideDown("slow");
	} else {
		$(this).parent().find('div#mininew_content').slideUp("slow");
	}

});

$('.pay_h3').bind('click', function(){
	var x = $(this).parent().next();
	if (x.css('display') == 'none') {
		x.slideDown("slow");
	} else {
		x.slideUp("slow");
	}

});


$('#add_address_button').bind('click', function(){
	var i = $("#add_address div").length + 1;
	$("#add_address_button").slideUp("slow");
	$('#add_address').append('<div id="addr_" class="a_" style="display:none;"><hr /><table width="100%"><tr><td width="186px" align="right">ФИО:</td><td><input type="text" name="fullname_" value="" /></td></tr><tr><td align="right">Страна:</td><td><select name="country_" size="1"><option value="">не выбрана</option><option value="176">Россия</option><option value="220">Украина</option><option value="20">Белоруссия</option><option value="109">Казахстан</option></select></td></tr><tr><td align="right">Город:</td><td><input type="text" name="city_" value="" /></td></tr><tr><td align="right">Адрес:</td><td><input type="text" name="adds_" value="" /></td></tr><tr><td align="right">Почтовый индекс:</td><td><input type="text" name="zip_" value="" /></td></tr></table><input type="button" id="save_address_button" value="Добавить в адресную книгу"></div>').find('div#addr_').slideDown("slow");

});


});
