

// rounded corners (requires jQuery)
$.fn.border=function(_2){
	var _3=["box","north","east","south","west"];
	this.each(function(){
		var _4="";
		for(var _5=_3.length-1,cn;cn=_3[_5];_5--)
			_4=(cn=="box")?"<div style=\"display:block; zoom:1;\" class=\""+this.className+"\" id=\""+this.id+"\">"+_4+"</div>":"<div class=\""+cn+"\">"+_4+"</div>";
		$(this).wrap(_4);
		$(this).removeClass().addClass("northwest");
		this.id="";
	});
	return this;
};

// generic function to capitalize the first letter of every word in a string
function capitalizeWords(s){
	var a, b, c, d, e;
	a=s.toLowerCase();
	e=a.length;
	if(e>0){
		for(i=0;i<e;i++){
			if(i==0){
				b=a.substring(0,1).toUpperCase();
				d=a.substring(1,e);
				a=b+d;
			}else{
				b=a.substring(i,i+1);
				if(b==" "&&i<(e-1)){
					b=a.substring(i+1,i+2).toUpperCase();
					c=a.substring(0,i+1);
					d=a.substring(i+2,e);
					a=c+b+d;
				}
			}
		}
	}
	return a;
};

// ajax function to load suburbs based on a postcode - paired to /postcode-to-suburb.php
function ajaxLoadTowns(f,_9,_a,_b,ol){
	var _d=f[_a].value;
	if(_d=="")return;
	var _e=function(_f){
		var id,ll,state,loc,i=0,j=0;
		try{
			var _11=_f.documentElement.getElementsByTagName("postcode");
			var _12=f[_9];
			_12.options.length=0;
			if(_11.length>1)_12.options[0]=new Option("--Please Select--","none");
			while(loc=_11[i++]){
				ll=capitalizeWords(loc.getAttribute("locality"));
				state=loc.getAttribute("state");
				_12.options[_12.options.length]=new Option(ll,ll);
			};
			if(_11.length>0)f[_b].value=state;
			else{
				_12.options[0]=new Option("Please enter your postcode first...","");
				alert("This postcode is invalid... please try again!");
			};
			if (_11.length>1){
				if(ol && ($("#postbackSuburb").val() != "")){
					var mm;
					while(mm = _12.options[j++].value)
						if(mm == $("#postbackSuburb").val()) _12.selectedIndex = j-1;
				};
				_12.focus();
			}
			else $(_a).focus();
		}
		catch(exc){}
	};
	$.get("/postcode-to-suburb.php?postcode="+_d,_e)
};


function ajaxLoadCategories(f,searchText,selectBox){
	var searchVal = f[searchText].value;
	if(searchVal=="")return;
	
	var cbMethod=function(response){
		var catsEl = response.documentElement.getElementsByTagName("category");
		if (catsEl.length == 0) {
			alert('Sorry, no categories were found.\n\nPlease try some different words that describe your business');
			return;
		}
		var box = document.getElementById(selectBox);
		try{
			// remove old options
			while(box.options.length > 0) { box.remove(0); }
			
			// add new options
			for (var i = 0; i < catsEl.length; i++) {
				var opt = new Option(catsEl.item(i).getAttribute('name'),catsEl.item(i).getAttribute('id'));
				if (catsEl.item(i).getAttribute('important')) {
					opt.className = 'importantOption';
				}
				box.options[box.options.length] = opt;
			}
		}
		catch(exc){ }
		$("select").dblclick( function() { addSelectedCategory(); });
	};
	$.get("../category-search.php?text="+searchVal,cbMethod)
};
var selectedCategories = new Array();
function categorySelectionBtnClick(btn)
{
	var btnNum = btn.id.replace('btn','');
	var catNum = Math.abs(btnNum.replace('cat',''));
	var txtBox = document.getElementById(btnNum+'txt');
	var selBox = document.getElementById('categorySelectBox');
	var radBtn = document.getElementById('primary_'+btnNum);
	var nxtBtn = document.getElementById('cat'+(catNum+1)+'btn');
	var valEl = document.getElementById(btnNum+'val');

	if (selectedCategories[btnNum]) {
		// remove selected category
		btn.src = '../images/arrow-right.gif';
		btn.className = 'enabled_right';
		txtBox.value = '(not selected)';
		
		if (radBtn.checked) {
			if (catNum == 1) { 
				radBtn.checked = false; 
			} else {
				var cat1rad	= document.getElementById('primary_cat1');
				if (cat1rad) { cat1rad.checked = true; }
			}
		}
		radBtn.disabled = true;

		valEl.value = '';
		
		
		var opt = new Option(selectedCategories[btnNum][0], selectedCategories[btnNum][1]);
		if (selectedCategories[btnNum][2] != '') {
			opt.className = selectedCategories[btnNum][2];
		}
		
		try { selBox.add(opt,selBox.options[0]); } catch (exc) { selBox.add(opt); }
		selectedCategories[btnNum] = null;
		switch (catNum) {
			case 1:
				switchSelectedCats(1,2);	
			case 2:
				switchSelectedCats(2,3);
			case 3:
				switchSelectedCats(3,4);
		}
		if (nxtBtn) { nxtBtn.className = 'enabled_right' };
	
		
	} else {
		// add selected category
		
		var selOpt = selBox.options[selBox.selectedIndex];
		var newText = selOpt.text;
		var newId = selOpt.value;
		
		selectedCategories[btnNum] = new Array(newText, newId, selOpt.className);
		
		txtBox.value = newText;
		selBox.remove(selBox.selectedIndex);
		
		radBtn.disabled = false;
		if (catNum == 1) { radBtn.checked = true; }
		btn.src = '../images/arrow-left.gif';
		btn.className = 'enabled_left';
		
		valEl.value = newId;
		
		if (nxtBtn) {
			if (nxtBtn.className == 'disabled') { nxtBtn.className = 'enabled_right' };	
		}
	}
	
	// update category listing text
	var cats = new Array();
	for (var i = 1; i <= 4; i++) {
		var el = document.getElementById('cat'+i+'txt');
		if (el) { if (el.value != '(not selected)') { cats.push(el.value); } }
	}
	
	var tmpTextBox = document.getElementById('tmpText');
	tmpTextBox.value = cats.join(', ');
	if (tmpTextBox.value == '') {
		tmpTextBox.value = '[Your category will display here]';
	}
	updateListing(tmpTextBox, 'spnCategories');
	
	// ensure all buttons are displayed when necessary and hidden when not
	for (var i = 4; i > 1; i--) {
		var el = document.getElementById('cat'+i+'txt');
		if (el) {
			if (el.value == '(not selected)') {
				if (document.getElementById('cat'+(i-1)+'txt').value == '(not selected)') {
					document.getElementById('cat'+i+'btn').className = 'disabled';
				}
			}	
		}
	}
	return false;
}

function addSelectedCategory() 
{
	for (var i = 1; i <= 4; i++) {
		if (document.getElementById('cat'+i+'val').value == '') {
			var btn = document.getElementById('cat'+i+'btn');
			categorySelectionBtnClick(btn);
			return;
		}
	}
}

function switchSelectedCats(num1, num2) {
	var catNum1 = 'cat'+num1;
	var catNum2 = 'cat'+num2;
	
	if (!document.getElementById(catNum1+'txt')) return;
	if (!document.getElementById(catNum2+'txt')) return;
	
	var txtBox1 = document.getElementById(catNum1+'txt');
	var valField1 = document.getElementById(catNum1+'val');
	var radBtn1 = document.getElementById('primary_'+catNum1);
	var cat1Btn = document.getElementById(catNum1+'btn');
	
	var txtBox2 = document.getElementById(catNum2+'txt');
	var valField2 = document.getElementById(catNum2+'val');
	var radBtn2 = document.getElementById('primary_'+catNum2);
	var cat2Btn = document.getElementById(catNum2+'btn');
	
	
	// switch data array
	var tmpArr = selectedCategories[catNum1];
	selectedCategories[catNum1] = selectedCategories[catNum2];
	selectedCategories[catNum2] = tmpArr;
	// switch text vals
	var tmp = txtBox1.value;
	txtBox1.value = txtBox2.value;
	txtBox2.value = tmp;
	
	// switch values
	tmp = valField1.value;
	valField1.value = valField2.value;
	valField2.value = tmp;
	
	// switch radio button
	tmp = radBtn1.checked;
	radBtn1.checked = radBtn2.checked;
	radBtn2.checked = tmp;

	tmp = radBtn1.disabled;
	radBtn1.disabled = radBtn2.disabled;
	radBtn2.disabled = tmp;


	// switch selector buttons
	tmp = cat1Btn.className;
	cat1Btn.className = cat2Btn.className;
	cat2Btn.className = tmp;
	tmp = cat1Btn.value;
	cat1Btn.value = cat2Btn.value;
	cat2Btn.value = tmp;

	radBtn1.disabled = (txtBox1.value == '(not selected)');
	radBtn2.disabled = (txtBox2.value == '(not selected)');
}



function setPrimaryCategoryId(catId) { document.getElementById('pcid').value = document.getElementById(catId).value; }

/* ----- Dynamically added event handlers, etc ----- */
$(function(){
	
	// Add "ready" CSS class to the body tag.
	// This allows us to restyle some elements as/after rounded corners are applied to them (typically, removal of padding).
	$('body').addClass("ready");
	
	// onload addition of rounded corners
	$('#miniSearchForm, #memberpanel, #randomtip, #pagingpanel, #picpanel, div.pink, div.promo, div.related, div.cloud').border();
	$('div.grey, div.shortcut').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("grey").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("grey")});
	$('div.listItemNormal').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("listItemNormal").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("listItemNormal")});
	$('div.listItemNormalAlt').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("listItemNormalAlt").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("listItemNormalAlt")});
	$('div.listItemPrem').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("listItemPrem").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("listItemPrem")});
	$('div.listItemPremAlt').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("listItemPremAlt").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("listItemPremAlt")});
	$('div.male').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("male").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("male")});
	$('div.maleAlt').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("maleAlt").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("maleAlt")});
	$('div.fem').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("fem").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("fem")});
	$('div.femAlt').border().hover(function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("femAlt").addClass("pink")},function(){$(this.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass("pink").addClass("femAlt")});

	// onload flash replacement of headline text/font
	if(typeof sIFR == "function"){
		sIFR.replaceElement("h1", named({sFlashSrc: "/din.20060907.swf", sColor: "#BA0978", sLinkColor:"#BA0978", sBgColor:"#FFFFFF", sHoverColor:"#BA0978"}));
		sIFR.replaceElement("h3.replace", named({sFlashSrc: "/din.20060907.swf", sColor: "#333333", sLinkColor:"#333333", sBgColor:"#FFFFFF", sHoverColor:"#333333"}));
	};
	
	// legend styling SUCKS! So wrap legends in H3 and style that instead
	$('legend').wrap('<h3></h3>');

	// universal focus/blur background colour change on inputs and textareas
	$('input.input').focus(function(){$(this).removeClass("input").addClass("inputFocus")}).blur(function(){$(this).removeClass("inputFocus").addClass("input")});
	$('textarea.textarea').focus(function(){$(this).removeClass("textarea").addClass("textareaFocus")}).blur(function(){$(this).removeClass("textareaFocus").addClass("textarea")});
	
	// universal mouseover/mouseout background colour change on TRs
	$('tr').focus(function(){$(this).removeClass("tr").addClass("trOver")}).blur(function(){$(this).removeClass("trOver").addClass("tr")});

	$('div.pagelinks a.submit').click(function(e){
		// search results page - print button clicked
		if(this.className.indexOf('print') != -1){
			$('#util').attr("name","print");
			document.forms[0].submit();
			return false;
		};
		// search results page - quote button clicked
		if(this.className.indexOf('quote') != -1){
			$('#util').attr("name","quote");
			document.forms[0].submit();
			return false;
		};
		// search results page - page link clicked
		if (this.className.indexOf('print') != -1) $('#util').attr("name",'print');
		else if (this.className.indexOf('quote') != -1) $('#util').attr("name",'quote');
		else if (this.className.indexOf('first') != -1) $('#util').attr("name",'first');
		else if (this.className.indexOf('last') != -1) $('#util').attr("name",'last');
		else if (this.className.indexOf('prev') != -1) $('#util').attr("name",'prev');
		else if (this.className.indexOf('next') != -1) $('#util').attr("name",'next');
		else if (this.className.indexOf('page') != -1) $('#page').val(parseInt(this.innerHTML));
		else return false;
		document.forms[0].submit();
		return false;
	});

	// search results page - category filter option clicked
	$('#filters ul.category a').click(function(e){
		var a = this.href.split("/");
		$('#c').val(a[a.length-2]);
		$('#s').val($(this).html().replace(/&amp;/g,"&"));
		document.forms[0].submit();
		return false;
	});
	
	// search results page - postcode filter option clicked
	$('#filters ul.postcode a').click(function(e){
		var a = this.href.split("/");
		$('#l').val(a[a.length-5]);
		document.forms[0].submit();
		return false;
	});
	
	// search results page - range filter option clicked
	$('#filters ul.range a').click(function(e){
		var a = this.href.split("/"), i=0, opt;
		while (opt = $('#r').get(0).options[i++])
			if (a[a.length-3] == opt.value) $('#r').get(0).selectedIndex=i-1;
		document.forms[0].submit();
		return false;
	});

	// front page links (top 40 & shortcuts) updated onclick with updated search form field values
	$('div.cloud a[href*=/directory/national], div.shortcut a[href*=/directory/national]').click(function(e){
		if($('#l').val() != ""){
			top.location.href = this.href.replace("national", $('#l').val().replace(" ", "-")).replace("/100/", "/"+$("#r").val()+"/");
			return false;
		}
	});
	
	// onfocus/onblur background colour changes of dating site form elements
	$('#pm-message-body-inner select, #pm-message-body-inner textarea, #pm-message-body-inner input[type=text]')
		.focus(function(){$(this).css("background-color","#fff3fa")}).blur(function(){$(this).css("background-color","#fff")});
	
	
	// the logout button (top menu) has a JS confirm added for safety
	$('#menu li a.noborder strong').click(function(){
		return confirm('Do you really want to logout?');
	});

	// external links use rel=external so XHTML not broken by deprecated target attrib.
	$('a.popup, a[rel=external]').click(function(e){
		window.open(this.href); return false;
	});
	
	//If browser is IE, disable BackgroundImageCache
  if(jQuery.browser.msie){
    try{document.execCommand("BackgroundImageCache", false, true);}
		catch(err){}
  };
	
});

// JS error suppression
function se(){return true;}
window.onerror=se;

