var row_count   = "1";
var cart        = new Object();
var def_stuff   = new Array();
var stuff       = new Array('HA','LE','BO','HE','CH','EY','LI','NO','HA2','GL','CA');
var pgeItemType = "";
var defaultStuff_src = new Array();
var defStuff = new Array();

function addPrice(tbl_id){

  var tbody = document.getElementById(tbl_id).getElementsByTagName("TBODY")[0];
  var row = document.createElement("TR");// create row
  
  var td1 = document.createElement("TD");
  var strHtml1 = "<INPUT TYPE=\"text\" NAME=\"duration[]\" SIZE=\"10\" MAXLENGTH=\"5\">";
  td1.innerHTML = strHtml1.replace(/!row_count!/g,row_count);
  
  var td2 = document.createElement("TD");
  var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"price[]\" SIZE=\"10\" MAXLENGTH=\"10\">";
  td2.innerHTML = strHtml2.replace(/!row_count!/g,row_count);
  
  var td3 = document.createElement("TD");
  var strHtml3 = "<INPUT TYPE=\"Button\" CLASS=\"Button\" onClick=\"delPrice()\" VALUE=\"Delete\">";
  td3.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  
  // append data to row
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  
  // add to row_count variable
  row_count = parseInt(row_count) + 1;
  
  // append row to table
  tbody.appendChild(row);
}

function delPrice(){
	var current = window.event ? window.event.srcElement : event.target; 
  
  //here we will delete the line
  while ( (current = current.parentElement)  && current.tagName !="TR");
       current.parentElement.removeChild(current);
}

function addStuffCart(itemId,cate,itemName,cateName){

	var tblElemt = window.parent.document.getElementById('tbl_cart');
  var tbody    = tblElemt.getElementsByTagName("TBODY")[0];
  var row      = window.parent.document.createElement("TR");// create row
  row.setAttribute("id", itemId);
  //
  // Category
  //
  var td1 = window.parent.document.createElement("TD");
  td1.setAttribute("width", "20%");
  td1.setAttribute("align", "center");
  var strHtml3 = "<p id='cate"+cate+"'>"+cateName+"</p>";
  td1.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  //
  // Item Name
  //
  var td2 = window.parent.document.createElement("TD");
  td2.setAttribute("width", "30%");
  td2.innerHTML = itemName.replace(/!row_count!/g,row_count);
  //
  // Duration
  //
  var td3 = window.parent.document.createElement("TD");
  td3.setAttribute("width", "35%");
  td3.setAttribute("align", "left");
  var strHtml3  = "<select id='d_"+itemId+"' name='"+pgeItemType+":"+cate+"' onchange='countPt("+itemId+");' class='sel_item'></select>";
  td3.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  //
  // Point
  //
  var td4 = window.parent.document.createElement("TD");
  td4.setAttribute("width", "5%");
  td4.setAttribute("align", "center");
  var strHtml4  = "<div class='item_point' id='point_"+itemId+"'></div>";
  td4.innerHTML = strHtml4.replace(/!row_count!/g,row_count);
  //
  // Action
  //
  var td5 = window.parent.document.createElement("TD");
  td5.setAttribute("width", "10%");
  td5.setAttribute("align", "center");
  var img_url   = window.parent.document.getElementById('img_del_url').value;
  
//  if(arguments[4] == 'wish'){
//  	var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\",\""+arguments[4]+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
//  }
//	else{
//		var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
//	}
  
  var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delStuff(\""+cate+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
  td5.innerHTML = strHtml5.replace(/!row_count!/g,row_count);
  
  // append data to row
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  row.appendChild(td4);
  row.appendChild(td5);
  
  // add to row_count variable
  row_count = parseInt(row_count) + 1;
  
  // append row to table
  tbody.appendChild(row);
    
  //
  // Select Option
  //
  var selOpt     = window.parent.document.getElementById('d_'+itemId);
  var point      = window.parent.document.getElementById('point_'+itemId);
	
	if(arguments[4] == 'wish'){
		var itemOpt90     = parent.content.getFrameElement(arguments[5]+'-90');
		var itemOpt180    = parent.content.getFrameElement(arguments[5]+'-180');
		var val30         = parent.content.getFrameElement(arguments[5]+'-30').value;
		selOpt.options[0] = new Option("30 days",'30:'+val30,false,true);
		point.innerHTML   = parent.content.getFrameElement(arguments[5]+'-30').value;
	}
	else{
		var itemOpt90     = parent.content.getFrameElement(itemId+'-90');
		var itemOpt180    = parent.content.getFrameElement(itemId+'-180');
		var val30         = parent.content.getFrameElement(itemId+'-30').value;
		selOpt.options[0] = new Option("30 days",'30:'+val30,false,true);
		point.innerHTML   = parent.content.getFrameElement(itemId+'-30').value;
	}	
	
	if(itemOpt90){
		var val90 = itemOpt90.value;
		selOpt.options[1] = new Option("90 days",'90:'+val90,false,false);
	}
	
	if(itemOpt180){
		var val180 = itemOpt180.value;
		selOpt.options[2] = new Option("180 days",'180:'+val180,false,false);
	}
	
	countTotalPt();
}

function addItemCart(itemId,cate,cateName,itemName){
	
	var tblElemt = window.parent.document.getElementById('tbl_cart');
  var tbody    = tblElemt.getElementsByTagName("TBODY")[0];
  var row      = window.parent.document.createElement("TR");// create row
  row.setAttribute("id", itemId);
  //
  // Category
  //
  var td1 = window.parent.document.createElement("TD");
  td1.setAttribute("width", "20%");
  td1.setAttribute("align", "center");
  var strHtml3 = "<p id='"+cate+"'>"+cateName+"</p>";
  td1.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  //
  // Item Name
  //
  var td2 = window.parent.document.createElement("TD");
  td2.setAttribute("width", "30%");
  td2.innerHTML = itemName.replace(/!row_count!/g,row_count);
  //
  // Duration
  //
  var td3 = window.parent.document.createElement("TD");
  td3.setAttribute("width", "35%");
  td3.setAttribute("align", "left");
  var strHtml3  = "<select id='d_"+itemId+"' name='"+arguments[4]+":"+cate+"' onchange='countPt("+itemId+");' class='sel_item'></select>";
  td3.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  //
  // Point
  //
  var td4 = window.parent.document.createElement("TD");
  td4.setAttribute("width", "5%");
  td4.setAttribute("align", "center");
  //var strHtml4  = "<div class='item_point' id='point_"+itemId+"'>"+itemOpt30.value+"</div>";
  var strHtml4  = "<div class='item_point' id='point_"+itemId+"'></div>";
  td4.innerHTML = strHtml4.replace(/!row_count!/g,row_count);
  //
  // Action
  //
  var td5 = window.parent.document.createElement("TD");
  td5.setAttribute("width", "10%");
  td5.setAttribute("align", "center");
  var img_url   = window.parent.document.getElementById('img_del_url').value;
  
  if(arguments[4] == 'wish'){
  	var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\",\""+arguments[4]+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
  }
	else{
		var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
	}
  
  td5.innerHTML = strHtml5.replace(/!row_count!/g,row_count);
  
  // append data to row
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  row.appendChild(td4);
  row.appendChild(td5);
  
  // add to row_count variable
  row_count = parseInt(row_count) + 1;
  
  // append row to table
  tbody.appendChild(row);
  
  //
  // Select Option
  //
  var selOpt     = window.parent.document.getElementById('d_'+itemId);
//  var itemOpt30  = window.parent.document.getElementById(itemId+'-30');
  var point      = window.parent.document.getElementById('point_'+itemId);
	
//	alert(itemId);
//	alert(arguments[5]);
	
	if(arguments[4] == 'wish'){
		var itemOpt90     = parent.content.getFrameElement(arguments[5]+'-90');
		var itemOpt180    = parent.content.getFrameElement(arguments[5]+'-180');
		var val30         = parent.content.getFrameElement(arguments[5]+'-30').value;
		selOpt.options[0] = new Option("30 days",'30:'+val30,false,true);
		point.innerHTML   = parent.content.getFrameElement(arguments[5]+'-30').value;
	}
	else{
		var itemOpt90     = parent.content.getFrameElement(itemId+'-90');
		var itemOpt180    = parent.content.getFrameElement(itemId+'-180');
		var val30         = parent.content.getFrameElement(itemId+'-30').value;
		selOpt.options[0] = new Option("30 days",'30:'+val30,false,true);
		point.innerHTML   = parent.content.getFrameElement(itemId+'-30').value;
	}	
	
	if(itemOpt90){
		var val90 = itemOpt90.value;
		selOpt.options[1] = new Option("90 days",'90:'+val90,false,false);
	}
	
	if(itemOpt180){
		var val180 = itemOpt180.value;
		selOpt.options[2] = new Option("180 days",'180:'+val180,false,false);
	}
	
	countTotalPt();
}

function addGiftCart(itemId,itemName,imgPath){
	var more_gift = arguments[3] == 'more_gift' ? 1 : 0;
	
	if(more_gift){
		var tblElemt = window.parent.document.getElementById('tbl_cart');
		var row      = window.parent.document.createElement("TR");// create row
		var td1      = window.parent.document.createElement("TD");
		var td2      = window.parent.document.createElement("TD");
		var td3      = window.parent.document.createElement("TD");
		var td4      = window.parent.document.createElement("TD");
		var td5      = window.parent.document.createElement("TD");
		var img_url  = window.parent.document.getElementById('img_del_url').value;
	}
	else{
		var tblElemt = $('tbl_cart');
		var row      = document.createElement("TR");// create row
		var td1      = document.createElement("TD");
		var td2      = document.createElement("TD");
		var td3      = document.createElement("TD");
		var td4      = document.createElement("TD");
		var td5      = document.createElement("TD");
		var img_url  = $('img_del_url').value;
	}
	
  var tbody    = tblElemt.getElementsByTagName("TBODY")[0];
  row.setAttribute("id", itemId);
  //
  // Item
  //
  td1.setAttribute("width", "20%");
  td1.setAttribute("align", "center");
  var strHtml1  = "<img src='"+imgPath+"'>";
  td1.innerHTML = strHtml1.replace(/!row_count!/g,row_count);
  //
  // Message
  //
  td2.setAttribute("width", "45%");
  td2.setAttribute("align", "center");
  var strHtml2  = "<textarea name='message[]' cols='30' rows='3'>Something Specials for you ;)</textarea>";
  td2.innerHTML = strHtml2.replace(/!row_count!/g,row_count);
  //
  // Duration
  //
  td3.setAttribute("width", "20%");
  td3.setAttribute("align", "center");
  var strHtml3  = "<select id='d_"+itemId+"' name='item[]' onchange='countPt("+itemId+",\"gift\");' class='sel_item'></select>";
  td3.innerHTML = strHtml3.replace(/!row_count!/g,row_count);
  //
  // Point
  //
  td4.setAttribute("width", "5%");
  td4.setAttribute("align", "center");
  var strHtml4  = "<div class='item_point' id='point_"+itemId+"'></div>";
  td4.innerHTML = strHtml4.replace(/!row_count!/g,row_count);
  //
  // Action
  //
  td5.setAttribute("width", "10%");
  td5.setAttribute("align", "center");
  
//  if(arguments[4] == 'wish'){
//  	var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\",\""+arguments[4]+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
//  }
//	else{
//		var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex);delDHTML("+itemId+",\""+cate+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
//	}
  
  var strHtml5  = "<img onclick='delItemCart(this.parentNode.parentNode.rowIndex,\"gift\");delGift("+itemId+",\""+arguments[3]+"\");' src=\""+img_url+"\" border=\"0\" style=\"cursor:pointer\">";
  td5.innerHTML = strHtml5.replace(/!row_count!/g,row_count);
  
  // append data to row
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  row.appendChild(td4);
  row.appendChild(td5);
  
  // add to row_count variable
  row_count = parseInt(row_count) + 1;
  
  // append row to table
  tbody.appendChild(row);
    
  //
  // Select Option
  //
  if(more_gift){
  	var selOpt     = window.parent.document.getElementById('d_'+itemId);
  	var point      = window.parent.document.getElementById('point_'+itemId);
//  	var itemOpt30  = window.parent.document.getElementById(itemId+'-30');
//  	var itemOpt90  = window.parent.document.getElementById(itemId+'-90');
//		var itemOpt180 = window.parent.document.getElementById(itemId+'-180');
  }
	else{
		var selOpt     = $('d_'+itemId);
  	var point      = $('point_'+itemId);
	}
	
	var itemOpt30  = $(itemId+'-30');
	var itemOpt90  = $(itemId+'-90');
	var itemOpt180 = $(itemId+'-180');
  
	if(pgeItemType == 'wish'){
		var temp     = itemOpt30.name.split('_');
		pgeItemType += ':'+temp[1];
	}
	
	var val30 = itemOpt30.value;
	selOpt.options[0] = new Option("30 days",itemId+':30:'+val30+':'+pgeItemType,false,true);
	point.innerHTML   = itemOpt30.value;
	
	if(itemOpt90){
		var val90 = itemOpt90.value;
		selOpt.options[1] = new Option("90 days",itemId+':90:'+val90+':'+pgeItemType,false,false);
	}
	
	if(itemOpt180){
		var val180 = itemOpt180.value;
		selOpt.options[2] = new Option("180 days",itemId+':180:'+val180+':'+pgeItemType,false,false);
	}
	
	if(more_gift){
		countTotalPt();
	}
	else{
		countTotalPt('gift');
	}
}

function countPt(itemId){	
	var selOpt = $('d_'+itemId).selectedIndex;
	var selVal = $('d_'+itemId).options[selOpt].value.split(':');
	
	if(arguments[1] == 'gift'){
		$('point_'+itemId).innerHTML = selVal[2];
		countTotalPt(arguments[1]);
	}
	else{
		$('point_'+itemId).innerHTML = selVal[1];
		countTotalPt();
	}
}

function countTotalPt(){
	if(arguments[0] == 'gift'){
		var allSelItem = document.getElementsByClassName('sel_item');
	}
	else{
		var allSelItem = window.parent.document.getElementsByClassName('sel_item');
	}
	var total      = 0;
	
	for(i = 0; i < allSelItem.length; i++){
		var selOpt = allSelItem[i].value.split(':');
		total += parseInt(selOpt[1]);
	}
	
	if(arguments[0] == 'gift'){
		$('total').innerHTML = total;
	}
	else{
		window.parent.document.getElementById('total').innerHTML = total;
	}
}

function delItemCart(i){
  //var i = r.parentNode.parentNode.rowIndex;
  
  if(arguments[1] == 'gift'){
  	$('tbl_cart').deleteRow(i);
  	countTotalPt(arguments[1]);
  }
	else{
		window.parent.document.getElementById('tbl_cart').deleteRow(i);
		countTotalPt();
	}
}

function resetGiftCart(room_id){
	var tbl_cart = window.parent.document.getElementById('tbl_cart');
	
	if(tbl_cart){
		for(i = 0; i < tbl_cart.rows.length; i++){
	  	tbl_cart.deleteRow(i);
	  }
	}
	window.parent.document.getElementById('room_id').value = room_id;
}

function delGift(itemId){
	var itemId    = 'DHTML'+itemId;
//	var hide      = $('H'+itemId);
//	var gift_cart = $('gift_cart');
	
	if(arguments[1] == 'more_gift'){
		var container = parent.frm_room.getFrameElement('container');
		var itemElemt = parent.frm_room.getFrameElement(itemId);
		
		parent.frm_room.dd.elements[itemId].del(); // Removes the DHTML element
	}
	else{
		var container = $('container');
		var itemElemt = $(itemId);
		
		dd.elements[itemId].del(); // Removes the DHTML element
	}
	
	if($('H'+itemId)){
		//gift_cart.removeChild(hide);
		parentObj = $('H'+itemId).parentNode; 
    childObj  = $('H'+itemId);  

    parentObj.removeChild(childObj);
	}
	if(itemElemt){
		container.removeChild(itemElemt);
	}
}

function delStuff(cate){
	var cateElemt = window.parent.document.getElementById(cate);
	var temp      = cateElemt.src.split('-');
	
	cateElemt.src  = window.parent.defStuff[cate] +'-'+ temp[1];
	cateElemt.name = "0:0";
	
	if(cate == 'HA' || cate == 'CA'){
		var hairElemt2 = window.parent.document.getElementById('HA2');
		var capElemt   = window.parent.document.getElementById('CA');
		var hairElemt  = window.parent.document.getElementById('HA').src.split("-");
		
		if(capElemt.name != '0:0'){
			hairElemt2.src = hairElemt[0] + "-front.gif";
		}
		else{
			hairElemt2.src = hairElemt[0] + "-top.gif";
		}
	}
}

function delDHTML(itemId){
	if(arguments[1] == 'WA' || arguments[1] == 'CP'){
		parent.frm_room.getFrameElement('DHTML_'+arguments[1]).src  = arguments[1] == 'WA' ? defWA : defCP;
		parent.frm_room.getFrameElement('DHTML_'+arguments[1]).name = 0;
	}
	else{
		var itemId    = arguments[2] == 'wish' ? 'WDHTML'+itemId : 'SDHTML'+itemId;
		var room_td   = parent.frm_room.getFrameElement('room_td');
		var hide      = parent.frm_room.getFrameElement('H'+itemId);
		var container = parent.frm_room.getFrameElement('container');
		var item      = parent.frm_room.getFrameElement(itemId);
		
		if(hide){
			room_td.removeChild(hide);
		}
		if(item){
			container.removeChild(item);
		}
		
		parent.frm_room.dd.elements[itemId].del(); // Removes the DHTML element
	}  
}

function getFrameElement(elemtId){
	return $(elemtId);
}

function changeMode(mode){
	var leg  = document.getElementById('LE'); // Leg
	var body = document.getElementById('BO'); // Body
	var eye  = document.getElementById('EY'); // Eye
	var lip  = document.getElementById('LI'); // Lip
	
	var leg_arr = leg.src.split("-");
	leg.src = leg_arr[0]+"-"+mode+".gif";
	
	var body_arr = body.src.split("-");
	body.src = body_arr[0]+"-"+mode+".gif";
	
	var eye_arr = eye.src.split("-");
	eye.src = eye_arr[0]+"-"+mode+".gif";
	
	var lip_arr = lip.src.split("-");
	lip.src = lip_arr[0]+"-"+mode+".gif";
	
	setCheckedValue(document.forms['form'].elements['mode'], mode);
}


function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var q  = 0;
var it = 0;

function swapStuff(cate,imgPath,itemId){
	var cateElemt = window.parent.document.getElementById(cate);
	var temp      = cateElemt.src.split('-');
	
	if(pgeItemType == 'surface'){
		var check = itemId+":surface"
	}
	else if(pgeItemType == 'store'){
		var check = arguments[3]+":"+itemId // arguments[3] => id
	}
	else if(pgeItemType == 'shop'){
		var check = "0:"+itemId
	}
	else if(pgeItemType == 'wish'){
		var check = "0:"+arguments[5];
	}

	if(cateElemt.name == check){
		cateElemt.src  = window.parent.defStuff[cate] +'-'+ temp[1];
		cateElemt.name = "0:0";
		
		if(pgeItemType == 'shop' || pgeItemType == 'wish'){
			var rElemt  = window.parent.document.getElementById('cate'+cate);
		  if(rElemt){
		  	delItemCart(rElemt.parentNode.parentNode.rowIndex); // delete item from cart
		  }
		}
	}
	else{
		cateElemt.src  = imgPath +'-'+ temp[1];
		if(pgeItemType == 'surface'){
			cateElemt.name = itemId+":surface";
		}
		else if(pgeItemType == 'store'){
			cateElemt.name = arguments[3]+":"+itemId;
		}
		else if(pgeItemType == 'shop'){
			cateElemt.name = "0:"+itemId;
		}
		else if(pgeItemType == 'wish'){
			cateElemt.name = "0:"+arguments[5];
		}
		
		if(pgeItemType == 'shop' || pgeItemType == 'wish'){
		  var rElemt  = window.parent.document.getElementById('cate'+cate);
		  if(rElemt){
		  	delItemCart(rElemt.parentNode.parentNode.rowIndex); // delete item from cart
		  }
			addStuffCart(itemId,cate,arguments[3],arguments[4],pgeItemType,arguments[5]);
		}
	}

	if(cate == 'HA' || cate == 'CA'){
		var hairElemt2 = window.parent.document.getElementById('HA2');
		var capElemt   = window.parent.document.getElementById('CA');
		var hairElemt  = window.parent.document.getElementById('HA').src.split("-");
		
		if(capElemt.name != '0:0'){
			hairElemt2.src = hairElemt[0] + "-front.gif";
		}
		else{
			hairElemt2.src = hairElemt[0] + "-top.gif";
		}
	}
}

function swapItem(pgeItem, cate, imgPath, itemId){
	//
	// If element exist, then remove item, else adding element into div
	//
	if(cate == 'WA' || cate == 'CP'){
		if($('DHTML_'+cate).name == itemId){
			$('DHTML_'+cate).src  = cate == 'WA' ? defWA : defCP;
			$('DHTML_'+cate).name = 0;
			
			if(pgeItem == 'shop' || pgeItem == 'wish'){
				var rElemt  = window.parent.document.getElementById(cate);
			  if(rElemt){
			  	delItemCart(rElemt.rowIndex); // delete item from cart
			  	//delItemCart(rElemt.parentNode.parentNode.rowIndex); // delete item from cart
			  }
			}
		}
		else{
			$('DHTML_'+cate).src  = imgPath;
			$('DHTML_'+cate).name = itemId;
			
			if(pgeItem == 'shop' || pgeItem == 'wish'){
			  var rElemt  = window.parent.document.getElementById(cate);
			  if(rElemt){
			  	delItemCart(rElemt.rowIndex); // delete item from cart
			  	//delItemCart(rElemt.parentNode.parentNode.rowIndex); // delete item from cart
			  }
				addItemCart(itemId,cate,arguments[4],arguments[5],pgeItem,arguments[6]); // Add item to cart
			}
		}
	}
	else{
		if(pgeItem == 'store'){
			var itemId = 'DHTML'+itemId;
		}
		else if(pgeItem == 'wish'){
			var itemId = 'WDHTML'+itemId;
		}
		else if(pgeItem == 'shop'){
			var itemId = 'SDHTML'+itemId;	
		}
		
		var cont = document.getElementById('container');
		
		if($(itemId)){
			$('room_td').removeChild($('H'+itemId));
		  $('container').removeChild($(itemId));
		  dd.elements[itemId].del(); // Removes the DHTML element
		  //
		  // Delete from shopping cart
		  //
		  if(pgeItem == 'shop' || pgeItem == 'wish'){
		  	var sItemId = itemId.split('DHTML');
			  var rElemt  = window.parent.document.getElementById(sItemId[1]);
			  delItemCart(rElemt.rowIndex); // delete item from cart
			  //delItemCart(rElemt.parentNode.parentNode.rowIndex); // delete item from cart
		  }
		}
		else{
			if(pgeItem == 'shop' || pgeItem == 'wish'){
				var sItemId = itemId.split('DHTML');
				addItemCart(sItemId[1],cate,arguments[4],arguments[5],pgeItem,arguments[6]); // Add item to cart
			}
			
			var newHide = document.createElement('input');
			newHide.setAttribute('type','hidden');
			newHide.setAttribute('id','H'+itemId);
			newHide.setAttribute((document.all ? 'className' : 'class'),'my_'+pgeItem);
			newHide.setAttribute('value','0x0x0');
			$('room_td').appendChild(newHide);
			
		  var newDiv = document.createElement('div');
		  newDiv.setAttribute('id',itemId);
	
		  if(dd.ie){
		  	newDiv.style.setAttribute('cssText','position:absolute;');
		  }
			else{
				newDiv.setAttribute('style','position:absolute;');
			}
		  newDiv.innerHTML = "<img src='"+imgPath+"'>";
		  $('container').appendChild(newDiv);

			ADD_DHTML(itemId); // Adding the DHTML element

			//dd.elements[itemId].maximizeZ();
			//dd.elements[itemId].show();
			dd.elements[itemId].setDraggable(true);
			//dd.elements[itemId].setScalable(true); 
		}
	}
}

function swapGift(itemId,itemName){
	itemId = 'DHTML'+itemId;
	
	if(arguments[2] == 'more_gift'){
		var itemElemt = parent.frm_room.getFrameElement(itemId);
		var container = parent.frm_room.getFrameElement('container');
	}
	else{
		var itemElemt = $(itemId);
		var container = $('container');
	}
	//
	// If item exist, then delete the item, else add the item
	//
	if(itemElemt){
		if(arguments[2] == 'more_gift'){
			//var ele = window.parent.document.getElementById('H'+itemId);
			//window.parent.document.getElementById('gift_cart').removeChild(ele);
			parentObj = window.parent.document.getElementById('H'+itemId).parentNode; 
      childObj  = window.parent.document.getElementById('H'+itemId);  

      parentObj.removeChild(childObj);
		}
		else{
			$('gift_cart').removeChild($('H'+itemId));
		}

		container.removeChild(itemElemt);
		
		if(arguments[2] == 'more_gift'){
	  	parent.frm_room.dd.elements[itemId].del(); // Removes the DHTML element
		}
		else{
	  	dd.elements[itemId].del(); // Removes the DHTML element
		}
	  
	  var sItemId = itemId.split('DHTML');
	  
	  if(arguments[2] == 'more_gift'){
	  	var rElemt  = window.parent.document.getElementById(sItemId[1]);
	 	 	window.parent.document.getElementById('tbl_cart').deleteRow(rElemt.rowIndex);
	 	 	//window.parent.document.getElementById('tbl_cart').deleteRow(rElemt.parentNode.parentNode.rowIndex);
		}
		else{
			var rElemt  = $(sItemId[1]);
	 	 	$('tbl_cart').deleteRow(rElemt.rowIndex);
		}
	  
	  if(arguments[2] == 'more_gift'){
	  	countTotalPt();
	  }
		else{
			countTotalPt('gift');
		}
	}
	else{
		if(arguments[2] == 'more_gift'){
			var newHide = window.parent.document.createElement('input');
		}
		else{
			var newHide = document.createElement('input');
		}
		
		newHide.setAttribute('type','hidden');
		newHide.setAttribute('id','H'+itemId);
		newHide.setAttribute('name','H'+itemId);
		newHide.setAttribute((document.all ? 'className' : 'class'),'my_gift');
		newHide.setAttribute('value','0x0x0');
		
		if(arguments[2] == 'more_gift'){
			window.parent.document.getElementById('gift_cart').appendChild(newHide);
			
			var newDiv  = parent.frm_room.document.createElement('div');
			var browser = parent.frm_room.dd.ie;
		}
		else{
			$('gift_cart').appendChild(newHide);
			
			var newDiv  = document.createElement('div');
			var browser = dd.ie;
		}
	  
	  newDiv.setAttribute('id',itemId);
	
	  if(browser){
	  	newDiv.style.setAttribute('cssText','position:absolute;');
	  }
		else{
			newDiv.setAttribute('style','position:absolute;');
		}
		var imgPath = $('i'+itemId).src.split('-');
	  newDiv.innerHTML = "<img src='"+imgPath[0]+"-normal.gif'>";
	  container.appendChild(newDiv);
		
		if(arguments[2] == 'more_gift'){
			parent.frm_room.frameAddDHTML(itemId);
			
//			if(!parent.frm_room.dd.elements[itemId]){
//				parent.frm_room.frameAddDHTML(itemId);
//			}
		}
		else{
			ADD_DHTML(itemId); // Adding the DHTML element
			//dd.elements['DHTML'+itemId].maximizeZ();
//			if(!dd.elements[itemId]){
//				ADD_DHTML(itemId);
//			}
		}

		itemId = itemId.split('DHTML');
		
		addGiftCart(itemId[1],itemName,imgPath[0]+"-thumb.gif",arguments[2]);
	}
}

function frameAddDHTML(itemId){
	ADD_DHTML(itemId);
}

function shwGiftCart(command){
//	if(arguments[1] == 'more_gift'){
//		var allSelItem = window.parent.document.getElementsByClassName('sel_item');
//		var avt_room   = window.parent.document.getElementById('avt_room');
//		var gift_cart  = window.parent.document.getElementById('gift_cart');
//	}
//	else{
		var allSelItem = document.getElementsByClassName('sel_item');
		var avt_room   = $('avt_room');
		var gift_cart  = $('gift_cart');
//	}
	
	if(command == 'display'){
		
		if(allSelItem.length == 0){
			alert('Please select gift item.');
			return;
		}
		
		avt_room.style.display  = 'none';
		gift_cart.style.display = 'inline';
	}
	else if(command == 'none'){
		avt_room.style.display  = 'inline';
		gift_cart.style.display = 'none';
	}
}

function shwbuyItem(itemId, itemName){
	
	if(arguments[2] == 'more_gift'){
		$('item_img').src  = document.getElementById('iDHTML'+itemId).src;
	}
	else{
		$('item_img').src  = document.getElementById(itemId).src;
	}
	$('item_name').innerHTML = itemName;
	
	var price30  = "30 days @ only "+$(itemId+'-30').value;
	$('duration').options[0] = new Option(price30,'30',false,true);
	
	if($(itemId+'-90')){
		var price90  = "90 days @ only "+$(itemId+'-90').value;
		$('duration').options[1] = new Option(price90,'90',false,false);
	}
	
	if($(itemId+'-180')){
		var price180 = "180 days @ only "+$(itemId+'-180').value;
		$('duration').options[2] = new Option(price180,'180',false,false);
	}
	
	$('btn_buy').name = itemId;
	
	showMe(); // Show popup floating layer
}

function closeLightBox(){
	$('overlay').style.display  = "none";
	$('lightbox').style.display = "none";
}

function getRoomId(){
	return roomId;
}

function changeRoom(roomId){
	//
	// Change Menu
	//
	var allMenu = document.getElementsByClassName('textmenu');

	for(i = 0; i < allMenu.length; i++){
		var temp = allMenu[i].id.split('_');
		
		if(temp[0] == 'rid'){
			$(allMenu[i].id).setAttribute((document.all ? 'className' : 'class'),'menu');
		}
	}
	
	$('rid_'+roomId).setAttribute((document.all ? 'className' : 'class'),'textmenu');
	
	//
	// Clean up shopping cart
	//  
  for(i = 0; i < $('tbl_cart').rows.length; i++){
  	$('tbl_cart').deleteRow(i);
  }
}

function savAvtMsg(){
	$("msg").style.display         = 'none';
	$("btn_msg").style.display     = 'none';
	$('img_sav_msg').style.display = 'inline';

	var msg   = $("msg").value;
	var _data = "m=avatar2&c=sav_msg_ajax&msg="+msg+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_savAvtMsg,_data);
}

function buyUnitItem(itemType){
	$("sell_content").style.display = 'none';
	$('img_loading').style.display  = 'inline';

	var itemId   = $('btn_buy').name;
	var duration = $('duration').options[$('duration').selectedIndex].value;
	var point    = $(itemId+'-'+duration).value;
	var _data = "m=avatar2&c=buy_item_ajax&type="+itemType+"&item_id="+itemId+"&duration="+duration+"&point="+point+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_buyUnitItem,_data);
}

function addWish(itemType,itemId){
	var _data = "m=avatar2&c=add_wish_item_ajax&type="+itemType+"&item_id="+itemId+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_general,_data);
}

function buyWishItem(itemType){
	$("sell_content").style.display = 'none';
	$('img_loading').style.display  = 'inline';

	var itemId   = $('btn_buy').name;
	var duration = $('duration').options[$('duration').selectedIndex].value;
	var sid_arr  = $(itemId+'-'+duration).name.split('_');
	var sid      = sid_arr[1];
	var point    = $(itemId+'-'+duration).value;
	
	var _data    = "m=avatar2&c=buy_wish_item_ajax&type="+itemType+"&id="+sid+"&point="+point+"&duration="+duration+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_buyUnitItem,_data);
}

function delItem(itemType,itemId){
	if(confirmSubmit() == true){
		var _data = "m=avatar2&c=del_item_ajax&type="+itemType+"&id="+itemId+"&rn="+Math.floor(Math.random()*100000);
		httpRequest("POST",_url,true,res_delItem,_data);
	}
}

function setDefRoom(roomId){
	var _data = "m=avatar2&c=set_def_room_ajax&room_id="+roomId+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_setDefRoom,_data);
}

function showAvt(roomId){
	var _data = "m=avatar2&c=set_shw_avt_ajax&room_id="+roomId+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_showAvt,_data);
}

function cleanUp(roomId){
	if(confirmSubmit() == true){
		var _data = "m=avatar2&c=clean_up_room_ajax&room_id="+roomId+"&rn="+Math.floor(Math.random()*100000);
		httpRequest("POST",_url,true,res_cleanUp,_data);
	}
}

function savRoomName(roomId){
	$("room_name").style.display   = 'none';
	$("btn_save").style.display    = 'none';
	$('img_sav_msg').style.display = 'inline';
	
	var roomName = $('room_name').value;
	var _data    = "m=avatar2&c=sav_room_name_ajax&room_id="+roomId+"&room_name="+roomName+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,function(){res_savRoomName(roomId)},_data);
}

function savStuff(){
	var allStuff    = document.getElementsByClassName('avtStuff');
	var allSurface  = document.getElementsByClassName('my_surface');
	var item        = new Array();
	var itemId,cate,rowId,surface;
	var mode  	    = getCheckedValue(document.forms['form'].elements['mode']);
	var fix_surface = "HE,EY,LI,NO,CH";
	
	//
	// Get existing surface from hidden elements
	//
	for(i = 0; i < allSurface.length; i++){
		surface = allSurface[i].value;
		//alert(surface);
		if(surface != 0){
			item.push("surface:"+surface);
		}
	}
	
	//
	// Get all new stuff from image element
	//
	for(i = 0; i < allStuff.length; i++){
		cate   = allStuff[i].id;
		itemId = allStuff[i].name.split(':'); // itemId[0] => id, itemId[1] => stuff_id
		
		if(itemId[0] != 0){
			if(fix_surface.indexOf(cate)!==-1){
				if($('h'+cate).value == itemId[0]){
					item.push("new_surface:"+$('h'+cate).name);
				}
				else{
					item.push("new_surface:"+itemId[0]);
				}
			}
			else{
				item.push(cate+":"+itemId[0]);
			}
		}
	}
//alert(item);
	item.toJSON();

	var _data = "m=avatar2&c=sav_item_ajax&type="+pgeMode+"&mode="+mode+"&item="+item+"&rn="+Math.floor(Math.random()*100000);
	
	if(arguments[0] == 'buyItem'){
		httpRequest("POST",_url,true,function(){res_saveItem('buyItem','stuff')},_data);
	}
	else{

		httpRequest("POST",_url,true,res_saveItem,_data);
	}
}

function savItemPos(room_id){
	var allItem     = document.getElementsByClassName('my_store');
	var item    = new Array();
	var itemId;
	var person  = dd.elements['person'].visible ? $('HPerson').value : "";
		
	for(i = 0; i < allItem.length; i++){
		itemId = allItem[i].id.split('DHTML');
		item.push(itemId[1]+":"+allItem[i].value);
	}
	
	if(!window.parent.document.getElementById('WA')){
		if($('DHTML_WA').name != 0){
			item.push($('DHTML_WA').name+":0x0x0");
		}
	}
	
	if(!window.parent.document.getElementById('CP')){
		if($('DHTML_CP').name != 0){
			item.push($('DHTML_CP').name+":0x0x0");
		}
	}

	item.toJSON();

	var _data = "m=avatar2&c=sav_item_ajax&type="+pgeMode+"&room_id="+roomId+"&person="+person+"&item="+item+"&rn="+Math.floor(Math.random()*100000);
	
	if(arguments[1] == 'buyItem'){
		httpRequest("POST",_url,true,function(){res_saveItem('buyItem','item')},_data);
	}
	else{
		httpRequest("POST",_url,true,res_saveItem,_data);
	}
}

function buyItem(itemType){
	$('cart').style.display        = 'none';
	$('img_loading').style.display = 'inline';
	
	var allSelItem = document.getElementsByClassName('sel_item');
	var item       = new Array();
	
	if(itemType == 'stuff'){
		//
		// ********************************
		// $item_arr[0] // page item type
		// $item_arr[1] // stuff id
		// $item_arr[2] // duration
		// $item_arr[3] // point
		// ********************************
		//
		for(i = 0; i < allSelItem.length; i++){
			var pgeType = allSelItem[i].name.split(':'); // pgeType[0] => type, pgeType[1] => category
			var itemId  = allSelItem[i].id.split('_');
			var selVal  = allSelItem[i].value.split(':'); // selVal[0] => duration, selVal[1] => point
			
			item.push(pgeType[0]+":"+itemId[1]+":"+selVal[0]+":"+selVal[1]);
		}
	}
	else if(itemType == 'item'){
		//
		// ********************************
		// $item_arr[0] // page item type
		// $item_arr[1] // item id
		// $item_arr[2] // room id
		// $item_arr[3] // position
		// $item_arr[4] // duration
		// $item_arr[5] // point
		// ********************************
		//
		for(i = 0; i < allSelItem.length; i++){
			var pgeType = allSelItem[i].name.split(':'); // pgeType[0] => type, pgeType[1] => category
			var itemId  = allSelItem[i].id.split('_');
			var itemPos = parent.content.getFrameElement(itemId+'-180');
			var selVal  = allSelItem[i].value.split(':'); // selVal[0] => duration, selVal[1] => point
	
			if(pgeType[0] == 'wish'){
				if(pgeType[1] == 'WA' || pgeType[1] == 'CP'){
					var itemPos = '0x0x0';
				}
				else{
					var itemPos = parent.frm_room.getFrameElement('HWDHTML'+itemId[1]).value;
				}			
			}
			else if(pgeType[0] == 'shop'){
				if(pgeType[1] == 'WA' || pgeType[1] == 'CP'){
					var itemPos = '0x0x0';
				}
				else{
					var itemPos = parent.frm_room.getFrameElement('HSDHTML'+itemId[1]).value;
				}
			}
			
			item.push(pgeType[0]+":"+itemId[1]+":"+parent.frm_room.getRoomId()+":"+itemPos+":"+selVal[0]+":"+selVal[1]);
		}	
	}
//alert(item);
	item.toJSON();
	
	var _data = "m=avatar2&c=buy_more_item_ajax&type="+itemType+"&item="+item+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,function(){res_buyItem(itemType)},_data);
}

function buyWishStuff(itemType,avt_id){
	$("sell_content").style.display = 'none';
	$('img_loading').style.display  = 'inline';

	var itemId   = $('btn_buy').name;
	var duration = $('duration').options[$('duration').selectedIndex].value;
	var sid_arr  = $(itemId+'-'+duration).name.split('_');
	var sid      = sid_arr[1];
	var point    = $(itemId+'-'+duration).value;
	var msg      = $('message').value;
	
	var _data    = "m=avatar2&c=buy_wish_item_ajax&avt_id="+avt_id+"&msg="+msg+"&type="+itemType+"&id="+sid+"&point="+point+"&duration="+duration+"&rn="+Math.floor(Math.random()*100000);
	httpRequest("POST",_url,true,res_buyUnitItem,_data);
}

function res_savAvtMsg(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			$("msg").style.display         = 'inline';
			$("btn_msg").style.display     = 'inline';
			$('img_sav_msg').style.display = 'none';
			
			if(response['res'] == true){
				$("msg").value = response['msg'];
				hideMe(); // Hide popup floating layer
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_buyUnitItem(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			$("sell_content").style.display = 'inline';
			$('img_loading').style.display  = 'none';
			
			if(response['res'] == true){				
				hideMe(); // Hide popup floating layer
				
				if(response['refresh'] == true){
					//window.location.reload( false );
					window.location.href = window.location.href;
				}
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_general(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){				
				alert(response['msg']);
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_delItem(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){
				if(response['refresh'] == true){
					//window.location.reload( false );
					window.location.href = window.location.href;
				}
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_saveItem(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){
				if(arguments[0] == 'buyItem'){
					window.parent.buyItem(arguments[1]);
				}
				else{
					alert(response['msg']);
				}
			}
			else{
				shwErr(response['res'],response['msg']);
			}
			
			if(response['refresh'] == true && !arguments[0]){
				//window.location.reload( false ); // Refresh the page
				window.location.href = window.location.href;
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_setDefRoom(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){				
				alert(response['msg']);
				
				$('set_default').innerHTML = "Default Room";
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_showAvt(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){				
				//alert(response['msg']);
				
				$('shw_avt').innerHTML    = "Show Portrait";
				//$('person').style.display = "inline";
				dd.elements['person'].maximizeZ();
				dd.elements['person'].show();
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_cleanUp(){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			if(response['res'] == true){				
				alert(response['msg']);
				//window.location.reload( false ); // Refresh the page
				window.location.href = window.location.href;
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_savRoomName(roomId){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			$("room_name").style.display   = 'inline';
			$("btn_save").style.display    = 'inline';
			$('img_sav_msg').style.display = 'none';
			
			if(response['res'] == true){
				$('room_name').value       = response['msg'];
				window.parent.document.getElementById('rid_'+roomId).innerHTML = response['msg'];
				hideMe(); // Hide popup floating layer
			}
			else{
				shwErr(response['res'],response['msg']);
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function res_buyItem(itemType){
  if(request.readyState == 4){
    if(request.status == 200){
    	//alert(request.responseText);
    	var response = eval('(' + request.responseText + ')');
			
			$('cart').style.display        = 'inline';
	    $('img_loading').style.display = 'none';
			
			if(response['res'] == true){				
				alert(response['msg']);
			}
			else{
				shwErr(response['res'],response['msg']);
			}
			
			if(response['refresh'] == true){
				//window.location.reload( false ); // Refresh the page
				if(itemType == 'item'){
					window.location.href = _url+"?m=avatar2&c=shw_avt_room&goto=shop";
				}
				else{
					window.location.href = _url+"?m=avatar2&c=shw_avt_portrait&goto=shop";
				}
			}
    }
    else{
    	shwErr('err_comm');
    }
  }
}

function shwErr(errCode){
	if(errCode == 'err_comm'){
		alert("A problem occurred with communicating between "+
          "the XMLHttpRequest object and the server program.");
	}
	else if(errCode == 'err_auth'){
		alert(arguments[1]);
		parent.location.href = _url+"?m=account&c=show_login";
	}
	else if(errCode == 'err'){
		alert(arguments[1]);
	}
}
