/*
** Written by Andrew Dunn
**
** This file will be a collection of the common functions used in our web customization stack
*/

/*******************************************************************************
**  General Library
*******************************************************************************/

/*
**  Grabs Parameters from the URL string
**
** @param		parameter		The parameter you wish to get from the url string
** @return					The results of the query or a blank string when the parameter is non existant or empty
*/
function getUrlParameter( parameter )
{
  parameter = parameter.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+parameter+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

/*
**  Connection Function
**  uses XMLHTTPRequest Object synchronously to pull the whole page
**  
**  @param		url	an absolute url giving the base location of the page that needs to be processed
**  @return			returns a string value of the entire html content from the url
*/
function loadPageData(url){
	xmlhttp=null;
	if (window.XMLHttpRequest){// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null){
		//xmlhttp.onreadystatechange=stateChange;
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		return xmlhttp.responseText;
	}
	else {
		alert("Your browser does not support XMLHTTP.");
	}
}

/*******************************************************************************
**  Sharepoint Lists
*******************************************************************************/

/*
**  A NOTE ON M$ STANDARDS (STSNavigateWithCheckout):
**
**  Properly harness the STSNavigateWithCheckout function from Core.js
**  Unfortunately the links look cryptic to me and I dont have time to figure out
**  this particular issue
**
**  The reson for using STSNavigateWithCheckout is to take advantage of all
**  the internal data flagging that might be triggered by this function. However
**  I have captured the ID and Source so the new solution functions aesthetically
**  the same as the built in edit button. 
**
**  Seriously why make the urls so damn obfusticated, I would like to understand why
**  SharePoint couldnt just work off simple URL's, making them cryptic is not a noveltee, it is just plain stupid 
**
**  UPDATE [2008.11.13]: Code has been in place now for 2 months, there has been no noticeable
**  corruption of data from not using the core library's function mentioned above. 
**  No further investigation will be made into this functionality unless requested by
**  another member of the EA team.
*/

/*
**  Adds a verticle line seperator into a row at a specified point
** 
** @param		tableRow		The table row in which you wish to insert this seperator
** @param		nodeNumber	The node in the row where the element will be inserted
*/
function addSeperator ( tableRow, nodeNumber ) {
	this.editFormAddition = tableRow.insertCell(nodeNumber);
	
	editFormAddition.setAttribute("noWrap", "true");
	editFormAddition.setAttribute("class", "ms-separator");
	editFormAddition.setAttribute("className", "ms-separator");
	
	editFormAddition.innerHTML = "|";
}

/*
**  Adds a button into a row at a specific point, follows the style conventions of Sharepoints
**  default generated toolbar
** 
** @param		tableRow		The table row in which you wish to insert this button
** @param		logoUrl		The URL of the image that will be assembled into the button
** @param		linkUrl		The URL of the place where this button leads
** @param		linkText		The text that will be written on the button
** @param		nodeNumber	The node in the row where the element will be inserted
*/
function addButton ( tableRow, logoUrl, linkUrl, linkText, nodeNumber ) {
	this.editFormAddition = tableRow.insertCell(nodeNumber);
	
	editFormAddition.setAttribute("noWrap", "true");
	editFormAddition.setAttribute("class", "ms-toolbar");
	editFormAddition.setAttribute("className", "ms-toolbar");
	editFormAddition.setAttribute("id", "_soFocusHere");

	// Build out the markup, this is indented like markup to make for easy visualization of the end product. (xHTML compliant)
	var newInnerHTML = '<table cellspacing="0" cellpadding="1">' +
							'<tbody>' +
								'<tr>' +
									'<td class="ms-toolbar" noWrap="true">' +
										'<a href="' +
											linkUrl +
											iD;											
											//When user is 'hotlinked' into the site they have no source! So no use in appending it to the URL
											if (this.source != ""){
												newInnerHTML += '&Source=' + source;
											}
	newInnerHTML +=						'">' +
											'<img style="border-width:0px;padding-right:3px;" src="' +
											logoUrl +
											'"/>' +
										'</a>' +
									'</td>' +
									'<td class="ms-toolbar" noWrap="true">' +
										'<a href="' +
											linkUrl +
											iD;											
											//When user is 'hotlinked' into the site they have no source! So no use in appending it to the URL
											if (this.source != ""){
												newInnerHTML += '&Source=' + source;
											}
	newInnerHTML +=						'">' +
											linkText +
										'</a>' +
									'</td>' +
								'</tr>' +
							'</tbody>' +
						'</table>';
	editFormAddition.innerHTML = newInnerHTML;	
}

/*
**  Adds text into a row at a specified point
** 
** @param		text			The text that will be placed into the toolbar
** @param		tableRow		The table row in which you wish to insert this seperator
** @param		nodeNumber	The node in the row where the element will be inserted
*/
function addText( text, tableRow, nodeNumber) {
	this.editFormAddition = tableRow.insertCell(nodeNumber);
	editFormAddition.setAttribute("noWrap", "true");
	editFormAddition.setAttribute("class", "ms-toolbar");
	editFormAddition.setAttribute("className", "ms-toolbar");
	editFormAddition.setAttribute("id", "_soFocusHere");
	
	var newInnerHTML = text;
	
	editFormAddition.innerHTML = newInnerHTML;
}
