// $Id: util.js 2318 2008-06-04 19:12:47Z devonc $

//var CurrentPageObjectiveId = 2;

function htmlEncode(s) {
    var str = new String(s);
    str = str.replace(/&/g, "");
    str = str.replace(/</g, "");
    str = str.replace(/>/g, "");
    str = str.replace(/"/g, "");
    return str;
}

function trim(str){
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function OpenSizedWin(url, name, returnwin)
{    
	var win;
	//win = window.open(url,name,'toolbar=no,location=no,maximize=no,minimize=yes,directories=no,status=yes,menubar=no,resizable=yes,copyhistory=no,fullscreen=yes,scrollbars=yes,width='+ screen.width   + ',height=' + screen.height  + ',left=0,top=0');
	
	if (screen.height > 100)
	{
	    win = window.open(url,name,'toolbar=no,location=no,maximize=yes,minimize=yes,directories=no,status=yes,menubar=no,copyhistory=no,scrollbars=yes,width='+ screen.width   + ',height=' + (screen.height-100)  + ',left=0,top=0');
	}
	else
	{
	    win = window.open(url,name,'toolbar=no,location=no,maximize=yes,minimize=yes,directories=no,status=yes,menubar=no,copyhistory=no,scrollbars=yes,width='+ screen.width   + ',height=' + screen.height + ',left=0,top=0');
	}


	if (returnwin == true)    
		return win;

	win.focus();
}

/* DMC added 4/16/08 */
function openAutosizedWin(url, name, returnwin)
{    
	var win;
	//win = window.open(url,name,'toolbar=no,location=no,maximize=no,minimize=yes,directories=no,status=yes,menubar=no,resizable=yes,copyhistory=no,fullscreen=yes,scrollbars=yes,width='+ screen.width   + ',height=' + screen.height  + ',left=0,top=0');
	
	win = window.open(url,name,'toolbar=yes,location=yes,menubar=yes,scrollbars=yes,resizeable=yes');

	if (returnwin == true){    
		return win;
		}
    else {
        return false;
    }
	win.focus();
}
/* End DMC */

function zipValid(aZip) 
{
	if (aZip == "Zip/Postal Code") {
		alert("Please enter full zip code (should be at least 5 characters long)");
		return false;
	}
	
	if (aZip.length < 5) {
		alert("Please enter full zip code (should be at least 5 characters long)");
		return false;
	}
	if (aZip.length > 7) {
		alert("Zip/Postal code can not be greater than 7 characters long");
		return false;
	}
	if (!isAlphaNumeric(aZip)) {
		alert("Please enter full zip code (should contain only alpha-numeric characters)");
		return false;
	}
	needsValidation = false;
	return true;			
}

function optionValid(aOption)
{
	if (aOption == "")
	{
		alert("Please select a test type");
		return false;
	}
	return true;
}

function validateBasic(itemCode, zipcode) 
{
	if (!optionValid(itemCode)) return false;
	if (!zipValid(zipcode)) return false;
	return true;
}

function clearDefault()
{
	if (document.getElementById("txtFindCourse_keyword").value == "Enter Zip/Postal Code")
	 document.getElementById("txtFindCourse_keyword").value = "";
}

function isAlphaNumeric(aString) 
{
	var s = aString.toUpperCase();
	var ch;
	for (var i = 0; i < s.length; i++) 
	{
		ch = s.charAt(i);
		if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch == ' '))) 
		{
			return false;
		}
	}
	return true;		
}

function readCookie(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;
}

function createCookie(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 = "";
	}
	
	if (document.cookie.indexOf("CurrentPageObjective") < 0 )
	{
	    document.cookie	= name+"="+value+expires+";	path=/";
        CurrentPageObjectiveId = value;	    
	}
	else
	{
	    //fails to create
	}
}

function getVar(name) {
	get_string = document.location.search;		   
	return_value = '';	
	do { //This	loop is	made to	catch all instances	of any get variable.
		name_index = get_string.indexOf(name + '=');
			
		if(name_index != -1)
			{
			  get_string = get_string.substr(name_index	+ name.length +	1, get_string.length - name_index);
			  
			  end_of_value = get_string.indexOf('&');
			  if(end_of_value != -1)				
				value =	get_string.substr(0, end_of_value);				   
			  else				  
				value =	get_string;				   
				
			  if(return_value == ''	|| value ==	'')
				 return_value += value;
			  else
				 return_value += ',	' +	value;
			  }
			} while(name_index != -1)
			
		 //Restores	all	the	blank spaces.
		 space = return_value.indexOf('+');
		 while(space !=	-1)
			  {	
			  return_value = return_value.substr(0,	space) + ' ' + 
			  return_value.substr(space	+ 1, return_value.length);
			  space	= return_value.indexOf('+');
			  }
		  
	return(return_value);		 
}

function changeClass(Elem, myClass) {
	Elem.className = myClass;
}

/* parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com)
Splits any well-formed URI into the following parts (all are optional):
----------------------
• source (since the exec() method returns backreference 0 [i.e., the entire match] as key 0, we might as well use it)
• protocol (scheme)
• authority (includes both the domain and port)
    • domain (part of the authority; can be an IP address)
    • port (part of the authority)
• path (includes both the directory path and filename)
    • directoryPath (part of the path; supports directories with periods, and without a trailing backslash)
    • fileName (part of the path)
• query (does not include the leading question mark)
• anchor (fragment)
*/
function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};
    
    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }
    
    // Always end directoryPath with a trailing backslash if a path was present in the source URI
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }
    
    return uri;
}