﻿/********************************************************************************************************/
/*                                                                                                      */
/*Author: Tony Smith-Brewster                                                                           */
/*Date: 1st August 2007                                                                                 */
/*Description:                                                                                          */
/*   Javascript for results of initial search.                                                          */
/********************************************************************************************************/
var c_objMaxPrice ;
var c_objMinPrice;
var c_objRadius;
var c_objSchool;
var c_objCategory;
var intSelectedIndex;

document.onkeypress = checkKey;

function checkKey(Event){
    var ret = Event ? Event.which : window.event.keyCode;
    if(ret == 13){
        if(List.Base.style.visibility != "visible"){
            Search(false);
        }
    }
    return ret != 13;
}

AssignObjects();

/**********************************************************************/
/*Function: Search                                                    */
/*   Selects the hint highlighted and applys it to the town text box  */
/**********************************************************************/
function Search(AllowPartialPostcodeSearch){
var intCount;
var isValidSearch = false;

    if(objSearchBox.value.charAt(2) != ""){
        if(!isNaN(objSearchBox.value.charAt(2))){
    	        var postcode = new clsPostcode(objSearchBox.value);
	            isValidSearch = postcode.isValid;
	            if(AllowPartialPostcodeSearch){
	                if(!isValidSearch){
                        isValidSearch = postcode.IsPartPostcodeValid;
                    }
                }
        }else{
            if(objSearchBox.value.length > 2){  
                re = new RegExp("^([A-Za-z -]*)","i"); 
                var strValue = objSearchBox.value.toString();
                var arr = re.exec(strValue);
                if(arr){
                    isValidSearch = true;
                }
            }
        }
    }

    if(!isValidSearch){
        switch(getFileName().toLowerCase()){
            case "propertyresults.aspx":
                if (document.getElementById("Town").value.toUpperCase() == objSearchBox.value.toUpperCase()){
                    isValidSearch = true;
                }
            break;
            case "estateagentresults.aspx":
                isValidSearch = (document.getElementById("wSCAgent").value != "") ? true : isValidSearch;
                break;
            case "schoolresults.aspx":
                isValidSearch = (document.getElementById("wSCSchool").value != "") ? true : isValidSearch;
                break;
        }
    }

    if(isValidSearch){ 
        var arr = new Array();
        
        if(objSearchBox.value == "")
        {
            arr[0] = "NA";
        }else{
            arr[0] = objSearchBox.value.toString().replace(",","_"); //Cater for the array.
        }
        switch(getFileName().toLowerCase()){
            case "propertyresults.aspx":   
               // alert(c_objMaxPrice.options.count);      
                
                arr[1] = c_objMaxPrice.selectedIndex;  
                arr[2] = c_objMinPrice.selectedIndex;
                break;
                
            case "estateagentresults.aspx": 
                var strAgent = document.getElementById("wSCAgent").value;
                if(strAgent == "")
                {
                    arr[1] = "NA";
                }else{
                    arr[1] = strAgent.toString().replace(",","_");
                }     
                break;
                
            case "schoolresults.aspx":
                var strSchool = document.getElementById("wSCSchool").value;
                if(strSchool == "")
                {
                    arr[1] = "NA";
                }else{
                    arr[1] = strSchool.toString().replace(",","_");
                }             
                break;
               
            case "localdirectoryresults.aspx":
                intSelectedIndex = document.getElementById("wSCCategory").selectedIndex;
                arr[1] = document.getElementById("wSCCategory").options[intSelectedIndex].value; 
                break;
                
            default: 
                alert("No Valid Page");
       }
        
        var days = 21; //Cookie valid for 14 days
        var Cookie = new clsCookie(cookieName);
        Cookie.createCookie(cookieName,arr,days);
        window.location = GetURL();
        
    }else{
        alert(objSearchBox.value + " - does not appear to be a valid town or postcode.");
    }
		

}
/*************************************************************************/
/*Name: fnCheckPrice                                                     */
/*Purpose:                                                               */
/*  validates the price range                                            */
/*************************************************************************/
function fnCheckPrice(){
	
	var intMaxValue = c_objMaxPrice.selectedIndex;
	var intMinValue = c_objMinPrice.selectedIndex;
	
	if(intMaxValue < intMinValue){
		c_objMaxPrice.selectedIndex = intMinValue;
	}	
}
/*************************************************************************/
/*Name: getFileName                                                      */
/*Purpose:                                                               */
/*  Gets the current page name from the URL                              */
/*************************************************************************/
function getFileName() 
{
var url = document.URL;
var i = url.lastIndexOf('/') + 1;
var j = url.indexOf('#', i);
var k = url.indexOf('?', i);

    if(-1 == j){
            j = url.length;
    }
    if(-1 == k){
        k = url.length;
    }
    return (url.substring(i, Math.min(j, k)));
}

/*************************************************************************/
/*Name: AssignObjects                                                    */
/*Purpose:                                                               */
/*  Gets the current page name and loads the appropriate objects         */
/*************************************************************************/
function AssignObjects()
{
    switch(getFileName().toLowerCase())
    {
        case "propertyresults.aspx": LoadForProperty();
            break;
        case "estateagentresults.aspx": LoadForAgents();
            break;
        case "schoolresults.aspx": LoadForSchools();
            break;
        case "localdirectoryresults.aspx": LoadForDirectory();
            break;
       // default: alert("No Valid Page");
    }
}

/*************************************************************************/
/*Name: GetURL                                                           */
/*Purpose:                                                               */
/*  Gets the correct URL                                                 */
/*Actions:                                                               */
/*  Calls getFileName function to return the current page and uses that  */
/*  value to assign a URL                                                */
/*************************************************************************/
function GetURL(){
    switch(getFileName().toLowerCase())
    {
        case "propertyresults.aspx": 
             if(document.getElementById("Rent")){
                (document.getElementById("Rent").value.toLowerCase()=="true") ? 
                    strLocation = "propertyresults.aspx?Rent=true&Town=" + objSearchBox.value + "&MaxPrice=" + c_objMaxPrice[c_objMaxPrice.selectedIndex].value + "&MinPrice=" + c_objMinPrice[c_objMinPrice.selectedIndex].value + "&Radius=" + c_objRadius.value + "&OrderType=" + getCurrentOrder()		
                    : 
                    strLocation = "propertyresults.aspx?Town=" + objSearchBox.value + "&MaxPrice=" + c_objMaxPrice[c_objMaxPrice.selectedIndex].value + "&MinPrice=" + c_objMinPrice[c_objMinPrice.selectedIndex].value + "&Radius=" + c_objRadius.value + "&OrderType=" + getCurrentOrder();		
                    
            }
            break;
        case "estateagentresults.aspx": 
        if (c_objRadius==null){
                strLocation = "EstateAgentResults.aspx?Town=" + objSearchBox.value + "&Agent=" + c_objAgent.value;
            } else {
               strLocation = "EstateAgentResults.aspx?Town=" + objSearchBox.value + "&Radius=" + c_objRadius.value + "&Agent=" + c_objAgent.value;
            }
            break;
        case "schoolresults.aspx": 
            strLocation = "schoolresults.aspx?Town=" + objSearchBox.value + "&School=" + objSchool.value + "&SchoolType=" + getSchoolType();
            break;
         case "localdirectoryresults.aspx":
            strLocation = "localdirectoryresults.aspx?Town=" + objSearchBox.value + "&CategoryID=" + c_objCategory.options[intSelectedIndex].value + "&Radius=" + c_objRadius.value;
            break;   
        default: 
            strLocation = "";
    }
    return(strLocation);
}

/*****************************************************************************************/
/*Start Functions For PropertyResults.aspx Page:                                         */
/*****************************************************************************************/

function LoadForProperty()
{

    c_objMaxPrice = document.getElementById("iMaxPrice");
    c_objMinPrice = document.getElementById("iMinPrice");
    c_objRadius = document.getElementById("iRadius");
    
    (document.getElementById("Rent").value.toLowerCase()=="true") ? 
        LoadPricesForRent(c_objMinPrice,c_objMaxPrice) : 
        LoadPricesForSale(c_objMinPrice,c_objMaxPrice);

    ParsePropertyValues();  
}
/*************************************************************************/
/*Name: ParsePropertyValues                                              */
/*Purpose:                                                               */
/*  Loads the property results values on page entry                      */
/*************************************************************************/
function ParsePropertyValues(){
    var intRadius = parseInt(document.getElementById("Radius").value);
	var intMaxPrice = parseInt(document.getElementById("MaxPrice").value);
	var intMinPrice = parseInt(document.getElementById("MinPrice").value);
	var indexedQueryString = (intMaxPrice < 10000) ? true : false;
    
	objSearchBox.value = document.getElementById("Town").value;
	
		if(indexedQueryString){
			c_objMaxPrice.selectedIndex = c_objMaxPrice.options.length - 1;
		}
	
        for (i = 0; i < c_objMaxPrice.options.length; i++){
            if(c_objMaxPrice.options[i].value == intMaxPrice){
                c_objMaxPrice.options[i].selected = true;
                break;
            }
        }
		
		if(indexedQueryString){
            c_objMinPrice.selectedIndex = c_objMinPrice.options.length - 1;
        }
		
        for (i = 0; i < c_objMinPrice.options.length; i++){
            if(c_objMinPrice.options[i].value == intMinPrice){
                c_objMinPrice.options[i].selected = true;
                break;
            }
        }
        setPropertyOrder();
    
    switch(intRadius)
    {
        case 0: intRadius = 0; break; //Some logic can be irrelevant here but is in for readablity
        case 1: intRadius = 1; break;
        case 2: intRadius = 2; break;
        case 3: intRadius = 3; break;
        case 5: intRadius = 4; break;
        case 10: intRadius = 5; break;
        case 15: intRadius = 6; break;
        case 20: intRadius = 7; break;
        case 30: intRadius = 8; break;
        default: intRadius = 0; break;
    }
    
    if(c_objRadius != null){
	    c_objRadius.selectedIndex = intRadius;
	}
}
/*************************************************************************/
/*Name: getCurrentOrder                                                  */
/*Purpose:                                                               */
/*  Gets the current order for the property results                      */
/*************************************************************************/
function getCurrentOrder(){
var intOrder;

    if(document.getElementById("iPriceHigh").checked){
        intOrder = document.getElementById("iPriceHigh").value
    }
    else if(document.getElementById("iPriceLow").checked){
        intOrder = document.getElementById("iPriceLow").value
    }
    else{
        intOrder = document.getElementById("iProperty").value
    }
    return (intOrder);
}

/*************************************************************************/
/*Name: setPropertyOrder                                                 */
/*Purpose:                                                               */
/*  Sets the current order for the property results                      */
/*************************************************************************/
function setPropertyOrder()
{
    if(document.getElementById("OrderType")){
        switch(document.getElementById("OrderType").value){
            case "1":          
                document.getElementById("iPriceHigh").checked=true;  
                break;
            case "2":
                
                document.getElementById("iPriceLow").checked=true;
                
                break;
            default:
                document.getElementById("iProperty").checked=true;
        }
    }
}

/*****************************************************************************************/
/*Start Functions For EstateAgentResults.aspx Page:                                      */
/*****************************************************************************************/
function LoadForAgents()
{
    c_objRadius = document.getElementById("wSCRadius");
    c_objAgent = document.getElementById("wSCAgent");
    ParseAgentValues();
    
}

/*************************************************************************/
/*Name: ParseAgentValues                                                 */
/*Purpose:                                                               */
/*  Loads the agent results values on page entry                         */
/*************************************************************************/
function ParseAgentValues()
{
	var intRadius = parseInt(document.getElementById("Radius").value);
	objSearchBox.value = document.getElementById("Town").value;
	c_objAgent.value = document.getElementById("Agent").value;
	
    switch(intRadius){
        case 0: intRadius = 0; break;
        case 1: intRadius = 1; break;
        case 2: intRadius = 2; break;
        case 3: intRadius = 3; break;
        case 5: intRadius = 4; break;
        case 10: intRadius = 5; break;
        case 15: intRadius = 6; break;
        case 20: intRadius = 7; break;
        case 30: intRadius = 8; break;
        default: intRadius = 0; break;
    }
    
    if(c_objRadius != null){
	    c_objRadius.selectedIndex = intRadius;
	}
}

/*****************************************************************************************/
/*End Functions For EstateAgentResults.aspx Page:                                        */
/*****************************************************************************************/


/*****************************************************************************************/
/*Start Functions For SchoolResults.aspx Page:                                           */
/*****************************************************************************************/
function LoadForSchools()
{
	c_objTown = document.getElementById("wSCTown");
	objSchool = document.getElementById("wSCSchool");	

    ParseSchoolValues();
}

function ParseSchoolValues(){

	var intSchoolType = parseInt(document.getElementById("SchoolType").value);
	
	objSearchBox.value = document.getElementById("Town").value;
	objSchool.value = document.getElementById("School").value;
	
	if(intSchoolType == 1){
		document.getElementById("wSCPrimary").checked = true;
	}
	else{
		document.getElementById("wSCSecondary").checked = true;
	}
}

function getSchoolType(){

    if(document.getElementById("wSCPrimary").checked){
        return(1);
    }
    return(2);
}

/*****************************************************************************************/
/*End Functions For SchoolResults.aspx Page:                                             */
/*****************************************************************************************/


/*****************************************************************************************/
/*Start Functions For LocalDirectoryResults.aspx Page                                    */
/*****************************************************************************************/
function LoadForDirectory()
{
	c_objRadius = document.getElementById("iRadius");
	c_objCategory = document.getElementById("wSCCategory");
    GetCategories();
    ParseDirectoryValues();
}

function ParseDirectoryValues()
{
	var intRadius = parseInt(document.getElementById("Radius").value);
	
	for(var x = 0; x < c_objCategory.options.length; x++){
	    if(c_objCategory.options[x].value == parseInt(document.getElementById("Category").value))
	    {
	        c_objCategory.selectedIndex = x;
	    }
	}
	
	objSearchBox.value = document.getElementById("Town").value;
	
	switch(intRadius)
    {
        case 0: intRadius = 0; break;
        case 1: intRadius = 1; break;
        case 2: intRadius = 2; break;
        case 3: intRadius = 3; break;
        case 5: intRadius = 4; break;
        case 10: intRadius = 5; break;
        case 15: intRadius = 6; break;
        case 20: intRadius = 7; break;
        case 30: intRadius = 8; break;
        default: intRadius = 0; break;
    }
    if(c_objRadius != null){
	    c_objRadius.selectedIndex = intRadius;
    }
}

function GetCategories()
{
	LocalDirectoryResults.GetExpertCategories(GetCategories_callback);
}

function GetCategories_callback(response)
{
	if (response != null && response.value != null){		
		var c_objCategories = response.value;		
		var intCategoryID = parseInt(document.getElementById("Category").value);
		for(x = 0; x < c_objCategories.length; x++){					
			c_objCategory.options[x] = new Option(c_objCategories[x].Name,c_objCategories[x].ID);
			if(intCategoryID == c_objCategories[x].ID){
				c_objCategory.options[x].selected = true;
			}				
		}	
	}			
}
/*****************************************************************************************/
/*End Functions For LocalDirectoryResults.aspx Page                                      */
/*****************************************************************************************/
