// JavaScript Document
var $j = jQuery.noConflict();
var SITE_ROOT = "http://backpaindirectory.com.au/";


function getTherapySubCat(category_id,allThPid,selected_id,site_url){
	
	var url = site_url+'ajax.php?do=getTherapySubCat&category_id='+category_id+'&selected_id='+selected_id+'&rnd='+Math.random();
	//alert(url);
		//alert(allThPid);
		//alert(allThPid);
	new Ajax.Request(url,{
	 method: 'get',
	 onLoading: function(transport) {
		$('therapySub'+category_id).innerHTML = '<img src="/images/loading_small.gif">';
	 },
	 onSuccess: function(transport){
		
		var dr = transport.responseText;
		dr = dr.split("^");
		//alert(dr[1]);
		$('therapySub'+category_id).innerHTML = dr[1];
		
		allThPidArr = allThPid.split(",");
		
		for(var i=0;i<allThPidArr.length;i++){
			if( allThPidArr[i] != category_id ){
				//alert(allThPidArr[i]);
				$j("#therapySub"+allThPidArr[i]).slideUp('slow');
			}
		}
		$j("#therapySub"+category_id).slideDown('slow');
		
	 },
	 onFailure: function(transport) {
		 // when the request fails
		 alert('The request failed..');
	 },
	 onException: function(err) {
		 // When an exception is encountered while executing the callbacks
		 alert('Exception');
	 }
	 });
	return false;
}


function getProductSubCat(category_id,selected_id,site_url){
	
	var url = site_url+'ajax.php?do=getProductSubCat&category_id='+category_id+'&selected_id='+selected_id+'&rnd='+Math.random();
		//alert(allThPid);
	new Ajax.Request(url,{
	 method: 'get',
	 onLoading: function(transport) {
		$('productSub'+category_id).innerHTML = '<img src="/images/loading_small.gif">';
	 },
	 onSuccess: function(transport){
		
		var dr = transport.responseText;
		dr = dr.split("^");
		//alert(dr[1]);
		
		//alert('productSub'+category_id);
		$('productSub'+category_id).innerHTML = dr[1];
		
		/*allThPidArr = allThPid.split(",");
		
		for(var i=0;i<allThPidArr.length;i++){
			if( allThPidArr[i] != category_id ){
				//alert(allThPidArr[i]);
				$j("#therapySub"+allThPidArr[i]).slideUp('slow');
			}
		}
		$j("#therapySub"+category_id).slideDown('slow');*/
		
	 },
	 onFailure: function(transport) {
		 // when the request fails
		 alert('The request failed..');
	 },
	 onException: function(err) {
		 // When an exception is encountered while executing the callbacks
		 alert('Exception');
	 }
	 });
	return false;
}


function getSuburbList(state_id){
	
	var url = 'ajax.php?do=getSuburbList&state_id='+state_id+'&rnd='+Math.random();
		//alert(url);
		//alert(inner_id);
	$('suburbBox').style.display = '';	
	new Ajax.Request(url,{
	 method: 'get',
	 onLoading: function(transport) {
		$('suburbBox').innerHTML = '<img src="'+SITE_ROOT+'images/loader.gif">';
	 },
	 onSuccess: function(transport){
		
		var dr = transport.responseText;
		dr = dr.split("^");
		//alert(dr);
		$('suburbBox').innerHTML = dr[1];
	 },
	 onFailure: function(transport) {
		 // when the request fails
		 alert('The request failed..');
	 },
	 onException: function(err) {
		 // When an exception is encountered while executing the callbacks
		 alert('Exception');
	 }
	 });
	return false;
}

function getPostcodeList(suburb_id,selected_id){
	
	var url = 'ajax.php?do=getPostcodeList&suburb_id='+suburb_id+'&selected_id='+selected_id+'&rnd='+Math.random();
		//alert(url);
		//alert(inner_id);
	new Ajax.Request(url,{
	 method: 'get',
	 onLoading: function(transport) {
		//$('stateBox').innerHTML = '<img src="../images/front/loader.gif">';
	 },
	 onSuccess: function(transport){
		
		var dr = transport.responseText;
		dr = dr.split("^");
		//alert(dr);
		$('postcodeBox').innerHTML = dr[1];
	 },
	 onFailure: function(transport) {
		 // when the request fails
		 alert('The request failed..');
	 },
	 onException: function(err) {
		 // When an exception is encountered while executing the callbacks
		 alert('Exception');
	 }
	 });
	return false;
}

function removePhoto(id,photo,place_field,hidden,location){
	if( location == 'Front' )
		var url = 'ajax.php?do=removePhoto&id='+id+'&photo='+photo+'&place_field='+place_field+'&rnd='+Math.random();
	else
		var url = '../ajax.php?do=removePhoto&id='+id+'&photo='+photo+'&place_field='+place_field+'&rnd='+Math.random();
		//alert(url);
		//alert(inner_id);
	new Ajax.Request(url,{
	 method: 'get',
	 onLoading: function(transport) {
		if( location == 'Front' )
			$(place_field).innerHTML = '<img src="images/loader.gif">';
		else
			$(place_field).innerHTML = '<img src="../images/loader.gif">';
	 },
	 onSuccess: function(transport){
		
		var dr = transport.responseText;
		dr = dr.split("^");
		//alert(dr);
		if( dr[1] == 1 ){
			$(hidden).value = "";
			$(place_field+'_box').innerHTML = '';
		}
	 },
	 onFailure: function(transport) {
		 // when the request fails
		 alert('The request failed..');
	 },
	 onException: function(err) {
		 // When an exception is encountered while executing the callbacks
		 alert('Exception');
	 }
	 });
	return false;
}

 var sfsIndex       = new Object;
 sfsIndex.populated = false;
 
 function sfsComplete () {
 if (!document.getElementById) return;
 var text   = document.getElementById('tb');
 var select = document.getElementById('u');
 if (!sfsIndex.populated) sfsBuildIndex();
 var suburb = text.value.match(/,*([^,]+)$/);
 if (suburb) {
 var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
 for (var i = sfsIndex[name.charAt(0)]; i < select.options.length; i++) {
 if (select.options[i].text.toUpperCase().indexOf(name) == 0) {
 select.selectedIndex = i;
 break;
 }
 else {
 select.selectedIndex = -1;
 }
 }
 }
 }
 
 function sfsInsert () {
 if (!document.getElementById) return false;
 var text    = document.getElementById('tb');
 var select  = document.getElementById('u');
 
 text.focus();
 var suburb = select.options[select.selectedIndex];
 

 if (select.selectedIndex == 0) {
 text.value = '';
 return true;
 }
  
 // see if the selected suburb is in the list already
 var textSuburbs = text.value.split(/\s*,\s*/);
 if (!textSuburbs) return false;
 var pattern = new RegExp('\s*' + suburb.text + '\s*$');
 for (var i = 0; i < textSuburbs.length; i++) {
 // if it's in the list don't do anything
 if (pattern.exec(textSuburbs[i])) return false;
 }
 // its not in the list so lets add it replacing any incomplete words
 var newvalue = text.value.replace(
 /(^|,)([^,]*)$/,
 "$1 " + suburb.text + ', '
 );
 text.value = newvalue;
 return false;
 }

 function sfsBuildIndex () {
 if (!document.getElementById) return;
 var select = document.getElementById('u');
 for (var i = select.options.length; i--;) {
 sfsIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
 }
 sfsIndex.populated = true;
 }

