//to watch for cart changes (add, remove, etc)
var cartChanged=false;

//course-list table formatting vars with default values
var cartTblBorder=1;
var cartTblWidth=515;
var cartTblLeftCellWidth=25;
var cartTblCourseCellWidth=460;
var cartTblCostCellWidth=30;
var cartTotalCost=0;
var currencySymbol="$";

function program(){
	this.title;
	this.ID;
	this.html;
	this.cost;
	this.secondListingSuffix=null;
}
details=new Array();

var newWindow;
function showIt(which){
	if (!newWindow || newWindow.closed) {
          newWindow = window.open('','','status,height=500,width=560,scrollbars,resizable')
          if (!newWindow.opener) {
                  newWindow.opener = window;
          }
	}else{newWindow.focus();}
	html="<html><head><title>Program Detail</title></head><body bgcolor=white><font fac"+details[which].html+"</body></html>";
// write HTML to new window document
	newWindow.document.open();
	newWindow.document.write(html);
	newWindow.document.close();	
	newWindow.location.reload();
}

function removeFromCart(which,suffix){
	if(!suffix){
		suffix=get_secondListingSuffix(which);
	}

	cartChanged=true;
	tmpCourses="";
	//if can't get cookie (some remote programs pages don't set a cookie for some reason),
	//then use the coursesForm data
	if(GetCookie("courses")){
		tmpCourses=GetCookie("courses");
	}
	if(!tmpCourses){
		tmpCourses=document.forms["coursesForm"].cart_courses.value;
	}
	if(tmpCourses){
	
		//tmpCourses=GetCookie("courses");
		if(tmpCourses.indexOf(which)==-1){return;}//can't remove what's not there
		if(tmpCourses.indexOf(",")>-1){
			tmp=tmpCourses.split(",");
			tmpCourses="";
			for(g=0;g<tmp.length;g++){
				if(tmp[g]!=which){
					tmpCourses+=tmp[g]+",";
				}
			}
			tmpCourses=tmpCourses.substring(0,tmpCourses.length-1)//trim trailing comma
		}else{
			DeleteCookie("courses");
			if(self.opener){
				if(self.opener.findDOM(which+"_removefromcartDIV",0)){//make sure it exists on the page
					self.opener.document.forms["coursesForm"].cart_courses.value="";
					if(self.opener.findDOM(which+"_removefromcartDIV",0)){
						self.opener.findDOM(which+"_removefromcartDIV",1).display='none';
						self.opener.findDOM(which+"_addtocartDIV",1).display='inline';
					}
					//second listing for same course (possible when a course is featured)
					if(self.opener.findDOM(which+"_removefromcartDIV"+suffix,0)){
						self.opener.findDOM(which+"_removefromcartDIV"+suffix,1).display='none';
						self.opener.findDOM(which+"_addtocartDIV"+suffix,1).display='inline';
					}
				}
				self.opener.location.reload();//make sure the form and cookie data loaded into main page are correct
			}else{
				document.forms["coursesForm"].cart_courses.value="";
				if(findDOM(which+"_removefromcartDIV",0)){
					findDOM(which+"_removefromcartDIV",1).display='none';
					findDOM(which+"_addtocartDIV",1).display='inline';
				}
				//second listing for same course (possible when a course is featured)
				if(findDOM(which+"_removefromcartDIV"+suffix,0)){
					findDOM(which+"_removefromcartDIV"+suffix,1).display='none';
					findDOM(which+"_addtocartDIV"+suffix,1).display='inline';
				}
			}
			return;
		}
		
	}else{
		return;
	}
	SetCookie("courses",tmpCourses);
	if(self.opener){//remove link clicked in pop-up
		if(self.opener.findDOM(which+"_removefromcartDIV",0)){//make sure it exists on the page
			self.opener.document.forms["coursesForm"].cart_courses.value=tmpCourses;
			self.opener.findDOM(which+"_removefromcartDIV",1).display='none';
			self.opener.findDOM(which+"_addtocartDIV",1).display='inline';
			//second listing for same course (possible when a course is featured)
			if(self.opener.findDOM(which+"_removefromcartDIV"+suffix,0)){
				self.opener.findDOM(which+"_removefromcartDIV"+suffix,1).display='none';
				self.opener.findDOM(which+"_addtocartDIV"+suffix,1).display='inline';
			}
		}
	}else{
		document.forms["coursesForm"].cart_courses.value=tmpCourses;
		findDOM(which+"_removefromcartDIV",1).display='none';
		findDOM(which+"_addtocartDIV",1).display='inline';
		
		//second listing for same course (possible when a course is featured)
		if(findDOM(which+"_removefromcartDIV"+suffix,0)){
			findDOM(which+"_removefromcartDIV"+suffix,1).display='none';
			findDOM(which+"_addtocartDIV"+suffix,1).display='inline';
		}
	}
}


function addToCart(which){
	if(GetCookie("courses")){
		tmpCourses=GetCookie("courses");
		if(tmpCourses.indexOf(details[which].ID)>-1){return;}//don't re-add it
		if(tmpCourses.indexOf("=")>-1){
			tmpCourses=tmpCourses.split("=");
			tmpCourses=tmpCourses[1];
		}
		tmpCourses=tmpCourses+","+details[which].ID;
	}else{
		tmpCourses=details[which].ID;
	}
	SetCookie("courses",tmpCourses);
	document.forms["coursesForm"].cart_courses.value=tmpCourses;
	
	//main entry
	if(findDOM(details[which].ID+"_addtocartDIV",0)){
		findDOM(details[which].ID+"_addtocartDIV",1).display='none';
		findDOM(details[which].ID+"_removefromcartDIV",1).display='';
	}
	//second listing for same course (possible when a course is featured)
	if(details[which].secondListingSuffix!=null || findDOM(details[which].ID+"_addtocartDIV"+details[which].secondListingSuffix,0)){
		findDOM(details[which].ID+"_addtocartDIV"+details[which].secondListingSuffix,1).display='none';
		findDOM(details[which].ID+"_removefromcartDIV"+details[which].secondListingSuffix,1).display='';
	}
	
}

function viewCart(){
	if(GetCookie("courses")){
		if (!newWindow || newWindow.closed) {
			newWindow = window.open(cartPage,'','status,height=300,width=550,scrollbars,resizable');
			if (!newWindow.opener) {
				newWindow.opener = window;
			}
		}else{
			newWindow.focus();
		}
	}else{
		alert("You have no courses in your cart");
	}
}

function payForCourses(isRegisterLink){
	if(isRegisterLink){
		if(GetCookie("courses") || document.forms["coursesForm"].cart_courses.value!=""){
			document.forms["coursesForm"].submit();
		}else{
			window.location=reg_page;
		}	
	}else{
		if((document.forms["coursesForm"].cart_courses.value==''||document.forms["coursesForm"].cart_courses.value==null||document.forms["coursesForm"].cart_courses.value=='null')&&!GetCookie("courses")){
			alert("You have no courses in your cart");
		}else{
			document.forms["coursesForm"].submit();
		}	
	}
}

if(window.onload){
	onloadTmp=window.onload
	window.onload=function(){
		onloadTmp();
		initCart();
	}
}else{
	window.onload=function(){
		initCart();
	}
}

function initCart(){
	if(document.forms["coursesForm"]){document.forms["coursesForm"].cart_courses.value=GetCookie("courses");}
	if(GetCookie("courses")){
	coursesArray=GetCookie("courses").split(",");
		for(g=0;g<coursesArray.length;g++){
			if(findDOM(coursesArray[g]+"_addtocartDIV",0)){
				findDOM(coursesArray[g]+"_addtocartDIV",1).display='none';
				findDOM(coursesArray[g]+"_removefromcartDIV",1).display='';	
			}
		}
		if(findDOM("listCoursesHere",0)){
			listCourses(coursesArray);
		}
	}else{
		if(self.opener&&findDOM("listCoursesHere",0)){//check for both, as the visitor could be coming from another site via a new window link
			findDOM("listCoursesHere",0).innerHTML="<font color='#000000'><center>You have no courses in your cart</center></font>";
			findDOM("putCartTotalCostHere",0).innerHTML="0.00";
			findDOM("payTD",0).innerHTML="<a href='javascript:self.close();self.opener.location.reload();'>close window</a>";
		}
	}
}
function listCourses(coursesArray){	
	details=opener.details;
	coursesHTML="<table border="+cartTblBorder+" width="+cartTblWidth+" cellpadding=4>";
	courseCostTotal=0;
	for(g=0;g<coursesArray.length;g++){
		for(gg=0;gg<details.length;gg++){
			if(details[gg].ID==coursesArray[g]&&details[gg]){
				coursesHTML=coursesHTML+"<tr><td class='program' valign=top width="+cartTblLeftCellWidth+"><a href='javascript:removeFromCart(\""+details[gg].ID+"\");location.reload();'>remove</a></td><td class='program' width="+cartTblCourseCellWidth+">"+details[gg].title+"</td><td class='program' width="+cartTblCostCellWidth+" valign=top>"+currencySymbol+details[gg].cost;
				courseCostTotal+=details[gg].cost;
				break;
			}
		}
	}
	coursesHTML+="</table>";
	findDOM("listCoursesHere",0).innerHTML=coursesHTML;
	findDOM("putCartTotalCostHere",0).innerHTML=courseCostTotal;
}

function getCartTotal(){
	coursesArray=GetCookie("courses").split(",")
	courseCostTotal=0;
	for(g=0;g<coursesArray.length;g++){
		for(gg=0;gg<details.length;gg++){
			if(details[gg].ID==coursesArray[g]&&details[gg]){
				courseCostTotal+=details[gg].cost;
				break;
			}
		}
	}
	return courseCostTotal;
}

function set_secondListingSuffix(whichID,currentSuffix){
	for(g=0;g<currentSuffix;g++){
		if(details[g].ID==whichID){
			details[g].secondListingSuffix=currentSuffix;
			break;
		}		
	}
}

function get_secondListingSuffix(whichID){
	for(g=0;g<details.length;g++){
		if(details[g].ID==whichID){
			return details[g].secondListingSuffix;
			break;
		}		
	}
}