function setDropDown(formName, elementName, selField)
{	
	var selected_dep_index = 0;
	var checked = false;
	var tempObj = eval("window.document."+formName+"." +elementName);
	for (var j = 0; j < tempObj.length; j++)
	{
		if (selField == tempObj.options[j].value)
		{
			selected_dep_index = j;
			checked = true;
			break;
		}
	}
	if (checked)
	{
		tempObj.selectedIndex = selected_dep_index;
	}
	else
	{ 
		tempObj.selectedIndex = 0;
	} 
}



function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		district_over = newImage("/cyss0001/jsp/images/statsslices/district-over.gif");
		stats_menu_03_district_over = newImage("/cyss0001/jsp/images/statsslices/stats_menu_03-district_over.gif");
		school_district_over = newImage("/cyss0001/jsp/images/statsslices/school-district_over.gif");
		school_over = newImage("/cyss0001/jsp/images/statsslices/school-over.gif");
		classes_school_over = newImage("/cyss0001/jsp/images/statsslices/classes-school_over.gif");
		classes_over = newImage("/cyss0001/jsp/images/statsslices/classes-over.gif");
		students_classes_over = newImage("/cyss0001/jsp/images/statsslices/students-classes_over.gif");
		students_over = newImage("/cyss0001/jsp/images/statsslices/students-over.gif");
		preloadFlag = true;
	}
	//setTimeout("StartLooping()", 250);
}

//function StartLooping() {
//	ProcessWeek(1);
//}	
	
function GoToResultsMenu() {
	document.process_form.results_type.value = "MENU";
	document.process_form.submit();
}

function GoToNextPage(ResultsType, WeekNbr) {
	document.process_form.results_type.value = ResultsType;
	document.process_form.start_row_nbr.value = document.process_form.page_forward_start_row_nbr.value;
	document.process_form.week_nbr.value = WeekNbr;
	document.process_form.submit();
}

function GoToPrevPage(ResultsType, WeekNbr) {
	document.process_form.results_type.value = ResultsType;
	document.process_form.start_row_nbr.value = document.process_form.page_back_start_row_nbr.value;
	document.process_form.week_nbr.value = WeekNbr;
	document.process_form.submit();
}

function trim(str) { 
	//Remove leading spaces...
	while (str.substring(0,1) == ' ') {
		str = str.substring(1, str.length);
	}

	//Remove trailing spaces...
	while (str.substring(str.length-1, str.length) == ' ') {
		str = str.substring(0, str.length-1);
	}

    return str;
}

function trimAll (thisForm) {
	var tempStr;

	//loop through all "TEXT" & "TEXTAREA" elements on the form...
	for (var i = 0; i < thisForm.length; i++) {
		var thisField = thisForm[i];
		if (thisField.type == "text" || thisField.type == "textarea") {
			tempStr = thisField.value;
			//remove leading and trailing spaces...
			thisField.value = trim(tempStr);
		}
	}
}

function checkNum (thisField, errMsg) {
	thisField.value = trim(thisField.value);

	if (!isValidInt(thisField.value)) {
		alert(errMsg);
		thisField.focus();
		return false;
	}
	
	return true;
}

function isValidInt(num) {   
 	var valid = "0123456789";
	var temp;
	for (var i = 0; i < num.length; i++) {
		temp = num.substring(i, i + 1);
		if (valid.indexOf(temp) == "-1") {
			return false;
		}
	}
	return true;	  
}

function escapeStr (str) {
	var singleQuote = "'";
	str = str.replace(/"/g, singleQuote) ;
	str = str.replace(/'/g, "''") ;
	
	return str;
}

function GoToPage(TotalNoOfPages, NumOfRows, ResultsType, WeekNbr) {
	GoToPageNbr = document.process_form.GoToPageNbr.value;

	if (GoToPageNbr == "") {
		alert("Go To page number required.");
		return;
	}
	else {
		GoToPageNbr = parseInt(document.process_form.GoToPageNbr.value, 10);
	}

	if (GoToPageNbr == 0) {
		alert("Go To page number should be be greater than 0.");
		return;
	}
	
	if (GoToPageNbr > parseInt(TotalNoOfPages, 10)) {
		alert("Go To page number cannot be greater than " + TotalNoOfPages + ".");
		return;
	}

	GoToPageNbr--;
	document.process_form.results_type.value = ResultsType;
	document.process_form.start_row_nbr.value = (parseInt(NumOfRows, 10) * GoToPageNbr) + 1;
	document.process_form.week_nbr.value = WeekNbr;
	document.process_form.submit();
	
}

function ucase (this_field) {
	this_field.value = this_field.value.toUpperCase();
}

function escapeAll (thisForm) {
	var tempStr;
	var singleQuote = "'";

	//loop through all "TEXT" & "TEXTAREA" elements on the form...
	for (var i = 0; i < thisForm.length; i++) {
		var thisField = thisForm[i];
		if (thisField.type == "text" || thisField.type == "textarea") {
			tempStr = thisField.value;
			//escape single quotes...
			thisField.value = tempStr.replace(/"/g, singleQuote) ;
			thisField.value = tempStr.replace(/'/g, "''") ;
		}
	}
}

function ProcessCRChg() {
	document.menu_form.classroom_id_from_list.value = document.top_form.classroom_id_from_list.value;
	document.menu_form.nextpage.value = document.process_form.nextpage.value;
	if (document.menu_form.classroom_id_from_list.value == "") {
		document.menu_form.nextpage.value = "HOME";
	}
	document.menu_form.submit();
}

 


function ProcessMenuRequest (GoToPage, userLevel) {
	if (GoToPage == "REPORTS" ||   GoToPage == "USERADMIN") {
			document.menu_form.nextpage.value = GoToPage;
			document.menu_form.submit();
	}
	else if (GoToPage == "LOGOUT") {
		if (window.confirm("Are you sure you want to logout?")) {
			document.menu_form.nextpage.value = GoToPage;
			document.menu_form.submit();
		}	
	}
	else {
		if (GoToPage == "HOME") {
			if (userLevel != "CLASSROOM") {
				document.menu_form.classroom_id_from_list.value = document.top_form.classroom_id_from_list.value;
			}
			document.menu_form.nextpage.value = GoToPage;
			document.menu_form.submit();
		}
		else {
		   	if (userLevel != "CLASSROOM") {
				
				
				if ( document.top_form.classroom_id_from_list.value == "") {
					alert("Need to select a classroom.");
					document.top_form.classroom_id_from_list.focus();
				}
				else {
					document.menu_form.classroom_id_from_list.value = document.top_form.classroom_id_from_list.value;
					document.menu_form.nextpage.value = GoToPage;
					document.menu_form.submit();
				}
			} else {
				document.menu_form.nextpage.value = GoToPage;
				document.menu_form.submit();
			}
		}
	}
}

function UnescapeAll (thisForm) {
	var tempStr;
	var singleQuote = "'";

	//loop through all "TEXT" & "TEXTAREA" elements on the form...
	for (var i = 0; i < thisForm.length; i++) {
		var thisField = thisForm[i];
		if (thisField.type == "text" || thisField.type == "textarea") {
			tempStr = thisField.value;
			//Unescape single quotes...
			thisField.value = tempStr.replace(/''/g, singleQuote) ;
		}
	}
}

function GetRadioButtonValue (thisField) {
	var selIndex = GetRadioSelectedIndex(thisField);
	if (selIndex == "-1") {
		return "";
	}
	else {
		return thisField[selIndex].value;
	}

}

function GetRadioSelectedIndex (thisField) {
	for (var i = 0; i < thisField.length; i++) { 
		if (thisField[i].checked) { 
			return i;
		}
	} 

	return "-1";
}

/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit(theDigit) { 
	var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

	for (j = 0; j < digitArray.length; j++) {
		if (theDigit == digitArray[j]) 
			return true 
	} 

	return false 
} 

/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) { 
	var theData = new String(theString) 
	if (!isDigit(theData.charAt(0))) 
		if (!(theData.charAt(0)== '+')) 
			return false 

	for (var i = 1; i < theData.length; i++) 
		if (!isDigit(theData.charAt(i))) 
			return false 

	return true 
} 
/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) {
	var a1=s.split("/"); 
	var a2=s.split("-"); 
	var e=true; 
	if ((a1.length!=3) && (a2.length!=3)) { 
		e=false; 
	} 
	else {
		if (a1.length==3) 
			var na=a1; 
		if (a2.length==3) 
			var na=a2; 
		if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) { 
			if (f==1) {
				var d=na[1],m=na[0]; 
			} 
			else {
				var d=na[0],m=na[1]; 
			} 

		var y=na[2]; 

		if (((e) && (y<1000)||y.length>4)) 
			e=false 
		if (e) { 
			v=new Date(m+"/"+d+"/"+y); 
			if (v.getMonth()!=m-1) 
				e=false; 
			} 
		} 
		else { 
			e=false; 
		} 
	} 

	return e 
} 

function checkDate(v) { 
	var s=trim(v.value); 
	v.value = s;

	if (v.value.length > 0) {
		if (!isDate(s,1)) { 
			alert("Invalid date format. Should be in 'mm/dd/yyyy' format."); 
			v.focus();
			return false; 
		}
	}

	return true;
} 

function RemoveMutliSpaces (str) {
	var i;
	var SingleSpace = " ";
	var DoubleSpace = "  ";

	i = str.indexOf(DoubleSpace);

	while (i != -1) {
		str = str.replace(/  /g, SingleSpace);
		i = str.indexOf(DoubleSpace);
	}

	return str;
}

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++) {   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;

}

function reformat (s) {
	var arg;
	var sPos = 0;
	var resultString = "";

	for (var i = 1; i < reformat.arguments.length; i++) {
		arg = reformat.arguments[i];
		if (i % 2 == 1) resultString += arg;
		else {
			resultString += s.substring(sPos, sPos + arg);
			sPos += arg;
		}
	}

	return resultString;
}

function reformatPhone (PhoneNbr) {
	var tempStr = trim(PhoneNbr);
	tempStr = stripCharsInBag (tempStr, "()-. ");
	if (tempStr != "") {
		tempStr = reformat (tempStr, "(", 3, ") ", 3, "-", 4);
	}
	
	return tempStr;
}


