﻿        window.onresize=SetupGUI;
        function getElementsByClass(searchClass,node,tag) 
        {
            var classElements = new Array();
            if (node == null)
                    node = document;
            if (tag == null)
                    tag = '*';
            var els = node.getElementsByTagName(tag);
            var elsLen = els.length;
            var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
            var j = 0;
            for (i = 0; i < elsLen; i++) {
                    if (pattern.test(els[i].className) ) {
                            classElements[j] = els[i];
                            j++;
                    }
            }
            return classElements;
        }
        var roundedDivList = new Array();
        function RoundedRectanglesInit()
        {
            var divList = getElementsByClass("backgroundGradient",null,"img");
			roundedDivList = new Array();
            for (var i = 0 ;i < divList.length;i++)
            {
                divList[i].style.display = "inline-block";                                
				divList[i].id = "backgroundGradient" + i;
				divList[i].parentNode.onresize = RoundedRectanglesInit;
				var roundedDiv = new Object();
				roundedDiv.id = "backgroundGradient" + i;
				roundedDiv.actualHeight = divList[i].clientHeight +  "px";
                roundedDivList.push(roundedDiv);
				divList[i].parentNode.style.display="inline-block";
            }
            
        }
		function RoundedRectanglesDisplay()
		{
			for (var i = 0 ;i< roundedDivList.length;i++)
			{
					document.getElementById(roundedDivList[i].id).style.height = roundedDivList[i].actualHeight;
					alert("Actual height is " + roundedDivList[i].actualHeight);
			}
			
		}
        function SetupGUI()
        {
			if (roundedDivList.length==0)
				RoundedRectanglesInit();
            var curDimensions = GetWindowDimensions();
            /*if (curDimensions.x - 50 > 360)
            {
                document.getElementById("container").style.width = 1020 - 50 + "px";
                document.getElementById("topCenter").style.width = (1020 - 50 - 360) + "px";
                document.getElementById("bottomCenter").style.width = (1020- 50 - 50) + "px";				
            }*/
            
            if (curDimensions.y - 50 > 210)
            {
                //document.getElementById("mainContent").style.height = (curDimensions.y - 50 - 210) + "px";
            }
        }
        function GetWindowDimensions()
        {
            var winWidth = 0, winHeight = 0;
            if( typeof( window.innerWidth ) == 'number' ) 
            {
                winWidth = window.innerWidth;
                winHeight = window.innerHeight;
            } 
            else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
            {
                winWidth = document.documentElement.clientWidth;
                winHeight = document.documentElement.clientHeight;
            } 
            else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
            {
                winWidth = document.body.clientWidth;
                winHeight = document.body.clientHeight;
            }  
            var returnObj = new Object();
            returnObj.x = winWidth;
            returnObj.y = winHeight;
            return returnObj;
        }
        
            function stripPx(x) 
    {            
	    if (x.length <= 2)
		    return x;
	     return eval(x.substr(0,x.length-2));
    }
    function scrollMenuButtons()
    {
        var buttons = document.getElementById("mainButtonContainer").getElementsByTagName("img");
        for (var i = 0;i<buttons.length;i++)
        {
            buttons[i].style.top = i * 50 + "px";
            buttons[i].style.position = "relative";
            buttons[i].id = "mainButtonContainer" + i;
            moveIntoPosition(buttons[i].id);
        }
    }
    
    function moveIntoPosition(elementID)
    {
        var elem = document.getElementById(elementID);
        if (stripPx(elem.style.top) - 5 > 0)
        {
            elem.style.top = stripPx(elem.style.top) - 5 + "px";
            setTimeout("moveIntoPosition('" + elementID + "')",25);
        }
        else
        {
            elem.style.top = "0px";
        }
    }
    function SetModalPopupTransparency()
    {
        elements = getElementsByClass("nifty",document.body,"div");
        for (var i=0;i<elements.length;i++)
            elements[i].style.backgroundColor = 'transparent';
    }
    function IsIE6() 
    {
        var ua = navigator.userAgent;
        if (window.external && (  (/Win/.test(ua)) || (/Mac/.test(ua))))
        {
			try
			{
			var ver = Number(ua.match(/MSIE ([0-9.]+)/)[1])
            if (ver == null || ver >=7)
				return false;
			else
				return true;
			}
			catch (ex)
			{
				return false;
			}
        }
    }
    
    function RestrictToIntegersOnly(e) 
    {
        if (e == undefined) e = window.event;
        return ((e.keyCode >= 48 && e.keyCode <= 57) 
            || e.keyCode == 46 
            || e.keyCode == 8
            || e.keyCode == 9
            || (e.keyCode >= 96 && e.keyCode <= 105)
            || (e.keyCode >=37 && e.keyCode <= 40)) ? true : false
    }

    function RoundTextBoxes()
    {
		if (!IsIE6())
        {
        var textboxElements = document.getElementsByTagName("input");
        for(var i =0;i<textboxElements.length;i++)
        {
                if ((textboxElements[i].type=="text" || textboxElements[i].type=="password") && textboxElements[i].style.display!='none' && textboxElements[i].isStyled == undefined)
                {
                    if (textboxElements[i].className.indexOf("IntegerOnly")>-1)
                        textboxElements[i].onkeydown = RestrictToIntegersOnly;
                    textboxElements[i].isStyled = true;
                    var leftDiv = document.createElement("div");
                    leftDiv.className = "curvedTextboxLeft";
                    var rightDiv = document.createElement("div");
                    rightDiv.className = "curvedTextboxRight";
                    textboxElements[i].className += " curvedTextboxInner";
                    textboxElements[i].parentNode.insertBefore(leftDiv,textboxElements[i]);
                    textboxElements[i].parentNode.insertBefore(rightDiv,textboxElements[i].nextSibling);
                }
		}
			
        }
    }
    function SetupNiftyCorners(element)
    {
        var elements = getElementsByClass("nifty",element,"div");
        for(var i=0;i<elements.length;i++)
        {
            if (elements[i].isInitialised!=true)
            {
                var rTop = document.createElement("b");
                rTop.className= "rtop";
                for (var j = 1;j<5;j++)
                {
                    var bElement = document.createElement("b");
                    bElement.className = "r" + j;
                    rTop.appendChild(bElement);
                }
                
                var rBottom = document.createElement("b");
                rBottom.className ="rbottom";
                for (var j = 4;j>=1;j--)
                {
                    var bElement = document.createElement("b");
                    bElement.className = "r" + j;
                    rBottom.appendChild(bElement);
                }
                elements[i].insertBefore(rTop,elements[i].firstChild);
                elements[i].appendChild(rBottom);
                elements[i].isInitialised=true;
            }
        }
    }
    
    
function AddressSearcher(elementID, elementPropertyID, elementStreet, elementReceptionRooms, elementDistrict, elementTown, elementPropertyType, elementPropertySubType, elementBedrooms, elementOther, elementPCMRent, elementPostcode,elementModalAnimation, existingAddressID,foundAddressesID, elementAddressFinderPopup, elementSearchedPostcode, elementAddressFinder, elementMakeID, makePopup)
{
    this.SearchedAddresses = new Array();
    
    this.ID = document.getElementById(elementID); 
    this.PropertyID= document.getElementById(elementPropertyID); 
    this.Street = document.getElementById(elementStreet); 
    this.ReceptionRooms = document.getElementById(elementReceptionRooms); 
    this.District = document.getElementById(elementDistrict); 
    this.Town = document.getElementById(elementTown); 
    this.PropertyType = document.getElementById(elementPropertyType); 
    this.PropertySubType = document.getElementById(elementPropertySubType); 
    this.Bedrooms = document.getElementById(elementBedrooms); 
    this.Other = document.getElementById(elementOther); 
    this.Rent = document.getElementById(elementPCMRent); 
    this.Postcode = document.getElementById(elementPostcode)
    this.AddressFinderPop = document.getElementById(elementAddressFinderPopup);
    this.AddressFinder = document.getElementById(elementAddressFinder);    
    this.ModalAnimation = elementModalAnimation; 
    this.MakeID = document.getElementById(elementMakeID);    
    this.FindExistingAddress = function()
    {
        this.AddressObject.FindSearchedAddressGlobal(this.AddressObject.ExistingAddresses);
    }
    
    this.FindSearchedAddress = function()
    {
        this.AddressObject.FindSearchedAddressGlobal(this.AddressObject.FoundAddresses);
    }
    this.FindSearchedAddressGlobal = function(id)
    {
        if (id.options.selectedIndex >= 0)
        {
            for(var i=0;i<this.SearchedAddresses.length;i++)
            {
                var opt = id.options[id.options.selectedIndex].value;
                if (opt==this.SearchedAddresses[i].Id)
                {
                    if (this.SearchedAddresses[i].Id <= 0)             
                        this.ID.value  = -1;             
                    else
                    {
                        this.ID.value = this.SearchedAddresses[i].Id;             
                        ComponentInformation.IsPropertyLinkedToMake(this.ID.value,this.LinkedToMakeCallBack,errHandler,this.MakeLinked);
                    }
                    this.PropertyID.value = this.SearchedAddresses[i].Building;
                    this.Street.value = this.SearchedAddresses[i].Street;
                    this.CheckAndSetTextBoxValue(this.ReceptionRooms, this.SearchedAddresses[i].ReceptionRooms);
                    this.District.value = this.SearchedAddresses[i].District;
                    this.Town.value = this.SearchedAddresses[i].Town;
                    this.SetValueInDropDownList(this.PropertyType,this.SearchedAddresses[i].PropertyType);
                    this.SetValueInDropDownList(this.PropertySubType,this.SearchedAddresses[i].PropertySubType);
                    this.CheckAndSetTextBoxValue(this.Bedrooms,this.SearchedAddresses[i].Bedrooms);
                    this.CheckAndSetTextBoxValue(this.Other,this.SearchedAddresses[i].Other);
                    this.Rent.value = this.SearchedAddresses[i].PCMRent;
                    this.Postcode.value = this.SearchedAddresses[i].Postcode;
                    $find(this.ModalAnimation).hide();
                }
            }
        }
    }
    this.LinkedToMakeCallBack = function(obj1, obj2)
    {
        if (obj1>0)
        {
            document.getElementById(elementMakeID).value = obj1;
            $find(makePopup).show();
        }
    }
    this.ExistingAddresses = document.getElementById(existingAddressID);
    this.FoundAddresses = document.getElementById(foundAddressesID);
    this.FindAddress = function ()
    {
        var SearchedPostcode = document.getElementById(elementSearchedPostcode);
        this.SearchedAddresses= new Array();
        if (SearchedPostcode.value.length > 0)
        {
            AddressInformation.GetAddressesForPostcode(SearchedPostcode.value,this.AddressObject.ShowPostcode,errHandler,this.AddressObject);
            AddressInformation.GetExistingPropertyDetails(SearchedPostcode.value,this.AddressObject.ShowExistingPostcodes, errHandler,this.AddressObject);
        }
    }
    this.PopulateDropDownListWithAddresses = function (dropDownList,obj,newAddress)
    {
        dropDownList.options.length = 0;
        for(var i = 0;i<obj.length;i++)
        {
            if (newAddress)
                obj[i].Id = 0 - obj[i].Id;
            var opt = document.createElement("option");
            obj[i].Address = obj[i].Address.replace(/\r/g, " ");
            opt.text = obj[i].Address;
            opt.value = obj[i].Id;
            dropDownList.options.add(opt);
            this.SearchedAddresses.push(obj[i]);
        }
        
    }
    this.ShowPostcode = function(obj, AddressObject)
    {   
        AddressObject.PopulateDropDownListWithAddresses(AddressObject.FoundAddresses,obj, true);
    }
    this.ShowExistingPostcodes = function (obj, AddressObject)
    {
        AddressObject.PopulateDropDownListWithAddresses(AddressObject.ExistingAddresses,obj, false);
    }
    this.CheckAndSetTextBoxValue = function(txt, value)
    {
        if ((value == "") || (value < 0))
            txt.value = "";
        else
            txt.value = value;
    }
    this.SetValueInDropDownList = function(ddl, value)
    {
        for(var i = 0;i<ddl.options.length;i++)
        {
            if (ddl.options[i].value == value)
                ddl.options[i].selected = true;
        }    
    }
    try
    {
        this.ExistingAddresses.onclick = this.FindExistingAddress;
        this.FoundAddresses.onclick = this.FindSearchedAddress;
        this.FoundAddresses.AddressObject = this;
        this.ExistingAddresses.AddressObject = this;
        this.AddressFinderPop.AddressObject = this;
        this.AddressFinder.AddressObject = this;
        this.AddressFinderPop.onclick = this.FindAddress;
        this.AddressFinder.onclick = this.FindAddress;
    }
    catch (ex)
    {}
    
    
}
                    
function errHandler(err)
{
    alert(err._message);
}    