function GetStoreDesc(StoreID)
{
	$('#Store-Desc').html('<img src="/img/loading.gif" border=0 align=absmiddle>Loading ...');
	$.get(AjaxURL+'Mode=Store&Opt=GetDesc&StoreID='+StoreID, function(data){$('#Store-Desc').html(data);});
	return false;
}

function EditStorePanel(StoreID)
{
	OpenDialog($('#EditStore').html(), 480, 400);

	$('#Store-UploadLogo').upload({
		name: 'UploadLogo',
		params: {StoreID:StoreID},
		method: 'post',
		enctype: 'multipart/form-data',
		action: AjaxURL+'Mode=Store&Opt=Upload',
		onSubmit: function() {
			$('#Store-LogoProgress').html('<img src="/img/loading.gif" border=0 align=absmiddle>Uploading image...');
		},
		onComplete: function(data) {
			var re = '';
			re = eval('(' + data + ')');
			if(re.Result=='0')
				$('#Store-LogoProgress').html('<font color="#FF0000">Upload failure</font>');
			else
			{
				$('#Store-LogoProgress').html('<font color="#339900">Upload successful</font>');
				ShowTempTip('<center><img src="'+re.ImageURL+'" border=0><br /><font color="#339900">Upload successful</font></center>');
			}
		}
	});
}

function EditStore()
{
	var StoreID   = $("#Store-StoreID").val();
	var StoreName = $("#Store-StoreName").val();
	var OutURL    = $("#Store-OutURL").val();
	var Introduce = $("#Store-Introduce").val();

	if(StoreID<1)
	{
		alert('Sorry, we can not find this store id');
		return false;
	}
	else if(StoreName=='')
	{
		ShowTempTip("please input StoreName");
		$('#Store-StoreName').focus();
		return false;
	}
	else if(OutURL=='')
	{
		ShowTempTip("please input OutURL");
		$('#Store-OutURL').focus();
		return false;
	}
	else if(Introduce=='')
	{
		ShowTempTip("please input Introduce");
		$('#Store-Introduce').focus();
		return false;
	}
	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Store&Opt=Edit',
		dataType:"html",
		data: "StoreID="+StoreID+'&StoreName='+encodeURIComponent(StoreName)+'&OutURL='+encodeURIComponent(OutURL)+'&Introduce='+encodeURIComponent(Introduce),
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){$("#Store-Submit").attr('disabled','true');ShowTempTip('Posting...');},
		complete:function(){$("#Store-Submit").removeAttr("disabled");},
		success:
		function(data)
		{
			if(data==1)
				location.reload();
			else
				ShowTempTip("post fail!");
		}
	});
	return false;
}

function DelStore(StoreID)
{
	if(!confirm('Are you sure delete this store ?')) return false;
	ShowTempTip('please wait...');
	$.get(
		AjaxURL+'Mode=Store&Opt=Del&StoreID='+StoreID,
		function(data)
		{
			if(data==1)
			{
				ShowTempTip('delete succeed!');
				$('#Store-'+StoreID).hide();
			}
			else ShowTempTip("delete fail!");
		}
	);
	return false;
}

function ListStoreComment(StoreID)
{
	var json = new Array();
	$('#CommentHtml').html('<li><img src="/img/loading.gif" align=absmiddle>&nbsp;Loading....<br /></li>');

	$.getJSON(
		AjaxURL+'Mode=Store&Opt=ListComment&StoreID='+StoreID+'&'+GetTime(),
		function(json)
		{
			try
			{
				var CommentHtml = ''; var oTpl = ''; var Tpl = $("#CommentTpl").html();
				$.each(json.Data, function(i,item){
					oTpl =  Tpl.replace('#StoreID#'    , typeof(item.StoreID)=='undefined'   ? '' : item.StoreID);

					oTpl = oTpl.replace('#UserURL#'    , (item.UserID>0) ? '/profile/'+item.UserName : 'javascript:;');
					oTpl = oTpl.replace('#UserName#'   , typeof(item.UserName)=='undefined'  ? '' : item.UserName);
					oTpl = oTpl.replace('#PubTime#'    , typeof(item.PubTime)=='undefined'   ? '' : item.PubTime);
					oTpl = oTpl.replace('#Comment#'    , typeof(item.Comment)=='undefined'   ? '' : item.Comment);

					CommentHtml = CommentHtml+oTpl;
				});
				$('#CommentHtml').html(CommentHtml);
			}
			catch (e)
			{
				$('#CommentHtml').html('<li>No Access<br /></li>');
			}
		}
	);
}

function AddStoreComment()
{
	var StoreID  = $("#SC-StoreID").val();
	var NickName = (typeof($("#SC-NickName").val())=='undefined' || $("#SC-NickName").val()=='') ? 'Anonymous' : $("#SC-NickName").val();
	var Comment  = $("#SC-Comment").val();
	var Email    = $("#SC-Email").val();
	var EmailMe  = $("input[id=EmailMe][@type=checkbox][@checked]").val();
	var WebSite  = $("#SC-WebSite").val();

	if(StoreID<1)
	{
		alert('Sorry, we can not find this store id');
		return false;
	}
	else if(NickName=='')
	{
		ShowTempTip("please input nickname");
		$('#SC-NickName').focus();
		return false;
	}
	else if(Comment=='')
	{
		ShowTempTip("please input comment");
		$('#SC-Comment').focus();
		return false;
	}
	var PostStr = "StoreID="+StoreID+'&UserName='+encodeURIComponent(NickName)+'&Comment='+encodeURIComponent(Comment);
	if(typeof(Email)!='undefined' && Email!='') PostStr = PostStr+((typeof(EmailMe)!='undefined') ? "&Email="+encodeURIComponent(Email)+'&EmailMe='+EmailMe : "&Email="+encodeURIComponent(Email));
	if(typeof(WebSite)!='undefined' && WebSite!='') PostStr = PostStr+"&WebSite="+WebSite;

	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Store&Opt=AddComment',
		dataType:"html",
		data: PostStr,
		error: function(e) {ShowTempTip("Please try again later(as)");},
		beforeSend: function(){$("#SC-Submit").attr('disabled','true');ShowTempTip('Posting...');},
		complete:function(){$("#SC-Submit").removeAttr("disabled");},
		success:
		function(data)
		{
			if(data==1)
			{
				$("#comment_list").show();
				$("#DC-Comment").val('');
				ShowTempTip('comment succeed!');
				ListStoreComment(StoreID);
			}
			else ShowTempTip("post fail!");
		}
	});
	return false;
}

function SubClaim()
{
	if($('#DomainName').val()=='')
	{
		ShowTempTip("Enter your store's url e.g. amazon.com");
		$('#DomainName').focus();
		return false;
	}
	else
	{
		ShowTempTip('wating...');
		return true;
	}
}

function AddTags()
{
	var TagID = GetTime();
	var e = document.getElementsByName('Tags[]');
	var Tpl = '<div id="Tag'+TagID+'"><input name="Tags[]" type="text" size="30" />&nbsp;&nbsp;<a href="JavaScr'+'ipt:;" onclick="return DeleteTag(\''+TagID+'\');">delete</a></div>';
	if(e.length < 10)
	{
		$("#TagHtml").append(Tpl);
	}
	else alert('10 max !');
	return false;
}
function DeleteTag(TagID)
{
	$("#Tag"+TagID).hide();
	return false;
}

function VerifyBadge(StoreID)
{
	if(StoreID<1)
	{
		ShowTempTip("Sorry, we can not find this store id");
		return false;
	}
	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Store&Opt=VerifyBadge',
		dataType:"html",
		data: "StoreID="+StoreID,
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){$("#Verify-Submit").attr('disabled','true');OpenDialog('<br /><center><img src="/img/loading.gif" align=absmiddle>&nbsp;please waiting...</center>', 300, 50);},
		complete:function(){$("#Verify-Submit").removeAttr("disabled");CloseDialog();},
		success:
		function(data)
		{
			if(data==1)
			{
				ShowTempTip('verify succeed!');
				location.href='/?CH=My.Stores';
			}
			else
				ShowTempTip("verify fail!");
		}
	});
	return false;
}

function AddSurvey(StoreID, Type)
{
	if(StoreID<1)
	{
		ShowTempTip("Sorry, we can not find this store id");
		return false;
	}
	else if(Type=='')
	{
		ShowTempTip("Sorry, we can not find type");
		return false;
	}
	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Store&Opt=AddSurvey',
		dataType:"json",
		data: 'StoreID='+StoreID+'&Type='+Type,
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){ShowTempTip('Posting...');},
		complete:function(){},
		success:
		function(json)
		{
			if(json.Result==1)
			{
				ShowTempTip('Thank you for your opinion!');
				$('#SurveyOpt').html('yes:'+json.RecomYes+'('+json.PreRecomYes+'%)<br />No:'+json.RecomNo+'('+json.PreRecomNo+'%)');
			}
			else
				ShowTempTip("survey fail!");
		}
	});
	return false;
}


function OpenAddCoupon()
{
	$('#EditCouponArea').show();
	$('#EditCouponBtn').hide();
	
	$('#Coupon-UploadLogo').upload({
		name: 'UploadLogo',
		method: 'post',
		enctype: 'multipart/form-data',
		action: AjaxURL+'Mode=Coupon&Opt=Upload',
		onSubmit: function() {
			$('#Coupon-LogoProgress').html('<img src="/img/loading.gif" border=0 align=absmiddle>Uploading image...');
		},
		onComplete: function(data) {
			var re = '';
			re = eval('(' + data + ')');
			if(re.Result=='0')
				$('#Coupon-LogoProgress').html('<font color="#FF0000">Upload failure</font>');
			else
			{
				$('#Coupon-LogoProgress').html('<img src="'+re.ImageURL+'" border=0><br /><font color="#339900">Upload successful</font>');
			}
		}
	});
}

function OpenAddDeal()
{
	$('#EditDealArea').show();
	$('#EditDealBtn').hide();

	$('#Deal-UploadLogo').upload({
		name: 'UploadLogo',
		method: 'post',
		enctype: 'multipart/form-data',
		action: AjaxURL+'Mode=Deal&Opt=Upload',
		onSubmit: function() {
			$('#Deal-LogoProgress').html('<img src="/img/loading.gif" border=0 align=absmiddle>Uploading image...');
		},
		onComplete: function(data) {
			var re = '';
			re = eval('(' + data + ')');
			if(re.Result=='0')
				$('#Deal-LogoProgress').html('<font color="#FF0000">Upload failure</font>');
			else
			{
				$('#Deal-LogoProgress').html('<img src="'+re.ImageURL+'" border=0><br /><font color="#339900">Upload successful</font>');
			}
		}
	});
}