﻿// JScript File
function trim(inputString) {

	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function fnmouseover(cntrl)
{
   cntrl.style.cursor="pointer";
}

function PostBackPage(cmd, value)
{
    __doPostBack(cmd,value);
}

function fnEdit(ID)
{    
    document.getElementById(AdminMasterPgCPHID + "_hdnValues").value=ID;
    PostBackPage('edit',ID);  
}

function fnLogout()
{
    document.body.style.cursor = "wait";
    httpSession = createRequestObject();   
    httpSession.open('GET',  RelativeUrl + 'SessionData.aspx?logout=true', false);    
    httpSession.send(null);   
    document.location.href = httpSession.responseText;
}

function createRequestObject() {
    var xhhtp;
    if(window.ActiveXObject){
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }
    if(xhttp==null){
  	    fnShowErrorMessage("You are using an unsupported Browser. Please update it to the latest version. Thanks!",false);
    }	
    return xhttp;
}

/******************AgentDeposits****************/
function fnClearAgentDeposits()
{
    if(document.getElementById(AdminMasterPgCPHID + "_ddlAgent") != null)
        document.getElementById(AdminMasterPgCPHID+"_ddlAgent").value="-1";
    document.getElementById(AdminMasterPgCPHID+"_txtDeposit").value=""; 
    document.getElementById(AdminMasterPgCPHID+"_txtBTID").value="";
}
function fnCancelSearchAgentDeposits()
 {
    if(document.getElementById(AdminMasterPgCPHID + "_ddlSAgent") != null)
        document.getElementById(AdminMasterPgCPHID + "_ddlSAgent").value = "-1"; 
    document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value = "";
    document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value = "";   
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = "";
    PostBackPage('search','');  
 }
function fnSearchDeposits()
{
    var ag="";
    var tdate="";
    var tmail="";
    if(document.getElementById(AdminMasterPgCPHID + "_ddlSAgent") != null)
        ag=trim(document.getElementById(AdminMasterPgCPHID + "_ddlSAgent").value); 
    fdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value);
    tdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value);
      
    
    
    var searchCondition = "";
     if(ag != "" && ag != "-1")
        searchCondition = "AD.AgentID=" + ag; 
    if(fdate!= "" && tdate == "")
    {
        if(tdate == "")
        {
        
            tdate="-1";
        }
    }

    if(fdate != "" && tdate != "" )
    {    
            if(searchCondition != "")
            {  
                if( tdate=="-1")
                searchCondition += "&AD.DateDeposited="+ ParseDateM(fdate)+","+  tdate ;
                else 
                searchCondition += "&AD.DateDeposited="+ ParseDateM(fdate)+","+  ParseDateM(tdate) ;
            }
            else
            {
                if( tdate=="-1")
                 searchCondition += "AD.DateDeposited=" +ParseDateM(fdate)+","+  tdate;
                else 
                searchCondition += "AD.DateDeposited=" +ParseDateM(fdate)+","+  ParseDateM(tdate);
            }
             
    }
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = searchCondition; 
    PostBackPage('search',''); 
}
function fnCheckAgent(source, args)
{
   if(args.Value == "-1")
   {
       args.IsValid = false;
   }
   else
   {
        args.IsValid = true;
   }
}

/*******state population********/

var httpCity = null;
var httpState = null;
function fnCityChange(param)
    {
       //alert(q);
        var City= document.getElementById(MasterPgCPHID + "_ddlCity").value;
        var State= document.getElementById(MasterPgCPHID + "_ddlState").value;
        if(param=="City")
        {
            //if(State=="-1")
           // {
           httpCity = createRequestObject();
            httpCity.open('get',  RelativeUrl + 'states.aspx?CityID=' + City);
            httpCity.onreadystatechange = handleCityResponse;
             httpCity.send(null); 
           // }
        }
        /*if(param=="State")
        {
            if(City=="-1")
            {
            httpState = createRequestObject();
            httpState.open('get',  RelativeUrl + 'states.aspx?StateID=' + State);
            httpState.onreadystatechange = handleStateResponse;
             httpState.send(null); 
            }
        }*/
       
    }
   function handleCityResponse()
   {
       
        if (httpCity.readyState == 4)
         {
            var result = httpCity.responseText ;
            if(result!=null)
            {
                loadStateData(result);
            }
        }
   }
   function loadStateData(result)
    {
        var States = result;    
        var ddlState= document.getElementById(MasterPgCPHID + "_ddlState");   
        var Stateslst = States.split(';');
        if(Stateslst.length>0)    
            ddlState.value=Stateslst[0].split(',')[1];
        if(ddlState.value=="")
            ddlState.value = "-1";
    }
    
    
/*********************AgentTransactions****************/
function fnCancelSearchAgentTransactions()
{
    if(document.getElementById(AdminMasterPgCPHID + "_ddlAgent")!= null)
        document.getElementById(AdminMasterPgCPHID + "_ddlAgent").value = "-1";
    document.getElementById(AdminMasterPgCPHID + "_ddlAgentType").value = "-1";
    document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value = "";
    document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value = "";
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = "";
    PostBackPage('search',''); 
}
function fnSearchAgentTransactions()
{    
    var agent="";
    var tdate="";
    var tmail = "";
    var agenttype = "";

    if (document.getElementById(AdminMasterPgCPHID + "_ddlAgentType") != null)
        agenttype = trim(document.getElementById(AdminMasterPgCPHID + "_ddlAgentType").value); 
    if(document.getElementById(AdminMasterPgCPHID + "_ddlAgent")!= null)
        agent = trim(document.getElementById(AdminMasterPgCPHID + "_ddlAgent").value);
    fdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value);
    tdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value);
      
    
    
    var searchCondition = "";
     if(agent != "" && agent != "-1")
         searchCondition = "TT.AgentID=" + agent;

     if (agenttype != "" && agenttype != "-1") 
     {
         if (searchCondition != "")
             searchCondition += "&A.AgentCategoryID=" + agenttype;
         else
             searchCondition += "A.AgentCategoryID=" + agenttype;
     }
        
    if(fdate!= "" && tdate == "")
    {
        if(tdate == "")
        tdate="-1";
    }
    if(fdate != "" && tdate != "" )
    {    
            if(searchCondition != "") 
            {
                if(tdate=="-1")   
                 searchCondition += "&TT.TXNDATETIME=" + ParseDateM(fdate)+","+  tdate ;
                else
                 searchCondition += "&TT.TXNDATETIME=" + ParseDateM(fdate)+","+  ParseDateM(tdate) ;
             }
             else
             {
                 if(tdate=="-1") 
                 searchCondition += "TT.TXNDATETIME=" + ParseDateM(fdate)+","+  tdate ;
                 else
                  searchCondition += "TT.TXNDATETIME="+ ParseDateM(fdate)+","+  ParseDateM(tdate) ;
              }
             
    }
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = searchCondition; 
    PostBackPage('search',''); 
    
}

/***************************Agent Cancellation**********************/
function fnSearchAgentCancelaltions()
{
    var ag = ""; 
    var agtype = "";
    var tdate="";
    var tmail = "";
    if (document.getElementById(AdminMasterPgCPHID + "_ddlAgentType") != null)
        agtype = trim(document.getElementById(AdminMasterPgCPHID + "_ddlAgentType").value); 
    if(document.getElementById(AdminMasterPgCPHID + "_ddlAgent")!= null)
     ag=trim(document.getElementById(AdminMasterPgCPHID + "_ddlAgent").value); 
    fdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value);
    tdate=trim(document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value);      
    
    
    var searchCondition = "";
     if(ag != "" && ag != "-1")
         searchCondition = "TT.AgentID=" + ag;

     if (agtype != "" && agtype != "-1") {
         if (searchCondition != "")
             searchCondition += "&A.AgentCategoryID=" + agtype;
         else
             searchCondition += "A.AgentCategoryID=" + agtype;
     }
    if(fdate!= "" && tdate == "")
    {
        if(tdate == "")
            tdate="-1";
    }
    if(fdate != "" && tdate != "" )
    {    
            if(searchCondition != "")
            {
                 if( tdate=="-1")    
                 searchCondition += "&TC.CancellationDate="+ ParseDateM(fdate)+","+  tdate ;
                 else
                  searchCondition += "&TC.CancellationDate=" + ParseDateM(fdate)+","+  ParseDateM(tdate) ;
            }
             else
             {
             if( tdate=="-1")
             searchCondition += "TC.CancellationDate="+ ParseDateM(fdate)+","+  tdate ;
             else
              searchCondition += "TC.CancellationDate=" + ParseDateM(fdate)+","+  ParseDateM(tdate) ;
             }
             
    }
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = searchCondition; 
    PostBackPage('search',''); 
 }
 
 function fnCancelSearchAgentCancellations()
 {

    if (document.getElementById(AdminMasterPgCPHID + "_ddlAgent") != null)
        document.getElementById(AdminMasterPgCPHID + "_ddlAgent").value = "-1";
    if (document.getElementById(AdminMasterPgCPHID + "_ddlAgentType") != null)
        document.getElementById(AdminMasterPgCPHID + "_ddlAgentType").value = "-1";
    document.getElementById(AdminMasterPgCPHID + "_txtSTDate").value = "";
    document.getElementById(AdminMasterPgCPHID + "_txtSFDate").value = "";   
    document.getElementById(AdminMasterPgCPHID + "_hdnCondition").value = "";
    PostBackPage('search','');  
 }
 function getAgents() {
     if (document.getElementById(AdminMasterPgCPHID + "_ddlAgentType") != null) {
         document.getElementById(AdminMasterPgCPHID + "_hdnAgentTypeID").value = document.getElementById(AdminMasterPgCPHID + "_ddlAgentType").value;
         PostBackPage('fillagents', '');
     }
 }

 function ParseDateM( str1 )
{
    var sdt="";
    if(str1== null)
    {
        var today = new Date()+1;
        var dd = today.getDate();
        var mm = today.getMonth()+1;//January is 0!
        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd}
        if(mm<10){mm='0'+mm}
       
            sdt=mm+'/'+dd+'/'+yyyy;
        //sdt=  new Date();
    }
    else
    {
        var arr = str1.split('/');
       sdt = parseInt(arr[1], 10)+"/" + parseInt(arr[0], 10)+"/" +arr[2];
    }
    return sdt;
}

  /***************** Change Password ***************************/

function fnChangePassword(param)
{
    Page_ClientValidate();
    if(Page_IsValid)  
    {
         var bCheck=passwordverfication(param);
         if(bCheck==true)                 
            PostBackPage('changepwd','');               
        
    }
}

function passwordverfication(param)
{
    if(trim(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value)!="")
        {
             if(trim(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value).length < 8)
             {
                fnShowErrorMessage("Password should be minimum 8 characters.");
                return false;
             }
             else if(trim(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value).length >= 8)
             {
                
                var  re;
                if(param=='Agent')
                    re= /(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/;
                else
                    re= /(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&amp;*()_+]).*$/;
                var regex = new RegExp(re);                
                 var arr = regex.exec(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value );
                 if(arr==null)
                 {
                 if(param=='Agent')
                    fnShowErrorMessage("Password should contain 1 upper case letter,1 lowercase letter and 1 digit.");          
                   else      
                    fnShowErrorMessage("Password should contain 1 upper case letter,1 lowercase letter,1 digit and 1 special character.");   
                    return false;
                 }

                 else if(trim(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value) == trim(document.getElementById(AdminMasterPgCPHID + "_txtOldPwd").value))
                 {
                       
                    fnShowErrorMessage("The Old password matches with the New Password.");
                    return false;
                 }
                 else if(trim(document.getElementById(AdminMasterPgCPHID + "_txtNewPwd").value) != trim(document.getElementById(AdminMasterPgCPHID + "_txtConfirmPwd").value))
                 {
                    fnShowErrorMessage("The New Password and Confirm Password must match.");
                    return false;
                 }
             }
      }
      return true;
}
function fnCancelChangePassword()
{
    document.getElementById(MasterPgCPHID + "_txtOldPwd").value="";
    document.getElementById(MasterPgCPHID + "_txtNewPwd").value="";
    document.getElementById(MasterPgCPHID + "_txtConfirmPwd").value="";
}
function fnSessionExpire()
{
    if(document.getElementById("tblAddEdit")!= null)
        document.getElementById("tblAddEdit").style.display = "none";
    if(document.getElementById("tblView")!= null)
        document.getElementById("tblView").style.display = "none"; 
    if(document.getElementById(AdminMasterPgCPHID + "_tblSearch")!= null)   
        document.getElementById(AdminMasterPgCPHID + "_tblSearch").style.display = "none"; 
    if(document.getElementById("tblSearch")!= null)  
        document.getElementById("tblSearch").style.display = "none";
   
}




function fnDealsAlertClick()
{
    var email = document.getElementById(MasterPgID + "_txtDealsEmail").value;
    var phone = document.getElementById(MasterPgID + "_txtDealPhone").value;   
    var isValidEmail = validateEmail(email);
    var isValidPhone = validatePhone(phone);
    
    if(isValidEmail==true && isValidPhone==true)
    {        
        //var phone = document.getElementById(MasterPgID + "_txtDealPhone").value;    
        //var email = document.getElementById(MasterPgID + "_txtDealsEmail").value;    
        document.getElementById(MasterPgID + "_txtDealsEmail").value = "Enter Email Address";
        document.getElementById(MasterPgID + "_txtDealPhone").value = "Enter Phone Number";
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "none";
        document.getElementById(MasterPgID + "_txtDealsEmail").style.display = "none";
        document.getElementById(MasterPgID + "_txtDealPhone").style.display = "none";
        document.getElementById(MasterPgID + "_lnkDealAlert").style.display = "none";
        document.getElementById(MasterPgID + "_lblMessage").innerHTML = "Thank You for Subscribing to MyBusTickets.in Deal Alerts!";
        httpSession = createRequestObject();
        httpSession.open('GET', RelativeUrl + 'SessionData.aspx?deals=true'+"&Email=" + email + "&Phone=" + phone, false);
        httpSession.send(null);
        var result = httpSession.responseText;
    }
    else if (isValidEmail==false)
    {
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").innerHTML = "Enter valid Email Address";
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "";
    }
    else if(isValidPhone==false)
    {
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").innerHTML = "Enter valid Phone Number";
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "";
    }    
}

function validateEmail(elementValue){   
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;   
   return emailPattern.test(elementValue);   
}
var validEmail;

function validatePhone(elementValue){   
   //var PhonePattern = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
   var PhonePattern = /^[\d]{10}$/;
   return PhonePattern.test(elementValue); 
}

function fnvalidateEmail(element){   
    
    if (element.value == '')   
    {
        element.value = 'Enter Email Address'; 
        element.style.color  = '#999999';
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "none";
    }
    else
    {   
        var isValidEmail = validateEmail(element.value);
        if (isValidEmail == false)
        {
            document.getElementById(MasterPgID + "_lblValidateAlertMessage").innerHTML = "Enter valid Email Address";
            document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "";
            validEmail = false;
            element.focus();
        }
        else
        {
           validEmail = true;
           document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "none";
        }
        
    }
}

function fnvalidatePhone(element){
    if (validEmail == false && element.value == '') {
        element.value = 'Enter Phone Number'; 
        element.style.color  = '#999999';
    }
    
    if (validEmail == false)
    return false;
    
    if (element.value == '') {
        element.value = 'Enter Phone Number'; 
        element.style.color  = '#999999';
        document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "none";
    }
    else
    {
        var isValidPhone = validatePhone(element.value);
        if (isValidPhone == false)
        {
            document.getElementById(MasterPgID + "_lblValidateAlertMessage").innerHTML = "Enter valid Phone Number";
            document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "";
            element.focus();
        }
        else
        {
            document.getElementById(MasterPgID + "_lblValidateAlertMessage").style.display = "none";
        }
   }
}

/***************** Agent Register ***************************/
var httpAgent=null;
function fnAgentID()
{
     if(document.getElementById(MasterPgCPHID + "_ddlSTDCode").value != "Select")
     {
        httpAgent = createRequestObject();        
        httpAgent.open('get',  RelativeUrl + 'GenerateAgents.aspx?std=' + document.getElementById(MasterPgCPHID + "_ddlSTDCode").value);
        httpAgent.onreadystatechange = handleAgentResponse;
        httpAgent.send(null);   
        return false;
      }
}
 
 function handleAgentResponse() {
    if (httpAgent.readyState == 4) {
            var result = httpAgent.responseText ;
            if(result!=null){
                var Agent = result;
                document.getElementById(MasterPgCPHID+"_txtAgentID").value= result;           
            }
    }
}

function fnCheckSTDCodes(source, args)
{
   if(args.Value.toLowerCase() == "select")
   {
       document.getElementById(MasterPgCPHID + "_txtAgentID").value = ""; 
       args.IsValid = false;
   }
   else
   {
        args.IsValid = true;
   }
}

function fnSave(pgName)
{
    Page_ClientValidate();
    if(Page_IsValid)  
    {   
       PostBackPage('save','');
    }
}
function fnCancel(pageName)
{
    HideValidators();
  
    //fnHideErrorMessage();
     if(pageName!='Back')    
    PostBackPage('cancel','');  
}

