//this file contains code to extract the value from a query string passed by a link and format it to get 
//a file name - we can load this file into the iFrame or if there is no querystring, the default file will be loaded

var myQueryString = location.search.substring(1);  //look for the query string
var pos = myQueryString.indexOf('=');
	
if (pos != -1) { //there is a querystring, so get the value from it
		var argname = myQueryString.substring(0,pos);
		var myValue = myQueryString.substring(pos+1);
		fileToLoad = myValue + ".html";  //format the value to get a file name
}


//this file also contains functions to get and set cookies which are used for the individual course registration buttons

//this function writes transient cookies - this is called from the getCourseData function
function setCookie(name, value) {
         //If name is the empty string, it places a ; at the beginning
         //of document.cookie, causing clearCookies() to malfunction.
         if(name != '') {
            document.cookie = name + '=' + value;
         }
}
		 
//this function reads in values from cookies previously written to user computer
//need this fx to test for multiple course registrations
function getCookie(name)
         {
         //Without this, it will return the first value 
         //in document.cookie when name is the empty string.
         if(name == '')
            return('');
         
         name_index = document.cookie.indexOf(name + '=');
         
         if(name_index == -1)
            return('');
         
         cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }
			  
		 //note that we are unescaping all the cookie values, since we originally escaped them when writing the cookie
		 cookie_value = unescape(cookie_value);
		 
         return(cookie_value);
         }



// some regular expressions needed by the next functn
reStart1 = /\d+\/\d+/;
reStart2 = /\/\d\d\d\d/;
// some variables used in alertz in the next functn
tooLateMssg = "We're sorry, but it is too late to register for the\nsession of this course which begins on ";
chooseAnother = "\n\nPlease choose a different session of this course.";


// this function gets the course data when one of the course buttons on this page is clicked and writes it to a transient cookie before
// redirecting the user to the registration form where the cookie data is read and used to prefill the registration form

function getCourseData(myForm) {
	
	//first, make sure the start date of the course has not already passed
	//but this only applies to courses with dates, not to online/anytime courses
		myStartDate = myForm.courseDates.value;
		
		//do the next few steps only for courses with dates, not for online/anytime
			if(myForm.courseDates.value != "Online" && myForm.courseDates.value != "Anytime" && myForm.courseDates.value != "Online Anytime") {
			
			//this is NOT an online course, so check the dates
			startDate1 = myStartDate.match(reStart1); 
			startDate2 = myStartDate.match(reStart2);		
		
			myStartDate = startDate1 + startDate2;
		
			d = new Date(myStartDate);
		
			a = new Date();
		
			if (d.getTime() < a.getTime()) {
				//the start date of the course has passed, so alert the user
				alert(tooLateMssg + myStartDate + "." + chooseAnother);
				//we do not wish to record this registration, so return to page without executg remainder of functn
			return false;
			}
		
		}
		
        //if we have gotten this far, then the start date of the course is OK and we can continue
		//need to concatenate the course data from the form fields for the button which was clicked
		//escape all the form data values in case of problematical punctuation marks or symbols and 
		//use a question mark as a delimiter so we can store multiple form field values in one cookie
		
		courseData = escape(myForm.courseDates.value) + "?" + escape(myForm.courseTitle.value) + "?" + escape(myForm.courseTuition.value) + "?" + escape(myForm.courseCRN.value);

		 courseReg1 = getCookie('courseReg1');
		 courseReg2 = getCookie('courseReg2');
		 courseReg3 = getCookie('courseReg3');
		 courseReg4 = getCookie('courseReg4');
		 courseReg5 = getCookie('courseReg5');
		 courseReg6 = getCookie('courseReg6');
		 courseReg7 = getCookie('courseReg7');
		 courseReg8 = getCookie('courseReg8');
		 
		 if (courseReg1 == "") setCookie('courseReg1',courseData);			 		 
		 if (courseReg1 != "" && courseReg2 == "") setCookie('courseReg2',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 == "") setCookie('courseReg3',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 == "") setCookie('courseReg4',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 != "" && courseReg5 == "") setCookie('courseReg5',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 != "" && courseReg5 != "" && courseReg6 == "") setCookie('courseReg6',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 != "" && courseReg5 != "" && courseReg6 != "" && courseReg7 == "") setCookie('courseReg7',courseData);
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 != "" && courseReg5 != "" && courseReg6 != "" && courseReg7 != "" && courseReg8 == "") setCookie('courseReg8',courseData);
				 
		 //if the user has already selected eight courses, then alert user to that fact - can not register for more than eight at one time
		 if (courseReg1 != "" && courseReg2 != "" && courseReg3 != "" && courseReg4 != "" && courseReg5 != "" && courseReg6 != "" && courseReg7 != "" && courseReg8 != "") {
		 
		 		var sorryMssg = "You have already selected eight courses or programs.\nYou may not register for more than\neight courses or programs on one registration form.";
				alert(sorryMssg);
		}
return true;
}

