var currLocation = '';
var regMatchPattern = /\d/;
var timeoutID;
var currObjChckBx;
function getVideoListing(oLink, oVideoCat, oVideoLocation) {
	clearCartNotifyMessage();
	if (regMatchPattern.test(oVideoCat) && oVideoCat > 0) {
		displayCatHdr(oLink);
		displayNotifyMsg('Retrieving Video List...');
		currLocation = oVideoLocation;
		var poststr = "c="+escape(oVideoCat)+"&l="+oVideoLocation;
		makePOSTVideo(poststr,'list');
	} else {
		displayNotifyMsg('No information was found for this course.');
	}
}

function makePOSTVideo(parameters, strResponse) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		displayCourseSchedErrorMsg ('Sorry your browser does not support ajax and must update your browser in order to use this system.');
		return false;
	}
	
	if (strResponse == 'list') {
		http_request.onreadystatechange = VideoListingresponse;
		http_request.open('POST', 'scripts/getVideoListing.php', true);
	} else if (strResponse == 'bag') {
		http_request.onreadystatechange = addVideoToBagresponse;
		http_request.open('POST', 'scripts/buildVideoBag.php', true);
	}
	
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function VideoListingresponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			displayNotifyMsg('Initializing Video List...');
			var result = http_request.responseText;
			if (result.indexOf('error') == 0) {
				var errmsg = result.split(":::");
				displayNotifyMsg(errmsg[1]);
			} else {
				displayVideoList(eval('(' + result + ')'));
			}
		} else {
			displayNotifyMsg('There was an internal problem with processing your information.');
		}
	}
}

function displayVideoList(vidList) {
	var msg = '';
	
	msg += '<form name=\"frmVideoList\">'
	+ '<table id="tblVideoListing">'
	+ '<tr>'
	+ '<td></td>'
	+ '<td>Title</td>'
	+ '<td>Num</td>'
	+ '<td>Format</td>'
	+ '<td>Length</td>'
	+ '</tr>';
	
	for (var i = 0; i < vidList.length; i++) {
		msg += '<tr>' 
		+ '<td class="videoListCntr">' 
		+ '<input name="chckbxVideoID" type="checkbox" value="'+vidList[i].vidID+'" onclick="addVideoToBag(this);" ';
		if (vidList[i].vidBlnChecked) msg += 'checked="checked"';
		msg += '/>' 
		+ '</td>' 
		+ '<td id="title_'+vidList[i].vidID+'">'+vidList[i].vidTitle+'</td>' 
		+ '<td id="vidNum_'+vidList[i].vidID+'" class="videoListCntr">'+vidList[i].vidNum+'</td>' 
		+ '<td>'+vidList[i].vidVideoDVD+'</td>' 
		+ '<td id="vidLenght_'+vidList[i].vidID+'">'+vidList[i].vidLenght+'</td>' 
		+ '</tr>';
	}
	
	msg += '</table>'
	+ '<input name="l" id="vidlocation" type="hidden" value="'+currLocation+'">'
	+ '</form>';
	
	document.getElementById('videolistingBucket').innerHTML = msg;
}

function addVideoToBag(objChckbx) {
	clearCartNotifyMessage();
	var intitializemsg = (objChckbx.checked) ? 'Adding the selected video to your list...' : 'Removing the selected video from your list...' ;
	
	displayCartNotifyMsg(intitializemsg);
	//alert(objChckbx.value+"--"+objChckbx.checked);
	//objChckbx.checked = false;
	currObjChckBx = objChckbx;
	var iVid = objChckbx.value;
	var blnVidChecked = objChckbx.checked;
	var strVidLocation = document.getElementById('vidlocation').value;
	
	if (regMatchPattern.test(iVid) && iVid > 0) {
		var poststr = "v="+escape(iVid)+"&l="+strVidLocation+"&bchckd="+blnVidChecked;
		makePOSTVideo(poststr, 'bag');
	} else {
		displayNotifyMsg('No information was found for this video.');
	}
}

function addVideoToBagresponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var result = http_request.responseText;
			var currVideoCt = 0;
			if (result.indexOf('error') == 0) {
				var errmsg = result.split(":::");
				displayCartNotifyMsg(errmsg[1]);
				currVideoCt = errmsg[2];
			} else if (result.indexOf('limit') == 0) {
				currObjChckBx.checked = false;
				var limitmsg = result.split(":::");
				displayCartNotifyMsg(limitmsg[1]);
				currVideoCt = limitmsg[2];
			} else {
				var cartnotifymsg = result.split(":::");
				//alert(cartnotifymsg[1]);
				displayCartNotifyMsg(cartnotifymsg[1]);
				currVideoCt = cartnotifymsg[2];
			}
			timeoutID = setTimeout(clearCartNotifyMessage, 3000);
		} else {
			displayNotifyMsg('There was an internal problem with processing your information.');
		}
		displayVideoCounter(currVideoCt);
	}
}

function displayNotifyMsg(strmsg) {
	document.getElementById('videolistingBucket').innerHTML = '<div class="error_left">'+strmsg+'</div>';
}

function displayCatHdr(oHdrLink) {
	document.getElementById('videocatHdr').innerHTML = oHdrLink.innerHTML;
}

function displayCartNotifyMsg(oNotifyMsg) {
	//alert(oNotifyMsg);
	document.getElementById('cartNotify').innerHTML = oNotifyMsg;
}

function displayVideoCounter(iCt) {
	if (iCt > 0) document.getElementById('videoCartCounter').style.display = '';
	document.getElementById('videoCount').innerHTML = iCt;
	if (iCt == 0) document.getElementById('videoCartCounter').style.display = 'none';
}

function clearCartNotifyMessage() {
	clearTimeout(timeoutID);
	document.getElementById('cartNotify').innerHTML =  '';
}

















