﻿// JScript File
var RegularId = null;
var CaptchaId = null;


function ShowPhone(RegularIdPrefix, CaptchaIdPrefix, OrdinalNum)
{
    RegularId = RegularIdPrefix;
    CaptchaId = CaptchaIdPrefix;
    PageMethods.GetPhone(OrdinalNum, ShowPhoneOnSucceeded, ShowPhoneOnFailed);
}
function ShowPhoneOnSucceeded(phone)
{
    if(phone.Status == 1)
    {
        var cellPhone = document.getElementById(RegularId + 'cellPhone');
        cellPhone.innerHTML = phone.PhoneNum;
        cellPhone.style.display = '';
        // הצג מספר div
//        var rowButton = document.getElementById(RegularId + 'rowButton');
//        rowButton.style.display = 'none';
    }
    else if(phone.Status == 2)
    {
        var tblCaptcha = document.getElementById(CaptchaId + 'tblCaptcha');
        tblCaptcha.style.display = '';
    }
}
function ShowPhoneOnFailed(ex)
{
    alert("ShowPhoneOnFailed " + ex.get_message());
}

function btnCancelCaptcha_Click(CaptchaIdPrefix)
{
    var tblCaptcha = document.getElementById(CaptchaIdPrefix + 'tblCaptcha');
    tblCaptcha.style.display = 'none';
}

var Content3 = 'ctl00_middleContent_';
function ResetStreetAutoComplete()
{         
    var streetAutoComplete = $find('<%= aceStreet.BehaviorID %>');               
    var city = document.getElementById(Content3 + 'txtCity');
    
    if (city && streetAutoComplete)
    {
        // use encodeURI because hebrew letters will be tranfered
        // in the query string
        streetAutoComplete.set_serviceMethod('<%= ConfigurationManager.AppSettings["aceStreetServiceMethod"] %>'
            + '?city=' + encodeURI(city.value));
        streetAutoComplete._cache = null;
    }
}
                
function AutoCompleteInitialize()
{       
    var txtCity = document.getElementById(Content3 + 'txtCity');
    
    if (txtCity)
    {
        // onchange doesn't work when selecting from autoComplete with the mouse
        //city.onchange = ResetStreetAutoComplete; 
        txtCity.onblur = ResetStreetAutoComplete; 
    }

    //var cityAutoComplete = $find('<%= aceCity.BehaviorID %>');
    //var streetAutoComplete = $find('<%= aceStreet.BehaviorID %>');
}

function ChangeImgCaptcha(OrdinalNum)
{
    var img = document.getElementById('imgCaptcha_' + OrdinalNum);
    var seed = Math.floor(Math.random()*10000) + 1;
    img.src = AppRoot + '/CaptchaImage.ashx?random=' + seed + '&code=' + OrdinalNum;
}

function btnSearch_click()
{
    var content = 'ctl00_middleContent_';
    var name = $('#' + content + 'txtLastName').val();
    var city = $('#' + content + 'txtCity').val();
    var street = $('#' + content + 'txtStreet').val();
    var house = $('#' + content + 'txtHouse').val();
        
    var isValid = true;
    $('#rfvLastName').hide();
    $('#rfvCity').hide();
    $('#spCapp').hide();
    
    if(name.length < 2)
    {
        $('#rfvLastName').show();
        isValid = false;
    }
    if(city.length == 0)
    {
        $('#rfvCity').show();
        isValid = false;
    }
    var CaptchaImage = $('.CaptchaImage1').val();
    if(CaptchaImage.length != 4)
    {
    	
        $('#spCapp').show();
        isValid = false;
    }
    
    $('.CaptchaImage1').val('');
    

    if(isValid)
    {

        Waiting_ucWaiting.DisplayFloatingDiv();
        PageMethods.DoSearch(name, city, street, house, CaptchaImage, DoSearchSucceeded);
        //$("img.CapImg").attr({src: "/web/GetCaptchaRemote.aspx"});
    }
}
function DoSearchSucceeded(data)
{

    var randomNum = Math.floor(Math.random()*100001);
    $('#imgCaptcha').attr('src','');
	$('#imgCaptcha').attr('src','/web/captcha.aspx?id=' + randomNum);
        
        
        
    Waiting_ucWaiting.HiddenFloatingDiv();
    if(data != '')
    {
        transform.SetXmlText(data);
        
        ShowAll();
    }    
}

var CurrOrdinalNum = null;
function ShowPhoneA(OrdinalNum)
{
    CurrOrdinalNum = OrdinalNum;
    PageMethods.GetPhone(OrdinalNum, ShowPhoneAOnSucceeded, ShowPhoneAOnFailed);
}
function ShowPhoneAOnSucceeded(phone)
{
    if(phone.Status == 1)
    {
        $('#cellPhone_' + CurrOrdinalNum).html(phone.PhoneNum);
        $('#cellPhone_' + CurrOrdinalNum).show();
    }
    else if(phone.Status == 2)
    {
        $('#tblCaptcha_' + CurrOrdinalNum).show();
    }
}
function ShowPhoneAOnFailed(ex)
{
    alert("ShowPhoneAOnFailed " + ex.get_message());
}
function CancelCaptcha(OrdinalNum)
{
    $('#tblCaptcha_' + OrdinalNum).hide();
}

function CheckCaptchaA(OrdinalNum)
{
    CurrOrdinalNum = OrdinalNum;
    var txt = $('#txtCaptcha_' + CurrOrdinalNum).val();
    PageMethods.CheckCaptcha(CurrOrdinalNum, txt, CheckCaptchaAOnSucceeded, CheckCaptchaAOnFailed);
}
function CheckCaptchaAOnSucceeded(phone)
{
    if(phone.Status == 1)
    {
        $('#tblCaptcha_' + CurrOrdinalNum).hide();
        ShowPhoneAOnSucceeded(phone);
    }
    else if(phone.Status == -3)
    {
        alert("קוד שגוי");
    }
}
function CheckCaptchaAOnFailed(ex)
{
    alert("ShowPhoneOnFailed " + ex.get_message());
}
function OpenResehetResult(Code)
{
    var elem = $('#divReshetResult_' + Code);
    if(elem.is(':visible'))
    {
        elem.hide();
    }
    else
    {
        elem.show();
    }
}
function ShowAll()
{
    Show('All');
}
function ShowCell()
{
    Show('Cell');
}
function ShowLine()
{
    Show('Line');
}
function Show(type)
{
    var dPhoneNum = document.getElementById('divPhoneNum');
    var xpath = '//Configuration/Show';
    var value = type;
    
    transform.ChangeXmlElement(xpath, value);
    transform.TransformNodeIntoObj(dPhoneNum);
}

