﻿// JScript File 
// Author: Trung.Nguyen
// Com: ERA Viet Nam.

// START - Property form

// Enable or disable price textbox when click price checkbox
function ChangeEnableControl(sControlID, vState)
{
    var Control = document.getElementById(sControlID);
    if(vState)
    {
        Control.disabled = false;
        Control.value = '';
    }
    else
    {
        Control.value = '';
        Control.disabled = true;
    }
}
function ChangeNegotiateControl(NegotiateID, sCostID, chkID, IsNegotiate)
{
    var txt = document.getElementById(sCostID);
    var Negotiate = document.getElementById(NegotiateID);
    var chk = document.getElementById(chkID);
    var ischek = true;
    if(IsNegotiate)
    {
        txt.value = '';
        txt.disabled = ischek;
        chk.checked = !ischek;
        chk.disabled = ischek;
    }
    else
    {
        txt.value = '';
        txt.disabled = !ischek;
        chk.checked = ischek;
        chk.disabled = !ischek;
    }
}
function autoresize(txtbox)
{
    var cols = txtbox.cols ;
    var content = txtbox.value ;
    var lineCount = 0 ;

    var lastEOL = -1 ;
    do {
        var begin = lastEOL+1 ;
        lastEOL = content.indexOf("\n",lastEOL+1) ;
        var line = "" ;
        if(lastEOL != -1) {
            line = content.substring(begin,lastEOL) ;
        } else {
            line = content.substring(begin,content.length) ;
        }
        var rows_in_line = Math.floor(line.length/cols)+1 ;
        lineCount += rows_in_line
    } while (lastEOL != -1) ;
    txtbox.rows = lineCount ;
}
// Check 2 price control
function CheckEnterPrice(sRentPriceID, sSellPriceId, chkNegotiateSell, chkNegotiateRent,chkSell,chkRent)
{
    var RentControl = document.getElementById(sRentPriceID);
    var SellControl = document.getElementById(sSellPriceId);
    var NegotiateRentControl = document.getElementById(chkNegotiateRent);
    var NegotiateSellControl = document.getElementById(chkNegotiateSell);
    
    var chkRentControl = document.getElementById(chkRent);
    var chkSellControl = document.getElementById(chkSell);
    var IsValid = true;
    if(NegotiateRentControl.checked && NegotiateSellControl.checked)
        return true;
    if(!NegotiateRentControl.checked && !NegotiateSellControl.checked)
    {
        if(!chkRentControl.checked && !chkSellControl.checked)
        {
            return false;
        }
        else
        {
            if(chkRentControl.checked && chkSellControl.checked)
            {
                if(RentControl.value.trim().empty())
                    return false;
                if(RentControl.value <= 0)
                    return false;
                if(SellControl.value.trim().empty())
                    return false;
                if(SellControl.value <= 0)
                    return false;
                return true;
            }
            if(chkRentControl.checked && !chkSellControl.checked)
            {
                if(RentControl.value.trim().empty())
                    return false;
                if(RentControl.value <= 0)
                    return false;
                return true;
            }
            if(!chkRentControl.checked && chkSellControl.checked)
            {
                if(SellControl.value.trim().empty())
                    return false;
                if(SellControl.value <= 0)
                    return false;
                return true;
            }
            
        }
    }
    if(NegotiateRentControl.checked && !NegotiateSellControl.checked)
    {
        if(chkSellControl.checked)
        {
            if(SellControl.value.trim().empty())
                return false;
            if(SellControl.value <= 0)
                return false;
        }
        return true;
    }
    if(!NegotiateRentControl.checked && NegotiateSellControl.checked)
    {
        if(chkRentControl.checked)
        {
            if(RentControl.value.trim().empty())
                return false;
            if(RentControl.value <= 0)
                return false;
        }
        return true;
    }
    
    return true;
}

// END - Property form

// START - QUICK SEARCH

// set null when focus on textbox
function SetNullValue(txtName)
{
    txtName.value = "";
}

// set default value for textbox
function SetTextBoxDefaultValue(txtName, DV)
{
    if(txtName.value == "" || txtName.value == null)
        txtName = DV;
}

// END - QUICK SEARCH

// START - LISTINGPROPERTY

function HidenDivTag(dv1, dv2)
{
    document.getElementById(dv1).style.display='none';
    document.getElementById(dv2).style.display='none'; 
}

// END - LISTINGPROPERTY

// START - LOGIN on MASTERPAGE

function ValidateBeforeLogin()
  {
        var cEmail = document.getElementById('tEmail');
        var cPw = document.getElementById('tPw');
        //alert('call server: ' + cEmail.value + '-$$$-' + hex_md5(cPw.value));
        CallLoginServer(cEmail.value + '-$$$-' + hex_md5(cPw.value), 'Call login form server');
        //alert('call end');
  }
function CallIfHasLogin()
  {
       
        CallLoginServer('CheckLogin-$$$-');
        //alert('call end');
  } 
function ReceiveLoginResult(arg, context) {
    if(arg == "ok")
    {
        document.getElementById('sconLoginForm').style.display = 'none';
        document.getElementById('sconLogin').style.display = '';
    }
    else
    {
        if(arg != '')
          {
            var messBox = document.getElementById('Msg');
            messBox.innerHTML = arg;
            messBox.style.color="red"
         }
    }
}

function DoLoginByEnter(e)
{
    var key;
    if(window.event)
          key = window.event.keyCode;     //IE
    else
          key = e.which;     //firefox
    if (key == 13)
        ValidateBeforeLogin();
}
function CheckUserHasLogin(bLg)
{
    var isLogin = Boolean(bLg);
    if(isLogin)
    {
        document.getElementById('sconLoginForm').style.display = 'none';
        document.getElementById('sconLogin').style.display = '';
    }
}
String.prototype.trim = function() { 
  var start = -1,end = this.length;
  while( this.charCodeAt(--end) < 33 );
  while( this.charCodeAt(++start) < 33 );
  return this.slice( start, end + 1 );
};
String.prototype.empty = function() { 
  if(this.length == 0)
  {
    return true;
  }
  return false;
};
// END - LOGIN on MASTERPAGE
