﻿function login(uid,pw){
    $.ajax({
      url: 'login.ashx',
      type: 'POST',
      data: {
        'uid': uid,
        'pw':pw
      },
      dataType:'json', 
      error: function(xhr) {
        alert('Ajax request 發生錯誤');
      },
      success: function(data) {
        if (data.msg!=''){alert(data.msg);}
        if (data.nickname!='' || data.point!=''){
//            window.location.reload()
            if (location.href.indexOf('hotelone.com.tw')!=-1){
                location.href=String.format('http://www.landishotelsresorts.com/chinese-trad/login.ashx?uid={0}&pw={1}&toUrl={2}',uid,pw,location.href);
            }else{
                location.href=String.format('http://taichung.hotelone.com.tw/chinese-trad/login.ashx?uid={0}&pw={1}&toUrl={2}',uid,pw,location.href);
            }               
        }
          

      }
    });

}
String.format = function()
{

    if( arguments.length == 0 )
    {
        return null; 
    }
    
    var str = arguments[0]; 

    for(var i=1;i<arguments.length;i++)
    {

        var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};

function logout(){
    $.ajax({
      url: 'logout.ashx',
      type: 'POST',
      dataType:'json', 
      error: function(xhr) {
        alert('Ajax request 發生錯誤');
      },
      success: function(data) {
        if (data.msg!=''){alert(data.msg);}
//        window.location.reload()
            if (location.href.indexOf('hotelone.com.tw')!=-1){
                location.href=String.format('http://www.landishotelsresorts.com/chinese-trad/logout.ashx?toUrl={0}',location.href);
            }else{
                location.href=String.format('http://taichung.hotelone.com.tw/chinese-trad/logout.ashx?toUrl={0}',location.href);
            }               
      }
    });

}
