﻿
var strSpaceMin;
var strSpaceMax;

function $(strId) {
    return document.getElementById(strId);
}
var masterctl = 'ctl00_ContentPlaceHolder1_';

function Search() 
{
    var tempURL;
    var lstSubmarkets = document.getElementById('ctl00_ContentPlaceHolder1_lstSubmarkets');
	
    if (Verify())
    {
        tempURL = "PropertyListing.aspx?AvailableOnly=";
        if (document.getElementById('ctl00_ContentPlaceHolder1_chkAvailableOnly').checked == 1)
        {
	        tempURL = tempURL + "1";
        }
        else
        {
	        tempURL = tempURL + "0";
        }
        tempURL = tempURL + "&SpaceMin=" + strSpaceMin + "&SpaceMax=" + strSpaceMax;
		
        //Submarket
        if (lstSubmarkets[0].selected)
        {
	        tempURL = tempURL + "&Submarket=ALL";
        }
        else
        {
	        tempURL = tempURL + "&Submarket=";
	        for (i = 0; i < lstSubmarkets.length; i++)
	        {
		        if (lstSubmarkets[i].selected == true) 
		        {
			        tempURL = tempURL + (lstSubmarkets[i].value) + ";";
		        }
	        }
	        tempURL = tempURL.substring(0, tempURL.length-1);
        }
		
        //Address
        tempURL = tempURL + "&City=" + document.getElementById('ctl00_ContentPlaceHolder1_txtCity').value + "&State=" + document.getElementById('ctl00_ContentPlaceHolder1_drpState').value;
        tempURL = tempURL + "&ZipCode=" + document.getElementById('ctl00_ContentPlaceHolder1_txtZipCode').value;
		
        //Get MapView setting and pass it on to the new url
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        var view = "FALSE";
        for (var i=0;i<vars.length;i++) 
        {
            var pair = vars[i].split("=");
            if (pair[0] == "MapView") 
            {
              view = pair[1];
            }
        } 
        tempURL = tempURL + "&MapView=" + view;
        
        //alert(tempURL);
        location.href = tempURL;        				
    }	
}

function VerifySpace()
{
    if (document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMin').value == '')
    {
        strSpaceMin = 0;
    }
    else
    {
        strSpaceMin = document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMin').value;
    }
    if (document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMax').value == '')
    {
        strSpaceMax = 5000000;
    }
    else
    {
        strSpaceMax = document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMax').value;
    }
    if (validateNumber(document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMin').value))
    {
        if (validateNumber(document.getElementById('ctl00_ContentPlaceHolder1_txtSpaceMax').value))
        {
	        if (parseFloat(strSpaceMax) >= parseFloat(strSpaceMin))
	        {
		        return true;
	        }
	        else
	        {
		        alert('This maximum space must be greater than the minimum space');
		        return false;
	        }
        }
        else
        {
	        alert('You must enter a number for the maximum space');
	        return false;
        }
    }
    else
    {
        alert('You must enter a number for the minimum space');
        return false;
    }
}

function VerifySubmarket()
{
    var lstSubmarkets = document.getElementById('ctl00_ContentPlaceHolder1_lstSubmarkets');
    var boolSomethingSelected = false;
    for (i = 0; i < lstSubmarkets.length; i++)
    {
        if (lstSubmarkets[i].selected == true) 
        {
	        boolSomethingSelected = true;
        }
    }
    if (boolSomethingSelected) 
    {
        return true;
    }
    else
    {
        alert('You must select a Submarket to search for');
        return false;
    }
}

function VerifyAddress()
{
    if (validateString(document.getElementById('ctl00_ContentPlaceHolder1_txtCity').value) == true)
    {
        if (validateZipCode(document.getElementById('ctl00_ContentPlaceHolder1_txtZipCode').value) == true)
        {
	        return true;
        }
        else
        {
	        alert('You must enter a number for the zip code');
	        return false;
        }
    }
    else
    {
        alert('You must enter alphabetic characters for the city');
        return false;
    }
    return true;
}

function Verify()
{
    var boolSpace = false;
    var boolAddress = false;
    var boolSubmarket = false;
	
    boolSpace = VerifySpace();
    boolAddress = VerifyAddress();
    boolSubmarket = VerifySubmarket();
	
    if (boolSpace && boolAddress && boolSubmarket)
    {
        document.getElementById('aspnetForm').action = "PropertyListing.aspx";
        return true;
    }
    else
    {
        document.getElementById('aspnetForm').action = "";
        return false;
    }
}

function validateNumber(string) 
{
    var Chars = "0123456789,.";

    for (var i = 0; i < string.length; i++) 
    {
        if (Chars.indexOf(string.charAt(i)) == -1)
        return false;
    }
    return true;
} 

function validateString(string) 
{
    var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

    for (var i = 0; i < string.length; i++) 
    {
        if (Chars.indexOf(string.charAt(i)) == -1)
        return false;
    }
    return true;
} 

function validateZipCode(string) 
{
    var Chars = "0123456789- ";

    for (var i = 0; i < string.length; i++) 
    {
        if (Chars.indexOf(string.charAt(i)) == -1)
        return false;
    }
    return true;
} 

