﻿/***************************************************************************/
/*File Name:    jp_Frontpage.js                                            */
/*Date:         15 august 2007                                             */
/*Description:  Script used for the Frontpage in property site             */
/***************************************************************************/

var objMaxPrice = document.getElementById("MaxPrice");
var objMinPrice = document.getElementById("MinPrice");

LoadPage();

function imgErr(img){
    if((img != null) && (img != 'undefined')){
        img.src = "../images/comingsoon.gif";
    }
}

function LoadPage(){

    switch(getFileName().toLowerCase())
    {
        case "renting.aspx":
            LoadPricesForRent(objMinPrice, objMaxPrice);
            break;
        default: 
             LoadPricesForSale(objMinPrice, objMaxPrice);
    }
    
    LoadCookie(objMinPrice, objMaxPrice, objSearchBox);
    document.onkeypress = checkKey;
}

function checkKey(Event){
    var ret = Event ? Event.which : window.event.keyCode;
    if(ret == 13){
        if(List.Base.style.visibility != "visible"){
            Search();
        }
    }
    return ret != 13;
}

/*************************************************************************/
/*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)));
}

function Search(){
var isValidSearch = false; //Always presume search is invalid until validated.
var strDataCheck = "";
var isTown = false;
    
    var re = new RegExp("^([^0-9]*)$"); 
    var strValue = objSearchBox.value.toString();
    var arr = re.exec(strValue);
    if(arr){
        isTown = true;
    }
        if(!isTown){
	        var postcode = new clsPostcode(objSearchBox.value);
            isValidSearch = postcode.isValid;
            if(!isValidSearch){
                isValidSearch = postcode.IsPartPostcodeValid;
            }
        }else{
            isValidSearch = true;
        }

    
	if(isValidSearch){ 
	    var arr = new Array();
	        arr[0] = objSearchBox.value.toString().replace(",","_");;
	        arr[1] = objMaxPrice.selectedIndex;
	        arr[2] = objMinPrice.selectedIndex;
	    
	    var days = 21; //Cookie valid for 14 days
        var Cookie = new clsCookie(cookieName);
		Cookie.createCookie(cookieName,arr,days);
		
        switch(getFileName().toLowerCase())
        {
            case "renting.aspx":
                window.location = "propertyresults.aspx?Rent=true&Town=" + objSearchBox.value + "&MaxPrice=" + objMaxPrice[objMaxPrice.selectedIndex].value + "&MinPrice=" + objMinPrice[objMinPrice.selectedIndex].value + "&OrderType=1&Radius=0"; 
                break;
            default: 
                window.location = "propertyresults.aspx?Town=" + objSearchBox.value + "&MaxPrice=" + objMaxPrice[objMaxPrice.selectedIndex].value + "&MinPrice=" + objMinPrice[objMinPrice.selectedIndex].value + "&OrderType=1&Radius=0"; 
        }
    		
		
	}else{
	    alert(objSearchBox.value + " - does not appear to be a valid town or postcode.");
	}
}

function hintopen(){
	var strUrl = "";
	strUrl = document.location.href;
	strUrl = strUrl.substring(0, strUrl.lastIndexOf("/"));
	window.open (strUrl+"/hints.aspx","hint","location=0,status=0,scrollbars=0,width=470,height=510");
}

function LoadCookie(objMinPrice, objMaxPrice, objSearchBox){

var Cookie = new clsCookie(cookieName);


    if(Cookie.cookieData){
      if(Cookie.cookieData[0]) {
        objSearchBox.value = Cookie.cookieData[0].toString().replace("_",",");
        objMaxPrice.options[parseInt(Cookie.cookieData[1])].selected = true;
        objMinPrice.options[parseInt(Cookie.cookieData[2])].selected = true;
       }
    }
    else{
        objSearchBox.value = "";
	}		      	
}

function fnCheckPrice(){
	
	var intMaxValue = objMaxPrice.selectedIndex;
	var intMinValue = objMinPrice.selectedIndex;
	
	if(intMaxValue < intMinValue)
	{
		objMaxPrice.selectedIndex = intMinValue;
	}	
}
