//<![CDATA[

// Toggle All Checkboxes on the page
function toggleAll(masterCheckbox) {
	$("input").each(function() {
		this.checked = masterCheckbox.checked;
	});
}

function checkSearchForm() {
    var query = document.getElementById('query');
    var trimmed = query.value.replace(/^\s+|\s+$/g,"");

    if (trimmed == 'Enter a Condo Name, ZIP Code, or City' || trimmed == '') {
        alert("Please enter a search query");
        return false;
    }
    return true;
}

// Search Block Functions
function loadSearchForm(selectBox) {
    var inputName = selectBox.name;
    var inputValue = selectBox.options[selectBox.selectedIndex].value;
    alert(inputName + '=' + inputValue);
    switch (inputName) {
        case 'county_id':
            onLoadStart("submarket_id");
            $("#submarket_id").load("/includes/load_options.php?data_type=submarket&"+inputName+"="+inputValue,
                window.setTimeout("onLoadComplete('submarket_id')", 300));
            $("#zip_code").load("/includes/load_options.php?data_type=zip_code&"+inputName+"="+inputValue,
                window.setTimeout("onLoadComplete('zip_code')", 300));
            clearOptions("subdivision_id");
            break;
        case 'submarket_id':
            onLoadStart("zip_code");
            $("#zip_code").load("/includes/load_options.php?data_type=zip_code&"+inputName+"="+inputValue,
                window.setTimeout("onLoadComplete('zip_code')", 300));
            $("#subdivision_id").load("/includes/load_options.php?data_type=subdivision&"+inputName+"="+inputValue,
                window.setTimeout("onLoadComplete('subdivision_id')", 300));
            break;
        case 'zip_code':
            onLoadStart("subdivision_id");
            $("#subdivision_id").load("/includes/load_options.php?data_type=subdivision&"+inputName+"="+inputValue,
                window.setTimeout("onLoadComplete('subdivision_id')", 300));
            break;
        case 'subdivision_id':
        default:
            break;
    }
}
function onLoadStart(id) {
    selectBox = document.getElementById(id);
    selectBox.disabled = true;
    var option = document.createElement("option");
    option.appendChild(document.createTextNode("Loading..."));
    selectBox.appendChild(option);
}
function onLoadComplete(id) {
    selectBox = document.getElementById(id);
    selectBox.disabled = false;
    selectBox.selectedIndex = 0;
}
function clearOptions(id) {
    $("#"+id).load("/includes/load_options.php");
    document.getElementById(id).disabled = true;
}


// Cookie Functionality

/**
 * Sets a cookie.
 */
function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

/**
 * Gets a cookie.
 */
function getCookie(name) {
    var tag = name + "=";
    var cookies = document.cookie.split(';');
    for(var i=0; i < cookies.length; i++) {
        var c = cookies[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(tag) == 0) return c.substring(tag.length, c.length);
    }
    return null;
}

/**
 * Removes a cookie.
 */
function removeCookie(name) {
    setCookie(name, "", -1);
}

function windowPopUp(windowName, url) {
	newWindow=window.open(url,windowName,'height=500,width=775,resizable=1,scrollbars=1');
	if (window.focus) {newWindow.focus()}
	return false;
}
function swapImage(imageName, imageState) {
	document.images[imageName].src = '/images/menu/' + imageName + (imageState.length > 0 ? '_' +  imageState : '') + '.gif';
}
function goTo(targetUrl){
	window.location = targetUrl;
}
function initTableHighlight(tableId){
	var table = document.getElementById(tableId);		//alert(tableId);
	var tbody = table.getElementsByTagName('tbody')[0];
	var trs = tbody.getElementsByTagName('tr');
	for(var j=0;j<trs.length;j++){
		trs[j].onmouseover = highlightSiblings;
		trs[j].onmouseout = unhighlightSiblings;
	}
}
highlightSiblings = function(){
	targetTable = this.parentNode;						//alert(targetTable.className);
	targetClass = this.className;
	trs = targetTable.getElementsByTagName('tr');
	for(var i=0;i<trs.length;i++){
		var tr = trs[i];
		if(tr.className==targetClass){
			tr.style.backgroundColor = '#EEE';
		}
	}
}
unhighlightSiblings = function(){
	targetTable = this.parentNode;
	targetClass = this.className;
	trs = targetTable.getElementsByTagName('tr');
	for(var i=0;i<trs.length;i++){
		var tr = trs[i];
		if(tr.className==targetClass){
			tr.style.backgroundColor = 'transparent';
		}
	}
}
function accOver(currTr) {
	currTr.style.backgroundColor = "#DDD";
	table = currTr.parentNode;
	var trs = table.getElementsByTagName("tr");
	for (var i = 0; i < trs.length; i++) {
		tr = trs[i];
		if (tr.id.substr(0, currTr.id.length) == currTr.id) {
			tr.style.backgroundColor = "#CCC";
		}
	}
}
function accOut(currTr) {
	currTr.style.backgroundColor = "transparent";
	var trs = document.getElementsByTagName("tr");
	for (var i = 0; i < trs.length; i++) {
		tr = trs[i];
		if (tr.id.substr(0, currTr.id.length) == currTr.id) {
			tr.style.backgroundColor = "transparent";
		}
	}
}
function accClick(currTr) {
	links = currTr.getElementsByTagName("a");
	if (links.length == 1) {
		link = links[0].href;
	}
	window.location.href = link;
}
function selectProduct(prodId){
	document.select_product.innerHTML = document.select_product.innerHTML + '<input type="hidden" name="prodId" value="' + prodId +'">';
	document.select_product.submit();
}
//]]>
