// JavaScript Document
// JavaScript Document
var Cart = new function() {
	var cart = this;
	this.xmlhttp=false;
	this.getXmlHttpRequest = function(){
		if (window.XMLHttpRequest){
			this.xmlhttp = new XMLHttpRequest();
			this.xmlhttp.overrideMimeType('text/xml');
		}else{
			try{
				this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e){
				try{
					this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					this.xmlhttp = false;
				}
			}
		}
	}
	this.getXmlHttpRequest();

	this.addCart = function(aca_type, season, aca_idx){
		if (this.xmlhttp){
			var url = "/apply/cart.php";
			var params = "mode=add&aca_type="+aca_type+"&season="+season+"&aca_idx="+aca_idx;
			this.xmlhttp.open ("POST", url , true); 
			this.xmlhttp.onreadystatechange = function() {
				if (cart.xmlhttp.readyState == 4 && cart.xmlhttp.status == 200) {
					switch(cart.xmlhttp.responseText){
						case 'login':
							alert("[관심있는 강좌] 담기를 하시려면 로그인 하셔야 합니다.");
							break;
						case 'info':
							alert("error: information");
							break;
						case 'double':
							alert("이미 [관심있는 강좌]로 등록된 강좌입니다.");
							break;
						case 'apply':
							alert("이미 수강신청이 되어있는 강좌입니다.");
							break;
						case 'ok':
							alert("[관심있는 강좌]로 등록되었습니다.\n[관심있는 강좌] 확인은 [마이페이지]에서 하실 수 있습니다.");
							break;
						default:
						alert("error: db");
					}
				}
			};
			this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xmlhttp.setRequestHeader("Content-length", params.length);
			this.xmlhttp.setRequestHeader("Connection", "close");
			this.xmlhttp.send(params);
		}
	}
	
	this.removeCart = function(idx){
		var con = confirm('[관심있는 강좌]에서 삭제하시겠습니까?');
		if(!con) return;
		
		if (this.xmlhttp){
			var url = "/apply/cart.php";
			var params = "mode=remove&idx="+idx;
			this.xmlhttp.open ("POST", url , true); 
			this.xmlhttp.onreadystatechange = function() {
				if (cart.xmlhttp.readyState == 4 && cart.xmlhttp.status == 200) {
					switch(cart.xmlhttp.responseText){
						case 'login':
							alert("로그인이 필요한 기능입니다.");
							break;
						case 'info':
							alert("error: information");
							break;
						case 'ok':
							//alert("[관심있는 강좌]에서 삭제되었습니다.");
							window.location.reload();
							break;
						default:
						alert("error: db");
					}
				}
			};
			this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xmlhttp.setRequestHeader("Content-length", params.length);
			this.xmlhttp.setRequestHeader("Connection", "close");
			this.xmlhttp.send(params);
		}
	}
}