﻿function InitRequest()
{
    try
    {
        // Firefox, Opera 8.0+, Safari 
        request=new XMLHttpRequest(); 
    }
    catch (e)
    { // Internet Explorer 
        try
        { 
        request = new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch (e)
        { 
            try
            { 
                request = new ActiveXObject("Microsoft.XMLHTTP"); 
            }
            catch (e)
            { 
                alert("Your browser does not support AJAX!"); 
                return false; 
            } 
        } 
    } 
}

var request;
var refreshPage;
InitRequest();

function SendRequest(webServiceUrl, callBackFunction, refreshPage)
{
    this.refreshPage = refreshPage;
    request.open("GET", webServiceUrl, true);
    if (callBackFunction != null)
        request.onreadystatechange = callBackFunction;
    request.send(null);
}

function SendSyncRequest(webServiceUrl)
{
    request.open("GET", webServiceUrl, false);
    request.send(null);
    
    if (request.readyState == 4)
        if (request.status == 200)
			try
			{
				return request.responseXML.getElementsByTagName('string')[0].firstChild.nodeValue;
			}
			catch (e)
			{
				return null;
			}
	
	return null;
}

function GenericCallBack()
{
    if (request.readyState == 4)
    {
        if (request.status == 200)
        {
            alert(request.responseXML.getElementsByTagName('string')[0].firstChild.nodeValue);
            if (refreshPage == 1)
            {
                window.location.reload();
            }
            if (refreshPage == -1)
            {
                history.go(-1);
            }
        }
        else
        {
            alert(request.statusText);
        }
    }
}

function RedirectCallBack()
{
    if (request.readyState == 4)
    {
        if (request.status == 200)
        {
            location.href = request.responseXML.getElementsByTagName('string')[0].firstChild.nodeValue;
        }
        else
        {
            alert(request.statusText);
        }
    }
}

function SilentCallBack()
{
    if (request.readyState == 4)
    {
        if (request.status == 200)
        {
			// nothing
        }
        else
        {
            alert(request.statusText);
        }
    }
}

function setFocus(controlName)
{
    var control = document.getElementById(controlName);
    if(control)
        control.focus();
}

function Redirect(url, isNewWindow)
{
    if (url.toLowerCase().indexOf('http') < 0)
        url = 'http://' + url;
    if (isNewWindow == 1)
        window.open(url, Math.random().toString(16).replace('.', '').replace(',', ''));
    else
        location.href = url;
}

function ReplaceRedirect(url, oldValue, newValue, isNewWindow)
{
	url = url.replace(oldValue, newValue);
	Redirect(url, isNewWindow);
}

function IsSelectedValueEnabled(ctrlSelect)
{
    return ctrlSelect.options[ctrlSelect.selectedIndex].disabled != true;
}

function GetCheckedRadio(name)
{
    var radioButtons = document.getElementsByName(name);
    for (var x = 0; x < radioButtons.length; x ++)
    {
        if (radioButtons[x].checked)
        {
            return radioButtons[x].value;
        }
    }
}

// JOB Start //
function DeleteJobVacancy(id, refreshPage)
{
    SendRequest("/Job/JobWebService.asmx/DeleteJobVacancy?id=" + id, GenericCallBack, refreshPage)
}

function DeleteJobResume(id, refreshPage)
{
    SendRequest("/Job/JobWebService.asmx/DeleteJobResume?id=" + id, GenericCallBack, refreshPage)
}
// JOB End //

// Auto Start //
function DeleteAutoSale(id, refreshPage)
{
    SendRequest("/Auto/AutoWebService.asmx/DeleteAutoSale?id=" + id, GenericCallBack, refreshPage)
}
// Auto End //

// Realty Start //
function DeleteFlatSale(id, refreshPage)
{
    SendRequest("/Realty/RealtyWebService.asmx/DeleteFlatSale?id=" + id, GenericCallBack, refreshPage)
}
// Realty End //

// DoAdv Start //
function DeleteDoAdv(id, refreshPage)
{
    SendRequest("/Do/DoWebService.asmx/DeleteDoAdv?id=" + id, GenericCallBack, refreshPage)
}
// DoAdv End //

// Для всех объявлений Start //
function Ban(byClientHash, entityType, id, refreshPage)
{
    SendRequest("/Admin/AdminWebService.asmx/Ban?byClientHash=" + byClientHash + "&entityType=" + entityType + "&id=" + id, GenericCallBack, refreshPage)
}

function LimitCheckBoxListItemSelectedCount(cblName, maxSelectedCount)
{
    var chkboxList = document.getElementById(cblName);
    var chkboxes = chkboxList.getElementsByTagName("input");
    var selectedCount = 0;
    for( i = 0; i < chkboxes.length; ++i )
    {
        if (chkboxes[i].checked)
        {
            selectedCount ++;
            if (selectedCount > maxSelectedCount)
                chkboxes[i].checked = false;
        }
    }
}

function cblIsRequiredValidation(cblName, sender, args)
{
    var chkboxList = document.getElementById(cblName);
    var chkboxes = chkboxList.getElementsByTagName("input");
    var selectedCount = 0;
    for( i = 0; i < chkboxes.length; ++i )
    {
        if (chkboxes[i].checked)
        {
            args.IsValid = true;
            return;
        }
    }
   args.IsValid = false;
   return;            
}

function MinLengthValidation(tbName, minLength, sender, args)
{
    if (document.getElementById(tbName).value!="" && 
        document.getElementById(tbName).value.length < minLength)
    {
        args.IsValid = false;
    } else {
        args.IsValid = true;
    }
}

function RangeValidation(tbName, min, max, sender, args)
{
    var val = document.getElementById(tbName).value;
    if (val == null || val == "")
        return;
    
    if (val >= min && val <= max)
    {
        args.IsValid = true;
    } else {
        args.IsValid = false;
    }
}

// Для всех объявлений End //

String.prototype.format = function()
{
	var pattern = /\{\d+\}/g;
	var args = arguments;
	return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}

// Ec Start//
function GetLatLngByAddress(streetId, houseId)
{
    return SendSyncRequest("/Ec/EcWebService.asmx/GetLatLngByAddress?streetIdStr=" + streetId + "&houseIdStr=" + houseId)
}

function DeleteEcUserComment(id, refreshPage)
{
    SendRequest("/Ec/EcWebService.asmx/DeleteEcUserComment?id=" + id, GenericCallBack, refreshPage)
}

function DeleteEcUser(id, refreshPage)
{
    SendRequest("/Ec/EcWebService.asmx/DeleteEcUser?id=" + id, GenericCallBack, refreshPage)
}

function DeleteEcDiscount(id, refreshPage)
{
    SendRequest("/Ec/EcWebService.asmx/DeleteEcDiscount?id=" + id, GenericCallBack, refreshPage)
}

function EcUser_UpdateRating(id, rating)
{
    SendRequest("/Ec/EcWebService.asmx/EcUser_UpdateRating?userId=" + id + "&rating=" + rating, SilentCallBack, 0);
    divRating.style.display = "none";
    hdnRatingValue = parseInt(document.getElementById('hdnRating').value);
    hdnRatingVoteCountValue = parseInt(document.getElementById('hdnRatingVoteCount').value);
    hdnRatingVoteCountValue = hdnRatingVoteCountValue + 1;
    document.getElementById('bCurrentRating').innerText = parseInt((hdnRatingValue + parseInt(rating))/hdnRatingVoteCountValue) + " (" + hdnRatingVoteCountValue + ")";
    debugger;
}

// Ec End//
