/**
 * Popup function
 */
var openpopup = function(url, width, height, centre){
	/*
	Notes:
	(assuming this function is called from the onclick in a <a> tag.
	
	return true/false... 
		- false stops execution, pop up is shown but contents of href not
		- true carries on with execution so href is shown
	*/

	/* Parameters not supplied, return true. */
	if (url == null || width == null || height == null){
		return true;
	}
	
	var options = "toolbar=no, location=yes, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=" + width + ", height=" + height;
	
	if (centre){
		//Centre the window
		var w = 800, h = 600;
		if (screen && screen.availWidth && screen.availHeight){
			w = screen.availWidth;
			h = screen.availHeight;
		}
		var leftPos = (w-width)/2;
		var topPos = (h-height)/2;
		options +=  ",left=" + leftPos + ",top=" + topPos;
	}

	var popup = window.open(url,"_blank", options);
	
	if(popup==null || typeof(popup)=="undefined"){
		alert("Popup window has been blocked. \nPlease enable popups for this web site or disable your popup blocker.");		
		return true;
	}
	
	//Everything went ok, show popup
	return false;
};

 // refinelinks(['qm0','section-navigation'],'fpath', 'http://www.communitybuilders.nsw.gov.au/' , 'pre');
/**
 * full path link function
 * linksArr - array of divs id
 * clsname - class name
 * str - string to be inserted
 * cmd - command: append as prefix/suffix
 * 
 */
var refinelinks = function(linksArr, clsname, str, cmd ){
	/*
	// this function is usually called on the onload event
	// adding full qualified path to the 
	
	return true/false... 
		- false - the function fails to execute/error in function
		- true - operation complete
	*/

	for (var i = 0; i < linksArr.length; i++) {

	if (document.getElementById(linksArr[i])) { // Check that the target element actually exists
			var links = document.getElementById(linksArr[i]).getElementsByTagName('a');
		
			for (var l = 0; l < links.length; l++) {
				var link = links[l].href;					
				var clink = links[l].className;
				alert(clink)			
				if ( clink.search(clsname) ){
					// check for query string
					var qs = link.indexOf('?');
					// escape on mailto & full qualified links
					if (link.substr(0,7) == 'mailto:' ) continue;	
						
					switch (cmd) {
					case 'pre':
						// Start here if n == 1
						// escape on mailto & full qualified links
						if (link.substr(0,7) == 'http://' ) continue;	
						// check relative/absolute
						link = ( link.substr(0,1) == '/' ) ? str + link.substr(1,link.length) : str + link ;
						break; // Stop here
					case 'suf':
						// Start here if n == 2
						// check for '/'
						ls = link.substr(link.length-1,link.length) 
						link = ( ls == '/' ) ? link.substr(0,link.length-1) + str : link + str ;		
						break; // Stop here 
					}
					links[l].href = link;	
					alert(links[l].href);
				}
			}
		}
	}
	return true;
};

/**
 * Google Analytics
 */

	//include google analytics
var gStat = function() {
	var gaJsHost = (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.');
	document.write(unescape('<script src="' + gaJsHost + 'google-analytics.com/ga.js" type="text/javascript"></script>'));
};

var vcookie = {
  // Save cookie - holds user's text size preference
  createCookie : function (name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
  },

  // Read cookie - grab text size data
  readCookie: function (name) {
  	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
  },
  
  // Delete the cookie used to store the user's text size preference
  eraseCookie : function (name) { createCookie(name,"",-1); }
  
};

/**
 * generic javascript solution 
 * to get query string parameter
 */

var getParameterByName = function (name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
};

var getUrlParameters = function () {
    var urlParams = {};
    var e, 
	q = window.location.search.substring(1),
        r = /([^&=]+)=([^&]+)/g;

    while (e = r.exec(q))
    urlParams[decodeURIComponent(e[1])] = decodeURIComponent(e[2]);

    return urlParams;

};

