function strTrim(str)
{
    return str.replace(/^\s+/,'').replace(/\s+$/,'');
}
function openAppletWin(url){
	var maxHeight	= 800;
	var minHeight	= 528;
	
	var height		= window.screen.availHeight ;
	height			= height < minHeight ? minHeight: height;
	height 			= height > maxHeight ? maxHeight: height;
	
	Win( url, 'IaSession',850,height,0,'no','yes','yes','no');
}
function openPlayerWin(url, playerFileName){
	var maxHeight	= 800;
	var minHeight	= 528;
	
	var height		= window.screen.availHeight ;
	height			= height < minHeight ? minHeight: height;
	height 			= height > maxHeight ? maxHeight: height;
	playerFileName	= playerFileName + "";
	if ( playerFileName ) 
		playerFileName	= playerFileName.replace(".","_");
	else
		playerFileName 	= "";
	
	Win( url,"Player_" + playerFileName,727,height,0,'no','yes','yes','no');
}

function Win(url, winname, width, height, location, menuBar, scrollBar, resizable , toolBar)
{
    w1 = (window.screen.width/2) - ((width/2)  +  (width/58));
    h1 = (window.screen.height/2) - (height/2  +  (height/14));

    params  = "width=" + width + ",";
    params  += "height=" + height + ",";
    params  += "toolbar=" +  toolBar + ",";
    params  += "location=" + location + ",";
    params  += "directories=0,";
    params  += "status=0,";
    params  += "menubar=" + menuBar + ",";
    params  += "scrollbars=" + scrollBar + ",";
    params  += "resizable=" +  resizable + ",";
    if ( document.layers )
        params += "screenX=" + w1 + ",screenY=" + h1 + ",";
    else
        params += "left=" + w1 + ",top=" + h1;

	openWinCheckPopup (url, winname, params);
}

function openWinCheckPopup (url, winname, params)
{
    var win = window.open(url, winname, params);
    
    if (win==null || typeof(win)=="undefined" || typeof(win.location)=="undefined") 
    {
    	if ( confirm("You have popup blocker(s) running which prevents popup window from opening. Click OK to view instructions on how to disable the popup blocker(s) for Brainfuse.") )
    	{
    		window.location.href = "/help/popupHelper.asp";
    		return true;
    	}
    }
    else
    //if ( win ) //to avoid the refrence being null from poppup blockers.
    	win.focus();

}

function openSearchPage (webRoot)
{
	var searchUrl = webRoot + "/admin/search.asp?viewStyle=Poppup&fromPage=opener";
	Win ( searchUrl, 'Search', 550, 500, 0, 0, 1, 1, 0 );
}

function openSearchPage_1 (webRoot, searchType, single, displayType)
{
	var multipleStr	= ( single)? "&multiple=false":"&multiple=true";
	var display 	= (displayType)? "&display=" + displayType : "&display=active";
	var searchUrl 	= webRoot + "/admin/search.asp?viewStyle=Poppup&fromPage=opener&searchTypeParam=" + searchType + multipleStr + display;
	Win ( searchUrl, 'Search', 550, 500, 0, 0, 1, 1, 0 );
}

function openWinWhoIs ( username){
	openWin_WhoIs(username, leftNavWebRoot);
}



function openWin_WhoIs ( username, webRoot )
{
	var winWidth	= 650;
	var winHeight	= 600;
	var winResize	= 1;
	var winName 	= CleanNonAlphaNumeric ( username );
	var detailsUrl 	= webRoot + "/tutor/whois.asp?username=" + username +  "&viewStyle=Poppup";
	Win ( detailsUrl, winName, winWidth, winHeight, 0, 0, 1, winResize, 0 )
}

function openWin_AcctContact ( uid, acct_id, acct_name, pageToOpen, webRoot )
{
	var winWidth	= 450
	var winHeight	= 300
	var winResize	= 1
	var detailsUrl  = webRoot + "/admin/account_contacts_frame.asp?Contact_ID=" + uid +  "&acct_id=" + acct_id +  "&acct_name=" + acct_name
	if ( pageToOpen != '')
		detailsUrl += "&pageToOpen=" + pageToOpen
	Win(detailsUrl, 'ContactDetail', winWidth, winHeight, 0, 0, 1, winResize, 0 )
}

function openQuizWindow(encodedURL, webRoot)
{
	encodedURL 		= escape(encodedURL);
    var newWinURL = webRoot + "/includes/gateway.asp?redirectPage=" + encodedURL;
	Win(newWinURL, 'quizWindow', 785, 530, 0, 0, 1, 1, 0 )
}



function IsValidTime(timeStr)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null)
	{
		alert( timeStr + " is not in a valid format.");
		return false;
	}

	hour 	= matchArray[1];
	minute 	= matchArray[2];
	second 	= matchArray[4];
	ampm 	= matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23)
	{
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}

	if (hour <= 12 && ampm == null)
	{
			alert("You must specify AM or PM.");
			return false;
	}

	if  (hour > 12 && ampm != null)
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}

	if (minute < 0 || minute > 59)
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}

	if (second != null && (second < 0 || second > 59))
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return true;
}

function IsNumeric(strString)
//  check for valid numeric strings
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
	   	if (strValidChars.indexOf(strChar) == -1)
	    {
	    	alert (strString + " is not a valid number")
			blnResult = false;
	    }
	}
	return blnResult;
}

function IsAlphaNumeric( strString )
{
	var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var strChar;
	var blnResult = true;

	//  test strString consists of valid characters listed above
	for (k = 0; k < strString.length && blnResult == true; k++)
	{
		strChar = strString.charAt(k);
	   	if (strValidChars.indexOf(strChar) == -1)
			blnResult = false;
	}
	return blnResult;
}

function CleanNonAlphaNumeric ( strToClean )
{
	var strChar;
	var strReturn = "";
	for (j = 0; j < strToClean.length; j++)
	{
		strChar = strToClean.charAt(j);
		if ( IsAlphaNumeric(strChar) )
			strReturn += strChar;
	}
	//alert(strReturn);
	return strReturn;
}

function isValidDate(dateStr, isPastYear)
{
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	// if isPastYear is true, checks for year < current year
	
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var isPast = false;
	if ( isPastYear == true )
		isPast = true;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		alert("Date is not in a valid format.")
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) 
	{ // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
	   }
	}
	if ( isPast )
	{
		var thenDat = new Date(year, month-1, day);
		if ( thenDat.getTime() >= new Date().getTime() )
		{
			alert("Please input a past date.")
			return false;
		}
	}
	return true;  // date is valid
}

function isValidZip(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter your zip code in 00000 or 00000-0000 format.");
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}


function checkAll(objCheck)
{
	var     value   = objCheck.checked;
	if ( value == "" ) value = false;
	var name        = objCheck.value;
	var objStudents   = objCheck.form[name];
	for ( var counter=0;counter< objStudents.length;counter++)
	{
		objStudents[counter].checked=value;
	}
}


var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name)
{
	var obj = new Object;
	if (document.getElementById)
	{
		obj = document.getElementById(name);
		//obj.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		obj = document.all[name];
		//obj.style = document.all[name].style;
	}
	else if (document.layers)
	{
		obj = document.layers[name];
		//obj.style = document.layers[name];
	}
	return obj;
}

function disableButton (divID)
{
	var objDiv = getObj(divID);
    objDiv.innerHTML = "<font face=verdana size=1 color=red>processing now... please wait.</font>"
}


function checkTimeout(timeout){
	window.setTimeout(displayTimeoutWarning, (timeout - 5 * 60 * 1000));
}
function displayTimeoutWarning(){
	window.alert("Your session will timeout out in 5 minute please save your work by submitting the form and return to edit the page."  );
}
