function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setconfirmedcookie() {
var conf = confirm('Are you sure you want to delete this user?');
setCookie('deleteconfirmed',conf,1,'/','','');  	
}		 

function fullscreen(enrollid,moduleid,lessonid,examid,url) {
 setCookie('refreshneeded',0,1,'/','','');
 setCookie('enrollid',enrollid,1,'/','','');
 setCookie('moduleid',moduleid,1,'/','','');
 setCookie('lessonid',lessonid,1,'/','','');
 setCookie('examid',examid,1,'/','','');
 setCookie('starttime',Date(),1,'/','',''); 
 params  = 'width='+screen.width;
 params += ', height='+screen.height;
 params += ', top=0, left=0'; 
 params += ', toolbar=no,location=no,scrollbars=no,resizable=yes';
 newwin=window.open(url,'windowcalled', params);
 if (window.focus) {newwin.focus()}
 return false;
}
function popupcenter(url,wwidth,wheight) {
 var wleft = (screen.width - wwidth) / 2;
 var wtop = (screen.height - wheight) / 2;
 params  = 'width='+wwidth;
 params += ', height='+wheight;
 params += ', top='+wtop;
 params += ', left='+wleft;
 params += ', toolbar=no,location=no,scrollbars=yes,resizable=yes';
 newwin=window.open(url,'windowcalled', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function hlink(url) {
 window.location = url;
}

function closewindow() {
window.close();
return false;
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,showerr){
 	if (showerr == 2 && !dtStr) {
		return true;
	}

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
	 	if (showerr > 0) {
			alert("The date format should be : mm/dd/yyyy")
		}
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
	 	if (showerr > 0) {
			alert("Please enter a valid month")
		}
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
	 	if (showerr > 0) {
			alert("Please enter a valid day")
		}
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
	 	if (showerr > 0) {
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		}
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
	 	if (showerr > 0) {
			alert("Please enter a valid date")
		}
		return false
	}
return true
}


function todayStr() {
	var today=new Date();
//	return today.getMonth()+1+"/"+today.getDate()+"/"+(today.getYear());
	return formatDate(today);
}

function addDays(myDate,days) {
var d1 = new Date(myDate);
d1.setDate(d1.getDate() + days);
return(d1);
}

function formatDate(myDate) {
if (!myDate) {
	return '';
}
var d1 = new Date(myDate);
var dd = d1.getDate();
var mm = d1.getMonth()+1;//January is 0!
var yyyy = d1.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
return(mm+'/'+dd+'/'+yyyy);
}

function displayRow(rowid,showhide){
	var row = document.getElementById(rowid);
	if (showhide) {
	 	row.style.display = 'none';
	} else {
	 	row.style.display = '';
	}
}
function trim(instr) {
	retstring = instr.replace(/^\s+|\s+$/g, '');
	return retstring;
}

