//String函数之LTrim(),用于清除字符串开头的空格\换行符\回车等
function ltrim(str) {
        var pattern = new RegExp("^[\\s]+","gi");
        return str.replace(pattern,"");
}
//String函数之RTrim(),用于清除字符串结尾的空格\换行符\回车等
function rtrim(str) {
        var pattern = new RegExp("[\\s]+$","gi");
        return str.replace(pattern,"");
}


//String函数之Trim(),用于清除字符串开头和结尾部分的空格\换行符\回车等
function trim(str) {
        return rtrim(ltrim(str));
}
//如果输入量字符串str通过验证返回true,否则返回false
function isInteger(str){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}

var xmlHttp;
function createXmlHttp() {
    if (window.XMLHttpRequest) {
       xmlHttp = new XMLHttpRequest();                  //FireFox、Opera等浏览器支持的创建方式
    } else {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
    }
}
//道具购买
function shopping(shop_id,shop_num){
	createXmlHttp();
	var num=document.getElementById(shop_num).value;
	if(num==""){
		alert("请输入旗帜数量!");
		return false;	
	}
	var url = "/shop/shopping/Shopping/shop_id/"+shop_id+"/num/"+num;
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				if(info.length<40){
					alert(info);
				}
				else {
					question = confirm(info)
					if (question != "0"){
						shopping_in(shop_id,num);
					}
				}
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}

function shopping_in(shop_id_in,num_in){
	createXmlHttp();
	var url = "/shop/shopping/Myshop/shop_id/"+shop_id_in+"/num/"+num_in+"/t="+Math.random();
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				alert(info);
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}

//实物道具购买
function shopping1(shop_id,shop_num){
	createXmlHttp();
	var num=document.getElementById(shop_num).value;
	if(num==""){
		alert("请输入旗帜数量!");
		return false;	
	}
	var url = "/shop/shopping/Shopping/shop_id/"+shop_id+"/num/"+num;
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				if(info.length<40){
					alert(info);
				}
				else {
					question = confirm(info)
					if (question != "0"){
						shopping_in1(shop_id,num);
					}
				}
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}

function shopping_in1(shop_id_in,num_in){
	createXmlHttp();
	var url = "/shop/shopping/Myshop/shop_id/"+shop_id_in+"/num/"+num_in+"/t="+Math.random();
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				alert(info);
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}


//道具赠送
function winopen(shop_id,user_id){
	if(user_id==0){
		alert("对不起，进行道具[赠 送]时请您先登陆!");
		return false;
	}
	window.open ("/shop/shopping/giveas/shop_id/"+shop_id, "newwindow", "height=250, width=510, toolbar =no, menubar=no, scrollbars=no, resizable=no,location=no, status=no, top=250, left=300")
}
function giveas(shop_id){
	createXmlHttp();
	var giveasuser=document.getElementById("giveasuser").value;
	var giveasnum=document.getElementById("giveasnum").value;
	if(!isInteger(giveasnum)){
		alert("请输入整数!");
		return false;
	}
	if(giveasuser==""){
		alert("请输入爱赠人的用户名!");
		return false;	
	}
	if(giveasnum==""){
		alert("请输入要赠送的旗帜数量!");
		return false;	
	}
	var url = "/shop/shopping/giveasok/shop_id/"+shop_id+"/giveasnum/"+giveasnum+"/giveasuser/"+giveasuser+"/t/"+Math.random();
	
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				if(info.length<40){
					alert(info);
				}
				else {
					question = confirm(info)
					if (question != "0"){
						shopping_giveas(shop_id,giveasnum);
					}
				}
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}

function shopping_giveas(shop_id,giveasnum){
	createXmlHttp();
	var url = "/shop/shopping/giveasshop/shop_id/"+shop_id+"/num/"+giveasnum+"/t/"+Math.random();	
	if(xmlHttp){	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status==200){
				var area = xmlHttp.responseText;
				info = trim(area);
				window.close("/shop/shopping/giveas");
			}
		} 
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
}