
var liveHelp = false; //聊天;

/* *
 * 添加商品到购物车
 */
function addToCart(goodsId,sellerId)
{
	//alert(goodsId);
  var goods        = new Object();
  var spec = "";
  //var fittings_arr = new Array();
  var number = 1;
  var formBuy      = document.forms['FORMBUY'];

  // 检查是否有商品规格
  if (formBuy)
  {
    if(formBuy.elements['spec']){
		spec = formBuy.elements['spec'].value;
	}
	

    if (formBuy.elements['number'])
    {
      number = formBuy.elements['number'].value;
	  if(number < min_quantity){
		  alert('Quantity error');
		  return;
	  }
    }
  }
  
  goods.spec     = spec;
  goods.goods_id = goodsId;
  goods.number   = number;
  goods.seller_id = sellerId;
  goods.is_bind = 0;
  //alert(goods.toJSONString());
  
  Ajax.call('/flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
}

/* *
 * 添加绑定商品到购物车
 */
function addBindToCart(goodsId,sellerId)
{
	//alert(goodsId);
  var goods        = new Object();
  var spec = "";
  //var fittings_arr = new Array();
  var number = 1;
 
  if (document.getElementById('bind_num_'+goodsId))
 {
  number = document.getElementById('bind_num_'+goodsId).value;
 
 }
  
  goods.spec     = '';
  goods.goods_id = goodsId;
  goods.number   = number;
  goods.seller_id = sellerId;
  goods.is_bind = 1;
  
  Ajax.call('/flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
}

/* *
 * 处理添加商品到购物车的反馈信息
 */
function addToCartResponse(result)
{
  if (result.error > 0)
  {
    
    alert(result.message);
  }
  else
  {
    var cart_count = document.getElementById('cart_count');
	/*
    if (cart_count)
    {
      cart_count.innerHTML = result.content;
    }
	*/
	write_log(3);
	location.href = '/flow.php';

  }
}


/**
* 根据是否登录 处理头部信息
*/
function openLiveHelp(str){
	window.open('http://service.tradetang.com/livehelp.php?'+str, 'chat54050872', 'width=585,height=390,menubar=no,scrollbars=0,resizable=1');
}

function isLogin(return_fun){
	if(!return_fun)return_fun=isLoginResponse;
	if(location.search){
		
		if(!document.getCookie('PSESSION')){
			var psession = location.search.substring(1);
			var psession_array = psession.split("=");
			var name = psession_array[0];
			var value = psession_array[1];
			if(name=="PSESSION"){
				Ajax.call('/transport.php?act=valid_area', '', '', 'POST', 'JSON');
			}
		}
			
	}
	
	if(!document.getCookie('PSESSION')){
		if(document.getCookie('login_name')){
			var email = document.getCookie('login_name');
			Ajax.call('/valid_user.php', 'email='+email, '', 'POST', 'JSON');
		}
	}
	
	if(!document.getCookie('PSESSION')){
		Ajax.call('/transport.php?act=valid_country', '', '', 'POST', 'JSON');
	}	
	
	Ajax.call('/transport.php?act=is_login', '', return_fun, 'GET', 'JSON');
}

var department = 1;

function isLoginResponse(result){
	if(result.valid == 1){
		/*已登录*/	
		
		if(document.getElementById('welcome_info')){
			document.getElementById('welcome_info').innerHTML = 'Welcome to tradetang '+ result.user_name + '&nbsp;  <a href="http://www.tradetang.com/sign.php?act=logout">logout</a>&nbsp;&nbsp;';
		}
		//document.getElementById('login_message').innerHTML = '<a href="http://www.tradetang.com/sign.php?act=logout">logout</a> ';
		
		if(liveHelp == true){
			/*var script = document.createElement('script');
			script.src = 'http://service.tradetang.com/livehelp_js.php?department='+department+'&serversession=1&pingtimes=15&email='+result.email+'&username='+result.user_name;
			document.body.appendChild(script);*/
			
			document.getElementById('livehelp').innerHTML = "<a href=\"javascript:openLiveHelp('department="+department+"&cslheg=1&serversession=1&email="+result.email+"&username"+result.user_name+"')\" >LeaveMessage</a>";
		}
		
	}else{
		/*未登录*/
		//document.getElementById('login_message').innerHTML = '<a href="http://www.tradetang.com/sign.php?act=register">Join Now</a> | <a href="http://www.tradetang.com/sign.php?act=login" >Login</a> ';
		if(liveHelp == true){
			/*var script = document.createElement('script');
			script.src = 'http://service.tradetang.com/livehelp_js.php?department='+department+'&serversession=1&pingtimes=15';
			document.body.appendChild(script);*/
			
			document.getElementById('livehelp').innerHTML = "<a href=\"javascript:openLiveHelp('department="+department+"&cslheg=1&serversession=1')\" >LeaveMessage</a>";
		}
	}	
}

/**
* 返回购物车中商品数量
*/
function getCartNumber(){
	Ajax.call('/transport.php?act=cart_number', '', getCartNumberResponse, 'GET', 'JSON');
}

function getCartNumberResponse(result){
	document.getElementById('cart_count').innerHTML = result.content;
}




function login_popup_request(arg){
	var return_url = '';
	if(arg){
		
		var goods_id = arg[0];
		var seller_id = arg[1];
		
		return_url = '/popup.php?act=send_message&goods_id='+goods_id
				 +'&seller_id='+seller_id;
		return_url = encodeURIComponent(return_url);
	}		 
	//alert(return_url);
	Ajax.call('/popup.php?act=login', 'return_url='+return_url, login_popup_response, 'GET', 'JSON');
	
	
}

function login_popup_response(result){
	eval_script(result.content);
	popup = new Popup();
	popup.init(result.content);
}




function check_is_login(return_fun,arg){
	Ajax.call('/transport.php?act=is_login', '', function(return_fun,arg){
														  return function(result){
															  check_is_login_response(result,return_fun,arg)
														  }
												 }(return_fun,arg), 'GET', 'JSON');
}

function check_is_login_response(result,return_fun,arg){
	if(result.valid == 1){
		return_fun(arg);	
	}else{
		login_popup_request(arg);	
	}
}

var popup = null;

function send_message(goods_name,goods_sn,goods_id,seller_id,cat_id){
	check_is_login(send_message_popup,arguments);
}



function send_message_popup(arg){
	
	
	var goods_id = arg[0];
	var seller_id = arg[1];
	
	var arg = 'goods_id='+goods_id
			 +'&seller_id='+seller_id;
	
	Ajax.call('/popup.php?act=send_message', arg, send_message_popup_response, 'POST', 'JSON');
}

 
function send_message_popup_response(result){
	eval_script(result.content);
	popup = new Popup();
	popup.init(result.content);
}


function close_popup_window(){
	popup.remove();
}


function collect_goods(goods_name,goods_sn,goods_id,seller_id,seller_name,goods_img,goods_price){
	var arg = 'goods_name=' + goods_name
			 +'&goods_sn=' + goods_sn
			 +'&goods_id=' + goods_id
			 +'&seller_id=' + seller_id
			 +'&seller_name=' + seller_name
			 +'&goods_img=' + goods_img
			 +'&goods_price=' + goods_price;	
	Ajax.call('/transport.php?act=collect_goods', arg, collect_goods_response, 'POST', 'JSON');
}

function collect_goods_response(result){
	if(result.valid == 0){
		var url = document.URL;
		location.href = 'http://www.tradetang.com/sign.php?act=login&return_url='+url;
	}else if(result.valid == 1){
		write_log(44);
		alert('Add to my favorite product success');
	}else if(result.valid == 2){
		alert('Sorry, the same product has existed in your favorite list');
	}
}

function collect_category(cat_id , cat_name){
	var arg = 'cat_id=' + cat_id
			 +'&cat_name=' + cat_name;
	Ajax.call('/transport.php?act=collect_category', arg, collect_category_response, 'POST', 'JSON');
}

function collect_category_response(result){
	if(result.valid == 0){
		//alert('Your don\'t have login.');
		var url = document.URL;
		location.href = 'http://www.tradetang.com/sign.php?act=login&return_url='+url;
	}else if(result.valid == 1){
		write_log(46);
		alert('Add to my favorite category success');
	}else if(result.valid == 2){
		alert('Sorry, the same category has existed in your favorite list');
	}
}

function collect_seller(seller_id, seller_name){
	var arg = 'seller_id=' + seller_id
			 +'&seller_name=' + seller_name;
	Ajax.call('/transport.php?act=collect_seller', arg, collect_seller_response, 'POST', 'JSON');
}

function collect_seller_response(result){
	if(result.valid == 0){
		var url = document.URL;
		location.href = 'http://www.tradetang.com/sign.php?act=login&return_url='+url;
	}else if(result.valid == 1){
		write_log(45);
		alert('Add to my favorite seller success');
	}else if(result.valid == 2){
		alert('Sorry, the same seller has existed in your favorite list');
	}
}


var old_keyword = null;
var current_keyword_i = -1;



function show_search_keyword_list_response(result,word,obj){
	if(result.length == 0){
		remove_search_keyword_list();
		return;
	}
	if(!document.getElementById('list')){
		var div = document.createElement('div');
		div.id = 'list';
		div.className = 'listkey';
		div.style.width = obj.offsetWidth + 'px';
		div.style.top = findPosY(obj)+obj.offsetHeight+'px';
		div.style.left = findPosX(obj)+'px';
		document.body.appendChild(div);
	}
		
	var str = '<div><dl>';	
	for(var i = 0 ; i < result.length; i++){
		var reg = new RegExp("("+word+")","i");
		str += '<dd><a href="javascript:void(0)"><p style="float:left; margin:0">'+result[i].k.replace(reg,'<span class="redbold">$1</span>')+'</p></a></dd>';
	}
	str += '</dl></div><iframe class="iframeBackStyle" style="height:'+(22*result.length)+'px"></iframe>';
	
	document.getElementById('list').innerHTML = str;
	search_keywrod_bind(obj);
}



function remove_search_keyword_list(){
	if(document.getElementById('list')){
		current_keyword_i = -1;
		document.body.removeChild(document.getElementById('list'));
	}
}




function send_message_request(f){
	var formmsg = f;
	var goods_id = formmsg.goods_id.value;
	var seller_id = formmsg.seller_id.value;
	var comment = formmsg.comment.value;
	if(comment == ""){
		alert('please enter comment');	
		return false;
	}	
	var arg = 'goods_id='+goods_id
			 +'&seller_id='+seller_id
			 +'&comment='+comment;
	//alert(arg);
	Ajax.call('/transport.php?act=send_message', arg, send_message_response, 'POST', 'JSON');
	if(!document.getElementById('popup_content')){
		var content = '<div id="popup_content"></div>';
		popup = new Popup();
		popup.init(content);
	}
	document.getElementById('popup_content').innerHTML = '<div class="w_gg"><img src="'+image_url+'/images/loading.gif"/></div>';
	return false;
}

function send_message_response(result){
	if(result.valid == 0){
		popup.remove();
		login_popup_request();
		
	}else if(result.valid == 1){
		write_log(23);
		if(document.FORMMSG)document.FORMMSG.comment.value='';
		document.getElementById('popup_content').innerHTML = '<div class="w_gg"><img src="'+image_url+'/images/text.gif" />Note: Message sent successfully.<br/>'
																+'<input type="button" value="Close" onclick="close_popup_window();"></div>';
	}
	
}




function write_log(action_id){
	var str = "action="+action_id;
	Ajax.call("/log.php", str , "", "GET", "TEXT");
	
}


function write_user_cookie(){
	var tag = document.getCookie('tag');
	if(tag == 001){
	}else{
		var arg = location.search;
		if(arg == '?src=true'){
			document.setCookie('tag','001',60*60*24*365*1000);
		}else{
			Ajax.call('/transport.php?act=get_valid_area', '', '', 'POST', 'JSON',false);
			
		}
	}
}


function menuToggle(element, eventName, handler){
	element = document.getElementById(element);
	if (element.addEventListener) {
	element.addEventListener(eventName, handler, false);
	} else {
	element.attachEvent("on" + eventName, handler);
	}
}


















