// 定义 ajax loading 定时器 ID
var xajaxTimeoutId;
// 定义 ajax 使用的 IntervalId
var IntervalId;
var OpenIntervalId = null;
var CommentDivHight = new Array();
var ShareDivHeight = new Array();

function openwindow(url,name,iWidth,iHeight)
{
	var url;
	var name;
	var iWidth;
	var iHeight;
	var iTop = (window.screen.availHeight-30-iHeight)/2;
	var iLeft = (window.screen.availWidth-10-iWidth)/2;
	window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=no,resizeable=no,location=no,status=no');
}
function couponinfo(couponinfo_url)
{
	openwindow(couponinfo_url,'',610,230);
}
function AddCoupons(CouponId)
{
	if(document.getElementById(CouponId).checked == true)
	{
		// 让选择框恢复未选中的状态，然后通过 XAJAX 来改变
		document.getElementById(CouponId).checked = false;
		xajax_SelectCoupon(CouponId);
	}
	else
	{
		// 让选择框恢复选中的状态，然后通过 XAJAX 来改变
		document.getElementById(CouponId).checked = true;
		xajax_DeSelectCoupon(CouponId);
	}
}
/**
 * 显示 loading
 * 
 * @return void
 */
function ShowLoadingDiv()
{
	var topLoadingDivObj = document.getElementById("loadingTopMsgDiv");

	topLoadingDivObj.style.left = ((document.body.offsetWidth - parseInt(topLoadingDivObj.style.width)) / 2) + 'px';
	// 得到页面滚动的顶部坐标，兼容不同的浏览器
	var docScrollTop = document.body.scrollTop | document.documentElement.scrollTop;
	topLoadingDivObj.style.top = docScrollTop + 200 + 'px';

	//document.getElementById("loadingScreenMaskDiv").style.display = "block";
	topLoadingDivObj.style.display = "block";
}
/**
 * 隐藏 loading 并清除定时器
 * @return
 */
function HideLoadingDiv()
{
	clearTimeout(xajaxTimeoutId);
	var topLoadingDivObj = document.getElementById("loadingTopMsgDiv");

	//document.getElementById("loadingScreenMaskDiv").style.display = "none";
	topLoadingDivObj.style.display = "none";
}
/**
 * 设置定时器，如果 ajax 在 500ms 内没有返回则显示 loading
 * 
 * @return void
 */
function xajaxLoadingSetTimeout()
{
	// 定义 1000ms 的定时器
	xajaxTimeoutId = setTimeout("ShowLoadingDiv()", 1000);
}
/**
 * 设置 target 的 height 属性
 * @param TargetId	target ID
 * 
 * @return void
 */
function CloseTarget(TargetId)
{
	var obj = document.getElementById(TargetId);

	var height = parseInt(obj.style.height);

	height -= 20;

	if(height <= 0)
	{
		obj.style.height = 0 + "px";
		clearInterval(IntervalId);
		obj.style.display = "none";
		IntervalId = null;
		return;
	}

	obj.style.height = height + "px";
}
/**
 * 逐渐关闭target的定时器
 * 
 * @param TargetId target ID
 * @return void
 */
function CloseTargetInterval(TargetId)
{
	document.getElementById(TargetId).style.height = document.getElementById(TargetId).offsetHeight - 8 + "px";
	document.getElementById(TargetId).style.overflow = "hidden";
	IntervalId = setInterval("CloseTarget('" + TargetId + "')", 20);
}

/**
 * 设置 target 的 height 属性
 * @param TargetId	target ID
 * 
 * @return void
 */
function OpenTarget(TargetId, RealHeight)
{
	var obj = document.getElementById(TargetId);

	var height = parseInt(obj.style.height);

	height += 20;

	if(height >= RealHeight)
	{
		obj.style.height = RealHeight + "px";
		clearInterval(OpenIntervalId);
		OpenIntervalId = null;
		
		return;
	}
	
	obj.style.height = height + "px";
}

/**
 * 逐渐打开target的定时器
 * 
 * @param TargetId target ID
 * @return void
 */
function OpenTargetInterval(TargetId, RealHeight)
{
	OpenIntervalId = setInterval("OpenTarget('" + TargetId + "' , " + RealHeight + ")", 20);
}

/**
 * 当点击 comment 链接是触发的事件
 * @param CommentDivId 显示 comment 的DIV ID
 * @return
 */
function OnClickComment(CommentDivId, couponId)
{
	// 打开和关闭层的定时器都是 null 时才能执行打开或关闭操作
	if((OpenIntervalId != null) || (IntervalId != null))
		return;
	
	// 关闭已经打开的 share
	var ShareDivId = "couponShare_" + couponId;
	document.getElementById(ShareDivId).style.display = "none";
	
	var obj = document.getElementById(CommentDivId);
	
	if(obj.style.display == "block")
	{
		// 让 IntervalId 不等于 null，防止多次快速点击链接
		IntervalId = "";
		CloseTargetInterval(CommentDivId);
	}
	else
	{
		// 让 OpenIntervalId 不等于 null，防止多次快速点击链接
		OpenIntervalId = "";
		if(typeof(CommentDivHight[CommentDivId]) != "undefined")
		{
			document.getElementById(CommentDivId).style.height = 0 + "px";
			document.getElementById(CommentDivId).style.display = "block";
			
			OpenTargetInterval(CommentDivId, CommentDivHight[CommentDivId]);
		}
		else
		{
			xajax_ShowComment(couponId);
		}
	}
}

/**
 * 当点击 Share 链接时触发的事件
 * @param ShareDivId 显示 share 的DIV ID
 * @return
 */
function OnClickShare(ShareDivId, couponId)
{
	// 打开和关闭层的定时器都是 null 时才能执行打开或关闭操作
	if((OpenIntervalId != null) || (IntervalId != null))
		return;
	
	// 关闭已经打开的 comment
	var CommentDivId = "couponComment_" + couponId;
	document.getElementById(CommentDivId).style.display = "none";
	
	var obj = document.getElementById(ShareDivId);
	
	if(obj.style.display == "block")
	{
		// 让 IntervalId 不等于 null，防止多次快速点击链接
		IntervalId = "";
		CloseTargetInterval(ShareDivId);
	}
	else
	{
		// 让 OpenIntervalId 不等于 null，防止多次快速点击链接
		OpenIntervalId = "";
		
		ShareDivHeight[ShareDivId] = 215;
		document.getElementById(ShareDivId).style.height = 0 + "px";
		document.getElementById(ShareDivId).style.overflow = "hidden";
		document.getElementById(ShareDivId).style.display = "block";
		
		OpenTargetInterval(ShareDivId, ShareDivHeight[ShareDivId]);
	}
}

function SubmitComment(NewCommentTextId, couponId)
{
	var CommentStr = document.getElementById(NewCommentTextId).value;
	
	if(CommentStr == "")
	{
		alert(document.getElementById("commentjsalert").commentempty);
		return false;
	}
	xajax_AddComment(CommentStr, couponId);
}

function xajaxFailed()
{
	HideLoadingDiv();
}

function SubmitShare(couponId)
{
	var ToId = "shareTo_" + couponId;
	var FromId = "shareFrom_" + couponId;
	var YourNameId = "shareYourName_" + couponId;
	
	var fromEmail = document.getElementById(FromId).value;
	var toEmail = document.getElementById(ToId).value;
	var yourName = document.getElementById(YourNameId).value;
	
	// 检查数据是否有效
	var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;   
	if(reg.test(toEmail) == false)
	{
		alert(document.getElementById("sharejsalert").attributes['toemailinvalid'].nodeValue);
		//alert("The \"To\" email address is invalid");
		return false;
	}
	if(reg.test(fromEmail) == false)
	{
		alert(document.getElementById("sharejsalert").attributes['fromemailinvalid'].nodeValue);
		return false;
	}
	if(yourName == "")
	{
		alert(document.getElementById("sharejsalert").attributes['yournameempty'].nodeValue);
		return false;
	}
	
	xajax_SendShare(fromEmail, toEmail, yourName, window.location.toString(), couponId);
	
	return;
}