//***********************************************
// validatepagename.js
// created  01.08.10-jf
// modified 03.29.10-jf
//***********************************************
//
// code to pull possible page name from the calling URL, and, if we can, validate it against a valid list, and then pass it on
//
// Contains:
// function code [ validatepagename() ] and vars to test for passed valid page name to which we can switch
// function code [ debugvalidatepagename() ] and vars to debug ValidatePageName() to screen
// function code [ triggervalidatepagename() ] is exposed and is called to test, debug, and provide valid contents page name
// 
// vars are set by caller and triggervalidatepagename() is called set a valid contents page name for this frame set
//


// vars to control debug-output-to-screen-mode of various jf code
// Note: now in global vars in page head
//***********************************************
// - [set by caller in head] - var jf_dbugmode = false; 	// write output to screen for debugging?
// - [set by caller in head] - var jf_allow_dbug = true; 	// master switch to force off debug mode easily


// vars declared in HEAD of calling routine used by ValidatePageName()
// Note: now in global vars in page head
//***********************************************
// var holding default "homepage" name for this frame set
// - [set by caller in head] - var vpn_dflt_pg = "homepage6.htm"; 	// assign desired default page name
//
// var holding calculated "homepage" name for this frame set - this is set via the function call against a list of valid choices
// - [set by caller in head] - var vpn_linkURL = vpn_dflt_pg; 	// set as default -- ultimately passed as contents pagename
//
// setup string of valid possible page names, as lowercase, 
// wrapped and separated by asterisks - note syntax of line break inside quotes
// - [set by caller in head] - var validPgName = "*overview6.htm*features6.htm*benefits6.htm*suite6.htm\
// 		*require6.htm*price6.htm*order6.htm*tstmonal6.htm*tip6.htm*about6.htm\
// 		*index6.htm*homepage6.htm*";
//
// add optional pages for testing...careful with "*"s
// validPgName = validPgName + "events6.htm*thanku6.htm*"


// vars declared here and used locally in this file ...
// Note: can declare var in head before calling triggervalidatepagename() and capture result if needed elsewhere!
//***********************************************
// setup vars needed, used, and set by validatepagename - 01.08.10-jf
// validatepagename will pull possible page name from calling URL and if it validates, pass it on
// otherwise it passes on the default page name specified here.
// these vars may be edited to control action
// these vars declared and init in .head. before call to validatepagename, and active while page is active

// declare & init rest of vars needed internally by the actual function code
var vpn_callingURL = ""; 		// URL from caller of this page
var vpn_decodedURL = ""; 		// calling URL after decoding
var vpn_testURL = ""; 		// stripped param string following '?' in caller's URL
var vpn_state = "[ValidatePageName code did not run]";	// string showing current status of work - for reporting only
var vpn_state2 = "(empty)";		// additional work status info - for reporting only
var vpn_dbugstate = "(Debug off - vpn)"; 		// on/off/user

var vpn_rtrnvalue = ""; 		// setup var to hold function's return value



//***********************************************
function validatepagename() {
// created  01.08.10-jf
// modified 02.01.10-jf
// code to pull and test for a valid frameset contents page name from the passed URL
//***********************************************

// set initial test state so know the code was at least called
vpn_state = "[defaulted]";
// see if this JavaScript-capable browser understands the URL object
// otherwise the default pagename will still reside in var vpn_linkURL
if (parent.document.URL) {
	// grab the calling URL
	vpn_callingURL = parent.document.URL;
	// force it to decode properly
	vpn_decodedURL = decodeURIComponent(vpn_callingURL);
	// see if caller passed us URL with a question mark in it
	// otherwise vpn_testURL remains empty string
	if (vpn_decodedURL.indexOf('?') != -1) {
		// there is a ? so grab substring immediately following ?
		vpn_testURL = vpn_decodedURL.substring(vpn_decodedURL.indexOf('?')+1,vpn_decodedURL.length);
		// force it to lowercase
		vpn_testURL = vpn_testURL.toLowerCase();
	} // calling URL has a ?

	// set debug info for display
	if (jf_dbugmode) { vpn_dbugstate = "(Debug Set On - vpn)" }
	// see if caller wants to switch to debug mode
	if (vpn_testURL.indexOf('=jfdebug') != -1) {
		// there is a call for our debug token, so set debug mode on
		jf_dbugmode = true;
		// strip token from vpn_testURL before we go on
		vpn_testURL = vpn_testURL.replace("=jfdebug", "")
		// set info for display
		vpn_dbugstate = "(Debug on - User activated - vpn)"
	} // caller wants output in debug mode

	// see if they mistakenly added .html at the end and if so fix it
	if (vpn_testURL.indexOf('.html')  != -1) {
		vpn_state2 = "(had .html as ending)";
		vpn_testURL = vpn_testURL.replace(".html", ".htm")
	} // substitute .htm for .html
	// see if they mistakenly dropped .htm ending and if so append it
	if (vpn_testURL.indexOf('.htm')  == -1) {
		vpn_state2 = "(was missing .htm)";
		vpn_testURL = vpn_testURL + ".htm"
	} // append missing .htm
	// report syntax errors we will test for
	if (vpn_testURL.indexOf('*') != -1) { vpn_state2 = "(contains an *)" }
	if (vpn_testURL.indexOf(' ') != -1) { vpn_state2 = "(contains a space)" }
	if (vpn_testURL.length <= 4) { vpn_state2 = "(not long enough)" }
	// see if vpn_testURL string is long enough to be valid, i.e. "a.htm", and has no "*" or space chars
	// otherwise the default pagename will still reside in var vpn_linkURL
	if ((vpn_testURL.length > 4 ) && (vpn_testURL.indexOf('*') == -1) && (vpn_testURL.indexOf(' ') == -1)) {
		// see if vpn_testURL exactly matches somewhere in our string o' valid pagenames
		// otherwise the default pagename will still reside in var vpn_linkURL
		if (validPgName.indexOf('*'+vpn_testURL+'*') != -1) {
			// assign passed string pulled from vpn_callingURL as valid vpn_linkURL
			vpn_linkURL = vpn_testURL;
			vpn_state = "[ok: passed pagename validates]";
			if (vpn_testURL === vpn_dflt_pg) { 
				vpn_state = "[ok: passed pagename matches default]";
			} // passed URL exactly matches the default
		} // test string is in valid names list
	} // test string long enough & has no reserved characters

} else { 
	vpn_state = "[cannot do URL objects]"
} // browser does URL objects

return vpn_linkURL

} // end of validatepagename function
//***********************************************



//***********************************************
function debugvalidatepagename() {
// created  01.08.10-jf
// modified 03.29.10-jf
//***********************************************
// send validatepagename debug output to screen if desired...this script is not essential 01.10-jf

// vars declared and needed to format debug writing to screen
//***********************************************
var basefont = '<font face="Verdana, Arial" size="1">'
var greenfont = "<font color='green'>"
var bluefont = "<font color='blue'>"
var redfont = "<font color='red'>"
var varfont = "<font color='purple'>"
var endfont = "<\/font>"
var eol = "<br \/>"
var scoreline = "==============================="

if (jf_dbugmode && jf_allow_dbug) {
	document.writeln(basefont + "VARS and RESULTS for: validatepagename.js" + "&nbsp;&nbsp;&nbsp;" + greenfont + "<i>" + vpn_dbugstate + "<\/i>" + endfont + eol + scoreline + eol);
	document.writeln("Default vpn_linkURL: " + varfont + "vpn_dflt_pg" + endfont + " is: " + greenfont + vpn_dflt_pg + endfont + eol);
	document.writeln("Valid PageNames are: " + varfont + "validPgName" + endfont + " is: " + greenfont  + validPgName + endfont + eol);
	if (parent.document.URL) {
		document.writeln("Caller said: " + varfont + "vpn_callingURL" + endfont + " is: " + bluefont   + vpn_callingURL + endfont + eol);
		document.writeln("Which means: " + varfont + "vpn_decodedURL" + endfont + " is: " + bluefont   + vpn_decodedURL + endfont + eol);
		document.writeln("We test: " + varfont + "vpn_testURL" + endfont + " is: " + bluefont  + vpn_testURL + endfont + eol);
	} // can report these since browser does URL objects
	document.writeln("We pass on: " + varfont + "vpn_linkURL" + endfont + " is: " + greenfont  + vpn_linkURL + endfont + eol);

	document.writeln("Status: " + varfont + "vpn_state" + endfont + ", " + varfont + "<i>" + "vpn_state2" + "<\/i>" + endfont + " is: " + redfont  + vpn_state + "&nbsp;&nbsp;" + "<i>" + vpn_state2 + "<\/i>" + endfont + eol + endfont);

	document.writeln(basefont + "Return Value: " + varfont + "vpn_rtrnvalue" + endfont + " is: " + greenfont  + vpn_rtrnvalue + eol + eol + endfont);

} // jf_dbugmode AND jf_allow_dbug both true

} // end of debugvalidatepagename function
//***********************************************



//***********************************************
function triggervalidatepagename() {
// created  03.17.10-jf
// modified 03.28.10-jf
// code to trigger and test forcing desired top page frame 03.17.10-jf
//***********************************************

// required vars are setup and validatepagename function code is loaded
// so now we can call the validatepagename function

// fails gracefully by just using defaults if not called or script is missing
vpn_rtrnvalue = validatepagename()

// report to screen if debugging mode is active
debugvalidatepagename()

} // end of triggervalidatepagename function
//***********************************************


// fin


