var SeachWordText = 'search the store name, e.g. example.com';

function GoSearch(type)
{
	if(type=='2')
	{
		if($('#Foot_SeachWord').val()=='' || $('#Foot_SeachWord').val()==SeachWordText)
		{
			ShowTempTip('please enter key words!');
			$('#Foot_SeachWord').val('');
			$('#Foot_SeachWord').focus();
		}
		else location.href="/s/"+$('#Foot_SeachWord').val();
	}
	else
	{
		if($('#SeachWord').val()=='' || $('#SeachWord').val()==SeachWordText)
		{
			ShowTempTip('please enter key words!');
			$('#SeachWord').val('');
			$('#SeachWord').focus();
		}
		else location.href="/s/"+$('#SeachWord').val();
	}
	return false;
}

function ClearWord(type)
{
	if(type=='2')
	{
		$('#Foot_SeachWord').val('');
		$('#Foot_SeachWord').focus();
	}
	else if($('#SeachWord').val()==SeachWordText)
	{
		$('#SeachWord').val('');
		$('#SeachWord').focus();
	}
	return false;
}

function ResetAuthCode(name)
{
	$("#"+name).attr('src', '/getValidateImage.php?' + Math.random());
	$("#"+name).show();
}

function CheckUserName()
{
	var re2 = /[ａ-ｚＡ-Ｚ０-９！＠＃￥％＾＆＊（）＿＋｛｝［］｜：＂＇；．，／？＜＞｀～　]/;
	if($('#Reg-UserName').val()=='')
	{
		$('#TipUserName').html('<font color="#FF0000">please enter display name</font>');
		return false;
	}
	else if(re2.test($('#Reg-UserName').val()))
	{
		$('#TipUserName').html('<font color="#FF0000">some characters are not available</font>');
		$('#Reg-UserName').focus();
		return false;
	}
	else if($('#Reg-UserName').val().length<4 || $('#Reg-UserName').val().length>=20)
	{
		$('#TipUserName').html('<font color="#FF0000">4-20 characters only</font>');
		$('#Reg-UserName').focus();
		return false;
	}
	else
	{
		return QueryCheck('UserName', $('#Reg-UserName').val());
	}
}

function CheckEmail()
{
	var m=/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/;
	if($('#Reg-Email').val()=='')
	{
		$('#TipEmail').html('<font color="#FF0000">please enter email</font>');
		return false;
	}
	else if(!m.test($('#Reg-Email').val()))
	{
		$('#TipEmail').html('<font color="#FF0000">please check your email address</font>');
		$('#Reg-Email').focus();
		return false ;
	}
	else
	{
		QueryCheck('Email',$('#Reg-Email').val());
	}
}


function SubSignUp()
{
	var UserName = $("#Reg-UserName").val();
	var Email    = $("#Reg-Email").val();
	var UserPass = $("#Reg-UserPass").val();
	var StoreID  = $("#Reg-StoreID").val();
	var m=/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/;

	if(UserName=='')
	{
		ShowTempTip("please enter display name");
		$('#Reg-UserName').focus();
		return false;
	}
	else if(UserName.length<4 || UserName.length>=20)
	{
		ShowTempTip('please enter user name');
		$('#Reg-UserName').focus();
		return false;
	}
	else if(Email=='')
	{
		ShowTempTip("please enter email");
		$('#Reg-Email').focus();
		return false;
	}
	else if(!m.test(Email))
	{
		$('#TipEmail').html('please check your email address');
		$('#Reg-Email').focus();
		return false ;
	}
	else if(UserPass=='')
	{
		ShowTempTip("please enter your password");
		$('#Reg-UserPass').focus();
		return false;
	}
	else return true;
	/*
	var PostStr = "Email="+encodeURIComponent(Email)+'&UserName='+encodeURIComponent(UserName)+'&UserPass='+encodeURIComponent(UserPass);
	if(typeof(StoreID)!='undefined' && StoreID>0) PostStr = PostStr+"&StoreID="+StoreID;

	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Users&Opt=SignUp',
		dataType:"html",
		data: PostStr,
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){$("#Reg-Submit").attr('disabled','true');ShowTempTip('Posting...');},
		complete:function(){$("#Reg-Submit").removeAttr("disabled");},
		success:
		function(data)
		{
			if(data==1)
			{
				ShowTempTip("sign up ok!");
				
				if($("input[id=SetHomePage][@type=checkbox][@checked]")) AddFavorite();
				window.location = HostURL+"My";
			}
			else if(data=='-2')
				ShowTempTip("'please complete the form'");
			else
				ShowTempTip("Please try again later");
		}
	});
	return false;
	*/
}

function SubLogin()
{
	var Email      = $("#Login-Email").val();
	var UserPass   = $("#Login-UserPass").val();
	var CookieTime = $('input[@type=checkbox][@id=CookieTime][@checked]').val();

	if(Email=='')
	{
		ShowTempTip("please enter email");
		$('#Login-Email').focus();
		return false;
	}
	else if(UserPass=='')
	{
		ShowTempTip("please enter your password");
		$('#Login-UserPass').focus();
		return false;
	}
	if(typeof(CookieTime)!="undefined")
		var PostStr = "CookieTime="+CookieTime+"&Email="+Email+'&UserPass='+encodeURIComponent(UserPass);
	else
		var PostStr = "Email="+Email+'&UserPass='+encodeURIComponent(UserPass);

	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Users&Opt=Login',
		dataType:"json",
		data: PostStr,
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){$("#Login-Submit").attr('disabled','true');ShowTempTip('Posting...');},
		complete:function(){$("#Login-Submit").removeAttr("disabled");},
		success:
		function(json)
		{
			if(json.Result==1)
			{
				$.cookie('PubUserKey', json.PubUserKey, {expires: 365, path: '/'});
				window.location = "/?CH=My";
			}
			else if(json.Result=='-2') {}
			else
			{
				$('#Login-UserPass').val('');
				$('#Login-UserPass').focus();
				$.cookie("PubUserKey", null, {path: '/'});
			}
			ShowTempTip(json.Tip);
		}
	});
	return false;
}

function ClickCoupon(CouponID)
{
	$.get(AjaxURL+'Mode=Coupon&Opt=Click&CouponID='+CouponID, function(data){});
}

function GoURL(URL,CouponType)
{
  if(CouponType == '1')
  {
	var TempStr = '/t/9';
	var TempURL = URL.substr(4);
	URL = TempStr + TempURL;
	window.open(URL);
  }
  else
	  window.open(URL);
}

$(document).ready(function(){
	$("a.link_reveal").click(function(){
		new_win = window.open($(this).attr("href"),'_blank','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
		if(new_win) {new_win.blur();}
		return false;
	});
	if($('#SeachWord').val()=='') $('#SeachWord').val(SeachWordText);

	$("#SeachWord").autocomplete(AjaxURL+"Mode=Store&Opt=Search", {
		minChars:1,
		max:10,
		cacheLength:0,
		selectFirst:false,
		autoFill: false,
		scroll: false,
		width: SearchWidth,
		dataType:'json',
		parse: function(json){	
			return $.map(json.Data, function(row){
				return {
				data:row,
				value:(row.StoreID>0) ? row.DomainName : '',
				result:(row.StoreID>0) ? row.StoreName : ''
				}
			});
		},
		formatItem: function(item, i) {
			return "<span class=right><img src="+item.LogoURL+"></span>No."+i+"<br/>store:"+item.StoreName + "<br/>domain:" + item.DomainName + "";
		}
	}).result(function(e, item) {
		try{
			if(item.DomainName!='')
			{
				OpenDialog('Loading ....');
				location.href="/store/"+item.DomainName;
			}
		}
		catch (e){}
	});

	var clip = null;
	var curobj = null;
	ZeroClipboard.setMoviePath( '/img/ZeroClipboard.swf' );
	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	clip.addEventListener('complete', function(client,text){
		$.get(AjaxURL+'Mode=Coupon&Opt=Click&CouponID='+curobj.id);
		$(curobj).text('copied, click to copy again');
	});

	// assign a common mouseover function for all elements using jQuery
	$('a.cc-code').mouseover( function() {
		curobj = this;
		clip.setText($(this).attr("couponcode"));

		// reposition the movie over our element
		// or create it if this is the first time
		if (clip.div) {
			clip.receiveEvent('mouseout', null);
			clip.reposition(this);
		} else clip.glue(this);
      
		clip.receiveEvent('mouseover', null);
	});
	//clip.addEventListener( 'onMouseUp', my_mouse_up_handler );
	function my_mouse_up_handler( client ) {
		//alert("You are now being redirected to the store website. The coupon code has been copied to your clipboard.");
		var storeWin=window.open($(curobj).attr("href"));
		if (storeWin){
			return false;
		} else {
			document.location = $(curobj).attr("href");
		}				
	}

	$('.main_coupons td a').click(
		function(event)
		{
			event.stopPropagation();
		}
	);
});


function SubscribeTip()
{
	if($.cookie('SS')=='1') return '';
	$.cookie('SS', '1', {expires: 365, path: '/'});
	OpenDialog($('#SubscribeTipForm').html(), 480, 220);
	$('#Subscribe-YourMail').focus();
}


/* 邮件订阅 */
function AddSubscribe()
{
	var StoreID  = $("#Subscribe-StoreID").val();
	var YourMail = $("#Subscribe-YourMail").val();

	if(StoreID<1)
	{
		alert('Sorry, we can not find this store id');
		return false;
	}
	else if(YourMail=='')
	{
		ShowTempTip("please input your mail");
		$('#Subscribe-YourMail').focus();
		return false;
	}

	$.ajax({
		type:"POST",
		url: AjaxURL+'Mode=Store&Opt=AddSubscribe',
		dataType:"html",
		data: "StoreID="+StoreID+'&Email='+encodeURIComponent(YourMail),
		error: function(e) {ShowTempTip("Please try again later(a)");},
		beforeSend: function(){$("#Subscribe-Submit").attr('disabled','true');ShowTempTip('Posting...');},
		complete:function(){$("#Subscribe-Submit").removeAttr("disabled");},
		success:
		function(data)
		{
			if(data==1)
			{
				CloseDialog();
				ShowTempTip('subscribe succeed!');
			}
			else ShowTempTip("subscribe fail!");
		}
	});
	return false;
}


function GetCouponCode()
{
	$('.coupon_area').show();
	$('.link_reveal').hide();
}