
// ==========
// Functions for Photography Archive
function UpdateDiv(id, content) {
	//alert ("debugging message: Start POD Archive updateDiv function");
	var oDiv = document.getElementById(id);
	if (!oDiv) {
        // Can't find the div so force a reload to get the update
		location.reload(true);
	} else {
		oDiv.innerHTML = content;
	}
}
function submitA_click(oEvent, p_id) {
    // Get Data
	var pollForm = document.forms["poll_input"];
    // var poll_id = pollForm.elements["poll_id"].value;
	var poll_answer_radios = pollForm.elements["poll_answer"];
	var poll_answer = 0;
	for (var i = 0; i < poll_answer_radios.length; i++) {
		if (poll_answer_radios[i].checked) {
            // poll_answer = i + 1;
			poll_answer = poll_answer_radios[i].value;
			break;
		}
	}
	submitPoll_send(p_id, poll_answer);
}
function updateArchive_send(URL, param) {
	//alert ("debugging message: Start POD Archive updateArchive function");
	strHTMLWhileLoading = "<div class=\"loading\"><h3>This Page is Loading</h3><div class=\"graphic\"><img src=\"/staticfiles/NGS/Photography/SiteAssets/img/icons/loading-bar.gif\" alt=\"Currently Loading Photos\" /></div><p>Images should appear shortly.</p></div>";
	var myConn = new XHConn();
	if (!myConn) {
		alert("This page requires features that your web browser does not support (XMLHTTP not available.) Please try a more recent browser.");
	}
	UpdateDiv("pod-archive", strHTMLWhileLoading);
	var fnWhenDone = function (oXML) {
		updateArchive_done(oXML.responseText);
	};
	myConn.connect(URL, "GET", param, fnWhenDone);
}
function updateArchive_done(responseText) {
	//alert ("debugging message: Start POD Archive updateArchive_done function");
	UpdateDiv("pod-archive", responseText);
}
function gup(name) {
	//get url parameter
	//adapted from http://www.netlobo.com/url_query_string_javascript.html
	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];
	}
}

function getQueryString() {
	//get entire query string
	var results = window.location.href;
	var i = results.indexOf("?")+1;
	if (i>0) {
		return results.substring(i,String(results).length);
	} else {
		return "";	
	}
}

function getURLPath() {
	//get the URL minus server name and querystring.
	var results = window.location.href;
	var i = results.indexOf("?")+1;
	if (i>0) {
		return results.substring(i,String(results).length);
	} else {
		return "";	
	}
}


/** 
* @projectDescription 	Poly9's polyvalent URLParser class
*
* @author	Denis Laprise - denis@poly9.com - http://poly9.com
* @version	0.1 
* @namespace	Poly9
*
* Usage: var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment');
* p.getHost() == 'poly9.com';
* p.getProtocol() == 'http';
* p.getPathname() == '/pathname';
* p.getQuerystring() == 'arguments=1';
* p.getFragment() == 'fragment';
* p.getUsername() == 'user';
* p.getPassword() == 'password';
*
* See the unit test file for more examples.
* URLParser is freely distributable under the terms of an MIT-style license.
*/

if (typeof Poly9 == 'undefined')
 var Poly9 = {};

/**
 * Creates an URLParser instance
 *
 * @classDescription	Creates an URLParser instance
 * @return {Object}	return an URLParser object
 * @param {String} url	The url to parse
 * @constructor
 * @exception {String}  Throws an exception if the specified url is invalid
 */
Poly9.URLParser = function(url) {
 this._fields = {'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10};
 this._values = {};
 this._regex = null;
 this.version = 0.1;
 this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/;
 for(var f in this._fields)
  this['get' + f] = this._makeGetter(f);
 if (typeof url != 'undefined')
  this._parse(url);
}
 
/**
 * @method 
 * @param {String} url	The url to parse
 * @exception {String} 	Throws an exception if the specified url is invalid
 */
Poly9.URLParser.prototype.setURL = function(url) {
  this._parse(url);
}

Poly9.URLParser.prototype._initValues = function() {
   for(var f in this._fields)
   this._values[f] = '';
}

Poly9.URLParser.prototype._parse = function(url) {
  this._initValues();
  var r = this._regex.exec(url);
  if (!r) throw "DPURLParser::_parse -> Invalid URL"
  for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
   this._values[f] = r[this._fields[f]];
}

Poly9.URLParser.prototype._makeGetter = function(field) {
 return function() {
  return this._values[field];
 }
}

