
function getObjRef(obj) {
	if (document.getElementById) {
		return document.getElementById(obj);
	} else if (document.all) {
		return document.all[obj];
	} else {
		return null;
	}
}

function debugWrite(message)
{
    if(window.Debug)
    {
        window.Debug.writeln(message);
    }
}

// registered by basepage use to call all onload js functions
function basePageStartUp()
{
    hideEventValidatorDiv();
}

function hideEventValidatorDiv()
{
    inputObj = getObjRef("__EVENTVALIDATION");
    
    if (inputObj!=null && inputObj.parentElement !=null)
    {
        inputObj.parentElement.style.display = "none";
    }
}

function fillSearchDefaultText() {
	/* getObjRef('searchBox').value="Search using keywords" */;
}

function fillSearchHomesDefaultText(id) {
    txtPostcode = getObjRef(id);
    if (txtPostcode!=null && txtPostcode.value.length==0)
    {
	    getObjRef(id).value="Enter town, county or postcode";
    }
}

function fillDirectionsDefaultText(id) {
	getObjRef(id).value="Enter postcode";
}

function showImage(self, imageUrl, imageId, imageAlt, artistsImpressionRef, showImpression, typicalHomeRef, showTypical)
{
    var imageObj = getObjRef(imageId);
    var impressionInfoObj = getObjRef(artistsImpressionRef);
    var typicalInfoObj = getObjRef(typicalHomeRef);
    
    if(imageObj != null)
    {
        imageObj.src = imageUrl;
        if(imageAlt != null)
        {
            imageObj.alt = imageAlt;
        }
    }
    
    if(impressionInfoObj != null)
    {
        impressionInfoObj.style.display = showImpression ? '' : 'none';
    }
    
    if(typicalInfoObj != null)
    {
        typicalInfoObj.style.display = showTypical ? '' : 'none';
    }
    for(var i = 0; i < self.parentNode.childNodes.length; i++)
    {
        var sibling = self.parentNode.childNodes[i];
        
        if(sibling.nodeType == 1)
        {
            sibling.style.display = (self == sibling) ? 'none' : '';
        }
    }
}

function resetCountyList() 
{
    var countyListName = getObjRef("countylistId");
    var countyList;
    if (countyListName != null)
    {
        countyList = getObjRef(countyListName.value);
        
        if (countyList !=null)
        {
            countyList.selectedIndex = 0;
        }
    }
    return true;
}

function resetPostCode()
{
    var countyListId = getObjRef("countylistId");
    var countyList;
    if (countyListId != null)
    {
        countyList = getObjRef(countyListId.value);
        
        if (countyList !=null)
        {
            if (countyList.selectedIndex != 0)
            {
                var postCodeId = getObjRef("postcodeId");
                var postCode = getObjRef(postCodeId.value);
                if (postCode!=null)
                {
                    postCode.value = "Enter postcode";
                }
            }
        }
    }
    return true;
}

function compareMinmaxValuesCondition(cond)
{
    var result = 1;
    
    var minValue = VAM_GetTextValue(cond.IDToEval, cond.Trim);
    var maxValue = VAM_GetTextValue(cond.IDToEval2, cond.Trim);

    // if either are optional, no validation is required
    if((minValue != "minimum" && maxValue != "maximum") || (minValue != "-1" && maxValue != "-1"))
    {
        minValue_Int = parseInt(minValue);
        maxValue_Int = parseInt(maxValue);

        if(minValue_Int > maxValue_Int)
        {
            result = 0;
        }
    }
    return result;
}

function formCheckAll(sender)
{
    //Gets all 'input' tags in the 'ul' which contains the 'sender' checkbox
    var list = sender.parentNode.parentNode.parentNode.getElementsByTagName("input");

    //Loop through and find only the checkboxes; set their checkbox values to that of the senders
    for(var i = 0; i < list.length; i++)
    {
        if(list[i].type == "checkbox")
            list[i].checked = sender.checked;
    }
}

//this works because the 'All' checkbox is last in the list
//should maybe come up with a more elegant way of doing this
function formUnCheckAll(sender)
{
    //number of 'li'
    var listCount = sender.parentNode.parentNode.childNodes.length
    //get first 'input' of last 'li'
    var lastCheckBox = sender.parentNode.parentNode.childNodes[listCount - 1].getElementsByTagName("input")[0];

    if(lastCheckBox.type == "checkbox")
        lastCheckBox.checked = false;
}

//Refresh checkboxes on search controls
function RefreshCheckboxStatus(all, others, id)
{
    if(id == all)
    {
        var isChecked = document.getElementById(id).checked;
        var image;
        
        if(isChecked)
        {
            image = document.getElementById(id).ToggleButtonBehavior._CheckedImageUrl;
        }
        else
        {
            image = document.getElementById(id).ToggleButtonBehavior._UncheckedImageUrl;
        }
        
        for(i = 0; i < others.length; i++)
        {
            document.getElementById(others[i]).checked = isChecked;
            document.getElementById(others[i] + '_ToggleButton').style.backgroundImage = 'url(' + image + ')';
            
        }
    }
    else
    {
        var isChecked = true;
        var image;
        
        for(i = 0; i < others.length; i++)
        {
            isChecked = isChecked && document.getElementById(others[i]).checked;
        }
        
        if(isChecked)
        {
            image = document.getElementById(id).ToggleButtonBehavior._CheckedImageUrl;
        }
        else
        {
            image = document.getElementById(id).ToggleButtonBehavior._UncheckedImageUrl;
        }
        
        document.getElementById(all).checked = isChecked;
        document.getElementById(all + '_ToggleButton').style.backgroundImage = 'url(' + image + ')';
    }
}

function getThemeFromHost(hostname)
{
    if(hostname.search("georgewimpey") != -1 || hostname.search("gw") != -1)
    {
        return "GW";
    }
    else if(hostname.search("bryant") != -1 || hostname.search("bh") != -1)
    {
        return "BH";
    }
    else if(hostname.search("taylorwimpey") != -1 || hostname.search("tw") != -1)
    {
        return "TW";
    }
    else if(hostname.search("laing") != -1 || ((hostname.search("lh") != -1) && hostname != "localhost"))
    {
        return "LH";
    }
    else if(hostname.search("g2") != -1)
    {
        return "G2";
    }
    
    //if all else fails just use tw
    return "TW";
}

//Ajax navigation of search results
function navResults(index, query, updateMap)
{
    $("#searchResults").load("SearchPanel.aspx?pageIndex=" + index + "&searchAll=on&" + query, function(){if(updateMap){UpdateMapView();}});
    return false;
}

//Development drop down list redirect with interim screen if moving to different brand
function developmentListRedirect()
{
    var devListID = document.getElementById('devListID').value;
    var value = document.getElementById(devListID).value;
    
    if(value == "")
    {
        return true;
    }
    
    var params = value.split('|');
    
    if(getThemeFromHost(location.hostname) != params[0])
    {
        tb_show(null, "/gw/developmentRedirect.aspx?brand=" + params[0] + "&region=" + escape(params[1]) + "&devName=" + escape(params[2]) + "&width=800&height=24&modal=true", false);
        return false;
    }
    
    return true;
}