var spendUnitsPrompt = "Click OK to confirm the assignment of this activity and automatically deduct from available units.";

function getInsufficientUnitsMessage(type)
{
	var message = "";

	if(type == 1)
	    message = "You have insufficient units available to assign Diagnostic Assessment Tests. Please contact your A+dvancer Solution Consultant at 1-800-222-2811."
	else
	    message = "You have insufficient units available to assign Coursework. Please contact your A+dvancer Solution Consultant at 1-800-222-2811."
	return message;
}

function addToSelectBox(selName, otherSelName, bCopy)
{
	var Lessons = new Array(); 
	var Value = new Array();
	var j = selName.length; 
	var k = 0; 
	var sList = "";
	var l = 0;
	var sSaved = "";
	for(var i = 0; i<j; i++) 
	{
		if(selName[i].selected)
		{
			if(selName.options[i].value == -1)
				selName.options[i] == null;
			else{
			 	Lessons[k] = selName[i].text;

			Value[k] = selName[i].value;		 	
			 k++; 
			 }
		}
	}
	//move to other select box
	var sOption; 
	for(i = 0; i < k; i++) 
	{
		sOption = Lessons[i]; 
		if ( !bCopy )
			selName.options[selName.selectedIndex] = null;
		var newOption = new Option(sOption, Value[i]);
		otherSelName.options[otherSelName.length] = newOption;
	}
}

//additional parameters passed into the function will be for disabling objects after submit
//to handle the IE click happy case :).
function submitFormFunc(form) 
{
	form.submit();
	//start index of argument array is 1 because the form element is the first argument
	for(var x = 1; x<submitFormFunc.arguments.length;x++)
	{
		field = "";
		field = submitFormFunc.arguments[x];
		if(field != null && field != "")
			field.disabled = true;
	}
}

function disableFields() 
{
	for(var x = 0; x<disableFields.arguments.length;x++)
	{
		field = "";
		field = disableFields.arguments[x];
		if(field != null && field != "")
			field.disabled = true;
	}
}


function submitOnValidation(form, fields)
{
	var isValid = true;
	field = "";
	for(i = 0; i < fields.length; i++)
	{	
		currField = fields[i];
		field = currField;
		if(currField.value != "" && currField != null)
		{
			continue;
		}else
		{
			isValid = false;
			break;
		}
	}	
	if(isValid == false)
	{
		alert("Field " + field.name + " is invalid.");
		return false;
	}
	else
		form.submit();

	return true;
}

//this function escapes ampersand char's.
function buildFormPostStr(formEle)
{
	var formStr = "";
	for(i=0; i<formEle.length; i++) 
	{
		//formStr += formEle[i].name + "=" + formEle[i].value;
	
		var tmpStr = formEle[i].name + "=" + formEle[i].value;
		tmpStr = replaceString(tmpStr, "&", "%26");
		tmpStr = replaceString(tmpStr, "#", "%23");
	
		formStr += tmpStr;

		if(i+1 < formEle.length)
			formStr += "&";
	}
	return formStr;
}

function findParam(paramName)
{
  var indexstart;
	var indexend;
	var query;
	var paramValue = "_default";
	query = location.search;
	if(query.indexOf(paramName) != -1)
	{
		indexstart = query.indexOf(paramName)
		indexstart = query.indexOf("=", indexstart)
		indexend   = query.indexOf("&", indexstart)
		if(indexend != -1)
			paramValue = query.substring(indexstart + 1, indexend)
		else
			paramValue = query.substring(indexstart + 1);

		//alert("returning paramValue: " + paramValue);
	}
	return paramValue;
}

//this function DOES NOT escape ampersand char's.
function buildFormPostLiteral(formEle)
{
	var formStr = "";
	for(i=0; i<formEle.length; i++) 
	{
		//formStr += formEle[i].name + "=" + formEle[i].value;
	
		var tmpStr = formEle[i].name + "=" + formEle[i].value;
		tmpStr = replaceString(tmpStr, "#", "%23");
		formStr += tmpStr;

		if(i+1 < formEle.length)
			formStr += "&";
	}
	return formStr;
}


function buildFormPostStr2(formEle)
{
	var formStr = "";
	for(i=0; i<formEle.length; i++) 
	{
		if(formEle[i].type == "radio")
		{
			if(formEle[i].checked)
			{
				//formStr += formEle[i].name + "=" + formEle[i].value;
				var tmpStr = formEle[i].name + "=" + formEle[i].value;
				tmpStr = replaceString(tmpStr, "&", "%26");
				tmpStr = replaceString(tmpStr, "#", "%23");
				formStr += tmpStr;

				if(i+1 < formEle.length && tmpStr != "")
				{
					formStr += "&";
				}
			}
		}
		else if(formEle[i].type == "select-multiple")
		{
			//formStr += getSelectedListItems(formEle[i], formEle[i].name);
			var tmpStr = getSelectedListItems(formEle[i], formEle[i].name);
			tmpStr = replaceString(tmpStr, "&", "%26");
			tmpStr = replaceString(tmpStr, "#", "%23");
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}

		else
		{
			//formStr += formEle[i].name + "=" + formEle[i].value;
			var tmpStr = formEle[i].name + "=" + formEle[i].value;
			tmpStr = replaceString(tmpStr, "&", "%26");
			tmpStr = replaceString(tmpStr, "#", "%23");
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}
	}	
	return formStr;
}

function replaceString(thestring, srchstr, repstr)
{
	if(thestring == null || thestring == 'undefined')
		return thestring;
	if(srchstr == repstr)
		return thestring;
	var loc = thestring.indexOf(srchstr);
	while(loc >= 0)
	{
		thestring = thestring.replace(srchstr, repstr);
		loc = thestring.indexOf(srchstr, loc + repstr.length);
	}
	return thestring;
}

function buildFormPostStr2Literal(formEle)
{
	var formStr = "";
	for(i=0; i<formEle.length; i++) 
	{
		if(formEle[i].type == "radio")
		{
			if(formEle[i].checked)
			{
				//formStr += formEle[i].name + "=" + formEle[i].value;
				var tmpStr = formEle[i].name + "=" + formEle[i].value;
				tmpStr = replaceString(tmpStr, "#", "%23");
				formStr += tmpStr;

				if(i+1 < formEle.length && tmpStr != "")
				{
					formStr += "&";
				}
			}
		}
		else if(formEle[i].type == "select-multiple")
		{
			//formStr += getSelectedListItems(formEle[i], formEle[i].name);
			var tmpStr = getSelectedListItems(formEle[i], formEle[i].name);
			tmpStr = replaceString(tmpStr, "#", "%23");
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}

		else
		{
			//formStr += formEle[i].name + "=" + formEle[i].value;
			var tmpStr = formEle[i].name + "=" + formEle[i].value;
			tmpStr = replaceString(tmpStr, "#", "%23");
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}
	}	
	return formStr;
}

function buildFormPostForAddAssignmentBySubject(formEle)
{
	var formStr = "";
	for(i=0; i<formEle.length; i++) 
	{
		if(formEle[i].type == "radio")
		{
			if(formEle[i].checked)
			{
				//formStr += formEle[i].name + "=" + formEle[i].value;
				var tmpStr = formEle[i].name + "=" + formEle[i].value;
				tmpStr = replaceString(tmpStr, "&", "%26");
				tmpStr = replaceString(tmpStr, "#", "%23");
				formStr += tmpStr;

				if(i+1 < formEle.length && tmpStr != "")
				{
					formStr += "&";
				}
			}
		}
		else if(formEle[i].type == "select-multiple")
		{
			//formStr += getSelectedListItems(formEle[i], formEle[i].name);
			var tmpStr = getSelectedListItems(formEle[i], formEle[i].name);
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}

		else
		{
			//formStr += formEle[i].name + "=" + formEle[i].value;
			var tmpStr = formEle[i].name + "=" + formEle[i].value;
			tmpStr = replaceString(tmpStr, "&", "%26");
			tmpStr = replaceString(tmpStr, "#", "%23");
			formStr += tmpStr;
			if(i+1 < formEle.length && tmpStr != "")
				formStr += "&";
		}
	}	
	return formStr;
}

function getSelectedListItems(selName, selNameAttrib)
{
	var listStr = "";
	var firstTime = true;
	for(j=0; j<selName.length; j++) 
	{
		if(selName[j].selected)
		{
			if(firstTime)
			{
				listStr += selNameAttrib + "=" + selName[j].value;
				firstTime = false;
			}
			else
				listStr += "&" + selNameAttrib + "=" + selName[j].value;
		}
	}

	return listStr;
}

function getListItems(selName, selNameAttrib)
{
	var listStr = "";
	for(i=0; i<selName.length; i++) 
	{
		if(i==0)
			listStr += selName[i].value;
		else
			listStr += "&" + selNameAttrib + "=" + selName[i].value;
	}

	return listStr;
}

function selectAllListItems(selName)
{
	for(i=0; i<selName.length; i++) 
		selName[i].selected = true;
}

function getParamValue(paramName)
{
	var indexstart;
	var indexend;
	var query;
	var paramValue = "_default";
	query = document.location.search;
	if(query.indexOf(paramName) != -1)
	{
		indexstart = query.indexOf(paramName)
		indexstart = query.indexOf("=", indexstart)
		indexend   = query.indexOf("&", indexstart)
		if(indexend != -1)
			paramValue = query.substring(indexstart + 1, indexend)
		else
			paramValue = query.substring(indexstart + 1);
  
		//alert("returning paramValue: " + paramValue);
	}
	return paramValue;
}

function getParamMultiValue(paramName)
{
	var paramValue = "";
	var retVal = "";
	var query = document.location.search;

	while(query.indexOf(paramName) != -1)
	{

	//grab first instance of name-value pair
		paramstart = query.indexOf(paramName)
		valstart = query.indexOf("=", paramstart)
		valend   = query.indexOf("&", valstart)
		queryend = query.length;

		if(valend != -1)
			paramValue = query.substring(valstart + 1, valend)
		else
			paramValue = query.substring(valstart + 1);

	//append this name-value pair to the return value
		if(retVal == "")
			retVal = retVal + paramName + "=" + paramValue;
		else
			retVal = retVal + "&" + paramName + "=" + paramValue;

	//delete this name-value pair from the temp query string
		if(valend != -1)
			query = query.substring(0, paramstart) + query.substring(valend + 1, queryend);
		else
			query = query.substring(0, paramstart - 1);
	}

	return retVal;
}

function stripUnnessaryParams(paramName, query)
{
	var paramValue = "";
	var retVal = "";

	while(query.indexOf(paramName) != -1)
	{

	//grab first instance of name-value pair
		paramstart = query.indexOf(paramName)
		valstart = query.indexOf("=", paramstart)
		valend   = query.indexOf("&", valstart)
		queryend = query.length;

	if(valend != -1)
		paramValue = query.substring(valstart + 1, valend)
	else
		paramValue = query.substring(valstart + 1);

	//append this name-value pair to the return value
		if(paramValue.length > 0)
		{
			if(retVal == "")
				retVal = retVal + paramName + "=" + paramValue;
			else
				retVal = retVal + "&" + paramName + "=" + paramValue;
		}

	//delete this name-value pair from the temp query string
		if(valend != -1)
			query = query.substring(0, paramstart) + query.substring(valend + 1, queryend);
		else
			query = query.substring(0, paramstart - 1);
	}

	return retVal;
}

 var DISABLED_FORM_TIMEOUT = 60000;	 //delay the enabling of a disabled form in milliseconds

 function ALS_SubmitForm(form)
 {

	form.submit();

	disableForm(form);

	// delay the enabling of the form
	var timeout = setTimeout('enableForm(document.' + form.name + ')', DISABLED_FORM_TIMEOUT);
 }
 
 function disableFormElements()
 {
	form = document.forms[0];
	
	disableForm(form);

	//delay the enabling of the form
	var timeout = setTimeout('enableForm(document.' + form.name + ')', DISABLED_FORM_TIMEOUT); 
 }
 
 function disableForm(form)
 {
	if(typeof fb_DISABLE_CLICK != 'undefined')
		fb_DISABLE_CLICK = true;

	if(typeof pb_DISABLE_CLICK != 'undefined')
		pb_DISABLE_CLICK = true;		
	
	if(form.length > 0)
	{
		for(i=0; i<form.length; i++)
		{
			form.elements[i].disabled = true;
		}
	}
 }

 function enableForm(form)
 {
	if(typeof fb_DISABLE_CLICK != 'undefined')
		fb_DISABLE_CLICK = false;

	if(typeof pb_DISABLE_CLICK != 'undefined')
		pb_DISABLE_CLICK = false;		
	
	if(form.length > 0)
	{
		for(i=0; i<form.length; i++)
		{
			form.elements[i].disabled = false;
		}
	}
 }

function trim( inputString )
{
 	// Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if ( typeof inputString != "string" )
	{
		return inputString ;
	}

   var retValue = inputString ;
   var ch = retValue.substring( 0, 1 ) ;
   while ( ch == " " )
	{
		// Check for spaces at the beginning of the string
      retValue = retValue.substring( 1, retValue.length ) ;
      ch = retValue.substring( 0, 1 ) ;
   }

   ch = retValue.substring( retValue.length - 1, retValue.length ) ;

   while ( ch == " " )
	{
		// Check for spaces at the end of the string
      retValue = retValue.substring( 0, retValue.length - 1 ) ;
      ch = retValue.substring( retValue.length - 1, retValue.length ) ;
   }

   while ( retValue.indexOf( "  " ) != -1 )
	{
		// Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring( 0, retValue.indexOf( "  " ) ) + retValue.substring( retValue.indexOf( "  " ) + 1, retValue.length ); // Again, there are two spaces in each of the strings
   }

   return retValue; // Return the trimmed string back to the user
}

