/*
    Page Name : Head Hidden Cart
    Language : Javascript
    First Update : 2008. 10. 28(by ynmoon)
    Last Update : 2008. 10. 28(by ynmoon)
*/

// Cart List
cartList=function(objName)
{
	this.objName=objName;		// »ý¼ºµÈ °´Ã¼ ¸í(this)
	this.httpRequest = null;	// httpRequest°´Ã¼
	this.callback=null;		// XML°á°ú returnÇÔ¼ö
	this.docXML_FD;			// XMLµ¥ÀÌÅÍ return °ª
	
	this.countDiv;
	this.contentDiv;
	this.priceDiv;
	this.openCart;
	this.backDiv;
	this.openLink;
	this.backLink;
	this.openURL;
	this.closeURL;	
	this.marginDiv;
	this.delURL;
	
	this.openCart="close";
	
	this.objCart;              // Cart List Layer
	this.objMargin;          // Margin Layer
	this.m_height=0;       // Cart List Layer Margin
	this.t_height;             // Cart List Layer Top
	this.h_height;            // Cart List Layer Top(Hidden)
	this._margin=0;
	this._top;
	this._hidden;
	
	this.isFox=navigator.userAgent.toLowerCase().indexOf("firefox")>0;
	
	// httpRequest °´Ã¼ »ý¼º ÇÔ¼ö
	cartList.prototype.getXMLHttpRequest=function()
	{
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; }
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}
	}	//	end getXMLHttpRequest function
	
	// httpRequest send ÇÔ¼ö
	cartList.prototype.sendRequest=function(url,params,callback,method)
	{
		this.callback=callback;

		this.httpRequest=this.getXMLHttpRequest();
		var httpMethod=method ? method : 'GET';
		if (httpMethod!='GET' && httpMethod!='POST') {
			httpMethod='GET';
		}
		var httpParams=(params==null || params=='')?null:params;
		var httpUrl=url;
		if (httpMethod=='GET' && httpParams!=null) {
			httpUrl=httpUrl+"?"+httpParams;
		}
		this.httpRequest.open(httpMethod,httpUrl,true);
		this.httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var request=this;
		this.httpRequest.onreadystatechange=function()
		{
			request.onStateChange.call(request);
		}
		this.httpRequest.send(httpMethod=='POST'?httpParams:null);	
	}	// end sendRequest function

	// httpRequest return ÇÔ¼ö
	cartList.prototype.onStateChange=function()
	{
		this.callback(this.httpRequest);
	}	// end onStateChange end

	// Cart List Update
	cartList.prototype.updateCartList=function(obj,param)
	{ 
    	this.objCart=document.getElementById(obj);
    	
    	this.objMargin=document.getElementById(this.marginDiv);
    	
    	document.getElementById(this.openLink).style.display="none";
    	document.getElementById(this.closeLink).style.display="none";
    	
        if (param=="open") { 
            this.objCart.style.display="block";
            this.sendRequest("/union/ajax/cartlist.html","",this.updateCartResponse,"POST");
        } else {
            document.getElementById(this.backDiv).style.background=this.openURL;
            this.closeCartList();
        }
	}	// end updateCartList function
	
	// update Cart
	cartList.prototype.updateCartResponse=function()
	{
		if (this.httpRequest.readyState==4) {
			if (this.httpRequest.status==200) {
				this.docXML_FD=this.httpRequest.responseXML;
				var code=this.docXML_FD.getElementsByTagName("code").item(0).firstChild.nodeValue;		// Update ¼º°ø¿©ºÎ
				
				if (!code || code=="nomem") {
    				alert("È¸¿øÁ¤º¸°¡ ¾ø°Å³ª ·Î±×ÀÎ »óÅÂ°¡ ¾Æ´Õ´Ï´Ù");
    				document.getElementById(this.openLink).style.display="block";
    	            document.getElementById(this.closeLink).style.display="none";
				} else if (code=="nodata") {
    				if (this.openCart=="close") {
    				    alert("Àå¹Ù±¸´Ï°¡ ºñ¾îÀÖ½À´Ï´Ù");
        				document.getElementById(this.openLink).style.display="block";
        	            document.getElementById(this.closeLink).style.display="none";
				    }
    				
    				if (this.openCart!="close") {    				
                		document.getElementById(this.openLink).style.display="none";
                	    document.getElementById(this.closeLink).style.display="none";
                        document.getElementById(this.backDiv).style.background=this.openURL;
                        this.closeCartList();
                    }
				} else {
    				if (this.openCart=="open") {
        				document.getElementById(this.contentDiv).innerHTML="";
                        document.getElementById(this.priceDiv).innerHTML="";
                        document.getElementById(this.countDiv).innerHTML="";
                    }
                    
    	            var _cartList=this;
                        
    	            document.getElementById(this.backDiv).style.background=this.closeURL;
    	            
    				var qData=this.docXML_FD.getElementsByTagName("data");
    				
    				var qNo=this.docXML_FD.getElementsByTagName("qNo");
    				var qObject=this.docXML_FD.getElementsByTagName("qObject");
    				var qTitle=this.docXML_FD.getElementsByTagName("qTitle");
    				var qCode=this.docXML_FD.getElementsByTagName("qCode");
    				var qPrice=this.docXML_FD.getElementsByTagName("qPrice");
    				
    				var dataCount=qData.length;
    				
    				var cObject,cTitle,cCode,cPrice;
    				
    				var contentList=document.getElementById(this.contentDiv);
    				
        			for (var i=0;i<dataCount;i++) {
            			if (this.isFox && i>=3) {
                			break;
            			}
        				cNo=qNo.item(i).firstChild.nodeValue;
        				cObject=qObject.item(i).firstChild.nodeValue;
        				cTitle=qTitle.item(i).firstChild.nodeValue;
        				cCode=qCode.item(i).firstChild.nodeValue;
        				cPrice=qPrice.item(i).firstChild.nodeValue;
        				
        				var cell="cell"+i;
        				
        				cell=document.createElement("li");
        				
        				contentList.appendChild(cell); 	
        				
        				cell.style.width="472px";
        				cell.style.height="17px";
                		cell.style.position="relative";
                		if (i==0) {
                		    cell.style.marginTop="13px";
            		    } else {
                		    cell.style.marginTop="5px";
            		    }
            		    cell.style.marginLeft="10px";
                		cell.style.marginBottom="5px";	
                		cell.style.paddingTop="2px";	
                		cell.style.paddingLeft="8px";
                		cell.style.fontFamily="±¼¸²";
                		cell.style.fontSize="12px";
                		cell.style.color="#333333";
                		cell.style.textAlign="left";
                		cell.style.background="url('/images/web_standard/main/main_notice_dot.gif') 0px 7px no-repeat"; 
                		cell.style.display="block";
                		                		
                		switch(cObject) {
                    		case "DT" :
                    		    cell.innerHTML="[Àü¹®Áö½Ä] <a href=\"/report/view.html?dno="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                    		    break;
                    		case "JB" :
                    		    cell.innerHTML="[Ãë¾÷ÀÚ·á] <a href=\"/job/view.html?ino="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                    		    break;
                    		case "FM" :
                    		    cell.innerHTML="[¼­½Ä] <a href=\"/form/view.html?fno="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                    		    break;
                    		case "TP" :
                    		    cell.innerHTML="[ÅÛÇÃ¸´] <a href=\"/template/view.html?tno="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                    		    break;
                    		case "SO" :
                    		    cell.innerHTML="[ISO¹®¼­] <a href=\"/iso/view.html?sno="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                    		    break;
                    		default :
                    		    cell.innerHTML="[Àü¹®Áö½Ä] <a href=\"/report/view.html?dno="+cCode+"\" target=\"_blank\">"+cTitle+"</a>";
                		}
                		
                		var price="price"+i;
                		
                		price=document.createElement("span");
        				
        				cell.appendChild(price); 	
        				
        				price.style.width="90px";
        				price.style.height="20px";
                		price.style.position="absolute";
            		    price.style.top="2px";
            		    price.style.left="323px";
                		price.style.fontFamily="±¼¸²";
                		price.style.fontSize="12px";
                		price.style.color="#999999";
                		price.style.textAlign="left";
                		
                		price.innerHTML="°¡°Ý : "+cPrice+"¿ø";
                		
                		var del="del"+i;
                		
                		del=document.createElement("span");
        				
        				cell.appendChild(del); 	
        				
        				del.style.width="90px";
        				del.style.height="20px";
                		del.style.position="absolute";
            		    del.style.top="0px";
            		    del.style.left="415px";
                		
                		del.innerHTML="<a href='#' onclick='deleteCart.delCartList("+cNo+");return false;' onkeypress='return false;' title='Àå¹Ù±¸´Ï »èÁ¦ÇÏ±â'><img src='"+this.delURL+"' alt='Àå¹Ù±¸´Ï »èÁ¦ÇÏ±â' /></a>";
        			}
        			
        			var oUsem=this.docXML_FD.getElementsByTagName("tUsem").item(0).firstChild.nodeValue;
        			var oPrice=this.docXML_FD.getElementsByTagName("tPrice").item(0).firstChild.nodeValue;
        			var oCount=this.docXML_FD.getElementsByTagName("tCount").item(0).firstChild.nodeValue;
        			
        			var priceList=document.getElementById(this.priceDiv);
        				
        			var use_m=document.createElement("li");
        				
        			priceList.appendChild(use_m);
        				
        			use_m.style.width="185px";
        			use_m.style.height="13px";
                	use_m.style.position="relative";
            		use_m.style.marginLeft="13px";
                	use_m.style.marginBottom="3px";
                	use_m.style.paddingLeft="8px";
                	use_m.style.fontFamily="±¼¸²";
                	use_m.style.fontSize="12px";
                	use_m.style.color="#666666";
                	use_m.style.textAlign="left";
                	use_m.style.background="url('/images/web_standard/main/main_notice_dot.gif') 0px 5px no-repeat"; 
                	use_m.style.display="block";
                	
                	use_m.innerHTML="<strong>³» ³²Àº ÀÜ¾× : </strong><span style='position:absolute; top:0px; right:0px; text-align:right;'><span style='color:#ff6600;'><strong>"+oUsem+"</strong></span><span style='color:#666666;'> ¿ø</span></span>";
                	
                	var price=document.createElement("li");
        				
        			priceList.appendChild(price);
        				
        			price.style.width="185px";
        			price.style.height="13px";
                	price.style.position="relative";
            		price.style.marginLeft="13px";
                	price.style.paddingLeft="8px";
                	price.style.fontFamily="±¼¸²";
                	price.style.fontSize="12px";
                	price.style.color="#666666";
                	price.style.textAlign="left";
                	price.style.background="url('/images/web_standard/main/main_notice_dot.gif') 0px 5px no-repeat"; 
                	price.style.display="block";
                	
                	price.innerHTML="<strong>Àå¹Ù±¸´Ï ÃÑ¾× : </strong><span style='position:absolute; top:0px; right:0px; text-align:right;'><span style='color:#ff6600;'><strong>"+oPrice+"</strong></span><span style='color:#666666;'> ¿ø</span></span>";
                	
                	var countList=document.getElementById(this.countDiv);
        				
        			var count=document.createElement("span");
        				
        			countList.appendChild(count);
        			
                	count.style.position="relative";
                	count.style.fontFamily="±¼¸²";
                	count.style.fontSize="11px";
                	count.style.fontWeight="bold";
                	count.style.color="#ffff00";
                	count.style.textAlign="center";
                	
                	count.innerHTML="("+oCount+"°Ç)";     
                	
                	// Fire Fox Check
                	if (this.isFox) {    
                        document.getElementById(this.contentDiv).style.overflow="hidden";
                    }          	
        			
        			if (this.openCart=="close") {
            			this.openCartList();
        			} else {
            			document.getElementById(this.closeLink).style.display="block";
        			}
    			}
			}
		}
	}	// end updateCartResponse function
	
	// Open Cart List
	cartList.prototype.openCartList=function()
	{
    	var _cartList=this;
    	
    	this._margin+=20;
    	
    	if (this._margin<(this.t_height-this.h_height)) {
           	this.objCart.style.top=(this._margin+this.h_height)+"px";
           	this.objMargin.style.height=this._margin+"px";
    	    window.setTimeout(function() {_cartList.openCartList();},10);
	    } else {
    	    this.objCart.style.top=this.t_height+"px";
    	    this._margin=this.m_height;
           	this.objMargin.style.height=(this.t_height-this.h_height)+"px";
    	    document.getElementById(this.closeLink).style.display="block";
    	    
    	    this.openCart="open";
	    }
	} // end openCartList
	
	// Close Cart List
	cartList.prototype.closeCartList=function()
	{
    	var _cartList=this;
    	
    	this._top-=20;
    	
    	if (this._top>this.h_height) {
           	this.objCart.style.top=this._top+"px";
           	this.objMargin.style.height=(this._top-this.h_height)+"px";
    	    window.setTimeout(function() {_cartList.closeCartList();},10);
	    } else {
    	    this._top=this.t_height;
    	    this.objCart.style.top=this.h_height+"px";
    	    this.objMargin.style.height="0px";
    	    document.getElementById(this.contentDiv).innerHTML="";
    	    document.getElementById(this.priceDiv).innerHTML="";
    	    document.getElementById(this.countDiv).innerHTML="";
    	    document.getElementById(this.openLink).style.display="block";
    	    
    	    this.openCart="close";
	    }
	} // end closeCartList
	
	// Delete Cart List
	cartList.prototype.deleteCartList=function()
	{
        document.getElementById(this.contentDiv).innerHTML="";
        document.getElementById(this.priceDiv).innerHTML="";
        document.getElementById(this.countDiv).innerHTML="";
        document.getElementById(this.openLink).style.display="block";
        
        this.openCart="static";
	
        this.updateCartList('HHCList','open');
	} // end deleteCartList
	
}	//	end cartList Class

// Delete Cart List
deleteCart=function(objName)
{
	this.objName=objName;		// »ý¼ºµÈ °´Ã¼ ¸í(this)
	this.httpRequest = null;	// httpRequest°´Ã¼
	this.docXML_FD;			// XMLµ¥ÀÌÅÍ return °ª
	
	// httpRequest °´Ã¼ »ý¼º ÇÔ¼ö
	deleteCart.prototype.getXMLHttpRequest=function()
	{
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; }
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}
	}	//	end getXMLHttpRequest function
	
	// httpRequest send ÇÔ¼ö
	deleteCart.prototype.sendRequest=function(url,params,callback,method)
	{
		this.callback=callback;

		this.httpRequest=this.getXMLHttpRequest();
		var httpMethod=method ? method : 'GET';
		if (httpMethod!='GET' && httpMethod!='POST') {
			httpMethod='GET';
		}
		var httpParams=(params==null || params=='')?null:params;
		var httpUrl=url;
		if (httpMethod=='GET' && httpParams!=null) {
			httpUrl=httpUrl+"?"+httpParams;
		}
		this.httpRequest.open(httpMethod,httpUrl,true);
		this.httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var request=this;
		this.httpRequest.onreadystatechange=function()
		{
			request.onStateChange.call(request);
		}
		this.httpRequest.send(httpMethod=='POST'?httpParams:null);	
	}	// end sendRequest function

	// httpRequest return ÇÔ¼ö
	deleteCart.prototype.onStateChange=function()
	{
		this.callback(this.httpRequest);
	}	// end onStateChange end
	
    // Delete Cart List
	deleteCart.prototype.delCartList=function(param)
	{
    	if (confirm("¼±ÅÃÇÏ½Å ÀÚ·á¸¦ »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) {
    		var params="regdate="+encodeURIComponent(param);
            this.sendRequest("/union/ajax/cartdel.html",params,this.deleteCartResponse,"POST");
        } else {
            return;
        }
	} // end delCartList
	
	// Delete Cart
	deleteCart.prototype.deleteCartResponse=function()
	{
		if (this.httpRequest.readyState==4) {
			if (this.httpRequest.status==200) {
				this.docXML_FD=this.httpRequest.responseXML;
				var code=this.docXML_FD.getElementsByTagName("code").item(0).firstChild.nodeValue;		// Update ¼º°ø¿©ºÎ

				if (!code || code=="failure") {
    				alert("Àå¹Ù±¸´Ï »èÁ¦Áß ¿¡·¯°¡ ¹ß»ýÇß½À´Ï´Ù1");
				} else {
    				//alert("»èÁ¦µÇ¾ú½À´Ï´Ù");
    				cartList.deleteCartList();
				}
			}
		} 
	} // end Delete Cart
	
}