
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()
{
    // Only hide these fields if surrounded by a DIV
    inputObj = getObjRef("__EVENTVALIDATION");
    
    if (inputObj!=null && inputObj.parentElement !=null)
    {
		if (inputObj.parentElement.tagName.toLowerCase() == 'div')
		{        
		    inputObj.parentElement.style.display = "none";
		}
    }
}

function fillSearchDefaultText() {
	/* getObjRef('searchBox').value="Search using keywords" */;
}

function fillSearchHomesHomepageText(id) {
    txtPostcode = getObjRef(id);
    if (txtPostcode!=null && txtPostcode.value.length==0)
    {
	    getObjRef(id).value="Enter a location or development name";
    }
}

function fillSearchBoxDefaultText(id, initialValue) {
    txtPostcode = getObjRef(id);
    if (txtPostcode!=null && txtPostcode.value.length==0)
    {
	    getObjRef(id).value=initialValue;
    }
}

function fillDefaultValue(id, initialValue) {
    txtBox = getObjRef(id);
    if (txtBox!=null && txtBox.value.length==0)
    {
	    txtBox.value=initialValue;	        
    }
}

function fillDirectionsDefaultText(id) {
	getObjRef(id).value="Enter postcode";
}

function showImage(self, imageUrl, imageId, imageAlt, artistsImpressionRef, showImpression, typicalHomeRef, showTypical, showHomeRef, showShowHome, developmentRef, showDevelopment)
{
    var imageObj = getObjRef(imageId);
    var impressionInfoObj = getObjRef(artistsImpressionRef);
    var typicalInfoObj = getObjRef(typicalHomeRef);
    var showHomeInfoObj = getObjRef(showHomeRef);
    var developmentInfoObj = getObjRef(developmentRef);
    
    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';
    }
    
    if(typicalInfoObj != null)
    {
        showHomeInfoObj.style.display = showShowHome ? '' : 'none';
    }
    
    if(typicalInfoObj != null)
    {
        developmentInfoObj.style.display = showDevelopment ? '' : 'none';
    }
        
    var imgLinks = $("div.houseThumbs a");
        
    for(var i = 0; i < imgLinks.length; i++)
    {
        var sibling = imgLinks[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 ReloadResults(queryString, reloadMap)
{    
    $("#searchResults").load("searchpanel.aspx?" + queryString, function(){if(reloadMap){ map.clearOverlays(); initialiseMap(); }});
    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;
}

//Development drop down list redirect with interim screen if moving to different brand
function developmentShowHomeListRedirect()
{
    var devShowHomeListID = document.getElementById('devShowHomeListID').value;
    var value = document.getElementById(devShowHomeListID).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]) + "&propname=" + escape(params[3]) + "&width=800&height=24&modal=true", false);
        return false;
    }
    
    return true;
}

function DisableButton()
{
    document.forms[0].submit();
    window.setTimeout("disableButton('" + window.event.srcElement.id + "')", 0);
}

function disableButton(buttonID)
{
    document.getElementById(buttonID).disabled = true;
}

var KeyID;
var DevelopmentUrlFieldID;
var SearchButtonID;
var SearchFieldID;

function IsUrl()
{
    var url = document.getElementById(DevelopmentUrlFieldID).value;
    if(url != '' && url != null && url != 'null')
    {
        return true;
    }
    else
    {
        return false;
    }
}

function IsBlank()
{
    var postcode = document.getElementById(SearchFieldID);     
    if (jQuery.trim(postcode.value) == '')
    {
        alert('Please enter a search term');
        return true;
    }
    else
    {
        return false;
    }
}

function DoPostBack()
{
    if(IsUrl())
    {
        document.getElementById(SearchButtonID).disabled = true;
        var url = document.getElementById(DevelopmentUrlFieldID).value;
    
        if(url.indexOf('modal=true') > 0)
        {            
            tb_show(null, url, false);
        }
        else
        {
            window.location = url;
        }
    }
    else
    {
        document.getElementById(SearchButtonID).disabled = false;
    }
}

function GetDevelopmentRedirectUrl(source, eventArgs)
{   
    KeyCheck();
    document.getElementById(DevelopmentUrlFieldID).value = eventArgs.get_value();
    DoPostBack();
    
    if (KeyID == 13 && !IsUrl())
    {
        document.getElementById(SearchButtonID).click();
    }
}

function KeyCheck()
{
    KeyID = (window.event) ? event.keyCode : 0;
}

function checkMaxLength(e,el)
{
    switch(e.keyCode) {
        case 37: // left
        return true;
        case 38: // up
        return true;
        case 39: // right
        return true;
        case 40: // down
        return true;
        case 8: // backspace
        return true;
        case 46: // delete
        return true;
        case 27: // escape
        el.value='';
        return true;
    }
    return (el.value.length<el.getAttribute("maxlength"));
}

function setFocus(controlId)
{    
    var control = getObjRef(controlId);
    $(control).focus();
} 

function deleteConfirm()
{
    var ck = confirm("Are you sure you would like to delete this comment?");
    if (ck == true)
        return true;
    else
        return false;
}

function validationSummaryFocus(valSum)
{
    if(Page_ClientValidate())
    {
        return true;
    }
    else 
    {                       
        ///vsEnterPaymentInfo is id of validation control
        var vsummary = document.getElementById(valSum);
        if(vsummary != null)
        {
            vsummary.scrollIntoView(true);         
        }
        return false;
    } 
}
