//REUSABLE FUNCTIONS

//Allow you to add multiple events to the onload event handler - props to simon willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//get elements by class name - props to dustin diaz
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|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//Determine if a class is present on a target element
function hasClass(target, classValue) {
    var pattern = new RegExp("(^| )" + classValue + "( |$)");
    
	if (target.className.match(pattern)) {
    	return true;
    } 
    
	return false;
}

//Remove a class from a target element
function removeClass(target, classValue) {
    var removedClass = target.className;
    var pattern = new RegExp("(^| )" + classValue + "( |$)");
	
    removedClass = removedClass.replace(pattern, "$1");
    removedClass = removedClass.replace(/ $/, "");
    target.className = removedClass;

    return true;
}

//Add a class to a target element
function addClass(target, classValue) {
    if (!hasClass(target, classValue)) {
        if (target.className == "") {
            target.className = classValue;
        }
        else {
            target.className += " " + classValue;
        }
    }
    return true;
}


/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
	
	API Reference

	new Querystring([qs])
		Creates a new Querystring object, optionally passing a string qs to parse. If qs is omitted, the querystring from the current page is used. If qs is passed, it should not begin with a "?".
	
		// Parse the current page's querystring
		var qs = new Querystring()
	
		// Parse a given querystring
		var qs2 = new Querystring("name1=value1&name2=value2")
	
	Querstrying.get(name[, default_value])
		Returns the value of querystring parameter name if it exists, or default_value if it doesn't. If default_value is omitted and parameter name doesn't exist, returns null.
	
		var v1 = qs2.get("name1")
		var v3 = qs2.get("name3", "default value")
	
	Note: If a name appears more than once in a querystring only the last value is kept. 
	
	*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}
//end of client side query string access code


function getInnerText(target)
{
	var text = target.firstChild;

	while (text.nodeValue.match(/^\s*$/)) {
		text = text.nextSibling;
	}

	textString = text.nodeValue.replace(/^\W*(.*\w)\W*$/, "$1");

	return textString;
}

//----------------------------------------


//Makes link elements with class 'popup' open in a new browser window
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
	if (links[i].className.match("popup")) {
	  links[i].onclick = function() {
		window.open(this.getAttribute("href"));
		return false;
	  }
	}
  }
}
addLoadEvent(doPopups);

	
//ENQUIRY FORM - FORM TYPE QUICK PICK FUNCTIONS

//initiate form type QP buttons based on toggle value given in query string
function initialButtCheck(){
	if (!document.getElementById) return false;
	if (!document.fmEnq) return false;
	if (!document.fmEnq.enqtypeQP) return false;
	
	var qs = new Querystring();
	var ppt = qs.get('ppt','F');
	if (ppt == "T") {
		document.fmEnq.enqtypeQP[0].checked = false;
		document.fmEnq.enqtypeQP[1].checked = true;
	}
}
addLoadEvent(initialButtCheck);

//autoarrange form elements based on Quick Pick (QP) selections
function determineFormType() {
	if (!document.getElementById) return false;
	if (!document.fmEnq) return false;
	if (!document.fmEnq.enqtypeQP) return false;
	if (!document.getElementById("propinfo")) return false;
	if (!document.getElementById("enquiryinfo")) return false;
	
	if (document.fmEnq.enqtypeQP[0].checked == true) {
		document.getElementById("propinfo").style.display = 'none';
		document.getElementById("enquiryinfo").style.display = 'block';
	}
	
	if (document.fmEnq.enqtypeQP[1].checked == true) {
		document.getElementById("propinfo").style.display = 'block';
		document.getElementById("enquiryinfo").style.display = 'none';
	}
}
addLoadEvent(determineFormType);

function evhdlrFormType(){
	if (!document.fmEnq) return false;
	if (!document.fmEnq.enqtypeQP) return false;
	
	for (i = 0; i < document.fmEnq.enqtypeQP.length; i++){
		document.fmEnq.enqtypeQP[i].onclick = function () { //using onclick as onchange doesnt work in Safari
			return determineFormType();
		}
	}
}
addLoadEvent(evhdlrFormType);

//FORMS SUBMIT BUTTONS ROLLOVER EFFECT IN IE6 (CSS controlled in others)

//needed to get the hover to work in IE6 - hover works in better browsers without this.  Code adapted from www.themaninblue.com
function initButtonHover()
{
	var buttons = getElementsByClass('hoverBtn');
	for (var i = 0; i < buttons.length; i++) { 
		buttons[i].onmouseover = hoverOn;
		buttons[i].onmouseout = hoverOff;
		buttons[i].onfocus = hoverOn;
		buttons[i].onblur = hoverOff;
	}
	return true;
};
addLoadEvent(initButtonHover);


function hoverOn()
{
	if (!hasClass(this, "hover"))
	{
		addClass(this, "hover");
	}
	
	return true;
};

function hoverOff()
{
	removeClass(this, "hover");
	
	return true;
};



//FORM JS
//Brings associated form field into focus when the text within a label is clicked (and makes the field blank)
function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
		if (!labels[i].getAttribute("for")) continue;
		labels[i].onclick = function() {
			var id = this.getAttribute("for");
			if (!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}
addLoadEvent(focusLabels);

//Creates default placeholder text, which disappears and reappears automatically
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "reset"||element.type == "submit"||element.type == "radio"||element.type == "checkbox") continue;
		if (!element.defaultValue) continue;
		element.onfocus = function() {
			if (this.value == this.defaultValue) {
				this.value = "";
			}
		}
		element.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		}
	}
}

//The function to prepare the previous form enhancements for each form on a page
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		resetFields(thisform);
/*		thisform.onsubmit = function() {
			return validateForm(this);
		}*/
	}
}
addLoadEvent(prepareForms);



var i;
function fillSizes(i){
		var frmSearch = document.getElementById("frmSearch");
		frmSearch.selSize.options.length=0;

		switch(i) {	
			case 0:
				frmSearch.selSize.options[0] =new  Option('All', '0');
				break;
			case 1:
				frmSearch.selSize.options[0] =new  Option('All', '0');
				frmSearch.selSize.options[1] =new  Option('0 - 1,000', '1');
				frmSearch.selSize.options[2] =new  Option('1,000 - 5,000', '2');
				frmSearch.selSize.options[3] =new  Option('5,000 - 10,000', '3');
				frmSearch.selSize.options[4] =new  Option('10,000 - 20,000', '4');
				frmSearch.selSize.options[5] =new  Option('20,000 - 50,000', '5');
				frmSearch.selSize.options[6] =new  Option('Over 50,000', '8');
				break;
			case 2:
				frmSearch.selSize.options[0] =new  Option('All', '0');
				frmSearch.selSize.options[1] =new  Option('0 - 1,000', '1');
				frmSearch.selSize.options[2] =new  Option('1,000 - 5,000', '2');
				frmSearch.selSize.options[3] =new  Option('Over 5,000', '6');
				break;
			case 3:
				frmSearch.selSize.options[0] =new  Option('All', '0');
				frmSearch.selSize.options[1] =new  Option('0 - 1,000', '1');
				frmSearch.selSize.options[2] =new  Option('1,000 - 2,000', '9');
				frmSearch.selSize.options[3] =new  Option('2,000 - 3,000', '10');
				frmSearch.selSize.options[4] =new  Option('3,000 - 5,000', '11');
				frmSearch.selSize.options[5] =new  Option('5,000 - 10,000', '3');
				frmSearch.selSize.options[6] =new  Option('Over 10,000', '7');
				break;
			case 4:
			case 5:
				frmSearch.selSize.options[0] =new  Option('All', '0');
		}
	}