/*
	getUrlParameters(url)
	
	Description:
		Splits the given url on the ? character
		and builds an associative array of url parameters
		that were sent to this page.
	
	Parameters:
		url - the url to retrieve the query string parameters from.
	
	Returns:
		An Associative Array containing the 
		query string values.
		ex. var t = retArray["test1"]; // gives you the value for test1 from url.
		
	Notes:
		arrParamNames & arrParamValues are also filled
		with the name and values of the query string parameters
		respectively.  
*/
var arrParamNames;
var arrParamValues;
function getURLParameters(url) 
{
	var retArray = new Array();
	//
	
	if (url.indexOf("?") > 0)
	{
		var arrParams = url.split("?");
			
		var arrURLParams = arrParams[1].split("&");
		
		arrParamNames = new Array(arrURLParams.length);
		arrParamValues = new Array(arrURLParams.length);
		
		var i = 0;
		for (i=0;i<arrURLParams.length;i++)
		{
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "No Value";
		}
		
		for (i=0;i<arrURLParams.length;i++)
		{
			// setup associative array with the urls in it.
			retArray[arrParamNames[i]] = arrParamValues[i];
			//alert(arrParamNames[i]+" = "+ arrParamValues[i]);
		}
	}
	/*else
	{
		alert("No parameters.");
	}*/
	return retArray;
}

/*
	getCurrentURLParameters(url)
	
	Description:
		Splits the current url on the ? character
		and builds an associative array of url parameters
		that were sent to this page.
	
	Returns:
		An Associative Array containing the 
		query string values.
		ex. var t = retArray["test1"]; // gives you the value for test1 from url.
		
	Notes:
		arrParamNames & arrParamValues are also filled
		with the name and values of the query string parameters
		respectively.  
*/
function getCurrentURLParameters()
{
	return getURLParameters(window.document.URL.toString());
}

/*
	Description:
		Opens a new window.
	Parameters:
		url - url to open in new window.
		name - name of window being opened (no spaces are allowed).
		width - the width of the new window.
		height - the height of the new window.
*/
function openWindow(url, name, width, height){
	var options = 'directories=no, menubar=no, resizable=yes, width=' + width + ', height=' + height + ', scrollbars=no, toolbar=no, status=no';
	window.open(url, name, options);	
}

/*
	Description:
		Give the focus to the given control.
	Parameters:
		formControl - the control that is to receive focus
*/
function giveFocus(formControl){
	formControl.focus();
}

/*
	Description:
		Builds the url string to report
		an error on a web page.  Also
		prompts for a description of the error.
	Parameters:
		reportToUrl - the url to pass the error details to.
		script - the script that generated the error 
		reporter - the email address of the reporting person
*/
function reportWebError(reportToUrl, script, reporter){
	var reportUrl = reportToUrl + "?script=" + script + "&reporter=" + reporter;
	var description = prompt("Help me help you...\nPlease enter a BREIF description of the problem:");
	reportUrl += "&description=" + description;
	if(description != null)
		window.location = reportUrl;
	else
		alert("Error not reported.");
}

/*
	Description:
		Writes a cookie.
	Parameters:
		name - the name of the cookie.
		value - the value of the cookie.
		expires - the date in which the cookie expires.
		path - the path of the server that is able to read the cookie.
		domain - the domain that is able to read the cookie.
		secure - whether the cookie is secure or not.
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/*
	Description:
		Validates an email to make sure
		it is in the correct form.
	Parameters:
		emailStr - the email address to validate
	Return:
		Returns true if the email is in valid form
		and returns false if the email if not in correct form.
*/
function validateEmail(emailStr){
	var re = new RegExp(/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/);
	var m = re.exec(emailStr);
	if(m == null){
		return false;
	}else{
		return true;
	}
}

/*
	Executes an xml http request.
	Params:
	requestMethod		-	"GET" or "POST" determining 
							the request method to use with 
							xml request.
	requestUrl			-	URL to request.
	OnRequestComplete	-	Function that is called when
							the XMLHttpRequest has reached
							readyState 4 and has returned
							a status of 200 OK.
							This routine should accept a single
							parameter which is the XMLHttpRequest
							object that started request.
*/
function executeXmlHttpRequest(
	requestMethod, 
	requestUrl, 
	OnRequestComplete){

	var xhttp = null;
	if(document.all){
		xhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}else{
		xhttp = new XMLHttpRequest();
	}
	// register the http callback.
	xhttp.onreadystatechange = createXmlHttpCallback(xhttp, OnRequestComplete);
	// initiate the xml request.
	xhttp.open(requestMethod, requestUrl, true);
	xhttp.send(null);
}

/*
	Creates a new callback routine for 
	a given XMLHttpRequest.  
	Params:
	xhr - XMLHttpRequest object the callback is for.
	OnRequestComplete - The function which is called
						when the xhr has a ready state
						of 4 and status of 200.
*/
function createXmlHttpCallback(xhr, OnRequestComplete){
	return function(){
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				OnRequestComplete(xhr);
			} // if xhr.status == 200
		} // if xhr.readyState == 4
	} // closing function
}

/* usage:
	var elem_value = $('elem-id').value;
	var elems = $('elem-id-1', 'elem-id-2');
*/
function getElement() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function show(element_id){
	elem = getElement(element_id);
	elem.style.display = '';
}

function hide(element_id){
	elem = getElement(element_id);
	elem.style.display = 'none';
}


/*
	Description:
		validates an ACMID.  
	Parameters:
		acmID - the acmID to test
	Return:
		positive int - valid acmid
		negative int - invalid acmid
*/
function validateACMID(acmID)
{
		var regex = new RegExp("^15-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}$");
		var regexShort = new RegExp("^5-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}$");

		// Check for correct length
		if(acmID.search(regex) < 0 && acmID.search(regexShort) < 0)
		{
			return -1;
		}
		else 
		{
			return 1;
		}
}


/*
	Description:
		Checks to see if a string is alpha numeric
	Parameters:
		str - the string to test
	Return:
		true if the string is alpha numeric
		false otherwise
*/
function isAlphaNumeric(str)
{
		var regex = new RegExp("^[a-zA-Z0-9]*$");

		// Check for correct length
		return (str.search(regex) >= 0);
}

/*
	Description:
		Embeds a flash file in the given div.
	Parameters:
		div_id = the id of the element to write embed object to 
		src - the location of the flash file.
		height - the height of the flash file to embed.
		width - the width of the embed source.
*/
function embedFlash(div_id, src, height, width){
	var ee = getElement(div_id);
	var h = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
			"codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" " + 
			"width=\"" + width + "\" " +
			"height=\"" + height + "\" " +
			"align=\"\" >" +
			"<param name=movie value=\"" + src + "\">" + 
			"<param name=quality value=high>" +
			"<param name=bgcolor value=#ffffff>" +
			"<embed src=\"" + src + "\" quality=high bgcolor=#ffffff width=\"" + width + "\" height=\"" + height + "\" " +
			"type=\"application/x-shockwave-flash\" " +
			"pluginspage=\"http://www.macromedia.com/go/getflashplayer\">" +
			"</embed></object>";
	//alert(h);
	ee.innerHTML = h;
}
