/* このファイルはUTF-8のBOMなし(UTF-8N)で保存しています */
(function($) {

	$.extend({
		_jsonp : {
			scripts: {},
			charset: 'utf-8',
			counter: 1,
			head: document.getElementsByTagName("head")[0],
			name: function( callback ) {
				var name = '_jsonp_' +  (new Date).getTime()
					+ '_' + this.counter;
				this.counter++;
				var cb = function( json ) {
					eval( 'delete ' + name );
					callback( json );
					$._jsonp.head.removeChild( $._jsonp.scripts[ name ] );
					delete $._jsonp.scripts[ name ];
				};
				eval( name + ' = cb' );
				return name;
			},
			load: function( url, name ) {
				var script = document.createElement( 'script' );
				script.type    = 'text/javascript';
				script.charset = this.charset;
				script.src     = url;
				this.head.appendChild( script );
				this.scripts[ name ] = script;
			}
		},

		getJSONP : function ( url, callback ){
			var name = $._jsonp.name( callback );
			var url = url.replace( /{callback}/, name );
			$._jsonp.load( url, name );
			return this;
		}
	});

})(jQuery);


/**
 * メール送信
 */
function sendEmail(base_uri) {
    var toAddress = $('#toAddress').get(0).value;
    toAddress = toAddress.replace(/^[ ]+|[ ]+$/g, '');

    if (toAddress == '') {
        alert('請輸入電郵地址。');
        return false;
    } else if (!isValidEmail(toAddress)) {
        alert('電郵地址不正確。');
    } else {
        var url = base_uri + '/common/sendAddress_hk.php?countryCode=hk&toAddress=' + toAddress;
        $.getJSONP(url, alert('電郵已被發到 ' + toAddress + ' 。'));
    }
}

/**
 * メールアドレス文字列検証(簡易)
 */
function isValidEmail(str) {
    if (str.match(/[.+@.+\..+]/)) {
        return true;
    }
    return false;
}
