  	

//############################################
// UCA Javascript for MySource Matrix
// @author: Tim Everist
//############################################

var currenturl= unescape(window.location.pathname);


//############################################
// Common UCA JS Functions
//############################################


function uca_replaceChars(entry, pattern, replace) {
entry = uca_rightTrim(entry);
out = pattern; // replace this
add = replace; // with this
temp = "" + entry; // temporary holder


while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return temp;
}

function uca_stripHTML(oldString) {

   var newString = "";
   var inTag = false;

   for(var i = 0; i < oldString.length; i++) {   

        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              inTag = false;
              i++;
        }  

        if(!inTag) newString += oldString.charAt(i);
  }
   return newString;

}

function uca_closeWindow()
{
opener.uca_refreshPage();
self.close();
}

function uca_addFacilityImage(assetid, lineage)
{   
   url = rooturl+'/facilities_and_services_/add_image/?assetid='+assetid+'&SQ_DESIGN_NAME=ax';
   addFileWindow = window.open(url, 'addFileWindow','resizable=yes,scrollbars=yes,height=500,width=750');
   
	if (this.focus) 
   {
     addFileWindow.focus();
   }   
}

function uca_addFacilityFile(assetid, lineage)
{   
   url = rooturl+'/facilities_and_services_/add_file/?assetid='+assetid;
   addFileWindow = window.open(url, 'addFileWindow','resizable=yes,scrollbars=yes,height=500,width=750');
   
	if (this.focus) 
   {
     addFileWindow.focus();
   }   
}

function uca_addFacilityLink(assetid, lineage)
{   
   url = rooturl+'/facilities_and_services_/add_link/?assetid='+assetid+'&SQ_DESIGN_NAME=ax';
   addFileWindow = window.open(url, 'addFileWindow','resizable=yes,scrollbars=yes,height=500,width=750');
   
	if (this.focus) 
   {
     addFileWindow.focus();
   }   
}


function uca_addPage(assetid, lineage)
{   
   url = rooturl+'/add_page/?assetid='+assetid+'&SQ_DESIGN_NAME=ax';
   editFileWindow = window.open(url, 'editFileWindow','resizable=yes,scrollbars=yes,height=500,width=750');
   
	if (this.focus) 
   {
     editFileWindow.focus();
   }   
}

function uca_rollon(imgid, imgsrc)
{
	document[imgid].src = imgsrc;

}

function uca_rolloff(imgid, imgsrc)
{
	document[imgid].src = imgsrc;	
}


function uca_rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
{
	sString = sString.substring(0,sString.length-1);
}
	return sString;
}

function uca_trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

function uca_setUsername(field, input, usernameid) {

var usernamefield = document.getElementById(usernameid);

if (field == 'firstname')
{
firstname = input.value; 
firstname = uca_trimAll(firstname);
}
else if (field == 'lastname')
{
lastname = input.value;
lastname = uca_trimAll(lastname);
}
usernamefield.value = 'hrs.' + firstname + '.' + lastname; 
usernamefield.value = usernamefield.value.toLowerCase();

}

function uca_randPassword(passwordid) {
var passone = document.getElementById(passwordid+'_one');
var passtwo = document.getElementById(passwordid+'_two');
var password = Math.random();

passone.value = password;
passtwo.value = password;

}

function replaceChars(entry, prefix) {
entry = uca_rightTrim(entry);
out = " "; // replace this
add = "."; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
var temp2 = "";
temp2 = prefix + temp;
document.getElementById('username').value = temp2;
}

function uca_newSave()
{

var save = document.getElementById('sq_commit_button');

if (save != null)
{
save.onclick = new Function("uca_toggleDIV('grayload'); uca_grayOut(1);if (submit_form) {submit_form(this.form); } else { this.form.submit(); this.disabled = 'disabled'; }");
}

   
}

function uca_editPage() 
{
   this.location.href = currenturl+'/_edit?ignore_frames=1';
}


function uca_admin() 
{
   this.location.href = currenturl+'/_admin';
}

function uca_logout() 
{
   this.location.href = currenturl+'/?SQ_ACTION=logout';
}

function uca_deleteAsset2(assetid, assetname, type) 
{
   if(confirm('Are you sure you want to delete "'+assetname+'"?')==true)
   {
        var url = this.location.href = this.location.href;
        var url2 = uca_replaceChars(url, '_edit', '');


try {
        request = new XMLHttpRequest();
    } catch(e) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch(e) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                request = false;
            }// End try
        }// End try
    }// End try
    
    request.open("GET","./?a=" + assetid + "&delete=yes&rand=" + parseInt(Math.random()*99999999999999),true);
    request.send(null);

if (type == 'page')
{
window.location.href = '././';
}
else
{
uca_refreshPage();
}

        
   }     
   
}


function uca_deleteAsset(assetid, assetname, type) 
{
if(confirm('Are you sure you want to delete "'+assetname+'"?')==true)
   {
uca_grayOut(1);

 $.ajax({
   type: "GET",
   url: "/",
   data: "a=" + assetid + "&delete=yes&rand=" + parseInt(Math.random()*99999999999999),
   success: function(){

if (type == 'page')
{
location.href = './';
}
else
{
uca_grayOut(1);
uca_refreshPage();
}

   }
 });
}


}

function uca_editFile(assetid)
{   
   url = rooturl+'/edit/_edit?limbo_assetid='+assetid+'&ignore_frames=1';
   editFileWindow = window.open(url, 'editFileWindow','resizable=yes,scrollbars=yes,height=800,width=780');
   
	if (this.focus) 
   {
     editFileWindow.focus();
   }   
}

function uca_refreshPage() 
{
   uca_grayOut(1);
   //uca_toggleDIV('grayload');
   this.location.href = document.location.href;

}

function uca_toggleDIV( whichLayer )
{
   var elem, vis;
   if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
   else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
   else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
   vis = elem.style;
   // if the style.display value is blank we try to figure it out here
   if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
   vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}




//############################################
// Common AJAX send function
//
// Parameters;
//	## URL
//	## Result Div Control
//############################################
function uca_axSend(url, result) 
{
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function(){if(http_request.readyState==4){xhrHandler(http_request,result)}};
	http_request.open('GET', url, true);
	http_request.send(null);
}

function xhrHandler(xhr, resultid)
{
	var e = document.getElementById(resultid);
	if (e) {
                
		e.innerHTML = xhr.responseText;
                
	}
}



function ur_axAssetid(assetid, div)
{
  uca_axSend(rooturl+'/ax?a='+assetid,div);
}

function uca_grayOut(vis, options)
{
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis && dark.style.display != 'block') {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}



function uca_addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//uca_addLoadEvent(setTimeout("ur_randomResource()",10000));
//uca_addLoadEvent(setTimeout("uca_newSave()",3000));


//############################################
// Uniting Care Functions
//############################################  	


function uca_addFile(assetid)
{   
   url = '/employees_section/f/add_file/?assetid='+assetid+'&SQ_DESIGN_NAME=ax';
   addFileWindow = window.open(url, 'addFileWindow','resizable=yes,scrollbars=yes,height=500,width=750');
   
	if (this.focus) 
   {
     addFileWindow.focus();
   }   
}

function uca_editFile(assetid)
{   
   url = '/employees_section/f/add_file/_edit?limbo_assetid='+assetid+'&ignore_frames=1&SQ_DESIGN_NAME=ax';
   editFileWindow = window.open(url, 'editFileWindow','resizable=yes,scrollbars=yes,height=600,width=750');
   
	if (this.focus) 
   {
     editFileWindow.focus();
   }   
}

function uca_editCareData(assetid)
{   
   url = '/employees_section/care_services_policy__and__planning/care_services/data_manager/edit/_edit?limbo_assetid='+assetid+'&ignore_frames=1';
   editFileWindow = window.open(url, 'editFileWindow','resizable=yes,scrollbars=yes,height=600,width=750');
   
	if (this.focus) 
   {
     editFileWindow.focus();
   }   
}


function uca_addFolder(assetid)
{   
   url = '/employees_section/f/add_folder/?assetid='+assetid+'&SQ_DESIGN_NAME=ax';
   addFolderWindow = window.open(url, 'addFolderWindow','resizable=yes,scrollbars=yes,height=300,width=750');
   
	if (this.focus) 
   {
     addFolderWindow.focus();
   }   
}





function uca_showfiles(divid, div) {


if (div.className == 'filegroupselected')
{
div.className = 'filegroupunselected';
}
else
{
div.className = 'filegroupselected';
}

uca_toggleDIV(divid);

}

function uca_expandFolder(divid, div) {


if (div.className == 'filegroupselected')
{
div.className = 'filegroupunselected';
uca_toggleDIV('folder'+divid);
}
else
{
div.className = 'filegroupselected';
uca_toggleDIV('folder'+divid);

var fol = document.getElementById('folder'+divid);


if (fol.innerHTML == '') {

fol.innerHTML = "<div align='center'><br /><br /><img src='http://matrix.nsw.uca.org.au/__data/assets/image/0004/36355/ajax-loader.gif' alt='Loading' /><br /><br /></div>";

$('#folder'+divid).load('/employees_section/f/fileax/?assetid='+divid, function(){uca_addFileIcons();});




//uca_axSend2('/employees_section/f/fileax/?assetid='+divid, 'folder'+divid);


}

}

}

function uca_addFileIcons() {

$(document).ready(function(){
$("a[href$='.doc']").addClass("wordIcon");
$("a[href$='.pdf']").addClass("pdfIcon");
$("a[href$='.xls']").addClass("excelIcon");
$("a[href$='.ppt']").addClass("powerpointIcon");
$("a[href$='.pps']").addClass("powerpointIcon");

});
}

function uca_editcol(endpath) {

this.location.href = rooturl+addurl+'/'+endpath+'/_edit?ignore_frames=1';

}

function uca_showvideo(url) {
document.getElementById('video').style.display = 'block';
document.getElementById('outervideo').style.display = 'block';
document.getElementById('videoimg').style.display = 'none';
document.getElementById('video').focus();



flashembed("video", 
	
		/* 
			first argument supplies standard Flash parameters. See full list:
			http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701
		*/
		{
			src:'http://www.withoutfearorfavour.org.au/__data/assets/file/0018/34047/FlowPlayerLight.swf',
			width: 570, 
			height: 456
		},
		
		/*
			second argument is Flowplayer specific configuration. See full list:
			http://flowplayer.org/player/configuration.html
		*/
		{config: {   
			autoPlay: true,
			autoBuffering: false,
			controlBarBackgroundColor:'0xB23427',
			initialScale: 'scale',
                        showFullScreenButton: false,
                        showMenu: false,
                        loop: false,
			playList: [
		{ url: url, linkUrl: 'http://www.withoutfearorfavour.org.au' }
		]
		}} 
	);

}

function uca_closevideo() {
document.getElementById('video').style.display = 'none';
document.getElementById('video').innerHTML = '';
document.getElementById('outervideo').style.display = 'none';
document.getElementById('videoimg').style.display = 'block';
}



 	

// Service Directory JS


var currentLat;
var currentLng;

function getSuburbLatLong(pageid) {


   if (document.getElementById('kms').value != '' && document.getElementById('suburb').value != '') {
      geocoder = new GClientGeocoder();
      
      var address = document.getElementById('suburb').value+" NSW, Australia";
            if (geocoder) { 
              geocoder.getLatLng(
                address,
                function(point) {
                  if (!point) {
      alert("Please enter a valid suburb");
return;
                  } else {

      document.getElementById('lat').value = point.lat();
currentLat = point.lat();
      document.getElementById('lng').value = point.lng();
currentLng = point.lng();

      document.getElementById('suburb').name = 'queries_suburb2_query';
      document.getElementById('suburb2').name = 'queries_suburb_query';



 document.getElementById(pageid).submit();
      
      }
     
                  }
                
              );
              
            }
   }
else
{
document.getElementById('results_per_page').value = '10';
 document.getElementById(pageid).submit();
}
}



function addTen() {
kms = kms+10;
document.getElementById('kms').value = kms;
document.getElementById('setKMS').innerHTML = kms;
document.getElementById('setKMS2').innerHTML = kms+10;
sortList();
gmap.setCenter( latlngbounds.getCenter( ), gmap.getBoundsZoomLevel( latlngbounds ) );
}


function setCurrentType(stype, sstype) {

fso = document.getElementsByTagName('select')[0].options;
for (i=0; i<fso.length; i++) {
    if (fso[i].value == stype) {
		fso[i].selected = true;
		break;
    }
 }

}



// Care Services JS


function _size2cols(s) { return Math.round(parseInt(s)/1.35)+''; }
function __textareaIfy(element) {
   var textarea = $('<textarea>')
     .attr('cols', _size2cols(element.size||20))
     .attr('rows', '8')
     .val($(element).val());
   for (var i=0, len=element.attributes.length; i < len; i++) {
      if (element.attributes[i].nodeName != 'type') {
         textarea.attr(element.attributes[i].nodeName, element.attributes[i].nodeValue);
      }
   }

   textarea.insertBefore(element);

   __makeSmallerButton(element);

   $(element).remove();

}

function _cols2size(s) { return Math.round(parseInt(s)*1.35)+''; }
function __inputIfy(element) {
   var input = $('<input>')
     .attr('size', _cols2size(element.cols||20))
     .val($(element).val());
   for (var i=0, len=element.attributes.length; i < len; i++) {
      if (!/cols|rows|size/.test(element.attributes[i].nodeName)) {
         input.attr(element.attributes[i].nodeName, element.attributes[i].nodeValue);
      }
   }

   input.insertBefore(element);

   __makeBiggerButton(element);

   $(element).remove();
}

function __makeBiggerButton(element) {
   $('#unexpander4'+element.id).remove();

   var button = $('<img>')
     .attr('src','plus.gif')
     .attr('border','0')
     .attr('id','expander4'+element.id)
     .bind('click', function() {
        __textareaIfy(document.getElementById(element.id));
        });

   button.insertAfter(element);
}

function __makeSmallerButton(element) {
   $('#expander4'+element.id).remove();

   var button = $('<img>')
     .attr('src','minus.gif')
     .attr('border','0')
     .attr('id','unexpander4'+element.id)
     .bind('click', function() {
        __inputIfy(document.getElementById(element.id));
        });

   button.insertAfter(element);
}

function initExpandableInputs() {
   $('input.expandable').each(function() {
      // the element must have an ID
      if (!this.id) return;

      // if the value inside already has a newline character
      // make it expandable not and option to revert that
      if (/\n/.test($(this).val())) {
         if (this.nodeName == 'INPUT') {
            __textareaIfy(this);
         }
      } else {
         if (this.nodeName == 'INPUT') {
            // offer a "make bigger" option
            __makeBiggerButton(this);
         }
      }  
   });
}

$(function(){
   initExpandableInputs();
});









// Font increase decrease

var normalFont = 1;
var fontCounter = 0;


function setPlusText() {
  if (fontCounter < 0) fontCounter = 0;
  if (fontCounter < 2) fontCounter++;
  else return false; 
  normalFont= normalFont+0.1;
  size= normalFont+"em";
  document.getElementById('pagecontent').style.fontSize = size;   
  return false;
}
function setMinusText() {
  if (fontCounter > 0) fontCounter = 0;
  if (fontCounter > -2) fontCounter--;
  else return false;
  
  normalFont= normalFont-0.1;
  size= normalFont+"em";
  document.getElementById('pagecontent').style.fontSize = size;   
  return false;

}
