function checkout(form) {

// are there any sections at all? //

	if (form.section) {

	// how many sections are there? //

	no_of_sections=form.section.length;

	// if there is only 1, we have to do things differently //
	
	if (no_of_sections==null) {

		one_section(form)

		} else {

		more_than_one_section (form)

		}

	//edited by j.tubbs on 8.1.2007
	} else {
		
		alert("There was no selected course to be added. It is possible that there are no offerings for the course at this time.");
}



	function one_section(frmObj) {



		if (frmObj.section.checked) {

		var info=frmObj.section.value;

		make_cookie(info, frmObj);}

		else 	{

			alert ("You have forgotten to select a class. Please try again!")

		}

	}



	function more_than_one_section(frmObj) {


		no_of_sections=frmObj.section.length;

		for (var i=0; i < no_of_sections; i++) {

			if (frmObj.section[i].checked) {

				break

			}

		}



		// if we've gone through the sections and none are checked, let them know //



		if (i == no_of_sections) {

		alert ("You have forgotten to select a class. Please try again!")

		} else {

		var info=frmObj.section[i].value;

		make_cookie(info, frmObj)



	}



	}





}





function make_cookie(thedata, frmObj) {


	var other=thedata;


	var other1=other.split("*");
	var other2=other1[1].split("#");

	var coursesection=other1[0];

	var coursestartdate=other2[0];

	var semester=other2[1];

	//var coursename=frmObj.productName.value;
	//** updated to support '+' in course names -- ascii = &#43; = +
	var coursename=replaceStr2(frmObj.productName.value, '+', '&#43;');

	var coursenumber=frmObj.courseNo.value;

	var coursefee=frmObj.fee.value;

	var trn=frmObj.trn.value;





	var cookie1="coursenumber*"+ coursenumber + "+coursesection*" + coursesection + "+coursestartdate*" + coursestartdate + "+coursefee*" + coursefee + "+coursename*" + coursename + "+semester*" + semester + "+trn*" + trn; 



	var othercourses=Does_Cookie_Exist("ceregistration");

	var isDuplicated = checkDuplication(cookie1, othercourses);

    if (isDuplicated ) {
        alert("You have already chosen section : " + coursenumber + " "+coursesection + " ." );
        return false;
    }
    
    
	if (othercourses=="") {

		var newcookie=1;

		} else {

			var newcookie=0;

			var the_current_cookie=othercourses;

		}

	

	if (newcookie==1) {

			var the_cookie="ceregistration="+escape(cookie1);

			} else 	{

					var the_cookie="ceregistration="+ the_current_cookie + "^" + escape(cookie1);

					}







	
	
	// for TEST SYSTEM
	document.cookie=the_cookie+";path=/;domain=humber.ca;";
	//window.location="/LIS/WebCalendar/CE/pages/yourlist.htm";
	window.location="/LIS/WebCalendar/CE/YourList.do";
	
	// for PROD
	//document.cookie=the_cookie+";path=/;domain=humber.ca;domain=humber.ca;";
	//window.location="http://cecalendar.humber.ca/scripts/yourlist.htm";



}

function replaceStr2(val, rep, repWith){
	var s = new String(val);
	s.replace(rep, repWith);
	return s;
}

function checkDuplication (selectedCourse, existedCourses){
     if (existedCourses.length == 0) {
         return false;
     }    
     else {
     var course_cookies = existedCourses.split("^");

     for (i=0; i< course_cookies.length ; i++){
           var courseCookie =  unescape (course_cookies[i]);
           if (selectedCourse == courseCookie )   
                return true;     
     }
     return false;
     } 

}



function Does_Cookie_Exist(label) {

	var labelLen = label.length

	var cLen = document.cookie.length

	var x = 0

	var cEnd

	while (x < cLen) {

		var y=x + labelLen

		if (document.cookie.substring(x,y) == label) {

			cEnd = document.cookie.indexOf(";",y)

			if (cEnd == -1) {

				cEnd = document.cookie.length

			}

			return document.cookie.substring(y+1,cEnd)

		}

		x++

	}

	return ""

}


