﻿// JScript File

// server call back function - alert message on some failure during server function call
 function ServerFunctionCallFailed(Error, DestCtrl, MethodName)
 {
     alert(Error.get_message());
 }

// server call back function - for user verification   
 function VerifyUserCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (ReturnVal.HasError)
     {
        var Msg = document.getElementById(DestCtrl);
        Msg.innerHTML ="Invalid user name or password! " + ReturnVal.ErrorMessage;
     }
     else
     {
        //Redirect user to pageflex page
        var RUrl = ReturnVal.DeploymentURL + "/login.aspx?ticket=" + ReturnVal.LoginToken + "&logonname=" + ReturnVal.EMail;
        window.location = RUrl;
     }
 }

 function LoginButtonClick(UserEmailFieldID, UserEmailMsgID, PWFieldID, PWMsgID)
 {
    var Msg = document.getElementById(UserEmailMsgID);
    Msg.innerHTML = "";
 
    Msg = document.getElementById(PWMsgID);
    Msg.innerHTML = "";
    
    if (ValidateRequiredField(UserEmailFieldID, "Email", UserEmailMsgID) && ValidateEmail(UserEmailFieldID, UserEmailMsgID))
    {
        if  (ValidateRequiredField(PWFieldID, "Password", PWMsgID))
        {
            var UserName = document.getElementById(UserEmailFieldID).value;
            var LPW = document.getElementById(PWFieldID).value;
        
            PageMethods.VerifyUser(UserName, LPW, VerifyUserCallSuccess, ServerFunctionCallFailed, UserEmailMsgID);
        }
    }
 }
  
 // server call back function - for user registration   
 function RegisterUserCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    var RegMsg = document.getElementById(DestCtrl);
    
    if (ReturnVal.HasError)
    {
        if (ReturnVal.ErrorMessage != "")
        {
            RegMsg.innerHTML = ReturnVal.ErrorMessage;
        }
        else if (ReturnVal.ReferralUserErrorMessage != "")
        {
            RegMsg = document.getElementById(ReturnVal.ReferralUserMsgID);
            RegMsg.innerHTML = ReturnVal.ReferralUserErrorMessage;
        } 
    }
    else
    {
        RegMsg.innerHTML = "Register Successful";
        var RUrl = "RegisterNotice.aspx";
        window.location = RUrl;
    }
 } 
  
 //******* Click Register User button
 function RegisterUserButtonClick(UserEmailFieldID, UserEmailMsgID)
 {
    var RegMsg = document.getElementById(UserEmailMsgID);
    RegMsg.innerHTML = "";
      
    if (ValidateRequiredField(UserEmailFieldID, "Email", UserEmailMsgID))
    {
        if (ValidateEmail(UserEmailFieldID, UserEmailMsgID))
        {
             var UserName = document.getElementById(UserEmailFieldID).value;
            PageMethods.RegisterNewUser(UserName, RegisterUserCallSuccess, ServerFunctionCallFailed, UserEmailMsgID);
        }
    }
 }
 
 //******* Click Register & Active User button (user will be register and active at one step)
 function RegisterActiveUserButtonClick(UserEmailFieldID, UserEmailMsgID)
 {
    var RegMsg = document.getElementById(UserEmailMsgID);
    RegMsg.innerHTML = "";
      
    if (ValidateRequiredField(UserEmailFieldID, "Email", UserEmailMsgID))
    {
        if (ValidateEmail(UserEmailFieldID, UserEmailMsgID))
        {
            var UserName = document.getElementById(UserEmailFieldID).value;
            PageMethods.RegisterNewUser(UserName, RegisterUserCallSuccess, ServerFunctionCallFailed, UserEmailMsgID);
        }
    }
 }
  
 //******** Click "Referral" or "Already registered" checkbox 
 function ShowHideCheckBoxClick(ShowHideCheckBox, ShowHideRowID)
 {
    var ShowHideRow = document.getElementById(ShowHideRowID);
    
    if (ShowHideCheckBox.checked)
    {
        ShowHideRow.style.display = ''; 
    }
    else
    {
        ShowHideRow.style.display = 'none'; 
    }
 }
  
 //********* Click Register User button (need to check Referral checkbox)
 function RegisterUserWithReferralButtonClick(UserEmailFieldID, UserEmailMsgID, ReferralUserCheckboxID, ReferralUserEmailFieldID, ReferralUserEmailMsgID)
 {
    var RegMsg = document.getElementById(UserEmailMsgID);
    RegMsg.innerHTML = "";
    
    RegMsg = document.getElementById(ReferralUserEmailMsgID);
    if (RegMsg != null) RegMsg.innerHTML = ""; 
       
    if (ValidateRequiredField(UserEmailFieldID, "Email", UserEmailMsgID))
    {
        if (ValidateEmail(UserEmailFieldID, UserEmailMsgID))
        {
            if (document.getElementById(ReferralUserCheckboxID).checked)
            {
                if (ValidateRequiredField(ReferralUserEmailFieldID, "Email", ReferralUserEmailMsgID))
                {
                   if (ValidateEmail(ReferralUserEmailFieldID, ReferralUserEmailMsgID))
                   {
                        var UserName = document.getElementById(UserEmailFieldID).value;
                        var ReferralUserName = document.getElementById(ReferralUserEmailFieldID).value;
            
                        PageMethods.RegisterNewUserWithReferral(UserName, ReferralUserName, ReferralUserEmailMsgID, RegisterUserCallSuccess, ServerFunctionCallFailed, UserEmailMsgID);
                   }
                }
            }
            else
            {
                var UserName = document.getElementById(UserEmailFieldID).value;
                PageMethods.RegisterNewUser(UserName, RegisterUserCallSuccess, ServerFunctionCallFailed, UserEmailMsgID);
            }
        }
    }
 }
  
 // server call back function - for get Industry   
// function GetGetIndustryCallSuccess(ReturnVal, DestCtrl, MethodName)
// {   
//     if (!ReturnVal.HasError)
//    {
//        var cookie_date = new Date();
//        cookie_date.setMinutes(cookie_date.getMinutes()+ 1);
//                
//        var RUrl = ReturnVal.DeploymentURL + "/CreateUserDocument.aspx?product=" + ReturnVal.ID;
//        var cookieStr = "Industry="+DestCtrl+";expires="+ cookie_date.toGMTString()+ ";domain=" + ReturnVal.CookieDomain;
//        
//        document.cookie = cookieStr;
//        window.location = RUrl;
//    }
// } 
 
 function IndustryClick(ProductName ,IndustryName)
 {
    //PageMethods.GetProductID(ProductName, GetGetIndustryCallSuccess, ServerFunctionCallFailed, IndustryName);
    var RUrl = "showIndustry.aspx?pn=" + ProductName +"&in=" + IndustryName;
    window.location = RUrl;
 }
  
 //server call back function - for get product    
 function GetProductCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (!ReturnVal.HasError)
    {
        var RUrl = ReturnVal.DeploymentURL + "/CreateUserDocument.aspx?product=" + ReturnVal.ID;
        window.location = RUrl;
    }
 } 
 
 function ProductClick(ProductName)
 {
    PageMethods.GetProductID(ProductName, GetProductCallSuccess, ServerFunctionCallFailed);
 }
 
 // server call back function - for get category id   
 function GetCategoryIDCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (!ReturnVal.HasError)
    {
        var RUrl = ReturnVal.DeploymentURL + "/UserContentStart.aspx?category=" + ReturnVal.ID;
        window.location = RUrl;
    }
 } 
 
 function CategoryClick(CategoryName)
 {
    PageMethods.GetCategoryID(CategoryName, GetCategoryIDCallSuccess, ServerFunctionCallFailed);
 }
   
 function GetCurrentDeploymentCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (!ReturnVal.HasError)
    {
        var RUrl = ReturnVal.DeploymentURL + "/" + DestCtrl;
        window.location = RUrl;
    }
 } 
  
 function IsUserLoggedInCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    if (!ReturnVal.HasError)
    {
        var RUrl = getPFLoggedUser(ReturnVal.DeploymentURL);
        
        if (RUrl != null && RUrl.length > 4)
            RUrl = ReturnVal.DeploymentURL + "/" + DestCtrl;
        else
            RUrl = "loginDesc.aspx";
            
        window.location = RUrl;
    }
 } 
    
 function getPFLoggedUser(DeploymentURL)
 {
     var client;
     var tdate = new Date();
     
     DeploymentURL += "/getloginuser.aspx?t=" + tdate.getTime(); 
     
     if (window.XMLHttpRequest)// code for IE7+, Firefox, Chrome, Opera, Safari
         client=new XMLHttpRequest();
     else// code for IE6, IE5
         client = new ActiveXObject("Microsoft.XMLHTTP");
          
     //client.onreadystatechange=function() {if (client.readyState==4) {result = client.responseText;}};
     client.open("GET", DeploymentURL, false);
     client.send(null);
     
     if (client.readyState==4 && client.status == 200)
        return client.responseText;
     else
        return null;
  }
    
 //*********** Click My libarary link
 function MyLibraryClick()
 {
    //PageMethods.IsUserLoggedIn(IsUserLoggedInCallSuccess, ServerFunctionCallFailed, "UserContentLibrary.aspx");
     PageMethods.GetCurrentDeployment(IsUserLoggedInCallSuccess, ServerFunctionCallFailed, "UserContentLibrary.aspx");
 }
 
 //*********** Click My Profile link
 function MyProfileClick()
 {
    PageMethods.GetCurrentDeployment(IsUserLoggedInCallSuccess, ServerFunctionCallFailed, "UserContentProfile.aspx");
 }
 
 //*********** user click My Shopping Cart link
 function MyShoppingCartClick()
 {
    PageMethods.GetCurrentDeployment(IsUserLoggedInCallSuccess, ServerFunctionCallFailed, "UserContentShoppingCart.aspx");
 }
  
 //*********** user click My Shopping Cart link
 function MyOrderClick()
 {
    PageMethods.GetCurrentDeployment(IsUserLoggedInCallSuccess, ServerFunctionCallFailed, "UserContentOrders.aspx");
 } 
   
 //*********** user click banner link of western union
 function DefaultProductClick()
 {
    PageMethods.GetCurrentDeployment(GetCurrentDeploymentCallSuccess, ServerFunctionCallFailed, "UserContentStart.aspx");
 } 
   
 // server call back function - for get home page link    
 function GetHomePageCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (!ReturnVal.HasError)
    {
        var RUrl = ReturnVal.MainWebSiteURL + "/" + DestCtrl;
        window.location = RUrl;
    }
 } 
 
 //*********** Click "Home" link
 function HomePageClick()
 {
    PageMethods.GetCurrentDeployment(GetHomePageCallSuccess, ServerFunctionCallFailed, "index.aspx");
 }
   
  // server call back function - for forgot password   
  function ForgotPWCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     if (!ReturnVal.HasError)
    {
        var msg = "New password has been sent to your email.";
        var MsgCtrl = document.getElementById(DestCtrl);
        MsgCtrl.innerHTML = msg;
    }
 } 
 
 //***********Click Forgot Password link
 function ForgotPWClick(EmailFieldID, MsgFieldID)
 {
    if (ValidateRequiredField(EmailFieldID, "Email", MsgFieldID) && ValidateEmail(EmailFieldID, MsgFieldID))
    {
        var UserName = document.getElementById(EmailFieldID).value;
        PageMethods.ForgotPW(UserName, ForgotPWCallSuccess, ServerFunctionCallFailed, MsgFieldID);
    }
 }
 
 //***** Click Search button
 function SearchClick(SearchKeywordFieldID)
 {
     var SerachKeword = document.getElementById(SearchKeywordFieldID).value;
     var RUrl = "productView.aspx?kw=" + SerachKeword;
     window.location = RUrl;
 }
  
 //Server call back function for Register Promotion Code 
 function RegPromotionCodeCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    var Msg;
    var MsgCtrl;
     
    if (ReturnVal.HasError)
    {
        if (ReturnVal.ErrorMessage != "")
         {
            MsgCtrl = document.getElementById(DestCtrl);
            Msg = ReturnVal.ErrorMessage;
         }
         else
         {
            MsgCtrl = document.getElementById(ReturnVal.UserErrorMessageID);
            Msg = ReturnVal.UserErrorMessage;
         }
    }
    else
    {
        MsgCtrl = document.getElementById(DestCtrl);
        Msg = "Congratulations, You are approved to get this Promotion. You can use this promotion with your next order.";
    }
        
    MsgCtrl.innerHTML = Msg;
 }    
    
 //***** Click Register Promotion Code button  
 function RegPromotionCodeButtonClick(UserEmailFieldID, UserEmailMsgID, PromCodeFieldID, PromCodeMsgID)
 {
    var Msg = document.getElementById(UserEmailMsgID);
    Msg.innerHTML = "";
 
    Msg = document.getElementById(PromCodeMsgID);
    Msg.innerHTML = "";
    
    if (ValidateRequiredField(UserEmailFieldID, "Email", UserEmailMsgID) && ValidateEmail(UserEmailFieldID, UserEmailMsgID))
    {
        if  (ValidateRequiredField(PromCodeFieldID, "Promotion Code", PromCodeMsgID))
        {
            var UserName = document.getElementById(UserEmailFieldID).value;
            var PromCode = document.getElementById(PromCodeFieldID).value;
        
            PageMethods.RegisterPromotionCode(UserName, PromCode, UserEmailMsgID, RegPromotionCodeCallSuccess, ServerFunctionCallFailed, PromCodeMsgID);
        }
    }
 }
  
 //Server call back function for Get Current User Info function
 function GetCurrentUserCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    if (ReturnVal.EMail != "")
    {
        var UserNameTextBox = document.getElementById(DestCtrl);
        UserNameTextBox.value = ReturnVal.EMail;
    }
 }   
  
  //***** Get login user information 
 function GetCurrentUserInfo(UserNameFieldID)
 {
    PageMethods.GetCurrentUser(GetCurrentUserCallSuccess, ServerFunctionCallFailed, UserNameFieldID);
 }
   
   
  //Server call back function for Get Ecard Name function
 function GetECardNameCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    if (!ReturnVal.HasError)
    {
        var ECardImg = document.getElementById(DestCtrl);
        var ECardImgURL = "./ecard/" + ReturnVal.ECardName
        ECardImg.src = ECardImgURL;
    }
 }   
   
 //***** Get ECard Name
 function GetECardURL(ECardImgID)
 {
     PageMethods.GetECardName(GetECardNameCallSuccess, ServerFunctionCallFailed, ECardImgID);
 }  
     
 //Server call back function for Get Product Price
 function GetProductPriceCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
    if (!ReturnVal.HasError)
    {
        var PriceCol;
        var i = 0;
         
        for (i = 0; i<ReturnVal.length; i++)
        {
            PriceCol = document.getElementById(ReturnVal[i].ClientID);
            
            if (PriceCol != null)
               PriceCol.innerHTML = ReturnVal[i].Price;
        }
    }
 }   
 
 //********Defind printing product object template
 function ClsVProdPrice(ProductName, QtyList)
 {
     this.ProductName=ProductName;
     this.QtyList=QtyList;
     this.PriceTableList = new Array;
 }

 //********Defind printing option object template
 function ClsVPrnOpt(ClientPrcTableID, NumOfCol, Side, PaperList, AirTimeList, PocketTypeList)
 {
     this.ClientPrcTableID=ClientPrcTableID;
     this.NumOfCol=NumOfCol;
     this.Side=Side;
     this.PaperList=PaperList;
     this.AirTimeList=AirTimeList;
     this.PocketTypeList=PocketTypeList; 
 } 

//********Defind Product Price object to get price
 function ClsProdPrice()
 {
     var ProdPriceList= new Array();
     var objVProdPrice;
     
     //"NewProd" method
     this.AddProd = 
     function (ProductName, QtyList)
     {
        objVProdPrice = new ClsVProdPrice(ProductName, QtyList);
        ProdPriceList[ProdPriceList.length] = objVProdPrice;
     };
     
     //"NewPriceTableForProduct" method
     this.AddPriceTableForProduct =
     function (ClientPrcTableID, NumOfCol, Side, PaperList, AirTimeList, PocketTypeList)
     {
          
        var objVPrnOpt = new ClsVPrnOpt(ClientPrcTableID, NumOfCol, Side, PaperList, AirTimeList, PocketTypeList); 
        objVProdPrice.PriceTableList[objVProdPrice.PriceTableList.length] = objVPrnOpt;
     };
     
     //"GetPrice" method
      this.GetPrice = function()
     {
        GetProductPrice(ProdPriceList);
        
        ProdPriceList.splice(0, ProdPriceList.length); 
        objVProdPrice = null;
     };
     
     //***** Get Product Price
    function GetProductPrice(ProdPriceList)
    {
        PageMethods.GetProdPrice(ProdPriceList, GetProductPriceCallSuccess, ServerFunctionCallFailed);
    };
 } 
 
 //server call back function - for Contact Us   
 function ContactUsCallSuccess(ReturnVal, DestCtrl, MethodName)
 {   
     var Msg = document.getElementById(DestCtrl);
     
     if (ReturnVal.HasError)
     {
         Msg.innerHTML ="Error: " + ReturnVal.ErrorMessage;
     }
     else
     {
        Msg.innerHTML ="The comment has been sent.";
     }
 } 
  
 //********Defind ContactUs value class
 function ClsVContactUs(FieldID, FieldTitle, IsRequiredField, IsEMailAddress, IsEmailBody, ValidationMsgID)
 {
     this.FieldID = FieldID;
     this.FieldTitle = FieldTitle;
     this.IsRequiredField = IsRequiredField;
     this.IsEMailAddress = IsEMailAddress;
     this.IsEMailBody = IsEmailBody;
     this.ValidationMsgID = ValidationMsgID; 
 } 
 
 //User click "Contact Us"
 function ContactUsClick(FieldList)
 {
    //FieldList format : FieldID^FieldTitle^IsRequiredField^IsEMailAddress^IsEmailBody^ValidationMsgID|
    var AFieldList = new Array();
    var AFieldItemList = new Array();
    var EMailAddress = "", EMailBody = "", EMailBodyMsgID = "";
         
    AFieldList = FieldList.split("|");
    if (AFieldList.length > 0)
    {
        for (i=0; i<AFieldList.length; i++)
        {
            AFieldItemList = AFieldList[i].split("^");
            
            //ClsVContactUs(FieldID, FieldTitle, IsRequiredField, IsEMailAddress, IsEmailBody, ValidationMsgID)
            objVContactUs = new ClsVContactUs(AFieldItemList[0], AFieldItemList[1], AFieldItemList[2], AFieldItemList[3], AFieldItemList[4], AFieldItemList[5]);
            AFieldList[i] = objVContactUs;
        }
    }
    
    for (i=0; i<AFieldList.length; i++)
    {
        if (AFieldList[i].IsRequiredField == "1")
        {
            Msg = document.getElementById(AFieldList[i].ValidationMsgID);
            Msg.innerHTML = "";
        }
    }
        
    for (i=0; i<AFieldList.length; i++)
    {
        if (AFieldList[i].IsRequiredField == "1")
        {
           if (ValidateRequiredField(AFieldList[i].FieldID, AFieldList[i].FieldTitle, AFieldList[i].ValidationMsgID))
           {
               if (AFieldList[i].IsEMailBody == "1") 
                {
                    EMailBody = document.getElementById(AFieldList[i].FieldID).value + "\n\n";
                    EMailBodyMsgID = AFieldList[i].ValidationMsgID; 
                }
           }
           else
           {
                return;
           }
        }
        
        if (AFieldList[i].IsEMailAddress == "1")
        {
            if (ValidateEmail(AFieldList[i].FieldID, AFieldList[i].ValidationMsgID))
            {
                EMailAddress = document.getElementById(AFieldList[i].FieldID).value;
            }
            else
            {
                return;
            }
        }
    }     

    for (i=0; i<AFieldList.length; i++)
    {
        if ((AFieldList[i].IsEMailAddress == "0") && (AFieldList[i].IsEMailBody == "0"))
        {
            Msg = document.getElementById(AFieldList[i].FieldID).value; 
            if (Msg != "")
            {
                EMailBody += AFieldList[i].FieldTitle + " : " + Msg + "\n";
            }
        }
    }
    
    PageMethods.ContactUs(EMailAddress, EMailBody, ContactUsCallSuccess, ServerFunctionCallFailed, EMailBodyMsgID)
 }
 
 //*****field value validation function
 function trim(s)
 {
   return s.replace(/^\s+|\s+$/, '');
 } 
 
 //validate Required Field
 function ValidateRequiredField(VldCtrlID, VldCtrlName, MsgCtrlID)
 {
    var RequiredField = document.getElementById(VldCtrlID);
    var MsgCtrl = document.getElementById(MsgCtrlID);
    var returnVal = false;
        
    if (RequiredField.value == "")
    {
        MsgCtrl.innerHTML = VldCtrlName + " is required!";
        RequiredField.focus(); 
    }
    else
    {
        returnVal = true;
    }
    
    return returnVal;
 }
  
 //validate user email
 function ValidateEmail(VldCtrlID, MsgCtrlID)
 {
    var UserEmail = document.getElementById(VldCtrlID);
    var MsgCtrl = document.getElementById(MsgCtrlID);
    var returnVal = false;
    
    var error="";
    var tfld = trim(UserEmail.value);                      
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (tfld == "") {
        error = "Email address is required!";
        MsgCtrl.innerHTML = error;
    } 
    else if (!emailFilter.test(tfld)) 
    {   
        //test email for illegal characters
         error = "Invalid email address.";
         MsgCtrl.innerHTML = error;
         UserEmail.focus(); 
    } 
    else if (tfld.match(illegalChars)) 
    {
        error = "Invalid email address.";
        MsgCtrl.innerHTML = error;
        UserEmail.focus(); 
    } 
    else 
    {
        returnVal = true;
    }
    
    return returnVal;
}

// //Setp 1 for search engien ranking
// function SearchEngineCountStep1()
// {
//    try
//    {
//        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
//	    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//	}
//	catch (err)
//	{
//	
//	}
// }
// 
// //Setp 2 for search engien ranking
// function SearchEngineCountStep2()
// {
//    try
//    {
//        var pageTracker = _gat._getTracker("UA-651909-3");
//	    pageTracker._initData();
//	    pageTracker._trackPageview(); 
//	}
//	catch (err)
//	{
//	
//	}
// }
 
// //For search engien ranking
// function SearchEngineCountStep()
// {
//    var _gaq = _gaq || [];
//    _gaq.push(['_setAccount', 'UA-651909-3']);
//    _gaq.push(['_trackPageview']);
//    (function() {
//        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
//        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
//    })();
// }

