var url = window.location.host;
//var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//var pageTracker = _gat._getTracker("UA-5957829-1"); pageTracker._trackPageview();


function validateEmail(email)
{
    var reg = /(\w|\.+)@([a-z]|-|\d|_)+(\.[a-z]+){1,10}$/i;
 
    var result = reg.test(email);
 
    if(!result)
    {
        return false;
    }
    else
    {
        return true;
    }
}


function uploadMagazine(id)
{
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "journal",  
      func:    "fileUpload",  
      ident:   id
    },
    function ( data1 )
    {
    }
  );
}

function sendMail( idStatus, idComment, status )
{
  if( idStatus != '' && idComment != '' && status != '' )
  {
    $.post(
      "http://"+url+"/index.php",
      { 
        s:       "ajax",
        ajax:    "comments",  
        func:    "sendMail",  
        ident:   idStatus,
        comment: idComment,
        status:  status
      },
      function ( data )
      {
        alert( data );
        $("#send"+idComment).removeAttr('onclick');
      }
    );
  }
}

function deleteCatalogs()
{
  if(confirm('Вы действительно желаете удалить?')) 
  { 
    $("form").submit(); 
  }
}

function changeManual(id)
{
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "manual",  
      func:    "changeManual",  
      city_id:   id
    },
    function ( data1 )
    {
      $("#citi_box").html( data1 );
    }
  );
}

function OpenCloseComments()
{
  if($('#viewComments').css('display') == 'block' )
  { 
    $('#viewComments').css('display','none'); 
    $('#viewCommentsAddDown').css('display','none'); 
    $('#otkl_').html('Включить');
    $('#otkl').html('Включить');
  } 
  else 
  {
    $('#viewComments').css('display','block'); 
    $('#viewCommentsAddDown').css('display','block'); 
    $('#otkl_').html('Отключить');
    $('#otkl').html('Отключить');
  }
}

function changeNews(id)
{

  $('#title_news').html('Wait..');
    
    
  $('#inbox_news').empty();
  $('#inbox_news').html('Wait..');
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "news",  
      func:    "getNewsMenu",  
      ident:   id
    },
    function ( data1 )
    {
      $('#title_news').empty();
      $('#title_news').html(data1);
    }
  );
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "news",  
      func:    "getNews",  
      ident:   id
    },
    function ( data2 )
    {
      $('#inbox_news').empty();
      $('#inbox_news').html(data2);
    }
  );

}

function changeArticle(id)
{

  $('#title_article').html('Wait..');
    
    
  $('#inbox_article').empty();
  $('#inbox_article').html('Wait..');
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "article",  
      func:    "getArticleMenu",  
      ident:   id
    },
    function ( data1 )
    {
      $('#title_article').empty();
      $('#title_article').html(data1);
    }
  );
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "article",  
      func:    "getArticle",  
      ident:   id
    },
    function ( data2 )
    {
      $('#inbox_article').empty();
      $('#inbox_article').html(data2);
    }
  );

}

function changeNewsDet( id )
{
  $('#title_news').html('Wait..');
    
    
  $('#inbox_news').empty();
  $('#inbox_news').html('Wait..');
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "news",  
      func:    "getNewsMenuDet",  
      ident:   id
    },
    function ( data1 )
    {
      $('#title_news').empty();
      $('#title_news').html(data1);
    }
  );
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "news",  
      func:    "getNewsDet",  
      ident:   id
    },
    function ( data2 )
    {
      $('#inbox_news').empty();
      $('#inbox_news').html(data2);
    }
  );
}

function changeArticleDet( id )
{
  $('#title_article').html('Wait..');
    
    
  $('#inbox_article').empty();
  $('#inbox_article').html('Wait..');
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "article",  
      func:    "getArticleMenuDet",  
      ident:   id
    },
    function ( data1 )
    {
      $('#title_article').empty();
      $('#title_article').html(data1);
    }
  );
  
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "article",  
      func:    "getArticleDet",  
      ident:   id
    },
    function ( data2 )
    {
      $('#inbox_article').empty();
      $('#inbox_article').html(data2);
    }
  );
}

function fileDownload(id)
{
  return false;
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "journal",  
      func:    "fileDownload",  
      ident:   id
    },
    function ( data1 )
    {
      alert( data );
    }
  );
}

function clearAddComment()
{
 // alert(  $("#nameComment").val());
  $("#nameComment").val('');
  $("#textComment").val('');
  $("#captcha").val('');
  refreshimg();
}

function clearAddCommentAnother()
{
 // alert(  $("#nameComment").val());
  $("#nameComment1").val('');
  $("#textComment1").val('');
  $("#captcha1").val('');
  refreshimg1();
}

function addComment( action, ident )
{
  var status;
  
  if( action == "news" )
  {
    status = "bl_viewComment";
  }
  else if( action == "article" )
  {
    status = "bl_viewCommentArticle";
  }

  if(  $("#nameComment").val() != '' && $("#textComment").val() != '' )
  {
    $.post(
      "http://"+url+"/index.php",
      { 
        s:       "ajax",
        ajax:    "comments",  
        func:    "addComment",
        action:  action,
        ident:   ident,
        name:    $("#nameComment").val(),
        text:    $("#textComment").val() 
      },
      function ( data2 )
      {
        alert(data2);
        clearAddComment();
        $.post(
          "http://"+url+"/index.php",
          { 
            s:       "ajax",
            ajax:    "comments",  
            func:    status,
            id:      ident,
            action:  "addComment"
          },
          function ( data3 )
          {
            $("#viewComments").html(data3);
          }
        );
      }
    );
  }
}



function addCommentAnother( action, ident )
{
  var status;
  
  if( action == "news" )
  {
    status = "bl_viewComment";
  }
  else if( action == "article" )
  {
    status = "bl_viewCommentArticle";
  }
  
  if(  $("#nameComment1").val() != '' && $("#textComment1").val() != '' )
  {
    $.post(
      "http://"+url+"/index.php",
      { 
        s:       "ajax",
        ajax:    "comments",  
        func:    "addComment",
        action:  action,
        ident:   ident,
        name:    $("#nameComment1").val(),
        text:    $("#textComment1").val() 
      },
      function ( data2 )
      {
        alert(data2);
        clearAddCommentAnother();
        $.post(
          "http://"+url+"/index.php",
          { 
            s:       "ajax",
            ajax:    "comments",  
            func:    status,
            id:      ident,
            action:  "addComment"
          },
          function ( data3 )
          {
            $("#viewComments").html(data3);
          }
        );
      }
    );
  }
}

function viewAddComment()
{
  
  $('#addComm').css("display", "block");
  $('#addCommAnother').css("display", "none");
  refreshimg1();
}

function viewAddCommentAnother()
{
    $('#addCommAnother').css("display", "block");
   $('#addComm').css("display", "none");
   refreshimg();

}

function changePage( ident, page, status )
{
  var func;
  if( status == "article" )
  {
    func = "bl_viewCommentArticle";
  }
  else
  {
    func = "bl_viewComment";
  }
  $.post(
    "http://"+url+"/index.php",
    { 
      s:       "ajax",
      ajax:    "comments",  
      func:    func,
      id:      ident,
      action:  "addComment",
      page:    page
    },
    function ( data3 )
    {
      $("#viewComments").html(data3);
    }
  );
}

function viewContentSearchObj ( id )
{
  $("#searchRealEstate").empty();
  $("#searchRealEstate").html('Waiting..');
  $.post(
    "http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "objects",  
      func: "bl_viewCatObj",
      id:   id
    },
    function ( data )
    {
      $("#searchRealEstate").empty();
      $("#searchRealEstate").html(data);
    }
  );
}
function viewContentSearch( id )
{
  $("#searchRealEstate").empty();
  $("#searchRealEstate").html('Waiting..');
  $.post(
    "http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "catalog",  
      func: "viewContentSearch",
      id:   id
    },
    function ( data )
    {
      $("#searchRealEstate").empty();
      $("#searchRealEstate").html(data);
    }
  );
}

function viewContentSearchLeft( id )
{
  $("#searchRealEstateLeft").empty();
  $("#searchRealEstateLeft").html('Waiting..');
  $.post(
    "http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "objects",  
      func: "viewCatObjLeft",
      id:   id
    },
    function ( data )
    {
      $("#searchRealEstateLeft").empty();
      $("#searchRealEstateLeft").html(data);
    }
  );
}

//captcha
// CREATING THE REQUEST


function refreshimg()
{
	//var url = 'image_req.php';
	//dorefresh(url, displayimg);
  document.getElementById('captcha').style.border = '1px solid #ffffff';
  document.getElementById('captcha').style.background = '#fffffff';
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "comments",  
      func: "sessionComment"
    },
    function ( data )
    {
      $("#idCaptcha").html(data);
      $('#captcha').empty();
    }
  );
}

function refreshimg1()
{
	//var url = 'image_req.php';
	//dorefresh(url, displayimg);
  document.getElementById('captcha1').style.border = '1px solid #ffffff';
  document.getElementById('captcha1').style.background = '#fffffff';
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "comments",  
      func: "sessionComment1"
    },
    function ( data )
    {
      $("#idCaptcha1").html(data);
      $('#captcha1').empty();
    }
  );
}




// SUBMISSION

function check(type, ident)
{
	var submission = $('#captcha').val();
//	var url = 'process.php?captcha=' + submission;
	//docheck(url, displaycheck);
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "comments",  
      func: "checkCaptchaComment",
      captcha: submission
    },
    function ( data )
    {
      if(data == '1')
  		{
  			document.getElementById('captcha').style.border = '1px solid #49c24f';
  			document.getElementById('captcha').style.background = '#bcffbf';
        addComment(type, ident);
  		}
  		if(data == '0')
  		{
  			document.getElementById('captcha').style.border = '1px solid #c24949';
  			document.getElementById('captcha').style.background = '#ffbcbc';
        alert('Wrong image code');
  		}
    }
  );
}

function checkCaptcha()
{
	var submission = $('#captcha').val();
//	var url = 'process.php?captcha=' + submission;
	//docheck(url, displaycheck);
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "comments",  
      func: "checkCaptchaComment",
      captcha: submission
    },
    function ( data )
    {
      if(data == '1')
  		{
  			document.getElementById('captcha').style.border = '1px solid #49c24f';
  			document.getElementById('captcha').style.background = '#bcffbf';
        $("form").submit();
  		}
  		if(data == '0')
  		{
  			document.getElementById('captcha').style.border = '1px solid #c24949';
  			document.getElementById('captcha').style.background = '#ffbcbc';
        alert('Wrong image code');
  		}
    }
  );
}

function check1(type, ident)
{
	var submission = $('#captcha1').val();
//	var url = 'process.php?captcha=' + submission;
	//docheck(url, displaycheck);
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "comments",  
      func: "checkCaptchaComment1",
      captcha1: submission
    },
    function ( data )
    {
      if(data == '1')
  		{
  			document.getElementById('captcha1').style.border = '1px solid #49c24f';
  			document.getElementById('captcha1').style.background = '#bcffbf';
        addCommentAnother(type, ident);
  		}
  		if(data == '0')
  		{
  			document.getElementById('captcha1').style.border = '1px solid #c24949';
  			document.getElementById('captcha1').style.background = '#ffbcbc';
        alert('Wrong image code');
  		}
    }
  );
}

/*function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';

		}
	}
}*/

function addAttantion( id, status )
{
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "attantion",  
      func: "addAttantion",
      ident: id,
      status: status
    },
    function ( data )
    {
      if( data != 'false' )
      {
        $("#attantion_quest").html(data);
      }
    }
  );
}

function minusAttantion( id, status )
{
  $.post("http://"+url+"/index.php",
    { 
      s:    "ajax",
      ajax: "attantion",  
      func: "minusAttantion",
      ident: id,
      status: status
    },
    function ( data )
    {
      if( data != 'false' )
      {
        $("#attantion_quest").html(data);
      }
    }
  );
}

function funcDownloadSubmit()
{
    var error='';
    var check = false;
    
    if (( $("#url").val() == '' ) || ( $("#url").val() == 'http://' ))
    {
        error = error + "<font color='red'>URL cann't be empty</font><br />";
    }
    else
    {
        reg_url = /http:\/\//;       
        if ($("#url").val().search(reg_url)!=0)
        {
            error = error + "<font color='red'>URL is incorrect. It should begin from http:// </font><br />";
        }
    }
    
    if( $("#email").val() == '' ) 
    {
        error = error + "<font color='red'>Email Address cann't be empty</font><br />";
    }
    else
    {   
        if (!validateEmail($("#email").val()))
        {
            error = error + "<font color='red'>Incorrect e-mail</font><br />"; 
        }
        
    }
    
    if (error == '')
    {
        $.post(
            "http://"+url+"/admin/index.php",
            {
                s:  "ajax", 
                ev: "checkForCurrentPage",
                url: $("#url").val()
            },
            function( data )
            {
                if( data == 1)
                {
                    
                    $.post(
                        "http://"+url+"/admin/index.php",
                        {
                            s:  "ajax", 
                            ev: "checkEmailByUrl",
                            url: $("#url").val(),
                            email: $("#email").val()
                        },
                        function( data1 )
                        {
                            if( data1 == 1)
                            {
                                check = true;
                                if( (error == '') && (check == true) )
                                {
                                    $("#download_form").submit();
                                }
                            }
                            else
                            {
                                error = error + "<font color='red'>Such url has been used by another email</font><br />";
                                $("#errors").html(error);
                            }
                        }
                    );
                }
                else
                {
                    error = error + "<font color='red'>Url can not be the same as current site url</font><br />";
                    $("#errors").html(error);
                }
            }
        );
    }
    
    if( (error == '') && (check == true) )
    {
        $("#download_form").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;
}



function hideCopyButtons()
{
    if (navigator.appName.indexOf("Explorer") == -1)
        $(".copy_button").hide();
}


