
function close_tab(tab_name) {
    jQuery('#'+tab_name).hide("slow");
}
function open_tab(tab_name) {    
    jQuery('#'+tab_name).show("slow");
}
function check_tab(tab_name) {
	if (document.getElementById(tab_name).style.display == 'none') {
		open_tab(tab_name);
	} else {
		close_tab(tab_name);
	}
}

$(function () {
	$('.border_radius').css({"border-radius": "0 0 20px 20px", "-moz-border-radius":"0 0 20px 20px", "-webkit-border-radius":"0 0 20px 20px"});
});

function chenge_currency() {
	document.form_currency.submit();
}

function check_password (tab_name) {
	if (document.getElementById('password_tab_check').style.display == 'block') {
		close_tab('password_tab_check');
	}
	if (document.getElementById(tab_name).style.display == 'none') {
		document.getElementById('edit_password').value = '';
		document.getElementById('edit_password').disabled = false;
		document.getElementById('edit_confirm_password').value = '';
		document.getElementById('edit_confirm_password').disabled = false;
		open_tab(tab_name);
	} else {
		document.getElementById('edit_password').value = '';
		document.getElementById('edit_password').disabled = true;
		document.getElementById('edit_confirm_password').value = '';
		document.getElementById('edit_confirm_password').disabled = true;
		close_tab(tab_name);
	}
}

function MM_validateForm() { //v4.0

  if (document.getElementById){
    var text,i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { 
	  test=args[i+2];
	  text=args[i+1];
	  val=document.getElementById(args[i]);
      if (val) { 
	    nm=val.name;
		if ((val=val.value)!="") {
          if (nm=='reg_pass') {
		    if (val!=document.getElementById(args[i+3]).value){
		      errors+="- '"+text+"' и '"+args[i+4]+"' not match.\n";
		    }
		  } else if (test.indexOf('isEmail')!=-1) { 
		    p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+="- '"+text+"' Please provide email adress.\n";
          } else if (test!='R') { 
		    num = parseFloat(val);
            if (isNaN(val)) errors+="- '"+text+"' Please use numners.\n";
            if (test.indexOf('inRange') != -1) { 
		      p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+="- '"+text+"' Must be a number from "+min+" to "+max+".\n";
            } 
	      } 
	    } else if (test.charAt(0) == 'R') errors += "- '"+text+"' Empty. \n";
	  }
    } if (errors) alert("Please fix this errors:\n"+errors);
    document.MM_returnValue = (errors == '');
  } 
}

function changeListRegion(checked) {
	var objSel = document.getElementById('country'); 
	var objSelReg = document.getElementById('state');
	$.post('/index.html', {ajax:'change_region',page:'index',country_id:objSel.value},function(data) {
		if (data[0].error == 1) {
			alert('Страница не прогрузилась полностю, обновите её пожалуйста!!!');
		} else {
			var options = '';
			for (var i = 0; i < data.length; i++) {
				options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
			}
			$("select#state").html(options);
		}
	}, "json");
}

$(function () {

	$("select#countryListRegion").change(function () {
		var objSelval = $(this).val();
		$.post('/', {ajax:'change_region',page:'index',country_id:objSelval},function(data) {
			if (data[0].error == 1) {
				alert('Страница не прогрузилась полностю, обновите её пожалуйста!!!');
			} else {
				var options = '';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("select#stateListRegion").html(options);
			}
		}, "json");
	}).trigger('change');

});

function changeTab(openBlock) {
	newTab = (openBlock) ? openBlock : ((location.hash) ? location.hash.replace( /^#/, '' ) : 'comment');
	var tab = document.getElementById('tabs');
	var tabs = tab.parentNode.getElementsByTagName("li");
	for (i = 0; i < tabs.length; i++) {
	  attribute = tabs[i].getAttribute("rel");
	  if (tabs[i].hasAttribute("class")) {
		document.getElementById(attribute).style.display = 'none';
		tabs[i].removeAttribute("class");
	  }
	  if (newTab == attribute) {
		document.getElementById(attribute).style.display = 'block';
		tabs[i].setAttribute("class", "active");
	  }
	}
	//FormatTabs();
}

$(function(){

    $("a.add").click(function(){
        var id = $(this).attr('rel');
        $.post('/', {'ajax':'basket', 'qty':1, 'page':'added_product', 'product_id':id}, function(data) {
            
            if (data['status'] == 'good') {
                $('#basket_qty').html(parseInt($('#basket_qty').html())+1);
                $('#basket_price').html(parseFloat($('#basket_price').html())+ parseFloat(data.price));
                Popup.show('cart_popup');
            } else {
                $('.message').append(data.message);                
            }            
        }, "json");
        return false;
    });

});

