var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);

function validateEmail(str)
{
    var testresults
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
        testresults=true
    else
    {
        testresults=false
    }
    return (testresults)
}

function validateLogin(field) {
	var re = /^[0-9-'.'_']*$/;
	if (!re.test(field.value)) {
		field.value = field.value.replace(/[^0-9a-zA-Z-'-'_']/g,"");
	}
}

function shopAddToCart(productid,updatediv, modurl) 
{  
	if ($('#modelid').val() == 0 && $('#size').val() == 0)
		alert(cmnlang['choosesizeandmodel']);
	else if ($('#modelid').val()==0)
		alert(cmnlang['choosemodel']);
	else if ($('#size').val() == 0)
		alert(cmnlang['choosesize']);
	else
	{

		$("#cartLoader").html('<img src="/img/loader-cart.gif" />');
	postBody = '&dummy='+(new Date().getTime())+'&productid='+productid+'&mod=shop&action=add&lang='+currentlang+'&modurl='+modurl;
	if ($('#size').val())
		postBody = postBody + '&size='+$('#size').val();
	
		$.ajax({
			   type: "POST", 
			   url: "/ajax.php", 
			   data: postBody, 
			   dataType: "json",
			   //error: function(e, xhr){ 
			   		//alert(e);
				//	} ,
			   success: function(json){ 
						if(json.type == 'success')
						{
		
							$("#"+updatediv).empty().html(json.message).fadeIn('slow');
							$("#cartLoader").html('');
							//alert(cmnlang['addtocartsuccess']);
							jQuery.facebox("" + cmnlang['addtocartsuccess']); 
						}
						else if(json.type == 'error')
						{
							$("#cartLoader").html('');
							jQuery.facebox(json.message); 
						}
					} 
				});	
	}
}

function shopChangeModel(model,updatediv) 
{  
	$("#model"+model).click();
		
}

function shopApplyShipping(price)
{
	if (price == 0)
	{
		$('#shippinginfo').fadeOut();	
	}
	else
		$('#shippinginfo').fadeIn();	
	
	
	var total = parseInt($('#totalproductprice').val()) + price+parseInt($('#packingprice').text());
	$('#totalprice').text(total);
	$('#shippingprice').text(price);
}

function shopApplyPackaging(price,include)
{
	if (include.checked)
	{
		var total = parseInt($('#totalproductprice').val()) + price+parseInt($('#shippingprice').text());
	}
	else
	{
		var total = parseInt($('#totalproductprice').val())+parseInt($('#shippingprice').text());
		price = 0;
	}
	$('#totalprice').text(total);
	$('#packingprice').text(price);
}



function shopRemoveFromCart(product) 
{  
	if (product == 'bag')
		$("#includebag").val(0);
	else
	{
		$("#quantity"+product).val(0);
	}
	$("#formsubmit").click();
	
}




function closeHint()
{
	setCookie('drookyhinthide',1,365);
	
	$('#hintcorner').hide();
	$('#closehint').hide();
	
	var h = $('#infohint').height() + parseInt($('#infohint').css('paddingTop')) + parseInt($('#infohint').css('paddingBottom'))-8;

  $('#infohint').animate({h: '40px',marginTop: parseInt($('#infohint').css('marginTop')) < 0 ? 0 : -h},1000,function(){$('#hintbottom').show();fixFooterWidth()}); 

}

function showHint()
{
	setCookie('drookyhinthide',0,365);
	
	$('#hintbottom').hide();	
	
	
	var h = $('#infohint').height() + parseInt($('#infohint').css('paddingTop')) + parseInt($('#infohint').css('paddingBottom'))-8;

  $('#infohint').animate({h: '40px',marginTop: parseInt($('#infohint').css('marginTop')) < 0 ? 0 : -h},1000,function(){$('#hintcorner').show();$('#closehint').show();fixFooterWidth();}); 

}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; path=/; expires="+exdate.toGMTString());
}

function processAjaxContacts(cform,curlang) 
{  
	$('#contactmessage').html('<img src="/img/loader.gif" class="vmiddle" />');	
	postBody = '&dummy='+(new Date().getTime())+'&mod=feedback&name='+cform.name.value+'&email='+cform.email.value+'&message='+cform.message.value+'&zahystvidspameriv='+cform.zahystvidspameriv.value+'&lang='+curlang;
	$.ajax({type:"POST",url: '/ajax.php',data: postBody, success: function(html){$('#contactmessage').html(html);$('#sequrecode').attr('src','/zahystvidspameriv.php?'+(new Date().getTime()));if (html.indexOf('feedbackMessageErr') == -1) cform.reset()}});
}


function checkContactForm(cform, curlang) {
    if (cform.name.value == "" )
    {
		if (curlang == 'ru')
			errmsg = 'Введите свое имя!';
		else
			errmsg = 'Введіть своє ім\'я!';
		
		$('#contactmessage').html('<span  class="feedbackMessageErr">'+errmsg+'</span>');
        cform.name.focus();
        return false;
    }
	
	if (cform.email.value != "" )
    {
		if(!validateEmail(cform.email.value)) {
			if (curlang == 'ru')
				errmsg = 'Введите корректный e-mail!';
			else
				errmsg = 'Введіть коректний e-mail!';
			$('#contactmessage').html('<span  class="feedbackMessageErr">'+errmsg+'</span>');
			cform.email.focus();
			return false;				
		}	
    }

    if (cform.message.value == "" )
    {
		if (curlang == 'ru')
			errmsg = 'Введите свое сообщение!';
		else
			errmsg = 'Введіть текст повідомлення!';	
		$('#contactmessage').html('<span  class="feedbackMessageErr">'+errmsg+'</span>');
        cform.message.focus();
        return false;
    }
    if (cform.zahystvidspameriv.value == "" )
    {
		if (curlang == 'ru')
			errmsg = 'Введите код!';
		else if (curlang == 'en')
			errmsg = 'Enter code!';
		else
			errmsg = 'Введіть код!';			
		$('#contactmessage').html('<span  class="feedbackMessageErr">'+errmsg+'</span>');
			
        cform.zahystvidspameriv.focus();
        return false;
    } 

	//$('contactmessage').innerHTML = '<img  src="/img/progressbar.gif"  />';

    processAjaxContacts(cform,curlang);
	return false;

}

 

function fixFooterWidth()
{
	var maincontent = document.getElementById('maincontent');  
   	if (maincontent.clientHeight >= maincontent.scrollHeight)
    	document.getElementById('footer').style.right = 0;  
	else
		document.getElementById('footer').style.right = '15px';  
		
	if (maincontent.clientWidth >= maincontent.scrollWidth)
    	document.getElementById('footer').style.bottom = '-1px';  
	else
		document.getElementById('footer').style.bottom = '14px';  
}

// Process commenting
function sbmComment(comment_post_id,comment_type,comment_text, comment_subscribe) {
	$('#commentform').hide();	
	$('#comments-loading').show();
	if (comment_subscribe.checked)
		subscribe = comment_subscribe.value;
	else
		subscribe = 0;
	postBody = 'dummy='+(new Date().getTime())+'&mod=comments&pid='+comment_post_id+'&comment='+encodeURIComponent(comment_text.value)+'&type='+comment_type+'&lang='+currentlang+'&subscribe='+subscribe;
	$.ajax({type: "POST", url: "/ajax.php", data: postBody, success: function(html){ $('#comments-loading').hide();$("#commentsblock").empty().html(html).fadeIn('slow'); } });		
	comment_text.value = '';
}



function showCommentForm()
{
	var d=document.getElementById("commentform");
	if(d)
	{
		ce = document.getElementById("comment-error");
		if (ce)
			ce.style.display='none';
		if (d.style.display == 'block')
			d.style.display = 'none';
		else
		{
			d.style.display = 'block';
			d.focus();
		}
	}
}

function goToTop(){
	$('#maincontent').animate({scrollTop: '0px'}, 700);
}


function isNumber(field) {
	var re = /^[0-9'.']*$/;
	if (!re.test(field.value)) {
		field.value = field.value.replace(/[^0-9'.']/g,"");
	}
}

