/* For drop down menu */
function startList() {
	init_clearDefaultText();
  if (document.all && document.getElementById) {
		navRoot = document.getElementById("primaryMenu");
		for (i=0; i< navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " over";
				}

				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}

// Toggles the class name of the indexed div between 'hideMe' and 'showMe'
// and toggles the src attribute of the referenced img tag between openPicture and closePicture
function toggleBox(index, me, customPlus, customMinus) {
  var openPicture = customPlus == null ? "plus.gif" : customPlus;
  var closePicture = customMinus == null ? "minus.gif" : customMinus;

	var path = "/files/images/";
	
	var splitup = me.src.split("/");
	var name = splitup[splitup.length-1];
	var d = document.getElementById(index);
	if (me != undefined) {
		if (name == closePicture){
			me.src = path + openPicture;
		} else {
			me.src = path + closePicture;
		}
		hideShow(d);
	}
}

// Changes the class of the given tag from classa to classb or back
function hideShow(tag) {
	if (tag.style.display == 'none'){
		tag.style.display = 'block';
	} else {
		tag.style.display = 'none';
	}
}

function printerFriendly() {
	dd = document;
	cs = "/themes/propertyinvesting/css/print.css";
	dd.g=dd.getElementsByTagName;
	l="link";
	H=document.getElementsByTagName("head").item(0);
	c=(dd.all)?c=dd.all.tags(l):document.getElementsByTagName("link");
	L=dd.createElement(l);
	L.s=L.setAttribute;
	L.s('rel','StyleSheet');
	L.s('href',cs);
	H.appendChild(L);
	
	window.print();
  return false;
}

addLoadEvent(startList);

function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400,left = 440,top = 312');");
}

function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

function init_clearDefaultText() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bclearclicked\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
