﻿$(document).ready(function()
{
    LoadXMLDiv();
    $("#" + _controlName + "txtFirstname").focus();
   
      //************ On focus ******************
      $(":input[rel='required']").focus(function()
      {
      //alert(this.id);
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
        
        //calculating offset for displaying popup message
        extendedWidth=(this.id==_controlName + "txtPostalCode") ? extendedWidth=70 : extendedWidth=175;
        
        extendedWidth=$(this).width()-20;
        topVal=top + extendedHeigh  + "px";  
        leftVal=left + extendedWidth + "px";
        if(_dispayTipMsg)
        {
        DisplayTipMsg(this,this.id);
        }
        //custom display
        //DisplayTipMsg(this,_controlName + 'ddProvince');
    /*
        DisplayTipMsg(this,_controlName + 'txtFirstname');
        DisplayTipMsg(this,_controlName + 'txtLastname');
        DisplayTipMsg(this,_controlName + 'txtAddress');
        DisplayTipMsg(this,_controlName + 'txtCity');
        DisplayTipMsg(this,_controlName + 'ddProvince');
        DisplayTipMsg(this,_controlName + 'txtPostalCode');
    */  
    if(_hasTAF)
    {
        for (var i=1;i<=NumOfTafEmail;i++)
        {
            DisplayTipMsg(this,_controlName + "txtRecipientName_" + i);     
        }
     }   
      });    
    $(':input[rel=required_integer]').focus(function()
    {
        //$(this).validate.init(this);   
        
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
        extendedWidth=(this.id==_controlName + "txtPostalCode") ? extendedWidth=70 : extendedWidth=175;
            
        extendedWidth=$(this).width()-20;
        topVal=top + extendedHeigh  + "px";  
        leftVal=left + extendedWidth + "px";
        DisplayTipMsg(this,this.id);
    }); 
      
    $(":input[rel='required_postal']").focus(function()
    {
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
           
        //calculating offset for displaying popup message
        extendedWidth=(this.id==_controlName + "txtPostalCode") ? extendedWidth=73 : extendedWidth=175;
        
        topVal=top + extendedHeigh + "px";  
        leftVal=left + extendedWidth + "px";
        
        if(_dispayTipMsg)
        {
            DisplayTipMsg(this,_controlName + 'txtPostalCode');
        }
      });
      
      $(":input[rel='required_phone']").focus(function()
      {
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
           
        //calculating offset for displaying popup message  
        //extendedWidth=(this.id==_controlName + "txtPhone") ? extendedWidth=100 : extendedWidth=175;
        extendedWidth=141;
        if (this.id==_controlName + "txtAreaCode")
        {
            //alert("Area: " + $(this).width())
            extendedWidth=$(this).width() + 95;
        }
        else
        {
            //alert("phone: " + $(this).width());
            extendedWidth=$(this).width() -25;
            //extendedHeigh=top+2;
        }
        //extendedWidth=(this.id==_controlName + "txtAreaCode") ? $(this).width() + 45 : $(this).width() + 95;
        //extendedWidth=(this.id==_controlName + "txtPhone") ? $(this).width() : $(this).width() + 95;
        //alert(extendedWidth);
        
        topVal=top + extendedHeigh + "px";  
        leftVal=left + extendedWidth + "px";
        if (this.id.toLowerCase().indexOf("areacode")>0)
        {
            $("#popuup_" + _controlName + "txtPhone").hide();
            $("#invalid_" + _controlName + "txtPhone").hide();
            $("#invalid_" + this.id).hide();
            if(_dispayTipMsg)
            {
                DisplayTipMsg(this,_controlName + 'txtAreaCode');
            }
        }
        else
        {
            $("#popuup_" + _controlName + "txtAreaCode").hide();
            $("#invalid_" + _controlName + "txtAreaCode").hide();
            $('#invalid_' + this.id).hide();
            if(_dispayTipMsg)
            {
                DisplayTipMsg(this,_controlName + 'txtPhone');
            }
        }
      });
      
      $(":input[rel='required_email']").focus(function()
      {
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
        
        //calculating offset for displaying popup message
        extendedWidth=(this.id==_controlName + "txtPhone") ? extendedWidth=100 : extendedWidth=175;

        topVal=top + extendedHeigh + "px";  
        leftVal=left + extendedWidth + "px";
        
        $("#invalid_" + this.id).hide();
        if(_dispayTipMsg)
        {
            DisplayTipMsg(this,_controlName + 'txtEmail');   
        }   
        if(_hasTAF)
        {       
            for (var i=1;i<=NumOfTafEmail;i++)
            {
                if(_dispayTipMsg)
                {
                    DisplayTipMsg(this,_controlName + "txtRecipientEmail_" + i);     
                }
            }
        }
      });
      
      
      $(":input[rel='required_dob']").focus(function()
      {
        var pos =new FindPos(this);

        var top = pos.posY;
        var left = pos.posX;
        
        if (this.id.toLowerCase().indexOf("year")>=0)
        {
            document.getElementById(_controlName + 'txtYear').value="";
            //$('#' + _controlName + 'txtYear').val()="";
        }
        
        //calculating offset for displaying popup message
        if (this.id==_controlName + "ddDay")
        {
            extendedWidth=73;
        }
        else if (this.id==_controlName + "ddMonth")
        {
            extendedWidth=125;
        }
        else if (this.id==_controlName + "txtYear")
        {
            extendedWidth=24;
        }
        else
        {
             extendedWidth=175;
        }
      
        //alert("extended: " + extendedWidth);
        topVal=top + extendedHeigh + "px";  
        leftVal=left + extendedWidth + "px";
        //alert("leftpush " + leftVal);
        
        if (this.id.toLowerCase().indexOf("month")>0)
        {
            if(_dispayTipMsg)
            {
                DisplayTipMsg(this,_controlName + 'ddMonth');
            }
        }
        else if (this.id.toLowerCase().indexOf("day")>0)
        {
            if(_dispayTipMsg)
            {
                DisplayTipMsg(this,_controlName + 'ddDay');
            }
        }
        else if (this.id.toLowerCase().indexOf("year")>0)
        {
            if(_dispayTipMsg)
            {
                DisplayTipMsg(this,_controlName + 'txtYear');
            }
        }
        

    });
    
    
    //************ On Blur *******************
  //---------regular required fields----------------
  $(':input[rel=required]').blur(function()
  {
    //$(this).validate.init(this); 
    //alert(this.id);  
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    leftVal=left + extendedWidth + "px";
    //alert(left + " " + leftVal + " " + extendedWidth);
/*
    DisplayErrorMsg(this,_controlName + "txtFirstname");
    DisplayErrorMsg(this,_controlName + "txtLastname");
    DisplayErrorMsg(this,_controlName + "txtAddress");
    DisplayErrorMsg(this,_controlName + "txtCity");
    DisplayErrorMsg(this,_controlName + "ddProvince");
    DisplayErrorMsg(this,_controlName + "txtPostalCode");      
*/
//alert("this: " + this + " this.id=" + this.id);
    DisplayErrorMsg(this,this.id);
    
    if(_hasTAF)
    { 
        for (var i=1;i<=NumOfTafEmail;i++)
        {
            DisplayErrorMsg(this,_controlName + "txtRecipientName_" + i);     
        }
    }
  });   
  
  
  
  //---------Postal code required field----------------
  $(':input[rel=required_postal]').blur(function()
  { 
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    leftVal=left + extendedWidth + "px";
    
    if(this.value=="")
    {
        $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#img_OK_' + _controlName + "txtPostalCode").html("");
        //$('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        $('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }
    else
    {

        if (CheckWildChar(this.value))
        {
            $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
            $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
            $('#img_OK_' + _controlName + "txtPostalCode").html("");
            //$('#invalid_checkwildchar').css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
            $('#invalid_checkwildchar').css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
        }
        else if (!ValidatePostalCode(this))
        {
            $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
            $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
            $('#img_OK_' + _controlName + "txtPostalCode").html("");
            //$('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
            $('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
        }
        else
        {
            CreateDIV("img_OK_" + this.id,"popup_msg");

            $("#label_" + replace(this.id,_controlName,'')).removeClass("fRed");
            $('#empty_' + this.id).hide();
            $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
            $('#invalid_checkwildchar').hide();
            $('#img_OK_' + this.id).css({left:leftVal,top:topVal}).html('<img src="/images/accept.gif" border="0" style="float:left;" />');
        }
    }
  }); 
   
  
  //---------phone required field----------------
  $(':input[rel=required_phone]').blur(function()
  { 
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    leftVal=left + extendedWidth + "px";
 
    if (!ValidatePhone(this))
    {
        $("#label_txtPhone").addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#img_OK_' + _controlName + "txtPhone").html("");
        //$('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        //alert(this.id);
        $('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }
    else
    {
        if (this.id.toLowerCase().indexOf("phone")>0)
        {
        CreateDIV("img_OK_" + this.id,"popup_msg");
        }
        $("#label_txtPhone").removeClass("fRed");
        $('#empty_' + this.id).hide();
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#invalid_checkwildchar').hide();
        $('#img_OK_' + this.id).css({left:leftVal,top:topVal}).html('<img src="/images/accept.gif" border="0" style="float:left;" />');
    }
  }); 
  
  //---------- email required filed ----------------
  $(':input[rel=required_email]').blur(function()
  { 
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    extendedWidth=120; //custom hack extendedWidth
    //leftVal=left + extendedWidth + "px";
    //alert(left + " " + leftVal + " " + extendedWidth);
    if (this.value=="")
    {
        $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#img_OK_' + this.id).html("");
        //$('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        $('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }
    else if (!ValidateEmail(this.value))
    {
        //$("#label_txtEmail").addClass("fRed");
        $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#img_OK_' + this.id).html("");
        //$('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        $('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }
    else
    {
        CreateDIV("img_OK_" + this.id,"popup_msg");
        $("#label_" + replace(this.id,_controlName,'')).removeClass("fRed");
        $('#empty_' + this.id).hide();
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#invalid_checkwildchar').hide();
        $('#img_OK_' + this.id).css({left:leftVal,top:topVal}).html('<img src="/images/accept.gif" border="0" style="float:left;" />');
    }
  });   
  
  //---------Date of birth required field----------------
  $(':input[rel=required_dob]').blur(function()
  { 
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    leftVal=left + extendedWidth + "px"; 

    if (!ValidateDoB(this))
    {

        $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#img_OK_' + _controlName + "txtYear").html("");
        //$('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        $('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }   
    else
    {
        if (this.id.toLowerCase().indexOf("year")>0)
        {
        CreateDIV("img_OK_" + this.id,"popup_msg");
        }
        $("#label_" + replace(this.id,_controlName,'')).removeClass("fRed");
        $('#empty_' + this.id).hide();
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#invalid_checkwildchar').hide();
        $('#img_OK_' + this.id).css({left:leftVal,top:topVal}).html('<img src="/images/accept.gif" border="0" style="float:left;" />');
    }
  }); 
  
  //---------- required filed in integer value----------------
  $(':input[rel=required_integer]').blur(function()
  { 
    var pos =new FindPos(this);

    var top = pos.posY;
    var left = pos.posX;
    topVal=top + "px";
    leftVal=left + extendedWidth + "px";
    if(this.value=="")
    {
        $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
        $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
        $('#invalid_' + this.id).hide();
        $('#img_OK_' + this.id).html("");
        //$('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show().fadeOut(fadingOutSpeed);
        $('#empty_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
    }
    else
    {
        if (!isInt(this.value))
        {
            $("#label_" + replace(this.id,_controlName,'')).addClass("fRed");
            $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
            $('#img_OK_' + _controlName + "txtYear").html("");
            $('#invalid_' + this.id).css({left:leftVal,top:topVal}).fadeIn(fadingInSpeed).show();
        }
        else
        {
            CreateDIV("img_OK_" + this.id,"popup_msg");

            $("#label_" + replace(this.id,_controlName,'')).removeClass("fRed");
            $('#empty_' + this.id).hide();
            $('#invalid_' + this.id).hide();
            $('#popuup_' + this.id).css({left:leftVal,top:topVal}).hide();
            $('#invalid_checkwildchar').hide();
            $('#img_OK_' + this.id).css({left:leftVal,top:topVal}).html('<img src="/images/accept.gif" border="0" style="float:left;" />');
        }
    }
  });
    
	
});	
