/* *
 * 顯示載入信息
 */
function showLoader()
{
  $jQuery('#loader').show();
}

/* *
 * 隱藏載入信息
 */
function hideLoader()
{
	$jQuery('#loader').hide();
}

document.getCookie = function(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");

  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return decodeURIComponent(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

document.setCookie = function(sName, sValue, sExpires)
{
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if (sExpires != null)
  {
    sCookie += "; expires=" + sExpires;
  }

  document.cookie = sCookie;
}

document.removeCookie = function(sName, sValue)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function email_subscribe()
{
  if (check_email())
  {
    new Ajax.Request('common.php?act=email_list', {
                     method: 'get',
                     parameters: 'job=add&email=' + $('user_email').value,
                     onSuccess: rep_email_subscribe});
  }
}

function rep_email_subscribe(result)
{
  var result = result.responseText;
  alert(result);
  $('user_email').value = '';
}

function email_cancel()
{
  if (check_email())
  {
    new Ajax.Request('common.php?act=email_list', {
                     method: 'get',
                     parameters: 'job=del&email=' + $('user_email').value,
                     onSuccess: rep_email_cancel});
  }
}

function rep_email_cancel(result)
{
  var result = result.responseText;
  alert(result);
  $('user_email').value = '';
}

function check_email()
{
  var msg = '';
  if ($('user_email').value == '')
  {
    msg += '- Email' + '\n';
  }
  else
  {
    REstr = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
    if (! REstr.test($F('user_email')))
    {
        msg += '- incorrect email format!' + '\n';
    }
  }

  if (msg.length > 0)
  {
    alert(msg);
    return false;
  }
  else
  {
    return true;
  }
}

function checkHTMLTag(checkObj,alertMsg)
{
  var checkStr = checkObj.value;
  var counts = 0;
  for(i = 0;i < checkStr.length; i++)
  {
    if(checkStr.charAt(i) == '<')
    {
      counts = 1;
      continue;
    }
    if(counts>=1&&checkStr.charAt(i) != '>')
    {
      counts++;
    }
    if(checkStr.charAt(i) == '>')
    {
      if(counts > 1)
      {
        alert(alertMsg + "若要使用\"<\" \">\"符號，建議您使用全形或以其他符號代替，謝謝。");
        checkObj.focus();
        return false;
      }
      else
      {
        counts=0;
      }
    }
  }
  return true;
}

function setCurrency(id)
{
	$jQuery('form#setCurrency input#currency_id').val(id);
	$jQuery('form#setCurrency').submit();
}

function addBookmarkForBrowser(sTitle, sUrl)
{
  if (window.sidebar && window.sidebar.addPanel) {
    addBookmarkForBrowser = function(sTitle, sUrl) {
      window.sidebar.addPanel(sTitle, sUrl, "");
    }
  } else if (window.external) {
    addBookmarkForBrowser = function(sTitle, sUrl) {
      window.external.AddFavorite(sUrl, sTitle);
    }
  } else {
    addBookmarkForBrowser = function() {
      alert("do it yourself");
    }
  }
  return addBookmarkForBrowser(sTitle, sUrl);
}

if (navigator.appName.indexOf("Internet Explorer") != -1) document.onmousedown = noSourceExplorer;
function noSourceExplorer() { if (event.button == 2 | event.button == 3) alert("Sorry! Don’t click the right button！")};
$jQuery(document).ready(function() {
	$jQuery('#checkSearchForm').click(function() {
		if($jQuery('input[name="keywords"]').val() == 'Please enter the keywords')
		{
			alert('Please enter the keywords');
		}
		else if($jQuery('input[name="keywords"]').val().length <= 2)
		{
			alert('Please enter at least 3 accurate digit.');
		}
		else
		{
			$jQuery('form[name="searchForm"]').submit();
		}
		return false;
	});
	
	
	$jQuery('form[name="searchForm"]').submit(function() {
		if($jQuery('input[name="keywords"]').val() == 'Please enter the keywords')
		{
			alert('Please enter the keywords');
		}
		else if($jQuery('input[name="keywords"]').val().length <= 2)
		{
			alert('Please enter at least 3 accurate digit.');
		}
		else
		{
			return true;
		}
		return false;
	});
});
