// Automatically set the timeout period to 15 minutes
//setTimeout('ShowTimeoutWarning()', 900000);

// Lesson Plan Functions
// Pops up a window to allow the user the select an
// objective based on the State, Grade Level, and Subject
function Select_Objective(state, grade, subject, ctrl){
	var sURL;
	sURL = "lessonplan_selectobjective.aspx?Ctrl='" + ctrl + "'&State='" + state + "'&Grade=" + grade + "&Subject=" + subject;
	openWindow(sURL, 550, 450, '', false);
}
function Select_ResourceMaterial(subject, grade, ctrl){
	var sURL;
	sURL = "lessonplan_selectResMat.aspx?Ctrl='" + ctrl + "&Subject=" + subject + "&Grade=" + grade;
	openWindow(sURL, 550, 450, '', true);
}
//------------------------------------------------------------------------------------------------
//--- User Section Functions
//------------------------------------------------------------------------------------------------
function DeleteUser(ID){
	var sURL = 'myaccount_delete.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Cancel Account', false);
}
function DeleteAccountOwner(ID){
	DeleteUser(ID);
}
function DeleteTeacher(ID){
	DeleteUser(ID);
}
function ReinviteTeacher(ID){
	var sURL = 'myaccount_reinvite_confirm.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Reinvite Teacher', false);
}
//------------------------------------------------------------------------------------------------
//--- Training Section Functions
//------------------------------------------------------------------------------------------------
function AddTraining(){
	var sURL = 'training_edit.aspx?ID=0';
	window.location = sURL;
}
function DeleteTraining(ID){
	var sURL = 'training_delete_confirm.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Delete Training Session', false);
}
function CopyTraining(ID){
	var sURL = 'training_copy_confirm.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Copy Training Session', false);
}
//------------------------------------------------------------------------------------------------
//--- Lesson Plan Section Functions
//------------------------------------------------------------------------------------------------
function AddLessonPlan(){
	var sURL = 'lessonplan_edit.aspx?ID=0';
	window.location = sURL;
}
function DeleteLessonPlan(ID){
	var sURL = 'lessonplan_delete_confirm.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Delete Lesson Plan', false);
}
function CopyLessonPlan(ID){
	var sURL = 'lessonplan_copy_confirm.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Copy Lesson Plan', false);
}
function CopySharedLessonPlan(ID){
	var sURL = 'lessonplan_copy_confirm.aspx?ID=' + ID + '&shared=1';
	openWindow(sURL, 300, 115, '', 'Copy Lesson Plan', false);
}
//------------------------------------------------------------------------------------------------
//--- Graphic Organizers Section Functions
//------------------------------------------------------------------------------------------------
function DeleteOrganizer(ID){
	var sURL = 'organizer_delete.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Delete Organizer', false);
}
function ApproveOrganizer(ID){
	var sURL = 'organizer_approve.aspx?ID=' + ID;
	openWindow(sURL, 300, 115, '', 'Approve Organizer', false);
}

//function TimeoutNow(){
//    // The page has timed out so it is useless to ask the user
//    // if they want to stay on the page or not.
//    window.onbeforeunload = "";
//    window.location = "/Users/SessionExpired.aspx";
//	alert("Your session has timed out.");
//}
function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(ms) {
var sec = Math.floor(ms/1000)
ms = ms % 1000
t = three(ms)

var min = Math.floor(sec/60)
sec = sec % 60
t = two(sec) + ":" + t

var hr = Math.floor(min/60)
min = min % 60
t = two(min) + ":" + t

var day = Math.floor(hr/60)
hr = hr % 60
t = two(hr) + ":" + t
t = day + ":" + t

return t
}

function EditLookup( LookupType, LookupID ){
	var sURL = 'admin_editlookup.aspx?type=' + LookupType + '&ID=' + LookupID;
	openWindow(sURL, 300, 115, '', 'Edit Lookup', false);	
}
function DeleteLookup( LookupType, LookupID ){
	var sURL = 'admin_deletelookup.aspx?type=' + LookupType + '&ID=' + LookupID;
	openWindow(sURL, 300, 115, '', 'Edit Lookup', false);	
}
// Adds 5 days to a date.

function DXAddWeek(selectedDate, controlToUpdateID){
	var passedDate = new Date(selectedDate.value.toString());
	var selectedDay = passedDate.getDate();
	selectedDay = selectedDay + 4;
	var newDate = new Date(passedDate.setDate(selectedDay));
	// Update the control to update.
	document.getElementById( controlToUpdateID ).value = newDate.getMonth() + 1 + "/" + newDate.getDate() + "/" + newDate.getFullYear();
}
function getcookie(cookiename) {
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return ""; 
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length; 
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,duration){
	cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
	document.cookie=cookiestring;
	if(!getcookie(name)){
		return false;
	} else {
		return true;
	}
}
//------------------------------------------------------------------------------------------------
//--- openWindow
//------------------------------------------------------------------------------------------------
function openWindow(u,W,H,n,title,bModal) {
	if (bModal) {
		return window.showModalDialog(u, '', 'dialogHeight:' + H + 'px;dialogWidth:' + W + 'px;status:no;edge:raised;help:no;scroll:no;');
	} else {
		var sOptions = 'width=' + W + ',' + ',height=' + H + ',status=no,toolbar=no,scrollbars=yes,resizable=yes,location=no,menubar=no';
		return window.open(u, n, sOptions, true);
	}
}

//------------------------------------------------------------------------------------------------
//--- State / District / School Selection Functions
//------------------------------------------------------------------------------------------------
function SortDropDown(el){

	if(el.options.length < 2){ return; }
	var opts = new Array();
	var opts_text = new Array();
	var opts_value = new Array();	    
	
	for(var i=0; i<el.options.length; i++){
		opts[i] = el.options[i].text;
		opts_text[i] = el.options[i].text;
		opts_value[i] = el.options[i].value;
	}//end for
	
	opts.sort();	
	//clear list
	for(var i=el.options.length-1; i>=0; i--){
		el.options.remove(i);
	}//end for

	for(var index=0; index<opts.length; index++){    
		var unsort_index = null;
		for(var i=0; i<opts_text.length; i++){
			if(opts[index] == opts_text[i]){
				unsort_index = i;
				break;
			}//end if				    
		}//end for		
		var opt = document.createElement("OPTION");
		opt.text = opts_text[unsort_index];
		opt.value = opts_value[unsort_index];
		el.options.add(opt);
	}//end for	    
}//end SortDropDown()

function ClearList(el){

	if ( el.options.length > 0 ){
		el.options.length = 0;
	}
	var opt = document.createElement("option");
	opt.text = "(Please Select)";
	opt.value = "0";
	el.add(opt);
}

function ClearListById(ID){
	var el = document.getElementById(ID);
	if ( el != null ){ ClearList( el ); }
}
function DropDownContains(el, val){
	for(var i=0; i<el.options.length; i++){
		if(el.options[i].value == val)
			return true;
	}//end for 
	
	return false;
}//end DropDownContains()

function OnStateChange( stateEl ){
	var districtEl = document.getElementById( cboDistrictID );
	var schoolEl = document.getElementById( cboSchoolID );
	var x;
	var opt = stateEl.options[stateEl.selectedIndex];	    
	var stateID = opt.value;
	
	if(stateID == ""){
		districtEl.disabled = true;
		stateEl.value = "";
		return;
	} else {
		document.getElementById( "hiddenState" ).value = stateID;
		stateEl.value = stateID;
		districtEl.disabled = false;
					
	}//end if
	    
	ClearList(districtEl);
	ClearList(schoolEl);	    
	
	// Loop through the districts and find all with the
	// selected state id
	for( x=0; x < arrDistricts.length; x++){
		if ( arrDistricts[x][1] == stateID ){
			var o = document.createElement("OPTION");
			o.value = arrDistricts[x][0];
			o.text = arrDistricts[x][2];
			districtEl.add(o);		
		}//end if
	}//exit for

	SortDropDown(districtEl);
	schoolEl.disabled = true;
	
	if(districtEl.options.length == 2){
		districtEl.selectedIndex = 1;
		DistrictChange( districtEl );
		districtEl.disabled = true;
	}//end if
	
}
function DistrictChange( el ){
	var opt = el.options[el.selectedIndex];
	
	OnDistrictChange( opt.value );

}//end EventChange()

function OnDistrictChange( districtID ){

	var schoolEl = document.getElementById( cboSchoolID );
	var stateEl = document.getElementById( cboStateID );
	var stateID = stateEl.options[stateEl.selectedIndex].value;

	if(districtID == ""){
		schoolEl.disabled = true;
		return;
	} else {
		document.getElementById( "hiddenDistrict" ).value = districtID;
		schoolEl.disabled = false;
	}//end if	    

	ClearList(schoolEl);
	
	// Loop through the schools and find all with the
	// selected state id and district id
	for(var x=0; x < arrSchools.length; x++){
		if ( arrSchools[x][0] == districtID && arrSchools[x][1] == stateID ){
			var o = document.createElement("OPTION");
			o.value = arrSchools[x][2];
			o.text = arrSchools[x][3];
			schoolEl.add(o);		
		}//end if
	}//exit for  

	SortDropDown(schoolEl);
	
	if(schoolEl.options.length == 2){
		schoolEl.selectedIndex = 1;
		schoolEl.disabled = true;
	}//end if
}

function OnSchoolChange( el ){
	var opt = el.options[el.selectedIndex];
	document.getElementById( "hiddenSchool" ).value = opt.value;
}
