var nocache = 0;
function BindRequirement(pageIndex) {
    
        nocache = Math.random();
        nGridCnt = document.getElementById('cmbGridCnt').value;
        http.open('get', 'controller/propertylist.php?PropertyList=Show&GridCnt=' + nGridCnt + '&PageIndex=' + pageIndex + '&nocache = ' + nocache);
        http.onreadystatechange = GetListReply;
        http.send(null);
}

function OnLoadRequirement() {

    var Transaction_Type = "";
    var PropMinValue = "";
    var PropMaxValue = "";
    var Location = "";
    var PropType = "";
    var State = "";
    var City = "";
    var Location = "";
    var id = "";
    
    strFullURL = parent.document.URL;
    nTransType = strFullURL.substring(strFullURL.indexOf('?') + 1, strFullURL.length);
    //alert(decode64(nTransType));
    if(!nTransType.contains('SESSION'))
    {
        if(nTransType.length>100){
            nTransType = encode64(window.location.search);
            nTransType1 =decode64(nTransType);
        }
        else if(nTransType=="0&0&0&0&0"){
        nTransType1 = strFullURL.substring(strFullURL.indexOf('?') + 1, strFullURL.length);
        }
        else{
        nTransType = encode64(strFullURL.substring(strFullURL.indexOf('?') + 1, strFullURL.length));
        nTransType1 = decode64(strFullURL.substring(strFullURL.indexOf('?') + 1, strFullURL.length));
        }
    }
    else
    nTransType1="0&0&0&0&0";
    if (nTransType1.contains('&')) {
        
        var arrValue = nTransType1.split('&');
        if (arrValue.length == 6) {

            
            if (arrValue[0] != "0") {
                Transaction_Type = arrValue[0];
            }
            if (arrValue[1] != "0") {
                PropMinValue = arrValue[1];
            }
            if (arrValue[2] != "0") {
                PropMaxValue = arrValue[2];
            }
            if (arrValue[3] != "0") {
                if (arrValue[3].contains('%2C')) {
                    tempArrValue = arrValue[3].split('%2C');
                    Location = tempArrValue[0];
                    City = tempArrValue[1];
                    State = tempArrValue[2];
                    
                }
                else {
                    Location = arrValue[3];
                    City = arrValue[3];
                    State = arrValue[3];
                    
                }
            }
            if (arrValue[4] != "0") {
                PropType = arrValue[4];
            }

            if (Transaction_Type == "" && PropMinValue == "" && PropMaxValue == "" && Location == "" && PropType == "") {
                document.getElementById('login').innerHTML = "<b><a href='#' onclick='CheckLoggedOut()'>LOGOUT</a></b>";
                //document.getElementById('CP').innerHTML = "<a href='admin/controlpanel.html' style='cursor: pointer;'><img src='/images/goback.png' title='Click to go to Control panel Page' width='15px' height='15px' style='border: 0px' /></a>";
                ClearSearch();
                
            }
            else {
                 
                nocache = Math.random();
                document.getElementById('login').innerHTML = "<b><a href='login.html?"+encode64('10')+">LOGIN</a></b>";
                nGridCnt = document.getElementById('cmbGridCnt').value;
                http.open('get', 'controller/propertylist.php?PropertyList=Search&PriceRangeMinimum=' + PropMinValue + '&PriceRangeMaximum=' + PropMaxValue + '&PropertyType=' + PropType + '&TransactionType=' + Transaction_Type + '&Location=' + Location + '&City=' + City + '&State=' + State + '&PageIndex=' + nGridCnt + '&nocache = ' + nocache);
                http.onreadystatechange = GetListReply;
                http.send(null);
            }
        }
        else if(arrValue.length == 10 || arrValue.length == 9)
        {
            if(arrValue.length==9)
            {
               var arrExtrackarray;
               arrExtrackarray=nTransType1;
               arrExtrackarray=nTransType1.split('34sd3%24');
               var newstring=arrExtrackarray[0] + "&optType=null"+arrExtrackarray[1];
               nTransType1=newstring
            }
            var arrValues;var strPropMinValue="";var arrPropMinValue;
            var strPropMaxValue="";var arrPropMaxValue;var arrTransactionType;
            var arrLocation;var Location; var City; var State;var arrPropertyType;
            var strPropertyType;var strTransactionType;
            arrValues = nTransType1.split('&');
            
            arrPropMinValue=arrValues[2].split('=');
            
            if(arrPropMinValue[1]=='--+Select+--')
                strPropMinValue="";
            else
             strPropMinValue=arrPropMinValue[1];
            
            arrPropMaxValue=arrValues[3].split('=');
            
            if(arrPropMaxValue[1]=='--+Select+--')
                strPropMaxValue="";
            else
             strPropMaxValue=arrPropMaxValue[1];
            
            arrTransactionType=arrValues[1].split('='); 
            if(arrTransactionType[1]!='null')
            {
                strTransactionType=decode64(arrTransactionType[1]);
                strTransactionType=strTransactionType.replace('Γά0','').replace(/^\s*/, "").replace(/\s*$/, "");
            }
            else
             strTransactionType="";
            arrLocation=arrValues[4].split('='); 
            if (arrLocation[1] != "") {
                if (arrLocation[1].contains('%2C')) {
                    tempArrValue = arrLocation[1].split('%2C');
                    Location =tempArrValue[0];
                    City = tempArrValue[1];
                    State = tempArrValue[2];
                    
                }
                else {
                    Location = arrLocation[1];
                    City = arrLocation[1];
                    State =arrLocation[1];
                    
                }
            }
            arrPropertyType=arrValues[5].split('='); 
            
            if(arrPropertyType[1]!="MA%3D%3D"){
            strPropertyType=arrPropertyType[1];
            }
            else
            strPropertyType=0;
            
            document.getElementById('login').innerHTML = "<b><a href='login.html?"+encode64('10')+">LOGIN</a></b>";
            nGridCnt = document.getElementById('cmbGridCnt').value;
           
            http.open('get', 'controller/propertylist.php?PropertyList=Search&PriceRangeMinimum=' + strPropMinValue + '&PriceRangeMaximum=' + strPropMaxValue + '&PropertyType=' + strPropertyType + '&TransactionType=' + strTransactionType + '&Location=' + Location + '&City=' + City + '&State=' + State + '&PageIndex=' + nGridCnt + '&nocache = ' + nocache);
            http.onreadystatechange = GetListReply;
            http.send(null);
        }
        else
        {
            //document.getElementById('login').innerHTML = "<b><a href='#' onclick='CheckLoggedOut()'>LOGOUT</a></b>";
           
                ClearSearch();
                BindRequirement(1);
        }
                
    }
    else if(decode64(nTransType1)=="3 "){
    document.getElementById('login').innerHTML = "<b><a href='#' onclick='CheckLoggedOut()'>LOGOUT</a></b>";
    //document.getElementById('CP').innerHTML = "<a href='admin/controlpanel.html' style='cursor: pointer;'><img src='/images/goback.png' title='Click to go to Control panel Page' width='15px' height='15px' style='border: 0px' /></a>";
                ClearSearch();
                BindRequirement(1);
    }
    else 
    {
        id = nTransType.substring(nTransType.indexOf('=') + 1, nTransType.length);
        
        if (id != "") {
            nocache = Math.random();
            document.getElementById('login').innerHTML = "<b><a href='login.html?" + encode64('10') + ">LOGIN</a></b>";
            document.getElementById('CP').innerHTML = "";
            nGridCnt = document.getElementById('cmbGridCnt').value;
            http.open('get', 'controller/propertylist.php?PropertyList=GetParticularProperty&ID=' + id + '&GridCnt=' + nGridCnt + '&PageIndex=' + '1' + '&nocache = ' + nocache);
            http.onreadystatechange = GetListReply;
            http.send(null);
        }
    }
   
   
}


function GetListReply() {
    if (http.readyState == 4) {
        //alert(http.responseText);
        var response = http.responseText;
        document.getElementById('pnlDataGrid').innerHTML = response;
        InsertLog("Property List", 0);
        if (document.getElementById("hdsetpaging").value == "")
            SetPaging();
    }
}


function RequirementSearch() {
    var Location = "";
    var City = "";
    var State = "";
    nocache = Math.random();
    CreateSearchRequirementControls();
    if (ValidSearchAll() == true) {
        var Transaction_Type = "";
        if (optSell.checked == true) {
            Transaction_Type = "Sell";
        }

        if (optBuy.checked == true) {
            Transaction_Type = "Buy";
        }
        
        if (optRent.checked == true) {
            Transaction_Type = "Rent";
        }
        
        if (optLease.checked == true) {
            Transaction_Type = "Lease";
        }
        if (txtLocation.value == "[ e.g. Burma Colony, Subramaniapuram, Karaikudi, Tamil Nadu ]") {
            txtLocation.value = "";
        }
        else {
            if (txtLocation.value.contains('%2C')) {
                Loc = txtLocation.value.split('%2C');
                Location = Loc[0];
                City = Loc[1];
                State = Loc[2];
            }
            else {
                Location = txtLocation.value;
                City = txtLocation.value;
                State = txtLocation.value;
            }
           
        }
        var cmPropType = "";
        if (cmbPropertyType.value == "0") {
            cmPropType = "";
        }
        else {
            cmPropType = cmbPropertyType.value; 
        }

        http.open('get', 'controller/propertylist.php?PropertyList=Search&PriceRangeMinimum=' + cmbPriceRangeMinimum.value + '&PriceRangeMaximum=' + cmbPriceRangeMaximum.value + '&PropertyType=' + cmPropType + '&TransactionType=' + Transaction_Type + '&Location=' + Location + '&City=' + City + '&State=' + State + '&nocache = ' + nocache +'&Pagename='+ window.document.forms[0].name);
        http.onreadystatechange = GetSearchReply;
        http.send(null);
    }
}
function GetSearchReply() {
        if (http.readyState == 4) {
        var response = http.responseText;
        document.getElementById('pnlDataGrid').innerHTML = response;
        InsertLog("Property List", 0);
        if (document.getElementById("hdsetpaging").value == "")
            SetPaging();
    }
}

function CreateSearchRequirementControls() {
    optBuy = document.getElementById('optBuy');
    optSell = document.getElementById('optSell');
    optRent = document.getElementById('optRent');
    optLease = document.getElementById('optLease');
    cmbPriceRangeMinimum = document.getElementById('cmbPriceRangeMinimum');
    cmbPriceRangeMaximum = document.getElementById('cmbPriceRangeMaximum');
    txtLocation = document.getElementById('txtLocation');
    cmbPropertyType = document.getElementById('cmbPropertyType');
    btnSearch = document.getElementById('btnSearch');
}
function ClearSearchRequirementControls() {
    CreateSearchRequirementControls();
    optBuy.checked = false;
    optSell.checked = false;
    optRent.checked = false;
    optLease.checked = false;
    cmbPriceRangeMinimum.value = 0;
    cmbPriceRangeMaximum.value = 0;
    txtLocation.value = "";
    cmbPropertyType.value = 0;
    
}
function ValidSearchAll() {
    if (optBuy.checked == false && optSell.checked == false && optRent.checked == false && optLease.checked == false && cmbPriceRangeMinimum.value == "" && cmbPriceRangeMaximum.value == "" && txtLocation.value == "[ e.g. Burma Colony, Subramaniapuram, Karaikudi, Tamil Nadu ]" && cmbPropertyType.value == "0") {
        alert("Select any of the search criteria!");
        return false;
    }
    else
        return true;
}


function Formquery_String()
{
var type="";
    CreateSearchRequirementControls();
    if (optBuy.checked == true) {
            type = "Buy";
        }

        else if (optSell.checked == true) {
            if (type != "") {
                type = type + "&Sell";
            }
            else {
                type = "Sell";
            }
        }

        else if (optRent.checked == true) {

            type = "Rent";
        }
        else if (optLease.checked == true) {

            type = "Lease";
        }
        else {
            type = "0";
        }

        if (cmbPriceRangeMinimum.value != "") {
            if (type != "") {
                type = type + "&" + cmbPriceRangeMinimum.value;
            }
            else {
                type = cmbPriceRangeMinimum.value;
            }
        }
        else {
            type = type + "&0";
        }
        if (cmbPriceRangeMaximum.value != "") {
            if (type != "") {
                type = type + "&" + cmbPriceRangeMaximum.value;
            }
            else {
                type = cmbPriceRangeMaximum.value;
            }
        }
        else {
            type = type + "&0";
        }
        if (txtLocation.value != "[ e.g. Burma Colony, Subramaniapuram, Karaikudi, Tamil Nadu ]") {
            if (type != "") {
                type = type + "&" + txtLocation.value;
            }
            else {
                type = txtLocation.value;
            }
        }
        else {
            type = type + "&0";
        }
        if (cmbPropertyType.value != "0") {
            if (type != "") {
                type = type + "&" + cmbPropertyType.value;
            }
            else {
                type = cmbPropertyType.value;
            }
        }
        else {
            type = type + "&0";
        }
        type = type +"&"+window.document.forms[0].name;
        return encode64(type);
}

function GoFunction() {
    CreateSearchRequirementControls();
    //ClearSearch();
   
    if (optBuy.checked == false && optSell.checked == false && optRent.checked == false && optLease.checked == false && cmbPriceRangeMinimum.value == "" && cmbPriceRangeMaximum.value == "" && txtLocation.value == "[ e.g. Burma Colony, Subramaniapuram, Karaikudi, Tamil Nadu ]" && cmbPropertyType.value == "0") {
        alert("Select any of the search criteria!");
        return false;
    }
    else {
        if (parent.document.URL.search("/admin/") != -1) {
            type = Formquery_String();
            window.location = '../propertylisting.html?' + type;
        }
        else {
            type = Formquery_String();
            window.location = 'propertylisting.html?' + type;
        }
    }
}


function ClearSearch() {
    nocache = Math.random();
    http.open('get', 'controller/propertylist.php?PropertyList=ClearSearch&Clear=0&nocache = ' + nocache);
    http.onreadystatechange = GetSearchClearReply;
    http.send(null);
}
function GetSearchClearReply() 
{
    if (http.readyState == 4) 
    {
            var response="";
                nocache = Math.random();
                if(document.getElementById('cmbGridCnt')==null)
                    nGridCnt=5;
                else
                    nGridCnt = document.getElementById('cmbGridCnt').value;
                http.open('get', 'controller/propertylist.php?PropertyList=Show&GridCnt=' + nGridCnt + '&PageIndex=' + '1' + '&nocache = ' + nocache);
                http.onreadystatechange = GetListReply;
                http.send(null);
    }
}


function Paging(CurrPage, TotPage, ClickAction, WhichGrid) {
    cCurrPage = document.getElementById(CurrPage);
    cTotPage = document.getElementById(TotPage);
    chdPaging = document.getElementById("hdsetpaging");
    chdPaging.value = "";
    strCurPage = cCurrPage.innerHTML;
    strTotPage = cTotPage.innerHTML;
    pageIndex = 0;
    if (strTotPage != "0") {
        if (ClickAction == "First") {
            if (parseInt(strCurPage) > 1) {
                cCurrPage.innerHTML = 1;
                pageIndex = 1;

                if (WhichGrid == "propertylist")
                    BindRequirement(pageIndex);

            }
        }
        else if (ClickAction == "Prev") {
            if (parseInt(strCurPage) > 1) {
                cCurrPage.innerHTML = parseInt(cCurrPage.innerHTML) - 1;
                pageIndex = cCurrPage.innerHTML;

                if (WhichGrid == "propertylist")
                    BindRequirement(pageIndex);

            }
            else {
                cCurrPage.innerHTML = 1;
                pageIndex = 1;
            }
        }
        else if (ClickAction == "Next") {
            if (parseInt(strCurPage) < parseInt(strTotPage)) {
                cCurrPage.innerHTML = parseInt(cCurrPage.innerHTML) + 1;
                pageIndex = cCurrPage.innerHTML;

                if (WhichGrid == "propertylist")
                    BindRequirement(pageIndex);

            }
            else {
                cCurrPage.innerHTML = cTotPage.innerHTML;
                pageIndex = cTotPage.innerHTML;
            }
        }
        else if (ClickAction == "Last") {
            if (parseInt(strCurPage) < parseInt(strTotPage)) {
                cCurrPage.innerHTML = cTotPage.innerHTML;
                pageIndex = cTotPage.innerHTML;

                if (WhichGrid == "propertylist")
                    BindRequirement(pageIndex);

            }
        }
    }
    chdPaging.value = pageIndex;
}
function SetPaging() {
    nocache = Math.random();
    http.open('get', 'controller/propertylist.php?PropertyList=InitPaging' + '&nocache = ' + nocache);
    http.onreadystatechange = GetPagingDetail;
    http.send(null);
}
function GetPagingDetail() {
    if (http.readyState == 4) {
        var response = http.responseText;
        tmp = response.split(",");
        document.getElementById("lblTotRec").innerHTML = tmp[0];
        document.getElementById("lblTotPage").innerHTML = tmp[1];

        if (tmp[1] > 0)
            document.getElementById("lblCurrPage").innerHTML = 1;
        else
            document.getElementById("lblCurrPage").innerHTML = 0;
       
    }

}
function ChangeGridCnt() {
    document.getElementById("hdsetpaging").value = "";
    BindRequirement(1);
}

