function ajaxRequest(url, div)
{
	$('#'+div).fadeTo("fast", 0.33);

	$.ajax({
	  url: url,
	  cache: false,
	  success: function(data) {
		$('#'+div).html(data);
		$('#'+div).fadeTo("fast", 1);
	  }
	});

	return false;
}

function ajaxPostRequest(url, args, div)
{
	$('#'+div).fadeTo("fast", 0.33);

	$.ajax({
	   type: "POST",
	   url: url,
	   cache: false,
	   data: args,
	   success: function(data){
		 $('#'+div).html(data);
		 $('#'+div).fadeTo("fast", 1);
	   }
	 });

	 return false;
}

function validEmail(email)
{
	return (/^[\w\.\-@]+$/).test(email);
}


function checkCallbackForm()
{
	p = document.getElementById('id_callback_number');
	if(p && (p.value=="" || p.value=="Введите номер телефона"))
	{
		alert('Введите номер телефона');
		p.focus();
		return false;
	}
	
	return true;
}

function checkNotifyForm()
{
	p = document.getElementById('id_notify_email');
	if(p && p.value=="")
	{
		alert('Введите E-mail');
		p.focus();
		return false;
	}
	
	return true;
}

function checkCallbackForm2()
{
	p = document.getElementById('id_callback2_number');
	if(p && (p.value=="" || p.value=="Введите номер телефона"))
	{
		alert('Введите номер телефона');
		p.className = "required";
		p.focus();
		return false;
	}
	
	return true;
}

function countAddBtn(id, type)
{
	callback = "";
	if(type==1)
		callback = "AddToFav('"+window.location.href+"', '"+document.title+"')";
	if(type==2)
		callback = "callback_wishes()";
	if(type==3)
		callback = "callback_ukrnet()";
	ajaxRequest('action.php?countAddBtn=1&type='+type+'&id='+id, "adddiv", callback);
	return false;
}

function AddToFav(url, title)
{
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }

}

function callback_wishes()
{
	document.getElementById('wishes_form').submit();
}

function callback_ukrnet()
{
	UkrNet.AddLinkFloat();
}


function ctrlEnter(event, formElem)
{
	if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
		window.open('zvonok.htm', '', 'width=500,height=200,dependent=yes,scrollbars=1');
    }
}

function scrollItems(index, pos)
{
	current_pos = items_cfg[index][0];
	item_count = items_cfg[index][1];
	per_block = items_cfg[index][2];

	current_pos += pos;
	if(current_pos < 0)
		current_pos = 0;
	if(current_pos >= item_count - per_block)
		current_pos = item_count - per_block;
	
	if($('#'+index+'_items')[0]) {
		$('#'+index+'_items').fadeOut('fast', function() {
			// Animation complete.

			for(i=0;i<item_count;i++)
			{
				p = document.getElementById(index+'_item_'+i);
				if(!p)
					continue;
				p.style.display = (i >= current_pos && i < current_pos+per_block) ? "block" : "none";
			}

			$('#'+index+'_items').fadeIn('fast');
		});	
	} else {
		for(i=0;i<item_count;i++)
			{
				p = document.getElementById(index+'_item_'+i);
				if(!p)
					continue;
				p.style.display = (i >= current_pos && i < current_pos+per_block) ? "block" : "none";
			}
	}


	document.getElementById(index+'_scroll_left').style.display = (current_pos > 0) ? "block" : "none";
	document.getElementById(index+'_scroll_right').style.display = (current_pos < item_count - per_block) ? "block" : "none";

	items_cfg[index][0] = current_pos;
	return false;
}

function is_numeric (mixed_var) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: David
    // +   improved by: taith
    // +   bugfixed by: Tim de Koning
    // +   bugfixed by: WebDevHobo (http://webdevhobo.blogspot.com/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: is_numeric(186.31);
    // *     returns 1: true
    // *     example 2: is_numeric('Kevin van Zonneveld');
    // *     returns 2: false
    // *     example 3: is_numeric('+186.31e2');
    // *     returns 3: true
    // *     example 4: is_numeric('');
    // *     returns 4: false
    // *     example 4: is_numeric([]);
    // *     returns 4: false

    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}

function showCollection(id)
{
	p = document.getElementById('citems'+id);
	if(!p)
		return false;

	p.style.display = p.style.display=='none' ? 'block' : 'none';
	pimg = document.getElementById('citemimg'+id);
	if(pimg)
		pimg.src = p.style.display=='none' ? 'img/splus.gif' : 'img/sminus.gif';
	return false;
}

function sendNotifyForm(v_product_id)
{
	var v_name = $('#notify_name').attr('value');
	var v_email = $('#notify_email').attr('value');
	var v_tel = $('#notify_tel').attr('value');

	if(v_email=='')
	{ alert('Укажите e-mail для уведомлений'); return false; }

	$('id_notify_text').innerHTML = "<img src='img/ajax-loader.gif' />";

	// ajax
	ajaxPostRequest('action.php?notify', 'name='+v_name+'&email='+v_email+'&id='+v_product_id+'&tel='+v_tel, 'id_notify_text');

	return false;
}


