//Data load

$(document).ready(function(){
$('#PasteSvhCallback').append('<a href="#svh_cb_dialog" name="cbmodal"><div id="svh_callback"><div id="cb_img"></div></div></a><div id="svh_boxes"><div id="svh_cb_dialog" class="cbwindow"><div class="cb_chello">Заказ звонка:</div><div id="svh_first"><div class="callback"><form method="post" action="" ><div class="l1_1">Вас зовут:</div><div class="l1_2"><input name="nv_params[name]" class="svh_input_a" value=""></div> <div class="l1_z"> </div><div class="l1_1">Ваш телефон:</div><div class="l1_2"><input name="nv_params[phone]" id="svh_mp" class="svh_input_a" value="+7"></div> <div class="l1_z"> </div><div class="l1_3"><input type="submit" name="svhBtn" id="getitem" class="sBtn" value="Заказать звонок!" disabled><input type="hidden" name="submit_callback" value=""></div></form></div></div><div id="svh_load">Размещение запроса...</div><div class="nv_error"></div><div class="nv_success"></div><div class="svh_copy">Работает на <a href="http://svh.by/solutions/simple-callback" target="_blank">Callback Simple</a></div><div class="svh_close"></div></div><div id="svh_mask"></div></div>');
$('#PasteSvhBpcBtn').append('<a href="#svh_bpc_dialog" name="bpcmodal"><div id="svh_bpc"><div id="bpc_img"></div></div></a>');
$('#PasteSvhBpcFooter').append('<div id="svh_bpc_load">Размещение запроса...</div><div class="nv_error"></div><div class="nv_success"></div><div class="svh_copy">Работает на <a href="http://svh.by/solutions/simple-bpc" target="_blank">Buy per click Simple</a></div><div class="svh_close"></div>');
});

//Form
$(document).ready(function(){
	
	$('.bpc form, .callback form').submit(function(e){
		
		e.preventDefault();
		
		$('.nv_success').html('');
		$('.nv_error').html('');
		
		var now = Math.round(new Date()/1000);
		
		$('.sBtn').attr('disabled', 1);
		
		var post_data = $(this).serialize();
		
		sendRequest('/folder/subfolder/subsubfolder/cb_bpc/index.php', post_data, function(result){
			if(!result.error[0])
			{
				$('.nv_success').html(result.success);
				document.cookie = 'lst='+now+';expires='+(now+3600*24*7)+';';
				
			}
			else
			{
				var error = '';
				
				for(var err in result.error)
				{
					error+=''+result.error[err]+'';
				}
				
				$('.nv_error').html(error);
				
			}
    
    		$('.sBtn').removeAttr('disabled');
		});
		
	});	
	
})

function sendRequest(url,post_data,callback)
{
	$('#svh_first').hide();
	$('#svh_bpc_first').hide();
	$('#svh_load').show();
	$('#svh_bpc_load').show();
	
    $.ajaxSetup({cache: false}); 
    
    var result_content = '';
    
	$.ajax({
	    type: 'POST',
	    url: url,
	    data: post_data,
	    dataType: "text",
	    cache: false,
	    timeout: 0,
	    async: true,
	    success: function(msg){
	    	$('#svh_load, #svh_bpc_load').hide();
			result_content = $.parseJSON(msg);
         	callback(result_content);
     	},
	        
	    error: function(msg){
	    	$('#svh_load, #svh_bpc_load').hide();
	        alert('ошибка запроса: '+url);
	    }   
     
	});
 
	return result_content;
}

//Phone number check
$(document).ready(function(){
		$('#svh_mp, #svh_cb_mp').bind("keyup", function()
		{
			if(isValidPhone($(this).val()))
			{
				$(this).addClass('svh_input_ok');
				$(this).removeClass('svh_input_a');
				$('.sBtn').removeAttr('disabled');
			}
			else
			{
				$(this).addClass('svh_input_a');
				$(this).removeClass('svh_input_ok');
				$('.sBtn').attr('disabled', 1);
			}
					
		});
	});
			
		
		function isValidPhone(phone) 
		{
		 	var pattern = new RegExp(/^((\+?7|8)(?!95[4-79]|99[^2457]|907|94[^0]|336)([348]\d|9[0-689]|7[07])\d{8}|\+?(99[^456]\d{7,11}|994\d{9}|9955\d{8}|996[57]\d{8}|380[34569]\d{8}|375[234]\d{8}|372\d{7,8}|37[0-4]\d{8}))$/);
			return pattern.test(phone);
		}

			

//Callback window
$(document).ready(function() {
 $('a[name=cbmodal]').click(function(e) {
  e.preventDefault();
  var id = $(this).attr('href');
  
  $('#svh_mask').fadeIn(1000);
  $('#svh_mask').fadeTo("slow",0.8);
  

  $(id).fadeIn(2000);
 });
 $('.cbwindow .close').click(function (e) {
  e.preventDefault();
  $('#svh_mask, .cbwindow').hide();
 });
 $('#svh_mask').click(function () {
  $(this).hide();
  $('.cbwindow').hide();
 });
  $('.svh_close').click(function () {
  $('#svh_mask').hide();
  $('.cbwindow').hide();
 });
});

//Buy per Click window
$(document).ready(function() {
 $('a[name=bpcmodal]').click(function(e) {
  e.preventDefault();
  var id = $(this).attr('href');
  
  $('#svh_mask').fadeIn(1000);
  $('#svh_mask').fadeTo("slow",0.8);
  

  $(id).fadeIn(2000);
 });
 $('.bpcwindow .close').click(function (e) {
  e.preventDefault();
  $('#svh_mask, .bpcwindow').hide();
 });
 $('#svh_mask').click(function () {
  $(this).hide();
  $('.bpcwindow').hide();
 });
  $('.svh_close').click(function () {
  $('#svh_mask').hide();
  $('.bpcwindow').hide();
 });
});


$(document).ready(function() {
	$('a[name=cbmodal], a[name=bpcmodal]').click(function(e) {
	$('#svh_first').show();
	$('#svh_bpc_first').show();
	$('#svh_load').hide();
	$('#svh_bpc_load').hide();
	$('.nv_error').html('');
	$('.nv_success').html('');
	});
	});