function Home_Init()
{
	AdjustContentHeights();
	SearchForm_Init();
	//Consignments_Init();
	frmConsignments_Init();     
    EquipmentForSaleSearchFormNew_Init();
	DivisionLogoSubmenu_Init();

	if(document.getElementById("UserID"))
		document.getElementById("UserID").focus();
}

// ensures that the heights of the content area and othercontent area are the same
function AdjustContentHeights()
{
	var content = document.getElementById("content");
	var othercontent = document.getElementById("othercontent");
	var myaccountlogin = document.getElementById("myaccountlogin");

	if(content.clientHeight < othercontent.clientHeight && !myaccountlogin)
	{
		content.style.minHeight = othercontent.clientHeight + "px";
		content.style.height = content.style.minHeight;		// required for MSIE
	}
	else if(content.clientHeight > othercontent.clientHeight)
	{
		othercontent.style.minHeight = (content.clientHeight - 12) + "px";
		othercontent.style.height = othercontent.style.minHeight;		// required for MSIE
	}
}

function SearchForm_Init()
{
	var Model = document.getElementById("Model");
	var defaultText = Model.value;

	document.getElementById('searchbutton').disabled = true;
	Model.onfocus = function()
	{
		if(this.value == defaultText)
		{
			document.getElementById('searchbutton').disabled = false;
			this.value = "";
			this.style.color = "black";
		}
	}
	Model.onblur = function()
	{
		if(this.value == "")
		{
			document.getElementById('searchbutton').disabled = true;
			this.value = defaultText;
			this.style.color = "#999999";
		}
    }

}

function Consignments_Init() {
    var oTxtModelConsignments = document.getElementById("txtModelConsignments");
    var oImageButtonConsignments = document.getElementById("imgBtnGoConsignmenets");
    var defaultText = oTxtModelConsignments.value;

    oTxtModelConsignments.onclick = function() {
        window.location.href = "wwm/BankOwnedConsignments.aspx";
    }

    oImageButtonConsignments.onclick = function() {
        window.location.href = "wwm/BankOwnedConsignments.aspx";
    }


    oTxtModelConsignments.onfocus = function() {
        if (this.value == defaultText) {
            this.value = "";
            this.style.color = "black";
        }
    }
    oTxtModelConsignments.onblur = function() {
        if (this.value == "") {
            this.value = defaultText;
            this.style.color = "#999999";
        }
    }

}


function frmConsignments_Init() {

    var frmConsignments = document.getElementById("frmConsignments");
    if (frmConsignments) {
        frmConsignments.onsubmit = function() {
            window.location.href = this.action;
            return false;
        }
    }
}



function EquipmentForSaleSearchForm_Init() {

    var frmEquipmentForSaleSearch = document.getElementById("frmEquipmentForSaleSearch");
    if (frmEquipmentForSaleSearch) {
        frmEquipmentForSaleSearch.onsubmit = function() {
            window.location.href = this.action + "?" +
				"Make=" + frmEquipmentForSaleSearch.make.value + "&" +
				"Model=" + frmEquipmentForSaleSearch.model_number.value + "&" +
				"Category=" + frmEquipmentForSaleSearch.category.value;

            return false;
        }
    }
}


function EquipmentForSaleSearchFormNew_Init() {
    var frmEquipmentForSaleSearch = document.getElementById("frmEquipmentForSaleSearch");

    if (frmEquipmentForSaleSearch) {
        frmEquipmentForSaleSearch.onsubmit = function() 
        {
            
            culture = GetCulture();
            if (culture == "en-US")
            {
                index = frmEquipmentForSaleSearch.category.selectedIndex;
                category = frmEquipmentForSaleSearch.category[index].text;
                indexMake = frmEquipmentForSaleSearch.make.selectedIndex;
                make = frmEquipmentForSaleSearch.make[indexMake].text;
                model = frmEquipmentForSaleSearch.model_number.value;
                model = ClearString(model)
                category = UriFriendly(category);
                //make = UriFriendly(make);
                //model = UriFriendly(model);
                

                newUrl = "WWM";
                if (frmEquipmentForSaleSearch.category[index].value != -1)
                    newUrl += "/" + category;
                if (frmEquipmentForSaleSearch.make[indexMake].value != -1)
                    newUrl += "/Make/" + make;
                if (model != "")
                    newUrl += "/Model/" + model;

                if (newUrl == "WWM") {
                    newUrl = "WWM/SearchEquipment.aspx";
                }

                window.location.href = newUrl;
                return false;
            }
            else 
            {

                window.location.href = this.action + "?" +
				"Make=" + frmEquipmentForSaleSearch.make.value + "&" +
				"Model=" + frmEquipmentForSaleSearch.model_number.value + "&" +
				"Category=" + frmEquipmentForSaleSearch.category.value;

                return false;
            
            }
        }
    }
}

function UriFriendly(oldstring) {

    oldstring = oldstring.replace("/", " ");
    oldstring = oldstring.replace(".", "-");
    oldstring = oldstring.replace("\\", "-");
    oldstring = oldstring.replace("+", "-");
    oldstring = oldstring.replace("*", "-");
    oldstring = oldstring.replace("@", "-");
    oldstring = oldstring.replace("%", "-");
    oldstring = oldstring.replace("~", "-");
    oldstring = oldstring.replace("$", "-");
    oldstring = oldstring.replace(" ", "-");
    oldstring = oldstring.replace(" ", "-");
    oldstring = oldstring.replace(" ", "-");
    oldstring= escape(oldstring)
    
    return oldstring;
}

function ClearString(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = new RegExp("(^[a-zA-Z0-9_-]*[\\s]*)");
    var match ;
    while (x < clearString.length) {


        match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '' && match[1] != ' ') {
            output += match[1];
            x += match[1].length;

        }
        else
            x++;
        if (x < clearString.length) {
        
            if (match[1].length>1 && match[1].substr(match[1].length - 1, 1) != " ")
                output += "-";
        }
    }
    return output;
}





function DivisionLogoSubmenu_Init()
{
	var submenu = document.getElementById("submenu");
	var divisionlogos = document.getElementById("divisionlogos");
	
	if(!submenu || !divisionlogos)
		return;
		
	var submenuItems = submenu.getElementsByTagName("li");
	var logo, division;
	
	for(var i = 0; i < submenuItems.length; i++)
	{
		division = submenuItems[i].id.substr("submenu_".length);
		logo = document.getElementById("divisionlogos_" + division);
		
		logo.SubmenuItem = submenuItems[i];
		logo.Description = document.getElementById("divisiondescriptions_" + division);
		logo.Image = logo.getElementsByTagName("img")[0];
		logo.Url = submenuItems[i].getElementsByTagName("a")[0].href;
		logo.SetVisibility = function(visible) { this.Image.style.visibility = visible ? "visible" : "hidden"; this.Description.style.display = visible ? "inline" : "none"; }
		logo.onmouseover = function() { this.SetVisibility(true); this.SubmenuItem.style.backgroundColor = "#E8EBF0"; }		// This color should be the same as in the style sheet.
		logo.onmouseout = function() { this.SetVisibility(false); this.SubmenuItem.style.backgroundColor = ""; }
		logo.onclick = function() { window.location = this.Url; }

		submenuItems[i].Logo = logo;
		submenuItems[i].onmouseover = function() { this.Logo.SetVisibility(true); }
		submenuItems[i].onmouseout = function() { this.Logo.SetVisibility(false); }
	}
}
