
function getFileFromURL(url) {		// Parses a URL and returns the name of the page without the full path

	url               = url.toString();
	var urlPath       = url.split("?");			// Drop any query portion to get the root URL
	var urlRoot       = urlPath[0];
	var urlSegments   = urlRoot.split("/");
	var lastSegment   = urlSegments.length - 1;
	var file          = urlSegments[lastSegment];
	
	if (!file) {
		file = 'index.html';	
	}	
	return file;
}

function escapeRegex(inputString) {			// Prevents invalid quantifier errors when searching for special characters in Regex
	
	return inputString.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}


function getQueryFromURL(url) {		// Parses a URL and returns the queryString from its path

	url            = url.toString();
	var searchChar = new RegExp( escapeRegex("?") );

	var queryStart = url.search(searchChar);	// Does the redirect path include a query?
		
	if ( queryStart > -1 ) {						// 	Yes: extract query string and append to the default queryURL
		queryString = url.substr(queryStart + 1);
	} else {
		queryString = '';							// No:   no additional query parameters
	}
	return queryString;
}

function checkAnchors() {		// Called by the onload event of an iFrame: declares its own ID and requests a check
								//	If top URL includes a queryString "anchor=" the frame will scroll to that position
	var anchorID = top.parseURL('anchor');
	
	if (anchorID) {
	    var anchorPos = getObjectByID(anchorID).offsetTop;
		
		var speed = 70000;
		var interval = parseInt( anchorPos / 25 );
		
		for (s = 0; s<=anchorPos; s+=interval) {
			self.scrollTo(0, s);
			
			for (pause = 0; pause<speed; pause++) {
					// NOP
			}
			
			speed = speed * 0.95;
		}
		
		self.scrollTo(0, anchorPos);
		
			// Handle special case: open a programDiv at majorsDegreesCourses.html if anchor is a "programAnchor"
		
		var searchString = new RegExp( "programAnchor" );
		if ( anchorID.search(searchString) > -1 ) {
			
			var anchorNameSegments = anchorID.split('Anchor');
			var anchorIDNumber     = anchorNameSegments[1];
			
			openProgram(anchorIDNumber );	
		}
	}
}


function checkParent(makeCurrent, defaultAction, param1, param2) {	// Prevent this document from loading outside the parent frame.

												// makeCurrent:   if set, request this page when opening the parent frame
												//				  (otherwise use defaultPage after reloading the parent)
												// defaultAction: perform a custom command only if the parent is OK
												// param1, param2: optional parameters for defaultAction
	var defaultPage   = 'whatIsSVShighSchoolSpanish.html';
	
	var thisURL       = self.location;					// What page is currently open?
	var thisPage      = getFileFromURL(thisURL);		// Extract the currentPage from the full URL
	var appendQuery   = getQueryFromURL(thisURL);		// Extract the queryString from the current URL (if any)
	var requestedPage = parseURL('page');				// Was a page requested?
	
	var folder = parseURL('folder');		// Is this a student page with a folder request? If so, include folder in thisPage
	
	if (folder) {
		thisPage += "?folder=" + folder;
	}

		// Has this page loaded outside the frameset?

	if (thisURL == top.location) {					// Yes: redirect to "index.html"

		// Perform defaultAction for parent frame violations (if any)

		

		// Should this page be stored as svsCurrentPage (cookie) before redirecting, or should we bounce directly to defaultPage?

		if (makeCurrent) {							// Store thisPage in cookie (svsCurrentPage)... then redirect to "index.html"

			// var cookieCallback = 'resetURL';							// Callback that redirects to index.html
			var cookieCallback = '';
			var cookiePassVals = '';
			top.storePage(thisPage, cookieCallback, cookiePassVals);
			
			setTimeout("top.location.href='index.html'; ", 1500);

		} else {		// makeCurrent is false: simply branch to defaultPage. (svsCurrentPage will load instead of thisPage)
		
			top.location.href = defaultPage;							// Redirect to primary page
		}

	} else {	// Not loaded outside the frame: reveal the page (default is diaplay:none) and set the menu button

		if (makeCurrent) {		// Highlight the appropriate menu button
			
				// Prototype: setButton(btnClass, btnClass_h, reqPage);
				
			if ( thisPage.search(/^partner/) > -1 ) {
				top.getObjectByID('sidebarFrame').contentWindow.setButton(thisPage);

			} else if ( thisPage.search(/^student/) > -1 ){		// Include queryString for student pages (for folder requests)
				top.getObjectByID('sidebarFrame').contentWindow.setButton(thisPage);
				
			} else {
				top.setButton(thisPage);
			}
			
			top.storePage(thisPage, '', '');										// Store thisPage to cookie (svsCurrentPage)
		}

		document.body.style.display = '';				// Show the body of the page if it was hidden

			// Are there any requests to scroll to a named anchor?
		
		if (param2 != 'ignoreAnchors') {
			setTimeout("checkAnchors('showCourses');", 500);
		}
		
			// Perform defaultAction after establishing OK parent (if any requested)

		
	}
}

function notUndef(inputVar) {		// Changes 'undefined' variables to empty values or returns any other value passed in

	if(typeof(inputVar)=="undefined"){ 
		return '';
	} else {
		return inputVar;
	}
}

function storePage(currentPage, cookieCallback, cookiePassVals) {		// Writes the currentPage to a cookie for later reference

		// * If cookieCallback = 'top.resetURL', the page will redirect to the parent frame after storing the cookie

	var cookieNames    = 'svsCurrentPage';
	var cookieValues   = currentPage;
	var cookieDur      = 12 * 60 * 60 * 1000;		// Remember this page for half a day
		
	if (currentPage) {
		phpCookie(cookieNames, cookieValues, cookieCallback, cookieDur, cookiePassVals);
	}
}

function phpCookie(cookieNames, cookieValues, cookieCallback, cookieDur, passVals) {	// Write, read, or delete a cookie via php

										// Multiple names and values can be passed via strings separated by commas	
										// cookieDur:      the duration of the cookie in ms. Use '-1' to delete

										// cookieCallback: optional name of a javascript function to run after reading a cookie
										//	...callback is constructed by passing the fetched value along with passVals
	if (!cookieValues) {
		cookieValues = '';
	}

	if (!cookieDur) {
		cookieDur = '';
	}

	if (!cookieCallback) {
		cookieCallback = '';
	}

	if(!passVals) {
		passVals = '';
	}

	var url          = "index-phpCookie.php";
	var targetID     = '';
	var callbackFunc = '';
	var oData        = {
		cookieNames: cookieNames,
		cookieValues: cookieValues,
		cookieCallback: cookieCallback,
		cookieExpire: cookieDur, 
		cookiePassVals: passVals
	};
	var syncReqFlag  = '';
	sendData(url, targetID, callbackFunc, oData, syncReqFlag);
}
