var script_root = 'http://www.yicike.com/'; //全局变量,网站网址
var MVC_URL_IMAGES = script_root+'MVCPage/tpl/Public/images'; //图片地址
function showPageAjax(url,related_id,page,return_id)
{
	$.get(url,
	{
		related_id:related_id,
		page:page,
		return_id:return_id
	},
	function(data){
		$('#'+return_id).html(data);
	});
}

//转向goto.php处理参数函数
function redirect_goto(param,target)
{
	var redirect = script_root+'goto.php?q='+param;
	if(target == '_blank')
	{
		window.open(redirect);
	}
	else
	{
		window.location.href = redirect;
	}
}

//判断email地址是否合法
function isValidEmail(email)
{
	var sRE = "^[\.A-Za-z0-9_-]{1,255}@[\.A-Za-z0-9_-]+\.[A-Za-z]{2,5}$";
	var oRE = new RegExp(sRE);
	if (!oRE.test(email)) {
		return false;
	}
	return true;
}


//购买页面登录购买，转向b2c产品购买页
function ajaxLogin2Project(xform)
{
	if(typeof(xform) != 'object')
	{
		xform = document.getElementById(xform);
	}
	var product_type = xform.product_type.value;
	if(product_type != 'b2c_shop' )
	{
		if(product_type != 'taobao_shop')
		{
		var prod_id = xform.prod_id.value;
		if(prod_id == '')
		{
			alert('产品id不能为空！');
			return false;
		}
		}
	}
	else
	{
		var shopuser_id = xform.shopuser_id.value;
		if(shopuser_id == '')
		{
			alert('商家id不能为空！');
			return false;
		}
	}
	if(product_type == 'taobao')
	{
		var num_iid = xform.num_iid.value;
		var is_shop = xform.is_shop.value;
		if(num_iid == '')
		{
			alert('淘宝iid不能为空！');
			return false;
		}
	}
	//判断用户是否存在
	var email = xform.email.value;
	if(email == '')
	{
		alert('请输入邮箱或用户名！');
		xform.email.focus();
		return false;
	}
	var is_true = true;
	$.ajax({
		async:false,
		type:'POST',
	    url:script_root+'ajaxData/has_email.php',
		data:'email='+email,
		success:function(msg){
			if(msg == 'no')
			{
				alert('您尚未注册！');
				is_true = false;
				return false;
			}
		}
	});
	if(!is_true)
	{
		return false;
	}
	var email = xform.email.value;
	
	switch(product_type)
	{
		case "b2c":
				ajaxGotoRedirect(prod_id,email);
		   break;
		case "b2c_shop":
				ajaxGotoRedirectB2cShop(shopuser_id,email);
			break;
		case "taobao":
				ajaxGotoRedirectTaobao(prod_id,num_iid,is_shop,email);
			break;
		case "taobao_shop":
				ajaxGotoRedirectTaobaoShop(shopuser_id,email);
			break;
	}
	//var r_url = script_root+'redirect.php?prod_id='+prod_id+'&email='+email;
	//window.location.href=r_url;
}

//ajax注册并转向购买页面
function ajaxRegister2Project(xform)
{
	if(typeof(xform) != 'object')
	{
		xform = document.getElementById(xform);
	}
	var product_type = xform.product_type.value;
	if(product_type != 'b2c_shop')
	{
		if(product_type != 'taobao_shop')
		{
		var prod_id = xform.prod_id.value;
		if(prod_id == '')
		{
			alert('产品id不能为空！');
			return false;
		}
		}
	}
	else
	{
		var shopuser_id = xform.shopuser_id.value;
		if(shopuser_id == '')
		{
			alert('商家id不能为空！');
			return false;
		}
	}	
	email = xform.email.value;
	if(email == '')
	{
		alert('请输入邮箱或用户名！');
		xform.email.focus();
		return false;
	}
	$.ajax({
		type:'POST',
		url:script_root+'ajaxData/ajaxRegisterEmail.php',
		data:'email='+email,
		success:function(msg){
			if(msg == 'ok')
			{
				 ajaxLogin2Project(xform);
			}
			else
			{
				alert('注册失败！');
				xform.email.focus();
				return false;
			}
		}
	});
}

function ajaxGotoRedirect(prod_id)
{
	if(arguments[1])
	{
		var email = arguments[1];
	}
	var r_url = script_root+'redirect.php?prod_id='+prod_id;
	if(email)
	{
		r_url += '&email='+email;
	}
	window.location.href=r_url;
}

//转向b2c商家
function ajaxGotoRedirectB2cShop(shopuser_id)
{
	if(arguments[1])
	{
		var email = arguments[1];
	}
	var r_url = script_root+'redirect_b2c_shop.php?shopuser_id='+shopuser_id;
	if(email)
	{
		r_url += '&email='+email;
	}
	window.location.href=r_url;
}

function ajaxGotoRedirectTaobao(prod_id,num_iid)
{
	if(arguments[2])
	{
		var is_shop = arguments[2];
	}
	if(arguments[3])
	{
		var email = arguments[3];
	}
	var r_url = script_root+'redirect2.php?prod_id='+prod_id+'&num_iid='+num_iid;
	if(is_shop)
	{
		r_url += '&is_shop='+is_shop;
	}
	if(email)
	{
		r_url += '&email='+email;
	}
	window.location.href=r_url;
}

function ajaxGotoRedirectTaobaoShop(shopuser_id)
{
	if(arguments[1])
	{
		var email = arguments[1];
	}
	var r_url = script_root+'redirect_tb_shop.php?id='+shopuser_id;
	if(email)
	{
		r_url += '&email='+email;
	}
	window.location.href=r_url;
}

//显示隐藏标签
function ShowHide(ids,classes)
{
	$('.'+classes).hide();
	$('#'+ids).show();
}

//获得并显示广告剩余时间
function GetRestTime(id,endtime)
{
	var NowTime = new Date();
	var nMS =endtime - NowTime.getTime();
	var nD=Math.floor(nMS/(1000*60*60*24));
	var nH=Math.floor(nMS/(1000*60*60)) % 24;
	var nM=Math.floor(nMS/(1000*60)) % 60;
	var nS=Math.floor(nMS/1000) % 60;
	var nText = nD.toString()+'天'+nH.toString()+'小时'+nM.toString()+'分'+nS.toString()+'秒';
	document.getElementById(id).innerHTML=nText;
}

//输入框获得焦点时是否清空内容，等于默认值时清空
 function get_focus_input(xform,compare,name)
 {
	  var input_obj =  eval('xform.'+name);
	  var compare_obj = eval('xform.'+compare);
	  if(compare_obj.value == input_obj.value)
	  {
		  input_obj.value = '';
	  }
  }
  //输入框失去焦点时是否添加内容，空时添加内容
  function get_blur_input(xform,compare,name)
  {
	  var input_obj = eval('xform.'+name);
	  var compare_obj = eval('xform.'+compare);
	  if(input_obj.value == '')
	  {
		  input_obj.value = compare_obj.value;
	  }
  }
  //复制网址
copyToClipboard = function(txt) {
	if(window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
		alert('已复制到粘贴板，在 MSN 或 QQ 中粘贴，即可发送给您的好友。');
	}
	else if(navigator.userAgent.indexOf("Opera") != -1) {
		window.location = txt;
	}
	else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e) {
			alert("您的firefox安全限制限制您进行剪贴板操作，请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试");
			return false;
		}
	}
}

