﻿$(document).ready
(
    function() 
    {
        //debugger;
        $(".datepicker").datepicker(
        {
            dateFormat: siteJqueryDateickerFormat,           //"d M yy" formats to Wednesday 17 September 2008
            showOn: "both", 
            buttonImage: imagesiteroot + "/_images/date.png", 
            buttonImageOnly: true, 
            yearRange:'-1:+3',
            defaultDate:'0'
        });
        //setTimeout('btnEditHide()',100);
    }
);

function btnEditHide()
{
        $(".btnEdit").attr("style","float:left;display:none");
}

function ValidateForm()
{
    var blnError = false;
    //debugger;
    try
    {
     

        
        $('.mandatory').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().text("");    
            }
        })
        
        
        //Check decimal values
        $('.isdecimal').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                $(this).attr("value","0");
            } 
            if ( IsNumeric($(this).attr("value"))==false)
            {
                blnError = true;
                $(this).next().text("Numeric only please");
            }
            else
            {
                //$(this).next().text("");
                var val=$(this).attr("value");
                if(val <0 || val >9999)
                {
                    blnError = true;
                    $(this).next().text("valid range 0 to 9999");                    
                }
                else
                {

                    $(this).next().text("");
                }
            }
            
            
            
        })

        //Check decimal values
        $('.txtFreePeriod').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                $(this).attr("value","0");
            } 
            if ( IsInteger($(this).attr("value"))==false)
            {
                blnError = true;
                $(this).next().text("Integer only please");
            }
            else
            {
                $(this).next().text("");
                var val=$(this).attr("value");
                if(val <0 || val >9999)
                {
                    blnError = true;
                    $(this).next().text("valid range 0 to 9999");                    
                }
                else
                {
                    $(this).next().text("");
                        
                }
            }
            
            
            
        })
                
        $('.mandatorydate').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } 
            else 
            {   
                $(this).next().next().text("");    
            }
        })        
        
        $('.isdate').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //Do nothing
            } 
            else 
            {   // Check if the date is a valid date
                var res = isDate($(this).attr("value"),siteShortDateFormat);
                //debugger;
                if(res==0)
                {
                    $(this).next().next().text("Invalid date");
                    blnError = true;
                    
                }
                else
                {
                    $(this).next().next().text("");  
                }
            }
        })
        
        //Difference between this and the madatory check above is the status field is further away
        $('.mandatorylookup').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().next().next().text("Required");
                if (!blnError) {
                    $(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().next().next().text("");    
            }
        })
                
        var txtEmail;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        $('.email').each(function() {
            txtEmail = $(this).attr("value");
            if  (!filter.test(txtEmail)) {
                $(this).next().text("Please specify a valid Email address");
                if (!blnError) {
                    $(this).focus();
                }
                blnError = true;
            } else {
               $(this).next().text("");
            }
       })
       
       
       //DOB format validator
        var txtDOB;
        var filter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
        var blnDateError = false;
        
        $('.date').each(function() {
            blnDateError = false;
            txtEmail = $(this).attr("value");
            if ((txtEmail!='') && (!filter.test(txtEmail))) {
            
                if (isNaN(Date.parse(txtEmail))) {
                    $(this).next().next().text("Invalid Date");
                    if (!blnError) {
                        $(this).focus();
                    }
                    blnDateError = true
                    blnError = true;
                } 
            }
            if (!blnDateError) {
                $(this).next().next().text("");                    
            }
       })
       
        //If we get here the date is present and is a valid date
        //now verify that the end date is >= to startdate
        $('.txtDateStart').each(function() {
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                //Do nothing
            } 
            else
            {
                //Ensure end date is after the start date
                var dstart =$(this).attr("value");
                var dend =$('.txtDateEnd').attr("value");
                
                if ( dend =='')
                {
                    //Do Nothing
                }
                else
                {
                    var res = compareDates(dstart,siteShortDateFormat,dend,siteShortDateFormat);
                    //debugger;
                    if (res==1 )
                    {
                      // date 2 is earlier than date 1
                      $('.txtDateEnd').next().next().text("This date is before the Start");
                      blnError = true;
                    }
                
                }
            }
        })       
    }
    catch (e) {
    }
    
    if (blnError) 
    {
        //debugger;
        $('.divMessages').attr("innerText","Please correct the entries in red below.");
        return false;
    } else {
        $('.status').text("");
        return true;
    }
}







function ValidateRatingsAndForm()
{
    var blnError = false;
    //debugger;
    try
    {


                

        
        $('.mandatory').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().text("");    
            }
        })
        

        //Validate: If status drop down is set to COMPLETED, that a completed by has been set
        $('.ddlHandicap').each(function() 
            {
            //If the status dropdown is 'completed' ensure 
            //completed date is set and completed by is set
            //debugger;
            if ($(this).attr("options")[$(this).attr("options").selectedIndex].value =='00000000-0000-0000-0000-000000000000') 
                {
                    $(this).next().text("Required");
                        blnError = true;
                }
             else
                {
                //debugger;
                $(this).next().text("");    
                }
            }
         )
         
                 //Validate: If status drop down is set to COMPLETED, that a completed by has been set
        $('.ddlAgeGroup').each(function() 
            {
            //If the status dropdown is 'completed' ensure 
            //completed date is set and completed by is set
            //debugger;
            if ($(this).attr("options")[$(this).attr("options").selectedIndex].value =='00000000-0000-0000-0000-000000000000') 
                {
                    $(this).next().text("Required");
                        blnError = true;
                }
             else
                {
                //debugger;
                $(this).next().text("");    
                }
            }
         )
         
                         
        var txtEmail;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        $('.email').each(function() {
            txtEmail = $(this).attr("value");
            if  (!filter.test(txtEmail)) {
                $(this).next().text("Please specify a valid Email address");
                if (!blnError) {
                    $(this).focus();
                }
                blnError = true;
            } else {
               $(this).next().text("");
            }
       })
       
       

       
      
    }
    catch (e) {
    }
    
    if (blnError) 
    {
        //debugger;
        $('.divMessages').attr("innerText","Please correct the entries in red below.");
        return false;
    } else {
        $('.status').text("");
        return true;
    }
}

function ShowAdvancedSearchOptions()
{
    
    //debugger;
    var x=$('.divAdvancedSearch').attr("currentStyle").display;
    if(x=='block')
    {
        $('.divAdvancedSearch').attr("style","display:none;");
        $('.lnkMoreOptions').attr("innerHTML","<b>More Search Options...</b>");
        $('.hidfield').attr("value","none");
        
    }
    else
    {
        $('.divAdvancedSearch').attr("style","display:block;");
        $('.lnkMoreOptions').attr("innerHTML","<b>Less Search Options...</b>");
        $('.hidfield').attr("value","block");
    }
        
}

function ShowSearchOptions()
{
    
    //debugger;
    var x=$('.divSearchOptions').attr("currentStyle").display;
    if(x=='block')
    {
        $('.divSearchOptions').attr("style","display:none;");
        $('.lnkSearchOptions').attr("innerHTML","<b>Show Search Options...</b>");
        $('.hidfield2').attr("value","none");
        
    }
    else
    {
        $('.divSearchOptions').attr("style","display:block;");
        $('.lnkSearchOptions').attr("innerHTML","<b>Hide Search Options...</b>");
        $('.hidfield2').attr("value","block");
    }
        
}