//// Tabs

$.tabs = function(selector, start) {$(selector).each(function(i, element) {$($(element).attr('rel')).css('display', 'none');$(element).click(function() {$(selector).each(function(i, element) {$(element).removeClass('selected');$($(element).attr('rel')).css('display', 'none');});$(this).addClass('selected');$($(this).attr('rel')).css('display', 'block');});});if (!start) {start = $(selector + ':first').attr('rel');} $(selector + '[rel=\'' + start + '\']').trigger('click');};

//// Json Constants Array

var JSonData = ConstJS.constants;

//// Validate Functions

function checkname()
	{var str=document.getElementById('first_name').value;
	 var filter=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('name_div').style.visibility="hidden"; document.getElementById('name_pic').style.visibility="visible";} else {document.getElementById('name_pic').style.visibility="hidden"; document.getElementById('name_div').style.visibility="visible";}
	}

function checksurname()
	{var str=document.getElementById('last_name').value;
	 var filter=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('surname_div').style.visibility="hidden"; document.getElementById('surname_pic').style.visibility="visible";} else {document.getElementById('surname_pic').style.visibility="hidden"; document.getElementById('surname_div').style.visibility="visible";}
   }

function checkpatronymic()
	{var str=document.getElementById('patronymic').value;
	 var filter=/^[?-?A-ZА-Я]{2,}( +)?([?-?A-ZА-Я]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('patronymic_div').style.visibility="hidden"; document.getElementById('patronymic_pic').style.visibility="visible";} else {document.getElementById('patronymic_pic').style.visibility="hidden"; document.getElementById('patronymic_div').style.visibility="visible";}}
	 
function checkgender()
	{var str=document.getElementById('gender').value;
	 var filter=/^[?-?A-ZА-Я]{1,}( +)?([?-?A-ZА-Я]{1,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('gender_div').style.visibility="hidden"; document.getElementById('gender_pic').style.visibility="visible";} else {document.getElementById('gender_pic').style.visibility="hidden"; document.getElementById('gender_div').style.visibility="visible";}}

function checkage()
	{var str=document.getElementById('age').value;
	  var filter=/^[?-?A-ZА-Я0-9]{1,}( +)?([?-?A-ZА-Я0-9]{1,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('age_div').style.visibility="hidden"; document.getElementById('age_pic').style.visibility="visible";} else {document.getElementById('age_pic').style.visibility="hidden"; document.getElementById('age_div').style.visibility="visible";}}
	 
function checheight()
	{var str=document.getElementById('height').value;
	  var filter=/^[?-?A-ZА-Я0-9]{1,}( +)?([?-?A-ZА-Я0-9]{1,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('height_div').style.visibility="hidden"; document.getElementById('height_pic').style.visibility="visible";} else {document.getElementById('height_pic').style.visibility="hidden"; document.getElementById('height_div').style.visibility="visible";}}

function checkmail()
	{var str=document.getElementById('email').value;
	 var filter=/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
	 if (filter.test(str)) {document.getElementById('mail_div').style.visibility="hidden"; document.getElementById('mail_pic').style.visibility="visible";} else {document.getElementById('mail_pic').style.visibility="hidden"; document.getElementById('mail_div').style.visibility="visible";}
	}

function checkcountry()
	{var str=document.getElementById('country').value;
	 var filter=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('country_div').style.visibility="hidden"; document.getElementById('country_pic').style.visibility="visible";} else {document.getElementById('country_pic').style.visibility="hidden"; document.getElementById('country_div').style.visibility="visible";}
  }

function checkzip()
	{var str=document.getElementById('zip').value;
	 var filter=/^[0-9]{5,}$/i;
	 if (filter.test(str)) {document.getElementById('zip_div').style.visibility="hidden"; document.getElementById('zip_pic').style.visibility="visible";} else {document.getElementById('zip_pic').style.visibility="hidden"; document.getElementById('zip_div').style.visibility="visible";}
	}

function checkcity()
	{var str=document.getElementById('city').value;
	 var filter=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('city_div').style.visibility="hidden"; document.getElementById('city_pic').style.visibility="visible";} else {document.getElementById('city_pic').style.visibility="hidden"; document.getElementById('city_div').style.visibility="visible";}
  }

function checkaddress()
	{var str=document.getElementById('address').value;
	 var filter=/^[А-ЯA-Z0-9/. ]{2,}( +)?([А-ЯA-Z0-9/. ]{2,})?( +)?$/i;
	 if (filter.test(str)) {document.getElementById('address_div').style.visibility="hidden"; document.getElementById('address_pic').style.visibility="visible";} else {document.getElementById('address_pic').style.visibility="hidden"; document.getElementById('address_div').style.visibility="visible";}
  }

function checkphone()
	{var str=document.getElementById('phone').value;
   
	 var filter=/^[+0-9]{2,2}\ \(\d{1,5}\)\ \d{1,5}\-\d{2}\-\d{2}$/;
	 if (filter.test(str)) {document.getElementById('phone_div').style.visibility="hidden"; document.getElementById('phone_pic').style.visibility="visible";} else {document.getElementById('phone_pic').style.visibility="hidden"; document.getElementById('phone_div').style.visibility="visible";}
	}
	
function open_window(link,w,h) //opens new window
	{
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
	}

function validate_order_info() //validate order information
	{

	var strFName=document.getElementById('first_name').value;
	var filterFName=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterFName.test(strFName))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

	var strSName=document.getElementById('last_name').value;
	var filterSName=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterSName.test(strSName))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strNPatronymic=document.getElementById('patronymic').value;
	var filterNPatronymic=/^[?-?A-ZА-Я]{2,}( +)?([?-?A-ZА-Я]{2,})?( +)?$/i;

	if (!filterNPatronymic.test(strNPatronymic))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}
		
	var strNGender=document.getElementById('gender').value;
	var filterNGender=/^[?-?A-ZА-Я]{1,}( +)?([?-?A-ZА-Я]{1,})?( +)?$/i;

	if (!filterNGender.test(strNGender))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strSAge=document.getElementById('age').value;
	var filterSAge=/^[?-?A-ZА-Я0-9]{1,}( +)?([?-?A-ZА-Я0-9]{1,})?( +)?$/i;

	if (!filterSAge.test(strSAge))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}
		
	var strSHeight=document.getElementById('height').value;
	var filterSHeight=/^[?-?A-ZА-Я0-9]{1,}( +)?([?-?A-ZА-Я0-9]{1,})?( +)?$/i;

	if (!filterSHeight.test(strSHeight))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strEmail=document.getElementById('email').value;
	var filterEmail=/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;

	if (!filterEmail.test(strEmail))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strPhone=document.getElementById('phone').value;
	var filterPhone=/^[+0-9]{2,2}\ \(\d{1,5}\)\ \d{1,5}\-\d{2}\-\d{2}$/;

  	if (str ="" )
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strCountry=document.getElementById('country').value;
	var filterCountry=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterCountry.test(strCountry))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strZip=document.getElementById('zip').value;
	var filterZip=/^[0-9]{5,}$/i;

	if (!filterZip.test(strZip))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strCity=document.getElementById('city').value;
	var filterCity=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterCity.test(strCity))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  var strAddress=document.getElementById('address').value;
	var filterAddress=/^[А-ЯA-Z0-9/. ]{2,}( +)?([А-ЯA-Z0-9/. ]{2,})?( +)?$/i;

	if (!filterAddress.test(strAddress))
		{
		alert(JSonData.ERROR_INPUT_ALL_FIELDS);
		return false;
		}

  return true;
	}

function validate_custinfo() //validate customer information
	{	
	
	var strFName=document.getElementById('first_name').value;
	var filterFName=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterFName.test(strFName)) 
		{
		alert(JSonData.ERROR_INPUT_NAME);
		return false;
		}

	var strSName=document.getElementById('last_name').value;
	var filterSName=/^[А-ЯA-Z]{2,}( +)?([А-ЯA-Z]{2,})?( +)?$/i;

	if (!filterSName.test(strSName)) 
		{
		alert(JSonData.ERROR_INPUT_NAME);
		return false;
		}

	var strEmail=document.getElementById('email').value;
	var filterEmail=/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;

	if (!filterEmail.test(strEmail)) 
		{
		alert(JSonData.ERROR_INPUT_EMAIL);
		return false;
		}

	var strPhone=document.getElementById('phone').value;
	var filterPhone=/^(?:8|\+7)? ?\(\d{1,5}\)? ?\d{1,5}\-\d{2}\-\d{2}$/;
	if (str ="" )
		{
		alert(JSonData.ERROR_INPUT_PHONE);
		return false;
		}
		
	return true;
	}


//// Payment functions

function checkSubmit() {
	if (document.getElementById("idto").value != "") return true;
	else 
	  {
		alert("Введите номер телефона в формате 9059103456");
		return false;
	  }
	}

//// Mode Functions

function moduleSearch()
	{location = "./index.php?searchstring="+$('#mod_search_searchword').attr('value');}


function googleTranslateElementInit()
	{
	new google.translate.TranslateElement(
		{pageLanguage: json_const.PAGE_LANG,includedLanguages: 'en, ru'}, 'google_translate_element');
	}

$(document).ready(function(){   

// Ajax cart function
    $('.ajaxcart').click(function () {                //Биндим клик по классу ajxcart
        var curid = this.id.split('_')[1];            //Смотрим id (вида tocard_123) и берем из него код продукта
        poststr = "shopping_cart=yes&add2cart=" + curid + "&type=ajax";        //Формируем POST
        $.ajax(                            //Отправляем
        {
            type: "GET",
            url: "./includes/shopping_cart.php",
            data: poststr,
            success:
                function (response)				   //Обрабатываем ответ
                    {
			var return_data = eval("("+response+")");  //JSON.parse();

                        $('#sci').text(return_data.info.count);    //Пишем количество товаров в элемент с id="sci"
                        $('#scs').text(return_data.info.cost);     //Пишем сумму товаров в элемент с id="scs"
            //Тут же функции анимации и прочих красивостей

				var image = $('#dp'+curid).offset();
				var cart  = $('#module_cart').offset();
	
				$('#dp'+curid).before('<img src="' + $('#dp'+curid).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
	
				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),  
					heigth : $('#module_cart').height()
				};		
	
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});
                    }
        });
	return false;
    });

// Jcarusel new_products show
    $(".carouselDiv .jCarouselLite").jCarouselLite({auto: JSonData.CONF_NEW_PRODUCTS_FRIQ, speed: JSonData.CONF_NEW_PRODUCTS_SPEED, vertical: true, visible: JSonData.CONF_SCROLL_NEW_PRODUCTS, easing: "easeinout"});
    
// HideSlide new_products show
//   var fadeTime = 2000*(JSonData.CONF_NEW_PRODUCTS_FRIQ/10000);var i = $('.slide').length;var x = 0;showSlide();function showSlide(){curSlide = //"#slide" + x;if(x == 0){prevSlide = "#slide" + (i - 1);} else {prevSlide = "#slide" + (x - 1);}$(prevSlide).fadeOut(fadeTime, //function(){$(curSlide).fadeIn(fadeTime);if(x == (i - 1)){x = 0;} else {x++;}});setTimeout(showSlide, JSonData.CONF_NEW_PRODUCTS_FRIQ);}

// Jcarusel hits show
    $(".carouselDiv .jCarouselLite").jCarouselLite({auto: JSonData.CONF_HITS_FRIQ, speed: JSonData.CONF_HITS_SPEED, vertical: true, visible: JSonData.CONF_SCROLL_HITS, easing: "easeinout"});

// HideSlide hits show
    var fadeTime = 2000*(JSonData.CONF_HITS_FRIQ/10000);var i = $('.slide').length;var x = 0;showSlide();function showSlide(){curSlide = "#slide" + x;if(x == 0){prevSlide = "#slide" + (i - 1);} else {prevSlide = "#slide" + (x - 1);}$(prevSlide).fadeOut(fadeTime, function(){$(curSlide).fadeIn(fadeTime);if(x == (i - 1)){x = 0;} else {x++;}});setTimeout(showSlide, JSonData.CONF_HITS_FRIQ);}

// Tabs

    if (JSonData.REVIEW_SAVED != 1) {$.tabs('.tabs a');} else {$.tabs('.tabs a', '#tab_review');}

// Search
    $('#mod_search_searchword').keydown(function(e) {if (e.keyCode == 13) {moduleSearch();}});

// Tag flash cloud
	    var rnumber = Math.floor(Math.random()*9999999);
	    var so = new SWFObject("./core/tagcloud.swf?r="+rnumber, "tagcloudflash", "160", "160", "9", "#ffffff");
	    so.addParam("allowScriptAccess", "always");
	    so.addParam("wmode", "transparent");
	    so.addVariable("tcolor", "0x565656");
	    so.addVariable("tspeed", "150");
	    so.addVariable("distr", "true");
	    so.addVariable("mode", "tags");
	    so.addVariable("tagcloud", JSonData.TAGS_CLOUD);
	    so.write("wpcumuluscontent");
});

