﻿Default = Class.create();
Default.prototype =
{
    // initialize data members
    initialize : function()
    {
        // for login user
        this.m_username = $("username");
        this.m_password = $("password");
        this.m_login = $("login");
        this.m_userAgencyname = $("txt_UserName_Agency");
        this.m_passAgencyword = $("txt_Pwd_Agency");
        this.m_login_Agency = $("btn_AgencyLogin");
        this.m_ruleNum = $("txtDCMRRuleNum");
        if (this.m_login != null  )
        {
            this.m_ruleNum.focus();
            this.bindEnterKey(this.m_username, this.m_login);
            this.bindEnterKey(this.m_password, this.m_login);         
            this.m_login.onclick = this.userLoginClick.bind(this);
            
        }
        if(this.m_login_Agency != null)
        {
            this.bindEnterKey(this.m_userAgencyname, this.m_login_Agency);
            this.bindEnterKey(this.m_passAgencyword, this.m_login_Agency); 
            this.m_login_Agency.onclick = this.userAgencyLoginClick.bind(this);
        }
         this.bindButtonClick($("btnFresh"), $("txtPublish"), "Default.aspx?PublishDate="); 
         //DCMR search
        this.bindButtonClick($("btnDCMRRuleNum"), $("txtDCMRRuleNum"), "Search/DCMRSearchByRules.aspx?SearchType=DCMRRuleNum&KeyValue=");                          
        this.bindButtonClick($("btnDCMRChaptNum"), $("txtDCMRChaptNum"), "Search/DCMRSearchByChapter.aspx?SearchType=DCMRChaptNum&KeyValue=");
        
        //Mayor's Order search                                       
        this.bindButtonClick($("btnOrderByNum"), $("txtOrderByNum"), "Search/OrderSearch.aspx?SearchType=Number&KeyValue=");  
        
         //DCR search
         this.bindButtonClick($("btnDCRRuleNum"), $("txtDCRRuleNum"), "Search/DCRSearchByRule.aspx?RuleNumber=");
         this.bindButtonClick($("btnDCRNoticeID"), $("txtDCRNoticeID"), "Gateway/NoticeHome.aspx?SearchType=DCR&NoticeID="); 
            
        //keyword search
        var selectArray = $$("#selKeywordSearchRow select");      
        if (selectArray.length > 0)            
            this.bindButtonTwoKeyClick($("btnKeywordSearch"), $("txtKeywordSearch"), selectArray[0], "Search/FullTextSearch.aspx?","KeyValue","SearchType");      
        
        selectArray = $$("#selDCMRAgencyRow select"); 
        if (selectArray.length > 0)
            this.bindButtonClick($("btnDCMRAgency"), selectArray[0], "Gateway/Agencyhome.aspx?SearchType=DCMRAgency&AgencyID=");
            
        selectArray = $$("#selDCRAgencyRow select");
        if (selectArray.length > 0)
            this.bindButtonClick($("btnDCRAgency"), selectArray[0], "Search/DCRSearchByAgency.aspx?SearchType=DCRAgency&AgencyID=");  
             
        selectArray = $$("#selOrderByDateRow select");
        if (selectArray.length > 0)
            this.bindButtonClick($("btnOrderByDate"), selectArray[0], "Search/OrderSearch.aspx?SearchType=Date&KeyValue=");
            
       
        
            
     
        
    },

    // user login click
    userLoginClick : function()
    {
        if(this.m_username.value=="" || this.m_username.value=="< email >" || this.m_password.value=="")
        {
            if((this.m_username.value=="" || this.m_username.value=="< email >") && this.m_password.value==""){
                alert("Please input username and password!");
                this.m_username.focus();
            }
            else if(this.m_username.value=="" || this.m_username.value=="< email >"){
                alert("Please input username!");
                this.m_username.focus();
            }
            else if(this.m_password.value==""){
                alert("Please input password!");
                this.m_password.focus();
            }
            return;
        }
        _Default.UserLogin(this.m_username.value, this.m_password.value, this.userLoginClickCallback.bind(this));
    },
    userAgencyLoginClick : function()
    {
        if(this.m_userAgencyname.value=="" || this.m_passAgencyword.value=="")
        {
            if(this.m_userAgencyname.value=="" && this.m_passAgencyword.value==""){
                alert("Please input username and password!");
                this.m_userAgencyname.focus();
            }
            else if(this.m_userAgencyname.value==""){
                alert("Please input username!");
                this.m_userAgencyname.focus();
            }
            else if(this.m_passAgencyword.value==""){
                alert("Please input password!");
                this.m_passAgencyword.focus();
            }
            return;
        }
        _Default.UserAgencyLogin(this.m_userAgencyname.value, this.m_passAgencyword.value, this.userAgencyLoginClickCallback.bind(this));
    },
    // a callback for user login
    // userId : user id if login successful, otherwise Constant.INVALID_USER_ID
    userLoginClickCallback :function(userId)
    {
        if (userId.value > 0)
            window.location = "Subscriber/SubscriberHome.aspx";
        else
        {
            alert("Username or password error, please try again!");
            this.m_username.focus();
        }
    },
     userAgencyLoginClickCallback :function(userId)
    {
        if (userId.value > 0)
        {
            window.location = "Agency/PickHats.aspx?user=" + userId.value;
        }
        else
        {
            alert("Username or password error, please try again!");
            this.m_userAgencyname.focus();
        }
    },
    
    bindButtonTwoKeyClick : function(button, textbox1,textbox2,targetUrl,Key1,Key2)
    {
        if (button != null)
        {
            this.bindEnterKey(textbox1, button);
            this.bindEnterKey(textbox2, button);
            button.onclick = this.buttonClickTwoKeyCallback.bind(this, textbox1,textbox2, targetUrl,Key1,Key2);
        }
    },
    
    // binds click event for a button
    bindButtonClick : function(button, textbox, targetUrl)
    {
        if (button != null)
        {
            this.bindEnterKey(textbox, button);
            button.onclick = this.buttonClickCallback.bind(this, textbox, targetUrl);
        }
    },
    // a callback function for user click button
    buttonClickTwoKeyCallback : function(textbox1,textbox2, targetUrl,Key1,Key2)
    {
        var value1 = null;
        var value2 = null;
        if (textbox1.tagName == "INPUT")
            value1 = textbox1.value;
        else if (textbox1.tagName == "SELECT")
            value1 = textbox1.options[textbox1.selectedIndex].value;
            
        if (textbox2.tagName == "INPUT")
            value2 = textbox2.value;
        else if (textbox2.tagName == "SELECT")
            value2 = textbox2.options[textbox2.selectedIndex].value;            
        value1=value1.replace(/(^\s*)|(\s*$)/g, ""); 
        value2=value2.replace(/(^\s*)|(\s*$)/g, ""); 
        value1=UrlEncode(value1);
        value2=UrlEncode(value2);
        if (value1.length > 0 && value1 != null)
        {
            if (value2.length > 0 && value2 != null)
                window.location = targetUrl +Key2.replace(" ","")+"="+ value2.replace(" ","")+"&"+Key1+"="+value1; 
            else                
                window.location = targetUrl +Key1+"="+ value1; 
        }  
        else
        {
            textbox1.focus()
            //alert("Keyword can not be empty!");
        }          
        
    },
    
    // a callback function for user click button
    buttonClickCallback : function(textbox, targetUrl)
    {
        var value = null;
        if (textbox.tagName == "INPUT")
            value = textbox.value;
        else if (textbox.tagName == "SELECT")
            value = textbox.options[textbox.selectedIndex].value;
        value=value.replace(/(^\s*)|(\s*$)/g, ""); 
        value=UrlEncode(value);
        if (value.length > 0 && value != null)
            window.location = targetUrl + value;            
        else
        {
            textbox.focus();
            //alert("Keyword can not be empty!");
        }   
    },

    // bind enter key
    bindEnterKey : function(input, button)
    {
        if (navigator.appName == 'Netscape')
            input.onkeydown = this.inputKeydown4Firefox.bindAsEventListener(this, button);
        else
            input.onkeydown = this.inputKeydown4IE.bindAsEventListener(this, button);
    },

    // for firefox
    inputKeydown4Firefox : function(eventObject, button)
    {
 
        if (eventObject.keyCode == 13)
            button.onclick();
    },

    // for ie
    inputKeydown4IE : function(eventObject, button)
    {
        if (event.keyCode == 13)
            button.onclick();
    }
}

       //add by bill
        function change()
        {
            var tbUser = window.document.getElementById("username");
	        if (tbUser.value =="< email >")
	        {	
	            tbUser.value = "";
	        }
        }
        
 //urlencode
 function   UrlEncode(strURL)   
  {   
  var   strSpecialUrl   =   "   <>\"#%{}|^[]`&?+";   
  var   strEncode="";   
  var   i,   j,   chUrl,   iCode,   iCodeBin,   num;   
  var   tempBin;   
  var   leadingzeros;   
    
  strURL+="";   
  for   (i=0;   i<strURL.length;   i++)   {   
  chUrl   =   strURL.charAt(i);   
  iCode   =   chUrl.charCodeAt(0);   
  if   (iCode<=parseInt("0x7F"))   {   
  if   (strSpecialUrl.indexOf(chUrl)!=-1)   {   
  //chUrl   is   a   special   character   that   needs   to   be   Url   encoded   
  strEncode+="%"+iCode.toString(16).toUpperCase();   
  }   else   {   
  //otherwise   chrUrl   is   normal   
  strEncode+=chUrl;   
  }   
  }   else   {   
  leadingzeros="";   
  iCodeBin=iCode.toString(2)   
  if   (iCode<=parseInt(0x7FF))   {   
  //glyph   is   represented   by   two   chars   
    
  //check   leading   zeros   on   iCodeBin   (should   be   11   digits)   
  for   (j=11;   j>iCodeBin.length;   j--)   leadingzeros+="0";   
  iCodeBin=leadingzeros+iCodeBin   
    
  tempBin="110"+iCodeBin.substr(0,5);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(5,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  }   else   {   
  if   (iCode<=parseInt(0xFFFF))   {   
  //glyph   is   represented   by   three   chars   
    
  //check   leading   zeros   on   iCodeBin   (should   be   16   digits)   
  for   (j=16;   j>iCodeBin.length;   j--)   leadingzeros+="0";   
  iCodeBin=leadingzeros+iCodeBin   
    
  tempBin="1110"+iCodeBin.substr(0,4);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(4,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(10,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  }   else   {   
  if   (iCode<=parseInt(0x1FFFFF))   {   
  //glyph   is   represented   by   four   chars   
    
  //check   leading   zeros   on   iCodeBin   (should   be   21   digits)   
  for   (j=21;   j>iCodeBin.length;   j--)   leadingzeros+="0";   
  iCodeBin=leadingzeros+iCodeBin   
    
  tempBin="11110"+iCodeBin.substr(0,3);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(3,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(9,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(15,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  }   else   {   
  if   (iCode<=parseInt(0x3FFFFFF))   {   
  //glyph   is   represented   by   five   chars   
    
  //check   leading   zeros   on   iCodeBin   (should   be   26   digits)   
  for   (j=26;   j>iCodeBin.length;   j--)   leadingzeros+="0";   
  iCodeBin=leadingzeros+iCodeBin   
    
  tempBin="111110"+iCodeBin.substr(0,2);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(2,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(8,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(14,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(20,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  }   else   {   
  if   (iCode<=parseInt(0x7FFFFFFF))   {   
  //glyph   is   represented   by   six   chars   
    
  //check   leading   zeros   on   iCodeBin   (should   be   31   digits)   
  for   (j=31;   j>iCodeBin.length;   j--)   leadingzeros+="0";   
  iCodeBin=leadingzeros+iCodeBin   
    
  tempBin="1111110"+iCodeBin.substr(0,1);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(1,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(7,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(13,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(19,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  tempBin="10"+iCodeBin.substr(25,6);   
  strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()   
  }   
  }   
  }   
  }   
  }   
  }   
  }   
  return   strEncode;   
  }   